protomine/src/player/update.c

53 lines
1.4 KiB
C

#include "grid.h"
#include "player.h"
#include <gint/keyboard.h>
void
player_update(struct Player *restrict player, struct Grid *restrict grid)
{
int new_x;
int new_y;
const int k_left = keydown(KEY_LEFT);
const int k_right = keydown(KEY_RIGHT);
const int k_up = keydown(KEY_UP);
const int k_down = keydown(KEY_DOWN);
const int move_x =
(k_right && !player->right_held) - (k_left && !player->left_held);
const int move_y =
(k_down && !player->down_held) - (k_up && !player->up_held);
player->left_held += (k_left) ? (1) : (-player->left_held);
if (player->left_held > 6)
player->left_held = 0;
player->right_held += (k_right) ? (1) : (-player->right_held);
if (player->right_held > 6)
player->right_held = 0;
player->up_held += (k_up) ? (1) : (-player->up_held);
if (player->up_held > 6)
player->up_held = 0;
player->down_held += (k_down) ? (1) : (-player->down_held);
if (player->down_held > 6)
player->down_held = 0;
new_x = player->x + move_x;
new_y = player->y + move_y;
if (new_x < 0)
new_x = 0;
if (new_y < 0)
new_y = grid->height - 1;
if (new_x >= grid->width)
new_x = grid->width - 1;
if (new_y >= grid->height)
new_y = 0;
if (!grid_get(*grid, new_x, new_y)) {
player->x = new_x;
player->y = new_y;
} else if (grid_get(*grid, new_x, new_y) != TILE_SOLID) {
grid_set(grid, new_x, new_y, grid_get(*grid, new_x, new_y) - 1);
/* get rich! */
if (!grid_get(*grid, new_x, new_y))
player->cash += 1;
}
}