0% found this document useful (0 votes)
35 views

Gamesm

This document describes a game state machine that incorporates part of a game except for scoring. It defines 7 states: PseudoState_Game, Uninitialized, SelectingPipe, WaterFlowing, IRRead, WaitforReset. It also defines variables, initialization functions, and event handling for each state.

Uploaded by

api-438010548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Gamesm

This document describes a game state machine that incorporates part of a game except for scoring. It defines 7 states: PseudoState_Game, Uninitialized, SelectingPipe, WaterFlowing, IRRead, WaitforReset. It also defines variables, initialization functions, and event handling for each state.

Uploaded by

api-438010548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

GAME_STATE_MACHINE

This state machine incorporates part of the game except the scoring

Data private to the module:


MyPriority

States:
PseudoState_Game
Uninitialized
SelectingPipe
WaterFlowing
IRRead
WaitforReset

*/

Include Dependent Files


Function define module functions

Define module defines like SCORE_TIME, MAX_DIFFICULTY, PIPE_TIMER


Define ports and pins for the decoder output
Define ports and pins for the IR sensors for each pipe
Define ports and pins for the Shift Register to pipe LEDs

Specify Module Variables:


MyPriority
CurrentState
PipeTimeMultiplier
pipeTime: stores the pipe timer count based on the difficulty level
WaterFlowTime: timer for water flow
random_pipe_index
SCORE_TIME

InitGameSM

Parameters
uint8_t : the priorty of this service

Returns
bool, false if error in initialization, true otherwise

Description
Saves away the priority, and does any
other required initialization for this service
Notes

Function

Initialize the MyPriority module variable with the passed priority


Initialize the hardware pins:
Set decoder pins as digital outputs
Set IR pipe pins as digital inputs
Initialise the pipe shift register
Initialize the short timer

Post ES_INIT to this state machine. Return true if successful, false in case of
any error.

PostGameSM
Parameters
ES_Event_t ThisEvent ,the event to post to the queue

Returns
bool false if the Enqueue operation failed, true otherwise

Description
Posts an event to this state machine's queue

Function:

Call ES_PostToService passing the priority through MyPriority and the assigned
event

RunGameSM

Parameters
ES_Event_t : the event from its service queue to process in this State
Machine
Types of ES_Event_t:

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description
Runs InGame state machine

Function

Declare a ReturnEvent and initialise it as ES_NO_EVENT. This would be returned


in case of no error.

If not in InGame state in the Control SM:


put current state to Uninitialized_Game

Switch CurrentState
If PseudoState_Game:
If event ES_INIT:
Put current state to Uninitialized_Game

If Uninitialized_Game:
If event START_GAME:
Post ACTIVATE_RANDOM_PIPE to self
Start a short timer for frequent scoring
Set the pipe timer based on the received difficulty from the
ControlSM
passed as a parameter to the START_GAME event
Initialize the random_pipe_index
Put current State to SelectingPipe

If SelectingPipe:
If event ACTIVATE_RANDOM_PIPE:
Select a random pipe among the pipes
Restart the pipe timer
Restart the score timer
Start Pipe Lights for the selected pipe
Put CurrentState to WaterFlowing

If event MARIO_END:
Switch off Pipe LEDs
Put current state to WaitForReset

If event is Reset (either LEAF_RESET or INACTIVE_RESET):


Turn off LEDs for all the pipes
Put current state to Uninitialized_Game
If WaterFlowing:
If event ES_SHORT_TIMEOUT (Score timer timeout):
Put current state to IRRead
Post SCORE_CHECK event to self

If event is Pipe timer timeout:


Post ACTIVATE_RANDOM_PIPE to self
Put current state to SelectingPipe

If event is water flow timer timeout:


Write a random value to the SR from 0 to 31 to glow a random
combination of LEDs
simulating a water flow and return back to the same state

If event is MARIO_END:
Switch off all LEDs
Put current state to WaitForReset

If event is Reset (either LEAF_RESET or INACTIVE_RESET):


Turn off LEDs for all the pipes
Put current state to Uninitialized_Game

If IRRead:
If event is MARIO_END
Turn off all pipe LEDs
Put current state to WaitForReset

If event is Reset (either LEAF_RESET or INACTIVE_RESET):


Turn off LEDs for all the pipes
Put current state to Uninitialized_Game

If event is SCORE_CHECK
If there is still an IR Connection for the active pipe:
Post SCORE_DEC to Scoring SM
Restart score timer
Put current state to WaterFlowing
Play bad action sound

Else if there is no IR Connection:


Restart the score timer
Play good action sound
Put current state to WaterFlowing

If WaitForReset:
If event is Reset (either LEAF_RESET or INACTIVE_RESET):
Turn off LEDs for all the pipes
Put current state to Uninitialized_Game

return ReturnEvent

You might also like