Add game events

This commit is contained in:
Miloslav Číž 2020-10-01 11:47:28 +02:00
parent 9c2cbe6d0e
commit a91092401d
4 changed files with 72 additions and 6 deletions

41
game.h
View File

@ -118,6 +118,24 @@ void SFG_playSound(uint8_t soundIndex, uint8_t volume);
*/ */
void SFG_enableMusic(uint8_t enable); void SFG_enableMusic(uint8_t enable);
#define SFG_EVENT_VIBRATE 0 ///< the controller should vibrate (or blink etc.)
#define SFG_EVENT_PLAYER_HURT 1
#define SFG_EVENT_PLAYER_DIES 2
#define SFG_EVENT_LEVEL_STARTS 3
#define SFG_EVENT_LEVEL_WON 4
#define SFG_EVENT_MONSTER_DIES 5
#define SFG_EVENT_PLAYER_TAKES_ITEM 6
#define SFG_EVENT_EXPLOSION 7
#define SFG_EVENT_PLAYER_TELEPORTS 8
#define SFG_EVENT_PLAYER_CHANGES_WEAPON 9
/**
This is an optional function that informs the frontend about special events
which may trigger something special, such as a controller vibration, logging
something etc. This function can do nothing.
*/
void SFG_processEvent(uint8_t event, uint8_t data);
#define SFG_SAVE_SIZE 12 #define SFG_SAVE_SIZE 12
/** /**
@ -1602,8 +1620,8 @@ void SFG_setAndInitLevel(uint8_t levelNumber)
SFG_game.spriteAnimationFrame = 0; SFG_game.spriteAnimationFrame = 0;
SFG_initPlayer(); SFG_initPlayer();
SFG_setGameState(SFG_GAME_STATE_LEVEL_START); SFG_setGameState(SFG_GAME_STATE_LEVEL_START);
SFG_processEvent(SFG_EVENT_LEVEL_STARTS,levelNumber);
} }
void SFG_init() void SFG_init()
@ -1879,7 +1897,11 @@ void SFG_playerChangeHealth(int8_t healthAdd)
SFG_player.health = health; SFG_player.health = health;
if (healthAdd < 0) if (healthAdd < 0)
{
SFG_player.lastHurtFrame = SFG_game.frame; SFG_player.lastHurtFrame = SFG_game.frame;
SFG_processEvent(SFG_EVENT_VIBRATE,0);
SFG_processEvent(SFG_EVENT_PLAYER_HURT,-1 * healthAdd);
}
} }
void SFG_playerChangeHealthWithMiltiplier(int8_t healthAdd) void SFG_playerChangeHealthWithMiltiplier(int8_t healthAdd)
@ -1986,6 +2008,7 @@ void SFG_createExplosion(RCL_Unit x, RCL_Unit y, RCL_Unit z)
SFG_ProjectileRecord explosion; SFG_ProjectileRecord explosion;
SFG_playGameSound(2,SFG_distantSoundVolume(x,y,z)); SFG_playGameSound(2,SFG_distantSoundVolume(x,y,z));
SFG_processEvent(SFG_EVENT_EXPLOSION,0);
explosion.type = SFG_PROJECTILE_EXPLOSION; explosion.type = SFG_PROJECTILE_EXPLOSION;
@ -2785,6 +2808,8 @@ void SFG_updateLevel()
monster->stateType = (monster->stateType & SFG_MONSTER_MASK_TYPE) | monster->stateType = (monster->stateType & SFG_MONSTER_MASK_TYPE) |
SFG_MONSTER_STATE_DYING; SFG_MONSTER_STATE_DYING;
SFG_processEvent(SFG_EVENT_MONSTER_DIES,SFG_MR_TYPE(*monster));
if (SFG_MR_TYPE(*monster) == SFG_LEVEL_ELEMENT_MONSTER_EXPLODER) if (SFG_MR_TYPE(*monster) == SFG_LEVEL_ELEMENT_MONSTER_EXPLODER)
SFG_createExplosion( SFG_createExplosion(
SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]), SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]),
@ -3318,8 +3343,11 @@ void SFG_gameStepPlaying()
case SFG_LEVEL_ELEMENT_FINISH: case SFG_LEVEL_ELEMENT_FINISH:
SFG_levelEnds(); SFG_levelEnds();
SFG_playGameSound(2,255);
SFG_setGameState(SFG_GAME_STATE_WIN); SFG_setGameState(SFG_GAME_STATE_WIN);
SFG_playGameSound(2,255);
SFG_processEvent(SFG_EVENT_VIBRATE,0);
SFG_processEvent(
SFG_EVENT_LEVEL_WON,SFG_currentLevel.levelNumber + 1);
eliminate = 0; eliminate = 0;
break; break;
@ -3335,6 +3363,7 @@ void SFG_gameStepPlaying()
SFG_player.lastItemTakenFrame = SFG_game.frame; SFG_player.lastItemTakenFrame = SFG_game.frame;
i--; i--;
SFG_playGameSound(3,255); SFG_playGameSound(3,255);
SFG_processEvent(SFG_EVENT_PLAYER_TAKES_ITEM,e->type);
#endif #endif
} }
else if ( else if (
@ -3376,6 +3405,7 @@ void SFG_gameStepPlaying()
SFG_player.justTeleported = 1; SFG_player.justTeleported = 1;
SFG_playGameSound(4,255); SFG_playGameSound(4,255);
SFG_processEvent(SFG_EVENT_PLAYER_TELEPORTS,0);
break; break;
} }
@ -3598,6 +3628,8 @@ void SFG_gameStepPlaying()
{ {
SFG_LOG("player dies"); SFG_LOG("player dies");
SFG_levelEnds(); SFG_levelEnds();
SFG_processEvent(SFG_EVENT_VIBRATE,0);
SFG_processEvent(SFG_EVENT_PLAYER_DIES,0);
SFG_setGameState(SFG_GAME_STATE_LOSE); SFG_setGameState(SFG_GAME_STATE_LOSE);
} }
#endif #endif
@ -4707,6 +4739,8 @@ uint8_t SFG_mainLoopBody()
uint8_t steps = 0; uint8_t steps = 0;
uint8_t previousWeapon = SFG_player.weapon;
// perform game logic (physics etc.), for each frame // perform game logic (physics etc.), for each frame
while (timeSinceLastFrame >= SFG_MS_PER_FRAME) while (timeSinceLastFrame >= SFG_MS_PER_FRAME)
{ {
@ -4718,6 +4752,9 @@ uint8_t SFG_mainLoopBody()
steps++; steps++;
} }
if (SFG_player.weapon != previousWeapon)
SFG_processEvent(SFG_EVENT_PLAYER_CHANGES_WEAPON,SFG_player.weapon);
if ((steps > 1) && (SFG_game.antiSpam == 0)) if ((steps > 1) && (SFG_game.antiSpam == 0))
{ {
SFG_LOG("failed to reach target FPS! consider setting a lower value") SFG_LOG("failed to reach target FPS! consider setting a lower value")

View File

@ -12,6 +12,8 @@
whatsoever. whatsoever.
*/ */
// #define JOYHAT // compiles the version for Pokitto with joystick hat
// #define SFG_START_LEVEL 8 // #define SFG_START_LEVEL 8
#include <stdio.h> #include <stdio.h>
@ -23,8 +25,15 @@
#define SFG_FPS 25 #define SFG_FPS 25
#define SFG_CAN_EXIT 0 #define SFG_CAN_EXIT 0
//#define SFG_TEXTURE_DISTANCE 6000 //#define SFG_TEXTURE_DISTANCE 6000
#define SFG_SCREEN_RESOLUTION_X 110
#define SFG_SCREEN_RESOLUTION_Y 88 #ifndef JOYHAT
#define SFG_SCREEN_RESOLUTION_X 110
#define SFG_SCREEN_RESOLUTION_Y 88
#else
#define SFG_SCREEN_RESOLUTION_X 88
#define SFG_SCREEN_RESOLUTION_Y 110
#endif
#define SFG_RESOLUTION_SCALEDOWN 1 #define SFG_RESOLUTION_SCALEDOWN 1
#define SFG_DITHERED_SHADOW 0 #define SFG_DITHERED_SHADOW 0
#define SFG_DIMINISH_SPRITES 0 #define SFG_DIMINISH_SPRITES 0
@ -56,7 +65,12 @@ uint8_t *pokittoScreen;
void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex) void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
{ {
#ifndef JOYHAT
pokittoScreen[y * SFG_SCREEN_RESOLUTION_X + x] = colorIndex; pokittoScreen[y * SFG_SCREEN_RESOLUTION_X + x] = colorIndex;
#else
pokittoScreen[x * SFG_SCREEN_RESOLUTION_Y + (SFG_SCREEN_RESOLUTION_Y - 1 - y)]
= colorIndex;
#endif
} }
uint32_t SFG_getTimeMs() uint32_t SFG_getTimeMs()
@ -150,6 +164,13 @@ void SFG_save(uint8_t data[SFG_SAVE_SIZE])
timerInit(8000); timerInit(8000);
} }
void SFG_processEvent(uint8_t event, uint8_t data)
{
#ifdef JOYHAT
// TODO: vibrate
#endif
}
uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE]) uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
{ {
for (uint8_t i = 0; i < SFG_SAVE_SIZE; ++i) for (uint8_t i = 0; i < SFG_SAVE_SIZE; ++i)

View File

@ -33,8 +33,8 @@
#define SFG_REVEAL_MAP 1 #define SFG_REVEAL_MAP 1
// #define SFG_INFINITE_AMMO 1 // #define SFG_INFINITE_AMMO 1
// #define SFG_SCREEN_RESOLUTION_X 127 #define SFG_SCREEN_RESOLUTION_X 88
// #define SFG_SCREEN_RESOLUTION_Y 42 #define SFG_SCREEN_RESOLUTION_Y 110
// #define SFG_SCREEN_RESOLUTION_X 80 // #define SFG_SCREEN_RESOLUTION_X 80
// #define SFG_SCREEN_RESOLUTION_Y 64 // #define SFG_SCREEN_RESOLUTION_Y 64
@ -146,6 +146,10 @@ void SFG_getMouseOffset(int16_t *x, int16_t *y)
#endif #endif
} }
void SFG_processEvent(uint8_t event, uint8_t data)
{
}
int8_t SFG_keyPressed(uint8_t key) int8_t SFG_keyPressed(uint8_t key)
{ {
switch (key) switch (key)

View File

@ -103,6 +103,10 @@ void SFG_getMouseOffset(int16_t *x, int16_t *y)
{ {
} }
void SFG_processEvent(uint8_t event, uint8_t data)
{
}
int8_t SFG_keyPressed(uint8_t key) int8_t SFG_keyPressed(uint8_t key)
{ {
switch (key) switch (key)