Enemyship 2
Enemyship 2
Module
EnemyShip.c
Revision
1.0.1
Description
This is a Service to update enemy ship locations in galaga
Notes
History
When Who What/Why
-------------- --- --------
10/30/20 EM Created File
****************************************************************************/
/*----------------------------- 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 "EnemyShip.h"
#include "UpdateOLED.h"
#include "Gameplay.h"
#include "Spaceship.h"
#include "Projectile.h"
#include "DotStarService.h"
/*----------------------------- Module Defines ----------------------------*/
#define OLED_TEST
#define NUM_INIT_ROWS 14
#define TIME_STEP 1000
#define NUM_SHIFTS 0
#define LOSE 2
// how hard game is
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/
void moveShipsOddREvenL(void);
void moveShipsOddLEvenR(void);
void addRowOddREvenL(void);
void addRowOddLEvenR(void);
bool checkLastRow(void);
void setEnemysToZero(void);
void clearShip(uint8_t index);
/*---------------------------- 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 EnemyShipState_t CurrentState;
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
static enemyArray_t XWingPosition;
static uint16_t counter;
static bool enemyRow1[X_WING_COLS];
static bool enemyRow2[X_WING_COLS];
static bool enemyRow3[X_WING_COLS];
static bool enemyRow4[X_WING_COLS];
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 InitEnemyShip(uint8_t Priority)
{
ES_Event_t ThisEvent;
MyPriority = Priority;
// put us into the Initial PseudoState
CurrentState = InitEnemyShipState;
// uint8_t row;
// uint8_t col;
// for(row=0;row<X_WING_ROWS; row++){
// for(col=0;col<X_WING_COLS;col++){
// DEFAULT_ENEMY_POS[row][col] = false;
// }
// }
/****************************************************************************
Function
PostEnemyShip
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 PostEnemyShip(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunEnemyShip
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 RunEnemyShip(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
//SAMPLE FSM
switch (CurrentState)
{
ES_Event_t newEvent;
newEvent.EventType = ES_UPDATE_OLED;
newEvent.EventParam = enemyshipID;
PostUpdateOLED(newEvent);
countMoves=0;
}
}break;
case InitOddLEvenR:
switch (ThisEvent.EventType)
{
case ES_TIMEOUT: //If event is event timeout
{ // Execute action function for state one : event one
//first check if there are any true values in the last row, if so game
is over (a ship has made it to the bottom)
ES_Event_t newEvent;
newEvent.EventType = ES_UPDATE_OLED;
PostUpdateOLED(newEvent);
// if(countMoves < NUM_SHIFTS){
// countMoves++;
// if(counter > NUM_INIT_ROWS){
// CurrentState = OddLEvenR;
// }else{
// CurrentState = InitOddLEvenR;
// }
// }else{
// countMoves=0;
if(counter > NUM_INIT_ROWS){
CurrentState = OddLEvenR;
}else{
CurrentState = InitOddLEvenR;
}
// }
}
break;
case ES_COLLISION:
{
puts("\rCollision Received in Enemy Ships");
clearShip(ThisEvent.EventParam);
ES_Event_t newEvent;
newEvent.EventType = ES_UPDATE_OLED;
newEvent.EventParam = enemyshipID;
PostUpdateOLED(newEvent);
}break;
case ES_GAME_OVER:
{
CurrentState = Idle;
//XWingPosition = DEFAULT_ENEMY_POS;
setEnemysToZero();
counter = 0;
}break;
default:
{}
}break;
//NOTE: This state was created so that we could make more complex ship
movements
// however, after testing, we liked the basic zig zag movement best.
// Therefore, this state and OddREvenL are not used.
case InitOddREvenL:
switch (ThisEvent.EventType)
{
case ES_TIMEOUT: //If event is event timeout
{ // Execute action function for state one : event one
ES_Event_t newEvent;
newEvent.EventType = ES_UPDATE_OLED;
case OddLEvenR:
switch (ThisEvent.EventType)
{
case ES_TIMEOUT: //If event is event timeout
{ // Execute action function for state one : event one
ES_Event_t newEvent;
newEvent.EventType = ES_UPDATE_OLED;
case OddREvenL:
switch (ThisEvent.EventType)
{
case ES_TIMEOUT: //If event is event timeout
{ // Execute action function for state one : event one
ES_Event_t newEvent;
newEvent.EventType = ES_UPDATE_OLED;
/****************************************************************************
Function
QueryEnemyShip
Parameters
None
Returns
EnemyShipState_t The current state of the enemy ships state machine
Description
returns the current state of the Template state machine
Notes
Author
J. Edward Carryer, 10/23/11, 19:21
****************************************************************************/
EnemyShipState_t QueryEnemyShip(void)
{
return CurrentState;
}
uint8_t j;
for(j=0; j<X_WING_COLS; j++){
(*posArray)[0][j] = enemyRow1[j];
}
for(j=0; j<X_WING_COLS; j++){
(*posArray)[1][j] = enemyRow2[j];
}
for(j=0; j<X_WING_COLS; j++){
(*posArray)[2][j] = enemyRow3[j];
}
for(j=0; j<X_WING_COLS; j++){
(*posArray)[3][j] = enemyRow4[j];
}
}
void moveShipsOddLEvenR(void){
bool newRow1 [X_WING_COLS];
bool newRow2 [X_WING_COLS];
bool newRow3 [X_WING_COLS];
bool newRow4 [X_WING_COLS];
uint8_t i;
for(i=0; i<X_WING_COLS; i++){
newRow1[i] = false;
newRow2[i] = false;
newRow3[i] = false;
newRow4[i] = false;
}
for(i=0; i<X_WING_COLS-1; i++){
newRow2[i] = enemyRow1[i+1];
newRow3[i+1] = enemyRow2[i];
newRow4[i] = enemyRow3[i+1];
}
for(i=0; i<X_WING_COLS; i++){
enemyRow1[i] = newRow1[i];
enemyRow2[i] = newRow2[i];
enemyRow3[i] = newRow3[i];
enemyRow4[i] = newRow4[i];
}
void addRowOddREvenL(void){
uint8_t i;
for(i=0; i<X_WING_COLS-1; i+=2){ //stop at 1 col from right because for row 0
(even) it is offset to the left
enemyRow1[i] = true;
//XWingPosition[0][i] = true; //add in first row of ships
}
void addRowOddLEvenR(void){
uint8_t i;
for(i=1; i<X_WING_COLS; i+=2){ //starting with i=1 because for row 0 (even) it
is offset to the right
enemyRow1[i] = true;
//XWingPosition[0][i] = true; //add in first row of ships
}
}
bool checkLastRow(void){
uint8_t i;
bool returnVal = false;
for(i=0; i<X_WING_COLS; i++){
if(enemyRow4[i]){
returnVal = true;
}
// if(XWingPosition[X_WING_ROWS-1][i]){ //look at every value in the last
row
// returnVal = true;
// }
}
return returnVal;
}
void setEnemysToZero(void){
//XWingPosition = DEFAULT_ENEMY_POS;
uint8_t i;
for(i=0; i<X_WING_COLS; i++){
enemyRow1[i] = false;
enemyRow2[i] = false;
enemyRow3[i] = false;
enemyRow4[i] = false;
}
}