86 lines
2.1 KiB
C
86 lines
2.1 KiB
C
#include "grid.h"
|
|
#include "player.h"
|
|
#include <gint/keyboard.h>
|
|
#include <gint/std/stdlib.h>
|
|
|
|
int
|
|
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 (new_x == player->x && new_y == player->y)
|
|
return 0;
|
|
|
|
switch (grid_get(*grid, new_x, new_y)) {
|
|
case TILE_VOID:
|
|
player->x = new_x;
|
|
player->y = new_y;
|
|
break;
|
|
case TILE_ZONE_TRANSITION:
|
|
grid_set(grid, new_x, new_y, TILE_VOID);
|
|
return 1;
|
|
case TILE_CONTRACTS_SPEED_UP:
|
|
player->x = new_x;
|
|
player->y = new_y;
|
|
grid_set(grid, new_x, new_y, TILE_VOID);
|
|
grid_set(grid, new_x + 4, new_y, TILE_VOID);
|
|
*player->cash += 50;
|
|
return -1;
|
|
case TILE_CONTRACTS_SLOW_DOWN:
|
|
player->x = new_x;
|
|
player->y = new_y;
|
|
if (*player->cash < 50)
|
|
break;
|
|
grid_set(grid, new_x, new_y, TILE_VOID);
|
|
grid_set(grid, new_x - 4, new_y, TILE_VOID);
|
|
*player->cash -= 50;
|
|
return -2;
|
|
case TILE_OBSTACLE_1:
|
|
/* rand between 1 and 4 */
|
|
*player->cash += 1 + rand() % 4;
|
|
/* fallthrough */
|
|
case TILE_OBSTACLE_2:
|
|
case TILE_OBSTACLE_3:
|
|
grid_set(grid, new_x, new_y, grid_get(*grid, new_x, new_y) - 1);
|
|
break;
|
|
case TILE_SOLID:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|