Skip to content

Commit

Permalink
States may implement the Free callback, which runs after a state tran…
Browse files Browse the repository at this point in the history
…sition just before the new state's Init.
  • Loading branch information
alxm committed May 29, 2018
1 parent 38860a3 commit 935f8cd
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 10 deletions.
16 changes: 16 additions & 0 deletions DefendPluto/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef struct {
ZStateInit* init;
ZStateTick* tick;
ZStateDraw* draw;
ZStateFree* free;
ZSwipeId intro;
ZSwipeId outro;
} ZState;
Expand All @@ -56,62 +57,71 @@ static const ZState g_states[Z_STATE_NUM] = {
z_state_died_init,
z_state_died_tick,
z_state_died_draw,
z_state_died_free,
Z_SWIPE_INVALID,
Z_SWIPE_HIDE,
},
[Z_STATE_INTRO] = {
z_state_intro_init,
z_state_intro_tick,
z_state_intro_draw,
z_state_intro_free,
Z_SWIPE_INVALID,
Z_SWIPE_HIDE,
},
[Z_STATE_NEW] = {
z_state_new_init,
NULL,
NULL,
NULL,
Z_SWIPE_INVALID,
Z_SWIPE_INVALID,
},
[Z_STATE_NEXT] = {
z_state_next_init,
z_state_next_tick,
z_state_next_draw,
z_state_next_free,
Z_SWIPE_SHOW,
Z_SWIPE_HIDE,
},
[Z_STATE_OVER] = {
z_state_over_init,
z_state_over_tick,
z_state_over_draw,
NULL,
Z_SWIPE_SHOW,
Z_SWIPE_HIDE,
},
[Z_STATE_PAUSE] = {
z_state_pause_init,
z_state_pause_tick,
z_state_pause_draw,
NULL,
Z_SWIPE_INVALID,
Z_SWIPE_INVALID,
},
[Z_STATE_PLAY] = {
NULL,
z_state_play_tick,
z_state_play_draw,
z_state_play_free,
Z_SWIPE_SHOW,
Z_SWIPE_HIDE,
},
[Z_STATE_TITLE] = {
z_state_title_init,
z_state_title_tick,
z_state_title_draw,
NULL,
Z_SWIPE_SHOW,
Z_SWIPE_HIDE,
},
[Z_STATE_WIN] = {
z_state_win_init,
z_state_win_tick,
z_state_win_draw,
NULL,
Z_SWIPE_SHOW,
Z_SWIPE_HIDE,
},
Expand Down Expand Up @@ -168,6 +178,12 @@ void z_state_tick(void)
z_light_tick();

if(g_state.next != Z_STATE_INVALID && g_swipe.swipeOut == Z_SWIPE_INVALID) {
if(g_state.current != Z_STATE_INVALID
&& g_states[g_state.current].free) {

g_states[g_state.current].free();
}

g_state.current = g_state.next;
g_state.next = Z_STATE_INVALID;

Expand Down
1 change: 1 addition & 0 deletions DefendPluto/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef enum Z_ENUM_PACK {
typedef void (ZStateInit)(void);
typedef void (ZStateTick)(void);
typedef void (ZStateDraw)(void);
typedef void (ZStateFree)(void);

extern void z_state_setup(void);

Expand Down
6 changes: 6 additions & 0 deletions DefendPluto/state_died.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ void z_state_died_draw(void)
z_pool_draw(Z_POOL_PARTICLE, z_particle_draw);
z_hud_draw();
}

void z_state_died_free(void)
{
z_timer_stop(Z_TIMER_G1);
z_timer_stop(Z_TIMER_G2);
}
3 changes: 2 additions & 1 deletion DefendPluto/state_died.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2017 Alex Margarit <alex@alxm.org>
Copyright 2017, 2018 Alex Margarit <alex@alxm.org>
Defend Pluto is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -22,3 +22,4 @@
extern ZStateInit z_state_died_init;
extern ZStateTick z_state_died_tick;
extern ZStateDraw z_state_died_draw;
extern ZStateFree z_state_died_free;
7 changes: 6 additions & 1 deletion DefendPluto/state_intro.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void z_state_intro_tick(void)
} break;

case 2: {
if(g_height <= z_sprite_getHeight(Z_SPRITE_ALXM)) {
if(g_height < z_sprite_getHeight(Z_SPRITE_ALXM)) {
g_height = i16(g_height + 2);
}
} break;
Expand Down Expand Up @@ -86,3 +86,8 @@ void z_state_intro_draw(void)
} break;
}
}

void z_state_intro_free(void)
{
z_timer_stop(Z_TIMER_G1);
}
1 change: 1 addition & 0 deletions DefendPluto/state_intro.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
extern ZStateInit z_state_intro_init;
extern ZStateTick z_state_intro_tick;
extern ZStateDraw z_state_intro_draw;
extern ZStateFree z_state_intro_free;
12 changes: 6 additions & 6 deletions DefendPluto/state_next.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "util_screen.h"
#include "util_str.h"

static uint8_t g_level;

void z_state_next_init(void)
{
z_hud_reset();
Expand All @@ -38,8 +36,6 @@ void z_state_next_init(void)
z_pool_clear(Z_POOL_BULLETP);

z_sfx_play(Z_SFX_LEVEL_COMPLETE);

g_level = z_player_getLevel();
}

void z_state_next_tick(void)
Expand All @@ -53,7 +49,6 @@ void z_state_next_tick(void)
if(z_button_pressedOnce(Z_BUTTON_A)) {
z_sfx_play(Z_SFX_PRESSED_A);
z_state_set(Z_STATE_PLAY, true);
z_player_setLevel(u8(g_level + 1));
}
}

Expand All @@ -69,7 +64,7 @@ void z_state_next_draw(void)
Z_FONT_FACE_YELLOWO,
Z_FONT_ALIGN_L);

z_font_int(g_level,
z_font_int(z_player_getLevel(),
61,
23,
Z_FONT_FACE_YELLOWO,
Expand All @@ -83,3 +78,8 @@ void z_state_next_draw(void)

z_screen_drawPressA(Z_SCREEN_W / 2, 48, Z_FONT_FACE_REDO, Z_FONT_ALIGN_C);
}

void z_state_next_free(void)
{
z_player_setLevel(u8(z_player_getLevel() + 1));
}
1 change: 1 addition & 0 deletions DefendPluto/state_next.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
extern ZStateInit z_state_next_init;
extern ZStateTick z_state_next_tick;
extern ZStateDraw z_state_next_draw;
extern ZStateFree z_state_next_free;
6 changes: 5 additions & 1 deletion DefendPluto/state_play.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ void z_state_play_tick(void)
z_hud_tick();

if(z_state_changed()) {
z_light_setBackground(Z_COLOR_INVALID);
return;
}

Expand All @@ -71,3 +70,8 @@ void z_state_play_draw(void)
z_player_draw();
z_hud_draw();
}

void z_state_play_free(void)
{
z_light_setBackground(Z_COLOR_INVALID);
}
3 changes: 2 additions & 1 deletion DefendPluto/state_play.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2017 Alex Margarit <alex@alxm.org>
Copyright 2017, 2018 Alex Margarit <alex@alxm.org>
Defend Pluto is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -21,3 +21,4 @@

extern ZStateTick z_state_play_tick;
extern ZStateDraw z_state_play_draw;
extern ZStateFree z_state_play_free;

0 comments on commit 935f8cd

Please sign in to comment.