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

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

This document contains code for a service that updates projectile locations in a Galaga-style game. It includes function definitions and constants used for tracking projectile state and movement. Projectiles are stored in an array and moved each timer interval. Collisions are detected by checking if a projectile overlaps an enemy ship location. The service responds to button presses by adding new projectiles and game events by updating projectile positions and removing any that leave the screen or collide with ships.

Uploaded by

api-532411015
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

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

This document contains code for a service that updates projectile locations in a Galaga-style game. It includes function definitions and constants used for tracking projectile state and movement. Projectiles are stored in an array and moved each timer interval. Collisions are detected by checking if a projectile overlaps an enemy ship location. The service responds to button presses by adding new projectiles and game events by updating projectile positions and removing any that leave the screen or collide with ships.

Uploaded by

api-532411015
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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

Module
Projectile.c

Revision
1.0.1

Description
This is a Service to update projectile 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 "Projectile.h"
#include "UpdateOLED.h"
#include "Spaceship.h"
#include "EnemyShip.h"
#include "Gameplay.h"
#include "DotStarService.h"

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


#define OLED_TEST
#define TimerTime 50 //every 200 ms projectile will move forward
#define ENEMY_WIDTH 9
#define ENEMY_HEIGHT 9
#define CORRIDOR 3
#define BOX_SIZE 12
#define LENGTH_OF_SCREEN 128
#define WIDTH_OF_SCREEN 64
#define PIXELS_TO_MOVE 2

#define TEMP_TEST

/*---------------------------- 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

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

static ProjectileLoc ProjectileArray[MAX_PROJECTILE_COUNT+1]; //+1 so there is a bumper of


sorts which you can save with status = false
static uint8_t ProjectileCount;
static ProjectileState_t CurrentState;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitProjectile

Parameters
uint8_t : the priorty of this service

Returns
bool, false if error in initialization, true otherwise

Description
Saves away the priority, and
Notes

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

MyPriority = Priority;

ThisEvent.EventType = ES_INIT;
CurrentState = InitProjectileState;
// post the initial transition event
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}

/****************************************************************************
Function
PostProjectile

Parameters
ES_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
C. Paullin
****************************************************************************/
bool PostProjectile(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunProjectile

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
C. Paullin
****************************************************************************/
ES_Event_t RunProjectile(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
ProjectileState_t NextState = CurrentState;
switch (CurrentState)
{
case InitProjectileState:
{
if(ThisEvent.EventType==ES_INIT){
uint8_t i;
//initialize array with all false values
for(i=0;i<MAX_PROJECTILE_COUNT+1;i++){
ProjectileArray[i].status = false;
}
// no projectiles exist
ProjectileCount =0;
//move to out of gameplay until game started
NextState = OutOfGameplay;
}
}break; //end projectile init state
case(Gameplay):
{
switch (ThisEvent.EventType)
{
case ES_BUTTON: // If event is button press, add projectile
{
if(ProjectileCount <MAX_PROJECTILE_COUNT){
ProjectileLoc newShipLoc; // to save the x pos of the spaceship to
newShipLoc.xpos = QuerySpaceshipPosition();//get spaceship x position
newShipLoc.ypos = WIDTH_OF_SCREEN -11;//spaceship always at same y loc
newShipLoc.status = true;
ProjectileArray[ProjectileCount] = newShipLoc; //add projectile at that
location
ProjectileArray[ProjectileCount].status = true; //Make that projectile
true to indicate it is in use
//init timer for projectile only if no projectiles already in array
if(ProjectileCount == 0 ){
ES_Timer_InitTimer(PROJECTILE_TIMER,TimerTime);
}

ProjectileCount++;
}
}
break;
case ES_TIMEOUT:
{

uint8_t i;
uint8_t xidx;
uint8_t yidx;
for(i = 0; i< ProjectileCount;i++){
ProjectileArray[i].ypos -= PIXELS_TO_MOVE; //move up by 2 pixels every
50 ms
if(ProjectileArray[i].ypos>WIDTH_OF_SCREEN){ //if outside of bounds,
remove that projectile since unsigned int, it will not go negative
ProjectileArray[i] = ProjectileArray[ProjectileCount-1]; //move last
element to the position now vacated
ProjectileArray[ProjectileCount-1].status = false; //vacate former
position
ProjectileCount--;// decrement count of active projectiles

ES_Event_t newEvent;
newEvent.EventType = ES_PROJ_MISS;
PostGameplay(newEvent);
}else{
xidx = (ProjectileArray[i].xpos-(CORRIDOR+2))/(ENEMY_WIDTH+CORRIDOR);
yidx = (ProjectileArray[i].ypos-2)/(ENEMY_HEIGHT+CORRIDOR);
// if this is in the box (1,4) row 1 column 4, it will pass the number
14 to the
//query location status function, that is what passIdx is checking,
only works with two single digit numbers
uint8_t passIdx = yidx*10+xidx;
//following boolean will be true when inside of the width x height
//box that the ship can occupy

bool notInCorridor = ((((ProjectileArray[i].xpos-(CORRIDOR))%


(BOX_SIZE))>=CORRIDOR)
&& (((BOX_SIZE)-((ProjectileArray[i].ypos-2)%
(BOX_SIZE)))>=CORRIDOR));

//following code will remove projectile in case that it collides


//with ship, or that it is outside of bounds of screen
//it is constructed so that active projectiles
//will always be in first indices

if(notInCorridor&&QueryLocationStatus(passIdx)){
ProjectileArray[i] = ProjectileArray[ProjectileCount-1]; //move
last element to the position now vacated
ProjectileArray[ProjectileCount-1].status = false; //vacate former
position
ProjectileCount--;
ES_Event_t Event2Post;
Event2Post.EventType = ES_COLLISION;
Event2Post.EventParam = passIdx;
/*post to gameplay so score can be incremented
*and post to enemyship so ship can be removed
*/
PostGameplay(Event2Post);
PostEnemyShip(Event2Post);
//Post to dotstar so that can flash
PostDotStarService(Event2Post);
}// end if not in corridor
}
}
ES_Event_t Event2Post;
Event2Post.EventParam = projectileID; //prepare event to send to oled
Event2Post.EventType = ES_UPDATE_OLED; // basically lets led know that it
should be updated
PostUpdateOLED(Event2Post);
if(ProjectileCount>0){
ES_Timer_InitTimer(PROJECTILE_TIMER,TimerTime);
}
}break;
case ES_GAME_OVER:
{
uint8_t i;
//clear all elements of the projectile array
for(i=0 ;i<MAX_PROJECTILE_COUNT;i++){
ProjectileArray[i].status = false;
}
ProjectileCount =0;
//change state
NextState = OutOfGameplay;
}break;

default:
;
} // end switch on ThisEvent
}break;//end case on Gameplay current state
case OutOfGameplay:
{
if(ThisEvent.EventType == ES_START_GAME){
NextState = Gameplay;
}
}
break; // end case out of gameplay
default:
;
}
CurrentState = NextState;
return ReturnEvent;
}

/****************************************************************************
Function
QueryProjectilePosition

Parameters
position array

Returns
none

Description
populates position array that is passed in
Notes

Author
C. Paullin
****************************************************************************/
void QueryProjectilePosition(ProjectileLoc *posArray){
uint8_t i;
for(i=0; i<MAX_PROJECTILE_COUNT+1; i++){
posArray[i] = ProjectileArray[i];
}
return;

}
/***************************************************************************
private functions
***************************************************************************/

You might also like