Odyssee/project/src/core.c

104 lines
2.7 KiB
C

#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include "core.h"
void next_frame(struct game *game)
{
game->total_tick += ENGINE_TICK;
if (!(game->total_tick % 200)) game->water_frame = (game->water_frame + 1) % 8;
if (!(game->total_tick % 500))
{
switch (game->player->animation_frame)
{
case 2:
game->player->animation_frame = 0;
break;
case 3:
game->player->animation_frame = 1;
break;
default:
game->player->animation_frame = (game->player->animation_frame + 1) % 2;
break;
}
}
}
void analyze_input(struct game *game, const int last_key)
{
switch (last_key)
{
case KEY_UP:
if (is_walkable(game, UP)) game->world_y -= 1;
game->player->direction = UP;
game->player->animation_frame = 2 + (game->player->animation_frame + 1) % 2;
break;
case KEY_RIGHT:
if (is_walkable(game, RIGHT)) game->world_x += 1;
game->player->direction = RIGHT;
game->player->animation_frame = 2 + (game->player->animation_frame + 1) % 2;
break;
case KEY_DOWN:
if (is_walkable(game, DOWN)) game->world_y += 1;
game->player->direction = DOWN;
game->player->animation_frame = 2 + (game->player->animation_frame + 1) % 2;
break;
case KEY_LEFT:
if (is_walkable(game, LEFT)) game->world_x -= 1;
game->player->direction = LEFT;
game->player->animation_frame = 2 + (game->player->animation_frame + 1) % 2;
break;
}
}
uint8_t is_walkable(const struct game *game, const int direction)
{
switch (direction)
{
case UP:
if (game->world_y + 4 >= 0) return game->map->collision[game->world_y + 4][game->world_x + 7];
break;
case RIGHT:
if (game->world_x + 8 < MAP_WIDTH) return game->map->collision[game->world_y + 5][game->world_x + 8];
break;
case DOWN:
if (game->world_y + 6 < MAP_HEIGHT) return game->map->collision[game->world_y + 6][game->world_x + 7];
break;
case LEFT:
if (game->world_x + 5 >= 0) return game->map->collision[game->world_y + 5][game->world_x + 6];
break;
}
return 0;
}
int rtc_key(void)
{
int opt = GETKEY_DEFAULT & ~GETKEY_MOD_SHIFT & ~GETKEY_MOD_ALPHA & ~GETKEY_REP_ARROWS;
int timeout = 1;
key_event_t ev = getkey_opt(opt, &timeout);
if (ev.type == KEYEV_NONE) return 0;
return ev.key;
}
int callback_tick(volatile int *tick)
{
*tick = 1;
return TIMER_CONTINUE;
}