"Es - Configure.H" "Es - Framework.H" "Gameplay.H" "Enemyship.H" "Projectile.H" "Spaceship.H" "Updateoled.H" "Dotstarservice.H"
"Es - Configure.H" "Es - Framework.H" "Gameplay.H" "Enemyship.H" "Projectile.H" "Spaceship.H" "Updateoled.H" "Dotstarservice.H"
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"
#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
*/
// 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;
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;
ES_Event_t titleEvent;
titleEvent.EventType = ES_UPDATE_OLED;
titleEvent.EventParam = titlescreenID;
PostUpdateOLED(titleEvent);
#ifdef DEBUG
puts("\n\rGameplay Initialized\r\n");
#endif
}
}
break;
if(gameMode < 0)
{
gameMode = 2; // Wraps around from easy to hard
}
else if(gameMode > 2)
{
gameMode = 0; // Wraps around from hard to easy
}
// 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
PostEnemyShip(endEvent);
PostProjectile(endEvent);
PostSpaceship(endEvent);
PostGameplay(endEvent);
PostDotStarService(endEvent);//post game over event to this service
}
}
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
// 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
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
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
***************************************************************************/