mystnb/src/engine.c

57 lines
1.2 KiB
C

#include <gint/display.h>
#include <gint/keyboard.h>
#include "engine.h"
#define CELL_X(x) (-2 + 10 * (x))
#define CELL_Y(y) (-3 + 10 * (y))
//---
// Rendering
//---
static void engine_draw_player(struct player const *player)
{
extern bopti_image_t img_spritesheet;
dsubimage(CELL_X(player->x) - 1, CELL_Y(player->y) - 5,
&img_spritesheet, player->dir * 12, 0, 12, 16, DIMAGE_NONE);
}
void engine_draw(struct game const *game)
{
dclear(C_WHITE);
for(int p = 0; game->players[p]; p++)
{
engine_draw_player(game->players[p]);
}
}
//---
// Physics
//---
/* Check whether a cell of the map is walkable */
static int map_walkable(struct map const *map, int x, int y)
{
/* TODO: Read the map's data array */
return (x >= 1) && (y >= 1) && (x < map->w - 1) && (y < map->h - 1);
}
int engine_move(struct game *game, struct player *player, int dir)
{
int dx = (dir == DIR_RIGHT) - (dir == DIR_LEFT);
int dy = (dir == DIR_DOWN) - (dir == DIR_UP);
/* Always update the direction */
player->dir = dir;
/* Only move the player if the destination is walkable */
if(!map_walkable(game->map, player->x + dx, player->y + dy)) return 0;
player->x += dx;
player->y += dy;
return 1;
}