Wed, 20 Aug 2025 23:51:40 +0200
move player controls into a behavior
demo/snake/snake.c | file | annotate | diff | comparison | revisions |
--- a/demo/snake/snake.c Wed Aug 20 23:45:23 2025 +0200 +++ b/demo/snake/snake.c Wed Aug 20 23:51:40 2025 +0200 @@ -382,7 +382,12 @@ } } -static void player_controls(Player *player) { +static void player_controls(AscBehavior *behavior) { + // TODO: instead of checking the game state, disable the behavior when the player is dead + if (game.state != GAME_STATE_PLAYING) return; + + Player *player = behavior->node->user_data; + // TODO: different key bindings for different player number in hot seat mode if (asc_key_pressed(ASC_KEY(LEFT))) { if (player->direction != MOVE_RIGHT) { player->target_direction = MOVE_LEFT; @@ -439,6 +444,8 @@ cxDefineDestructor(player->trace, player_trace_release_tile); node->draw_func = player_draw; node->user_data_free_func = (cx_destructor_func2)player_destroy; + // add behaviors (the order is important!) + asc_behavior_add(node, player_controls); asc_behavior_add(node, player_move); return player; } @@ -560,11 +567,8 @@ } // game states - // TODO: move all this into behaviors - if (game.state == GAME_STATE_PLAYING) { - // TODO: implement hot seat 1on1 multiplayer - player_controls(game.players[0]); - } else if (game.state == GAME_STATE_GAME_OVER) { + // TODO: move this into a behavior - probably add a state machine that enables/disables behaviors + if (game.state == GAME_STATE_GAME_OVER) { asc_scene_node_show(text_game_over); if (asc_key_pressed(ASC_KEY(R))) { // TODO: re-load the "level"