Add state

This commit is contained in:
Miloslav Číž 2020-10-24 16:17:02 +02:00
parent ec91af35ca
commit 0d7b6ffc2f
2 changed files with 50 additions and 38 deletions

16
game.h
View File

@ -315,7 +315,7 @@ typedef struct
int16_t direction[3]; /**< Added to position each game step. */
} SFG_ProjectileRecord;
#define SFG_GAME_STATE_MENU 0
#define SFG_GAME_STATE_INIT 0 ///< first state, waiting for key releases
#define SFG_GAME_STATE_PLAYING 1
#define SFG_GAME_STATE_WIN 2
#define SFG_GAME_STATE_LOSE 3
@ -323,6 +323,7 @@ typedef struct
#define SFG_GAME_STATE_OUTRO 5
#define SFG_GAME_STATE_MAP 6
#define SFG_GAME_STATE_LEVEL_START 7
#define SFG_GAME_STATE_MENU 8
#define SFG_MENU_ITEM_CONTINUE 0
#define SFG_MENU_ITEM_MAP 1
@ -1465,6 +1466,7 @@ uint8_t SFG_itemCollides(uint8_t elementType)
void SFG_setGameState(uint8_t state)
{
SFG_LOG("changing game state");
SFG_game.state = state;
SFG_game.stateChangeTime = SFG_game.frameTime;
}
@ -1728,7 +1730,7 @@ void SFG_init()
SFG_MUSIC_TURN_ON : SFG_MUSIC_TURN_OFF);
#if SFG_START_LEVEL == 0
SFG_setGameState(SFG_GAME_STATE_MENU);
SFG_setGameState(SFG_GAME_STATE_INIT);
#else
SFG_setAndInitLevel(SFG_START_LEVEL - 1);
#endif
@ -4787,6 +4789,9 @@ uint8_t SFG_mainLoopBody()
/* Standard deterministic game loop, independed of actual achieved FPS.
Each game logic (physics) frame is performed with the SFG_MS_PER_FRAME
delta time. */
if (SFG_game.state != SFG_GAME_STATE_INIT)
{
uint32_t timeNow = SFG_getTimeMs();
#if SFG_TIME_MULTIPLIER != 1024
@ -4839,6 +4844,13 @@ uint8_t SFG_mainLoopBody()
SFG_sleepMs(RCL_max(1,
(3 * (SFG_game.frameTime + SFG_MS_PER_FRAME - timeNow)) / 4));
}
}
else if (!SFG_keyPressed(SFG_KEY_A) && !SFG_keyPressed(SFG_KEY_B))
{
/* At the beginning we have to wait for the release of the keys in order not
to immediatelly confirm a menu item. */
SFG_setGameState(SFG_GAME_STATE_MENU);
}
return SFG_game.continues;
}

View File

@ -22,7 +22,7 @@
// #define SFG_UNLOCK_DOOR 1
// #define SFG_INFINITE_AMMO 1
#define SFG_FPS 22
#define SFG_FPS 20
#define SFG_CAN_EXIT 0
#define SFG_PLAYER_TURN_SPEED 135
//#define SFG_TEXTURE_DISTANCE 6000