# HG changeset patch # User Mike Becker # Date 1755726700 -7200 # Node ID 6ed9fb9662c03696a28961e962942025cf481a05 # Parent ddcf637587ce1bf89bb0e0ec06d5b62a6baac920 move player controls into a behavior diff -r ddcf637587ce -r 6ed9fb9662c0 demo/snake/snake.c --- 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"