0% found this document useful (0 votes)
87 views9 pages

"Es - Configure.H" "Es - Framework.H" "Gameplay.H" "Enemyship.H" "Projectile.H" "Spaceship.H" "Updateoled.H" "Dotstarservice.H"

This module implements a state machine to transition between a title screen, game, and end screen states for a game. It tracks the score, number of collisions, and game mode setting. It includes header files for sub-machines, defines constants, and function prototypes. Module functions initialize the state machine, post events, and run the state machine to process events and transition between states. The state machine implements different behaviors and transitions for each game state in response to different event types.

Uploaded by

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

"Es - Configure.H" "Es - Framework.H" "Gameplay.H" "Enemyship.H" "Projectile.H" "Spaceship.H" "Updateoled.H" "Dotstarservice.H"

This module implements a state machine to transition between a title screen, game, and end screen states for a game. It tracks the score, number of collisions, and game mode setting. It includes header files for sub-machines, defines constants, and function prototypes. Module functions initialize the state machine, post events, and run the state machine to process events and transition between states. The state machine implements different behaviors and transitions for each game state in response to different event types.

Uploaded by

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

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

Module
Gameplay.c

Revision
1.0.1

Description
This Module implements the overarching state machine to transition between title scree,
end screen and the game. It also keeps track of the score and leader boards.

****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "Gameplay.h"
#include "EnemyShip.h"
#include "Projectile.h"
#include "Spaceship.h"
#include "UpdateOLED.h"
#include "DotStarService.h"

/*----------------------------- Module Defines ----------------------------*/

#define WIN 1
#define LOSE 2
#define DEBUG
#define KEY_EVENTS
#define ONE_SECOND 1000
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/

/*---------------------------- Module Variables ---------------------------*/


// everybody needs a state variable, you may need others as well.
// type of state variable should match htat of enum in header file
static GameplayState_t CurrentState;

// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
static uint16_t score;
static uint8_t numCollisions;
static int8_t gameMode;

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************
Function
InitTemplateFSM

Parameters
uint8_t : the priorty of this service
Returns
bool, false if error in initialization, true otherwise

Description
Saves away the priority, sets up the initial transition and does any
other required initialization for this state machine
Notes

Author
J. Edward Carryer, 10/23/11, 18:55
****************************************************************************/
bool InitGameplay(uint8_t Priority)
{
ES_Event_t ThisEvent;

MyPriority = Priority;
// put us into the Initial PseudoState
CurrentState = InitPState;
// post the initial transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}

/****************************************************************************
Function
PostTemplateFSM

Parameters
EF_Event_t ThisEvent , the event to post to the queue

Returns
boolean False if the Enqueue operation failed, True otherwise

Description
Posts an event to this state machine's queue
Notes

Author
J. Edward Carryer, 10/23/11, 19:25
****************************************************************************/
bool PostGameplay(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunTemplateFSM

Parameters
ES_Event_t : the event to process
Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description
add your description here
Notes
uses nested switch/case to implement the machine.
Author
J. Edward Carryer, 01/15/12, 15:23
****************************************************************************/
ES_Event_t RunGameplay(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

switch (CurrentState)
{
case InitPState: // If current state is initial Psedudo State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
// this is where you would put any actions associated with the
// transition from the initial pseudo-state into the actual
// initial state
score = 0;
numCollisions = 0;
gameMode = 0;

// now put the machine into the actual initial state


CurrentState = TitleScreen;

ES_Event_t titleEvent;
titleEvent.EventType = ES_UPDATE_OLED;
titleEvent.EventParam = titlescreenID;
PostUpdateOLED(titleEvent);

#ifdef DEBUG
puts("\n\rGameplay Initialized\r\n");
#endif
}
}
break;

case TitleScreen: // If current state is state one


{
switch (ThisEvent.EventType)
{
case ES_JOYSTICK: // joystick to set easy, medium, hard game settings
{
// Switches game mode depending on input from joystick
gameMode = gameMode - 1 + ThisEvent.EventParam;

if(gameMode < 0)
{
gameMode = 2; // Wraps around from easy to hard
}
else if(gameMode > 2)
{
gameMode = 0; // Wraps around from hard to easy
}

// Updates selected game mode to OLED


ES_Event_t Event2Post;
Event2Post.EventParam = gameMode;
Event2Post.EventType = ES_JOYSTICK;
PostUpdateOLED(Event2Post);
}
break;
case ES_BUTTON: //If event is event one
{ // Execute action function for state one : event one
score = 0;
numCollisions = 0;
CurrentState = Game; //Decide what the next state will be

// Posts startGame event to all of the game services to get them out of
// their holding initialization state
ES_Event_t ThisEvent;
ThisEvent.EventType = ES_START_GAME;
ThisEvent.EventParam = gameMode;
PostEnemyShip(ThisEvent);
PostProjectile(ThisEvent);
PostSpaceship(ThisEvent);
PostUpdateOLED(ThisEvent);
PostDotStarService(ThisEvent);
}
break;
#ifdef KEY_EVENTS
case ES_NEW_KEY:
{
ES_Event_t keyEvent;
switch(ThisEvent.EventParam)
{
case 'b':
{
keyEvent.EventType = ES_BUTTON;
#ifdef DEBUG
puts("\rKeyboard Button Event Detected\r\n");
#endif
PostGameplay(keyEvent);
}
break;
case 's': //prints current state- does not post event
{
#ifdef DEBUG
puts("\rCurrent State: Title Screen\r\n");
#endif
}
break;
default:
;
}

}
break;

#endif
// repeat cases as required for relevant events
default:
;
} // end switch on CurrentEvent
}
break;
// repeat state pattern as required for other states
case Game: // If current state is state one
{
switch (ThisEvent.EventType)
{
case ES_COLLISION: //If event is event one

{ // Execute action function for state one : event one


score +=10; // increment the score counter if a collision is detected
numCollisions++;
//Check if the player has one by destroying all the ships (score equals max
score)
if(numCollisions >= MAX_COLLISIONS){
ES_Event_t endEvent;
endEvent.EventType = ES_GAME_OVER;
endEvent.EventParam = WIN;

PostEnemyShip(endEvent);
PostProjectile(endEvent);
PostSpaceship(endEvent);
PostGameplay(endEvent);
PostDotStarService(endEvent);//post game over event to this service

//CurrentState = EndGame; // not needed because state is changed when the


game over event is processed

}
}
break;
case ES_PROJ_MISS:
{
if(score > 1){
score--;
}
}break;
case ES_GAME_OVER: //If event is a game over event
{ // Execute action function for state one : event one

// Goes to waiting for the score board for one second


CurrentState = ScoreBoardWaiting;
ES_Timer_InitTimer(GAMEPLAY_TIMER, ONE_SECOND);

// Update the OLED with the end of game screen and scoreboard
ES_Event_t endEvent;
endEvent.EventType = ES_UPDATE_OLED;
endEvent.EventParam = gameoverID;
PostUpdateOLED(endEvent);
}
break;
//If enabled events are generated manually by keystrokes and posted to all
services
#ifdef KEY_EVENTS
case ES_NEW_KEY:
{
ES_Event_t keyEvent;
keyEvent.EventType = ES_NO_EVENT;
switch(ThisEvent.EventParam)
{
case 'b':
{
keyEvent.EventType = ES_BUTTON;
#ifdef DEBUG
puts("\rKeyboard Button Event Detected\r\n");
#endif
}
break;
case 'u':
{
keyEvent.EventType = ES_UPDATE_OLED;
keyEvent.EventParam = enemyshipID;
#ifdef DEBUG
puts("\rKeyboard Update OLED Event Detected\r\n");
#endif
}
break;
case 'c':
{
keyEvent.EventType = ES_COLLISION;
keyEvent.EventParam = 2;
#ifdef DEBUG
puts("\rKeyboard Collision Event Detected\r\n");
#endif
}
break;
case 'o':
{
keyEvent.EventType = ES_GAME_OVER;
#ifdef DEBUG
puts("\rKeyboard Game over Event Detected\r\n");
#endif
}
break;
case 's': //prints current state- does not post event
{
#ifdef DEBUG
puts("\rCurrent State: Game \r");
printf("\rCurrent Score: %d\r\n", score);
#endif
}
break;
default:
;
}
PostGameplay(keyEvent);
PostSpaceship(keyEvent);
PostEnemyShip(keyEvent);
PostUpdateOLED(keyEvent);
PostProjectile(keyEvent);
}
break;
#endif
default:
;
} // end switch on CurrentEvent
}
break;
case ScoreBoardWaiting:
{
// Changes CurrentState to ScoreBoard once the timer times out
if(ThisEvent.EventType == ES_TIMEOUT) CurrentState = ScoreBoard;
}break;
case ScoreBoard: // State at which scoreboard is displayed
{
switch(ThisEvent.EventType)
{
case ES_BUTTON:
{
// If button is pressed, calls UpdateOLED to display the
// scoreboard. Changes CurrentState to EndGame
CurrentState = EndGame;

ES_Event_t endEvent;
endEvent.EventType = ES_UPDATE_OLED;
endEvent.EventParam = scoreboardID;
PostUpdateOLED(endEvent);

}break;
}
}break;
case EndGame: // If current state is Endgame
{
switch (ThisEvent.EventType)
{
case ES_BUTTON: //If event is a button press
{ // Execute action function for state one : event one
CurrentState = TitleScreen; //Next state is the title screen

//Update the oled with the title screen


ES_Event_t titleEvent;
titleEvent.EventType = ES_UPDATE_OLED;
titleEvent.EventParam = titlescreenID;
PostUpdateOLED(titleEvent);

ES_Event_t Event2Post;
Event2Post.EventType = ES_INIT_TITLE_JOYSTICK;
PostSpaceship(Event2Post);
}
break;
#ifdef KEY_EVENTS
//if key events mode is defined, respond to a key press by generating a button
event
case ES_NEW_KEY:
{
ES_Event_t keyEvent;
switch(ThisEvent.EventParam)
{
case 'b':
{
keyEvent.EventType = ES_BUTTON;
#ifdef DEBUG
puts("\rKeyboard Button Event Detected\r\n");
#endif
PostGameplay(keyEvent);
}
break;
case 's': //prints current state- does not post event
{
#ifdef DEBUG
puts("\rCurrent State: Game Over\r\n");
printf("\rFinal Score: %d\r\n", score);
#endif
}
break;
}
}
break;

#endif
// repeat cases as required for relevant events
default:
;
} // end switch on CurrentEvent
}
break;
default:
;
} // end switch on Current State

//Prints current state


//#ifdef DEBUG
//char *stateInChar;
//if(CurrentState == 0){
// stateInChar = "Init ";
//}else if(CurrentState == 1){
// stateInChar = "Title";
//}else if(CurrentState == 2){
// stateInChar = "Game ";
//}else if(CurrentState == 3){
// stateInChar = "End ";
//}
//puts("\r");
//printf("\rCurrent State: %s\r\n", stateInChar);
//#endif

return ReturnEvent;
}
/****************************************************************************
Function
QueryTemplateSM

Parameters
None

Returns
TemplateState_t The current state of the Template state machine

Description
returns the current state of the Template state machine
Notes

Author
J. Edward Carryer, 10/23/11, 19:21
****************************************************************************/
GameplayState_t QueryGameplay(void)
{
return CurrentState;
}

uint16_t QueryGameplayScore(void){
return score;
}

uint8_t QueryGameplayCollisions(void){
return numCollisions;
}
/***************************************************************************
private functions
***************************************************************************/

You might also like