Skip to content

Commit

Permalink
Open mouth only when eating.
Browse files Browse the repository at this point in the history
  • Loading branch information
juanitobebe committed May 25, 2020
1 parent 0438295 commit 95dfbb3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
42 changes: 22 additions & 20 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ void main() {

UINT8 eating = 0;
UINT8 alive = 1;
UBYTE snake_new_direction =
0x0; // Placeholder, snake won't move until joypad press.
while (alive) {
AddToTimer();

// Register Input
UBYTE snake_previous_direction, snake_new_direction;
snake_previous_direction = snake_new_direction = snake_c.direction;
UBYTE snake_previous_direction = snake_c.direction;
switch (joypad()) {
case J_LEFT:
snake_new_direction = J_LEFT;
Expand All @@ -105,30 +106,31 @@ void main() {
}

// Move periodically
if ((GetTimeFromTimer() & (UBYTE)0x03) == 0) {
if ((GetTimeFromTimer() & (UBYTE)0x07) == 0) {
// Snake collisions
alive = !SnakeWallCollision(&snake_c, snake_new_direction) &&
!SnakeCollision(&snake_c, snake_previous_direction);
MoveSnake(&snake_c, snake_new_direction, snake_previous_direction);
}
!SnakeCollision(&snake_c, snake_new_direction);

if (alive) {
// Prey and Snake collisions
eating = EatingPreyCollision(&snake_c, &prey_c);
if (alive) {
MoveSnake(&snake_c, snake_new_direction, snake_previous_direction);

// Animation
RotateSnakeHead(snake_c.direction);
AnimateMouth(snake_c.direction);
// Prey and Snake collisions
eating = EatingPreyCollision(&snake_c, &prey_c);

// Prey handling
if (eating) {
snake_c.size += 1;
SpawnPrey(&prey_c, &snake_c);
} else if (IsSpecialPrey(&prey_c) && PreyTimeout(&prey_c)) {
SpawnPrey(&prey_c, &snake_c);
}
// Animation
RotateSnakeHead(snake_c.direction);
AnimateMouth(snake_c.direction, eating);

Draw(&snake_c, &prey_c);
// Prey handling
if (eating) {
snake_c.size += 1;
SpawnPrey(&prey_c, &snake_c);
} else if (IsSpecialPrey(&prey_c) && PreyTimeout(&prey_c)) {
SpawnPrey(&prey_c, &snake_c);
}

Draw(&snake_c, &prey_c);
}
}

// House keeping
Expand Down
19 changes: 8 additions & 11 deletions src/snake.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,34 +132,31 @@ void RotateSnakeHead(UBYTE direction) {
}
}

void AnimateMouth(UBYTE direction) {
void AnimateMouth(UBYTE direction, UINT8 eating) {
// Head sprite tiles are:
// 0: Closed Horizontal
// 1: Open Horizontal
// 2: Closed Vertical
// 3: Open Vertical
UINT8 current_tile = get_sprite_tile(0);
UBYTE is_mouth_closed = (current_tile == 0 || current_tile == 2) ? 1 : 0;

switch (direction) {
case J_LEFT:
case J_RIGHT:
set_sprite_tile(0, (is_mouth_closed) ? 1 : 0);
set_sprite_tile(0, (eating) ? 1 : 0);
break;
case J_UP:
case J_DOWN:
set_sprite_tile(0, (is_mouth_closed) ? 3 : 2);
set_sprite_tile(0, (eating) ? 3 : 2);
}
}

UINT8
SnakeCollision(SnakeCharacter* snake_c, UBYTE snake_previous_direction) {
SnakeCollision(SnakeCharacter* snake_c, UBYTE snake_next_direction) {
// Collided by going into oposite direction.
if (snake_c->size > 0 &&
((snake_c->direction == J_LEFT && snake_previous_direction == J_RIGHT) ||
(snake_c->direction == J_RIGHT && snake_previous_direction == J_LEFT) ||
(snake_c->direction == J_UP && snake_previous_direction == J_DOWN) ||
(snake_c->direction == J_DOWN && snake_previous_direction == J_UP))) {
((snake_c->direction == J_LEFT && snake_next_direction == J_RIGHT) ||
(snake_c->direction == J_RIGHT && snake_next_direction == J_LEFT) ||
(snake_c->direction == J_UP && snake_next_direction == J_DOWN) ||
(snake_c->direction == J_DOWN && snake_next_direction == J_UP))) {
return 1;
}

Expand Down
6 changes: 2 additions & 4 deletions src/snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ void UpdateBody(SnakeCharacter* snake_c, UINT8 snake_prev_x, UINT8 snake_prev_y,
// I kind of like the way it moves.
void RotateSnakeHead(UBYTE direction);

// Animates the mouth based on current direction.
// If the mouth is closed then we open it, if it was open then we close it.
// TODO(juanitobebe): I liked it more before but I guess I don't really need it.
void AnimateMouth(UBYTE direction);
// Animates the mouth to look like eating the prey.
void AnimateMouth(UBYTE direction, UINT8 eating);

// Returns 1 if there's a collision of the Snake head and body, 0 if not.
UINT8
Expand Down

0 comments on commit 95dfbb3

Please sign in to comment.