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) {
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];

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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.