Changed player-movement. From UP, DOWN, LEFT, RIGHT to LEFT, RIGHT. Turn relative to your snake's head.

This commit is contained in:
2025-12-18 14:09:04 +01:00
parent 5e6b042921
commit 0074ecd57b
4 changed files with 50 additions and 47 deletions

View File

@@ -47,34 +47,26 @@ static Simulation_Game_Input gather_input_for_local_player(uint8_t local_player_
switch(local_player_index) { switch(local_player_index) {
case 0: { case 0: {
return (Simulation_Game_Input) { return (Simulation_Game_Input) {
.up = IsKeyDown(KEY_W), .right = IsKeyPressed(KEY_D),
.down = IsKeyDown(KEY_S), .left = IsKeyPressed(KEY_A)
.right = IsKeyDown(KEY_D),
.left = IsKeyDown(KEY_A)
}; };
} }
case 1: { case 1: {
return (Simulation_Game_Input) { return (Simulation_Game_Input) {
.up = IsKeyDown(KEY_UP), .right = IsKeyPressed(KEY_RIGHT),
.down = IsKeyDown(KEY_DOWN), .left = IsKeyPressed(KEY_LEFT)
.right = IsKeyDown(KEY_RIGHT),
.left = IsKeyDown(KEY_LEFT)
}; };
} }
case 2: { case 2: {
return (Simulation_Game_Input) { return (Simulation_Game_Input) {
.up = IsKeyDown(KEY_I), .right = IsKeyPressed(KEY_L),
.down = IsKeyDown(KEY_K), .left = IsKeyPressed(KEY_J)
.right = IsKeyDown(KEY_L),
.left = IsKeyDown(KEY_J)
}; };
} }
case 3: { case 3: {
return (Simulation_Game_Input) { return (Simulation_Game_Input) {
.up = IsKeyDown(KEY_T), .right = IsKeyPressed(KEY_H),
.down = IsKeyDown(KEY_G), .left = IsKeyPressed(KEY_F)
.right = IsKeyDown(KEY_H),
.left = IsKeyDown(KEY_F)
}; };
} }
default: { default: {
@@ -96,10 +88,8 @@ static void state_tick(Presentation_State *state) {
{ // Push local input to queue. { // Push local input to queue.
if(instance->amount_of_local_players == 1) { // If playing alone (either singleplayer or online-multiplayer), more inputs are allowed. 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) { Simulation_Game_Input input = (Simulation_Game_Input) {
.up = IsKeyDown(KEY_UP) || IsKeyDown(KEY_W), .right = IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_D),
.down = IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S), .left = IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_A)
.right = IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D),
.left = IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)
}; };
Locally_Controlled_Player *player = &instance->locally_controlled_players[0]; Locally_Controlled_Player *player = &instance->locally_controlled_players[0];

View File

@@ -2,20 +2,11 @@
#define SIM_GAME_INPUT_H #define SIM_GAME_INPUT_H
typedef struct { 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 right;
bool left; bool left;
} Simulation_Game_Input; } Simulation_Game_Input;
static inline bool simulation_input_equal(Simulation_Game_Input a, Simulation_Game_Input b) { 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) { if(a.right != b.right) {
return false; return false;
} }

View File

@@ -141,9 +141,11 @@ void simulation_world_tick(Simulation_World *simulation_world) {
continue; continue;
} }
// printf("* Input for player %i - up: %i, down: %i, right: %i, left: %i.\n", // 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 - right: %i, left: %i.\n",
// i, // 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); Entity *player_entity = game_world_try_get_entity_by_id(gw, player->entity_id);
@@ -151,29 +153,49 @@ void simulation_world_tick(Simulation_World *simulation_world) {
// Snakes can't go backwards. They have to turn. // 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) { switch(player_entity->move_direction) {
case Entity_Movement_Direction_None: { case Entity_Movement_Direction_None: {
break; break;
} }
case Entity_Movement_Direction_Up: case Entity_Movement_Direction_Up: {
case Entity_Movement_Direction_Down: // Snakes can only go right/left if it's currently moving up/down. if(input.right) {
{
if(player->input.right) {
player_entity->move_direction = Entity_Movement_Direction_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; player_entity->move_direction = Entity_Movement_Direction_Left;
} }
break; break;
} }
case Entity_Movement_Direction_Right: case Entity_Movement_Direction_Down: {
case Entity_Movement_Direction_Left: // Snakes can only go up/down if it's currently moving right/left. if(input.right) {
{ player_entity->move_direction = Entity_Movement_Direction_Left;
if(player->input.up) { }
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; player_entity->move_direction = Entity_Movement_Direction_Up;
} }
else if(player->input.down) {
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; player_entity->move_direction = Entity_Movement_Direction_Down;
} }

View File

@@ -16,7 +16,7 @@
- [x] When a player disconnects, all children should recursively be destroyed too. Not just the head. - [x] When a player disconnects, all children should recursively be destroyed too. Not just the head.
- [ ] Send networked input. - [ ] Send networked input.
- [ ] Set up session when joining the host. - [ ] 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. - Only inputs needed are RIGHT and LEFT. Turn relative to your head.
- Right now we're moving relative to world-up. - 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. - This would fix issues with invalid inputs, for example when the player inputs RIGHT when going LEFT.