plugins: snake: simplification in direction calculation (#1361)
Co-authored-by: hedger <hedger@users.noreply.github.com>
This commit is contained in:
parent
43fd2e4433
commit
05b7b7f2db
@ -29,6 +29,8 @@ typedef enum {
|
|||||||
GameStateGameOver,
|
GameStateGameOver,
|
||||||
} GameState;
|
} GameState;
|
||||||
|
|
||||||
|
// Note: do not change without purpose. Current values are used in smart
|
||||||
|
// orthogonality calculation in `snake_game_get_turn_snake`.
|
||||||
typedef enum {
|
typedef enum {
|
||||||
DirectionUp,
|
DirectionUp,
|
||||||
DirectionRight,
|
DirectionRight,
|
||||||
@ -195,44 +197,9 @@ static bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Direction snake_game_get_turn_snake(SnakeState const* const snake_state) {
|
static Direction snake_game_get_turn_snake(SnakeState const* const snake_state) {
|
||||||
switch(snake_state->currentMovement) {
|
// Sum of two `Direction` lies between 0 and 6, odd values indicate orthogonality.
|
||||||
case DirectionUp:
|
bool is_orthogonal = (snake_state->currentMovement + snake_state->nextMovement) % 2 == 1;
|
||||||
switch(snake_state->nextMovement) {
|
return is_orthogonal ? snake_state->nextMovement : snake_state->currentMovement;
|
||||||
case DirectionRight:
|
|
||||||
return DirectionRight;
|
|
||||||
case DirectionLeft:
|
|
||||||
return DirectionLeft;
|
|
||||||
default:
|
|
||||||
return snake_state->currentMovement;
|
|
||||||
}
|
|
||||||
case DirectionRight:
|
|
||||||
switch(snake_state->nextMovement) {
|
|
||||||
case DirectionUp:
|
|
||||||
return DirectionUp;
|
|
||||||
case DirectionDown:
|
|
||||||
return DirectionDown;
|
|
||||||
default:
|
|
||||||
return snake_state->currentMovement;
|
|
||||||
}
|
|
||||||
case DirectionDown:
|
|
||||||
switch(snake_state->nextMovement) {
|
|
||||||
case DirectionRight:
|
|
||||||
return DirectionRight;
|
|
||||||
case DirectionLeft:
|
|
||||||
return DirectionLeft;
|
|
||||||
default:
|
|
||||||
return snake_state->currentMovement;
|
|
||||||
}
|
|
||||||
default: // case DirectionLeft:
|
|
||||||
switch(snake_state->nextMovement) {
|
|
||||||
case DirectionUp:
|
|
||||||
return DirectionUp;
|
|
||||||
case DirectionDown:
|
|
||||||
return DirectionDown;
|
|
||||||
default:
|
|
||||||
return snake_state->currentMovement;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Point snake_game_get_next_step(SnakeState const* const snake_state) {
|
static Point snake_game_get_next_step(SnakeState const* const snake_state) {
|
||||||
|
Loading…
Reference in New Issue
Block a user