From 0074ecd57bdf9ad1aa928968cc3180d1cce77312 Mon Sep 17 00:00:00 2001 From: samstalhandske Date: Thu, 18 Dec 2025 14:09:04 +0100 Subject: [PATCH] Changed player-movement. From UP, DOWN, LEFT, RIGHT to LEFT, RIGHT. Turn relative to your snake's head. --- src/game/presentation/states/state_ingame.c | 30 ++++------- src/game/simulation/input.h | 9 ---- src/game/simulation/simulation_world.c | 56 ++++++++++++++------- src/todo.md | 2 +- 4 files changed, 50 insertions(+), 47 deletions(-) diff --git a/src/game/presentation/states/state_ingame.c b/src/game/presentation/states/state_ingame.c index 5ed8750..b0ded9e 100644 --- a/src/game/presentation/states/state_ingame.c +++ b/src/game/presentation/states/state_ingame.c @@ -47,34 +47,26 @@ static Simulation_Game_Input gather_input_for_local_player(uint8_t local_player_ switch(local_player_index) { case 0: { return (Simulation_Game_Input) { - .up = IsKeyDown(KEY_W), - .down = IsKeyDown(KEY_S), - .right = IsKeyDown(KEY_D), - .left = IsKeyDown(KEY_A) + .right = IsKeyPressed(KEY_D), + .left = IsKeyPressed(KEY_A) }; } case 1: { return (Simulation_Game_Input) { - .up = IsKeyDown(KEY_UP), - .down = IsKeyDown(KEY_DOWN), - .right = IsKeyDown(KEY_RIGHT), - .left = IsKeyDown(KEY_LEFT) + .right = IsKeyPressed(KEY_RIGHT), + .left = IsKeyPressed(KEY_LEFT) }; } case 2: { return (Simulation_Game_Input) { - .up = IsKeyDown(KEY_I), - .down = IsKeyDown(KEY_K), - .right = IsKeyDown(KEY_L), - .left = IsKeyDown(KEY_J) + .right = IsKeyPressed(KEY_L), + .left = IsKeyPressed(KEY_J) }; } case 3: { return (Simulation_Game_Input) { - .up = IsKeyDown(KEY_T), - .down = IsKeyDown(KEY_G), - .right = IsKeyDown(KEY_H), - .left = IsKeyDown(KEY_F) + .right = IsKeyPressed(KEY_H), + .left = IsKeyPressed(KEY_F) }; } default: { @@ -96,10 +88,8 @@ static void state_tick(Presentation_State *state) { { // Push local input to queue. if(instance->amount_of_local_players == 1) { // If playing alone (either singleplayer or online-multiplayer), more inputs are allowed. Simulation_Game_Input input = (Simulation_Game_Input) { - .up = IsKeyDown(KEY_UP) || IsKeyDown(KEY_W), - .down = IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S), - .right = IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D), - .left = IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A) + .right = IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_D), + .left = IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_A) }; Locally_Controlled_Player *player = &instance->locally_controlled_players[0]; diff --git a/src/game/simulation/input.h b/src/game/simulation/input.h index 4362d06..9d6f906 100644 --- a/src/game/simulation/input.h +++ b/src/game/simulation/input.h @@ -2,20 +2,11 @@ #define SIM_GAME_INPUT_H typedef struct { - // Movement can probably just be one byte. For a traditional snake-game, only one directional-input is OK. - bool up; - bool down; bool right; bool left; } Simulation_Game_Input; static inline bool simulation_input_equal(Simulation_Game_Input a, Simulation_Game_Input b) { - if(a.up != b.up) { - return false; - } - if(a.down != b.down) { - return false; - } if(a.right != b.right) { return false; } diff --git a/src/game/simulation/simulation_world.c b/src/game/simulation/simulation_world.c index 6b6f3b2..2620397 100644 --- a/src/game/simulation/simulation_world.c +++ b/src/game/simulation/simulation_world.c @@ -140,43 +140,65 @@ void simulation_world_tick(Simulation_World *simulation_world) { if(!player->active) { continue; } + + // NOTE: SS - Not sure if this should be moved to game-world.. This is input that comes from the simulation. - // printf("* Input for player %i - up: %i, down: %i, right: %i, left: %i.\n", + // printf("* Input for player %i - right: %i, left: %i.\n", // i, - // player->input.up, player->input.down, player->input.right, player->input.left + // player->input.right, player->input.left // ); Entity *player_entity = game_world_try_get_entity_by_id(gw, player->entity_id); assert(player_entity != NULL); - + // Snakes can't go backwards. They have to turn. - + + // We only have input 'right' and 'up'. + Simulation_Game_Input input = player->input; + + // Turn relative to the player's current move_direction. switch(player_entity->move_direction) { case Entity_Movement_Direction_None: { break; } - case Entity_Movement_Direction_Up: - case Entity_Movement_Direction_Down: // Snakes can only go right/left if it's currently moving up/down. - { - if(player->input.right) { + case Entity_Movement_Direction_Up: { + if(input.right) { player_entity->move_direction = Entity_Movement_Direction_Right; } - else if(player->input.left) { + else if(input.left) { player_entity->move_direction = Entity_Movement_Direction_Left; } - + break; } - case Entity_Movement_Direction_Right: - case Entity_Movement_Direction_Left: // Snakes can only go up/down if it's currently moving right/left. - { - if(player->input.up) { - player_entity->move_direction = Entity_Movement_Direction_Up; + case Entity_Movement_Direction_Down: { + if(input.right) { + player_entity->move_direction = Entity_Movement_Direction_Left; } - else if(player->input.down) { + else if(input.left) { + player_entity->move_direction = Entity_Movement_Direction_Right; + } + + break; + } + case Entity_Movement_Direction_Right: { + if(input.right) { player_entity->move_direction = Entity_Movement_Direction_Down; } - + else if(input.left) { + player_entity->move_direction = Entity_Movement_Direction_Up; + } + + break; + } + case Entity_Movement_Direction_Left: { + if(input.right) { + player_entity->move_direction = Entity_Movement_Direction_Up; + } + else if(input.left) { + player_entity->move_direction = Entity_Movement_Direction_Down; + } + break; } } diff --git a/src/todo.md b/src/todo.md index 91c9e1c..268d7ce 100644 --- a/src/todo.md +++ b/src/todo.md @@ -16,7 +16,7 @@ - [x] When a player disconnects, all children should recursively be destroyed too. Not just the head. - [ ] Send networked input. - [ ] Set up session when joining the host. -- [ ] Change player-movement? +- [x] Change player-movement? - Only inputs needed are RIGHT and LEFT. Turn relative to your head. - Right now we're moving relative to world-up. - This would fix issues with invalid inputs, for example when the player inputs RIGHT when going LEFT.