Odyssee/project/src/core.c

75 lines
1.7 KiB
C

#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include "core.h"
void analyze_input(struct game *game, const int last_key)
{
game->total_tick += ENGINE_TICK;
switch (last_key)
{
case KEY_UP:
if (is_walkable(*game, UP)) game->world_y -= 1;
game->player.direction = UP;
break;
case KEY_RIGHT:
if (is_walkable(*game, RIGHT)) game->world_x += 1;
game->player.direction = RIGHT;
break;
case KEY_DOWN:
if (is_walkable(*game, DOWN)) game->world_y += 1;
game->player.direction = DOWN;
break;
case KEY_LEFT:
if (is_walkable(*game, LEFT)) game->world_x -= 1;
game->player.direction = LEFT;
break;
}
}
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;
}
int is_walkable(const struct game game, const int direction)
{
switch (direction)
{
case UP:
return game.map.collision[game.world_y + 4][game.world_x + 7];
break;
case RIGHT:
return game.map.collision[game.world_y + 5][game.world_x + 8];
break;
case DOWN:
return game.map.collision[game.world_y + 6][game.world_x + 7];
break;
case LEFT:
return game.map.collision[game.world_y + 5][game.world_x + 6];
break;
}
return 0;
}