Sie sind auf Seite 1von 7

/****************************************************************************

Module
GameStateMachine.c
Revision
1.0
Description
This is the GameStateMachine for Team 9 ME218b final project. It is a substate machine of MasterMachine.
Notes
History
When
Who
What/Why
----------------------02/08/2015 19:32 Sky
Initial version
****************************************************************************/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include
#include
#include
#include
#include
#include
#include

"inc/hw_memmap.h"
"inc/hw_types.h"
"inc/hw_gpio.h"
"inc/hw_sysctl.h"
"driverlib/sysctl.h"
"driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
"driverlib/gpio.h"

#include
#include
#include
#include
#include
#include
#include

"MasterMachine.h"
"GameStateMachine.h"
"RaceStateMachine.h"
"ObstacleStateMachine.h"
"ShootStateMachine.h"
"Calculation.h"
"Motion.h"

/*******************************Module Defines**********************************
*/
#define SHOOT 0
#define OBSTACLE 1
/*******************************Module Variables********************************
*/
static Target CurrentTarget;
static GameState CurrentState, HistoryState;
static uint8_t self;
static uint16_t CurrentTargetDirection;
/***************************Private Functions Prototype*************************
*/
static ES_Event DuringRaceState(ES_Event);
static ES_Event DuringObstacleState(ES_Event);
static ES_Event DuringShootState(ES_Event);
/********************************Public Functions*******************************
*/

/*******************************************************
FUNCTION RunGameSM
ARGUMENTS: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: the Run function of Game state machine
*******************************************************/
ES_Event RunGameSM(ES_Event CurrentEvent)
{
bool MakeTransition;
GameState NextState;
ES_Event ReturnEvent;
ES_Event LocalEvent;
//set MakeTransition to false
MakeTransition = false;
//set ReturnEvent to CurrentEvent since we assume that we are not consum
ing event
ReturnEvent = CurrentEvent;
//set NextState to CurrentState
NextState = CurrentState;
//switch current state to deal with the events
switch (CurrentState)
{
case Game_Race
: {
//pass down the even
t to any lower level state machine
LocalEvent = DuringRaceState(CurrentEvent);
if (ES_NO_EVENT != LocalEvent.EventType) // if there is still an active event
{
switch (LocalEvent.EventType)
{
case ES_ToShoot

{
//set next state to Game_Shoot

NextState = Game_Shoot;
MakeTransition = true;
ReturnEvent.EventType = ES_NO_EVENT;
break;
}
case ES_ToObstacle

: {
//set next state to Game_Obstacle

NextState = Game_Obstacle;
MakeTransition = true;
ReturnEvent.EventType = ES_NO_EVENT;

break;
}
default

: break;

}
}
break;
}
case Game_Obstacle

{
//pass down the even

t to any lower level state machine


LocalEvent = DuringObstacleState(CurrentEvent);
if (ES_NO_EVENT != LocalEvent.EventType) // if there is still an active event
{
switch (LocalEvent.EventType)
{
case ES_ToRace

NextState = Game_Race;
MakeTransition = true;
//set the EventParam of LocalEvent to OBSTACLE to indicate that enter
//race state from obstacle state
LocalEvent.EventParam = OBSTACLE;
ReturnEvent.EventType = ES_NO_EVENT;
break;
}
default

}
}
break;
}
case Game_Shoot

//pass down the event to any lower level state machine


LocalEvent = DuringShootState(CurrentEvent);

break;

if (ES_NO_EVENT != LocalEvent.EventType) // if there is still an active event


{
switch (LocalEvent.EventType)
{
case ES_ToRace

NextState = Game_Race;
MakeTransition = true;
//set the EventParam of LocalEvent to SHOOT to indicate that enter
//race state from Shoot state
LocalEvent.EventParam = SHOOT;
ReturnEvent.EventType = ES_NO_EVENT;
break;
}
default

: break;

}
}
break;
}
}
//if we are making transition
if (MakeTransition == true)
{
//execute exit function for current state
LocalEvent.EventType = ES_EXIT;
RunGameSM(LocalEvent);
//execute the entry function for next state
CurrentState = NextState;
LocalEvent.EventType = ES_ENTRY;
RunGameSM(LocalEvent);
}
return ReturnEvent;
}
/*******************************************************
FUNCTION StartGameSM
ARGUMENTS: ES_Event Event
RETURN: no return
DESCRIPTION: start the game state machine
*******************************************************/
void StartGameSM(ES_Event Event)
{
volatile ES_Event LocalEvent;

LocalEvent = Event;
//set CurrentState to Game_Race since we always assume that the game sta
rts at race region
//set self No.
self = QuerySelf();
if (LocalEvent.EventType == ES_ENTRY)
{
CurrentState = Game_Race;
HistoryState = CurrentState;
//call RunGameSM
RunGameSM(LocalEvent);
}
else if (LocalEvent.EventType == ES_ENTRY_HISTORY)
{
//enter the historystate
CurrentState = HistoryState;
RunGameSM(LocalEvent);
}
}
/*******************************************************
FUNCTION SetTarget
ARGUMENTS: an integer represents the region
RETURN: no return
DESCRIPTION: set the current target based on the region
*******************************************************/
void SetTarget(uint8_t whichRegion)
{
Point targetPoint = QueryTarget4Region(whichRegion);
CurrentTarget.x = targetPoint.x;
CurrentTarget.y = targetPoint.y;
printf("TargetX = %d, TargetY = %d\n\r", CurrentTarget.x, CurrentTarget.
y);
}
/*******************************************************
FUNCTION SetTargetDirection
ARGUMENTS: an integer represents the target direction
RETURN: no return
DESCRIPTION: set current target direction
*******************************************************/
void SetTargetDirection(int16_t dir)
{
CurrentTargetDirection = dir;
}
/*******************************************************
FUNCTION QueryTarget
ARGUMENTS: no argument
RETURN: a Target
DESCRIPTION: get the CurrentTarget
*******************************************************/
Target QueryTarget(void)
{
return CurrentTarget;
}
/*******************************************************
FUNCTION QueryTargetDirection
ARGUMENTS: no argument

RETURN: an integer reprensents the current target direction


DESCRIPTION: get the CurrentTarget
*******************************************************/
int16_t QueryTargetDirection(void)
{
return CurrentTargetDirection;
}
/***********************************Private Functions***************************
***************/
/*******************************************************
FUNCTION DuringRaceState
ARGUMENTS: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: deal with events, pass down events to lower
level state machine
*******************************************************/
static ES_Event DuringRaceState(ES_Event CurrentEvent)
{
ES_Event LocalEvent;
LocalEvent = CurrentEvent;
if ((LocalEvent.EventType == ES_ENTRY)||
(LocalEvent.EventType == ES_ENTRY_HISTORY))
{
//start the lower level state machine: RaceStateMachine
StartRaceSM(LocalEvent);
}
else if (LocalEvent.EventType == ES_EXIT)
{
//store the historystate
HistoryState = CurrentState;
//pass down ES_EXIT to let lower level state machine clean up fi
rst
LocalEvent = RunRaceSM(LocalEvent);
}
else
{
//pass down the event to lower level state machine to deal with the
//event first
LocalEvent = RunRaceSM(LocalEvent);
}
return LocalEvent;
}
/*******************************************************
FUNCTION DuringObstacleState
ARGUMENTS: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: deal with events, pass down events to lower
level state machine
*******************************************************/
static ES_Event DuringObstacleState(ES_Event CurrentEvent)
{
ES_Event LocalEvent;
LocalEvent = CurrentEvent;
if ((LocalEvent.EventType == ES_ENTRY)||
(LocalEvent.EventType == ES_ENTRY_HISTORY))
{

//start the lower level state machine: ObstacleStateMachine


StartObstacleSM(LocalEvent);
}
else if (LocalEvent.EventType == ES_EXIT)
{
//store the historystate
HistoryState = CurrentState;
//pass down ES_EXIT to let lower level state machine clean up fi
rst
LocalEvent = RunObstacleSM(LocalEvent);
}
else
{
//pass down the event to lower level state machine to deal with the
//event first
LocalEvent = RunObstacleSM(LocalEvent);
}
return LocalEvent;
}
/*******************************************************
FUNCTION DuringShootState
ARGUMENTS: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: deal with events, pass down events to lower
level state machine
*******************************************************/
static ES_Event DuringShootState(ES_Event CurrentEvent)
{
ES_Event LocalEvent;
LocalEvent = CurrentEvent;
if ((LocalEvent.EventType == ES_ENTRY)||
(LocalEvent.EventType == ES_ENTRY_HISTORY))
{
//start the lower level state machine: ObstacleStateMachine
StartShootSM(LocalEvent);
}
else if (LocalEvent.EventType == ES_EXIT)
{
//store the historystate
HistoryState = CurrentState;
//pass down ES_EXIT to let lower level state machine clean up fi
rst
LocalEvent = RunShootSM(LocalEvent);
}
else
{
//pass down the event to lower level state machine to deal with the
//event first
LocalEvent = RunShootSM(LocalEvent);
}
return LocalEvent;
}

Das könnte Ihnen auch gefallen