movement: generalize; use vectors (#16)
Generalized movement, so that all entities move the same. Reviewed-on: #16 Reviewed-by: raf <raf@notashelf.dev> Co-authored-by: Squirrel Modeller <squirrelmodeller@protonmail.com> Co-committed-by: Squirrel Modeller <squirrelmodeller@protonmail.com>
This commit is contained in:
parent
1d738c35d4
commit
4dfe52ae72
9 changed files with 124 additions and 89 deletions
69
src/main.c
69
src/main.c
|
|
@ -4,6 +4,7 @@
|
|||
#include "enemy.h"
|
||||
#include "items.h"
|
||||
#include "map.h"
|
||||
#include "movement.h"
|
||||
#include "player.h"
|
||||
#include "raylib.h"
|
||||
#include "render.h"
|
||||
|
|
@ -117,8 +118,8 @@ static void init_floor(GameState *gs, int floor_num) {
|
|||
gs->floors_reached = 1;
|
||||
} else {
|
||||
// Move player to new floor position
|
||||
gs->player.x = start_x;
|
||||
gs->player.y = start_y;
|
||||
gs->player.position.x = start_x;
|
||||
gs->player.position.y = start_y;
|
||||
}
|
||||
gs->player.floor = floor_num;
|
||||
|
||||
|
|
@ -137,7 +138,8 @@ static void tick_all_effects(GameState *gs) {
|
|||
// Player effects
|
||||
int player_effect_dmg = combat_tick_effects(&gs->player);
|
||||
if (player_effect_dmg > 0) {
|
||||
spawn_floating_text(gs, gs->player.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, player_effect_dmg, 0);
|
||||
spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE, player_effect_dmg,
|
||||
0);
|
||||
gs->screen_shake = SHAKE_EFFECT_DURATION;
|
||||
}
|
||||
|
||||
|
|
@ -155,7 +157,7 @@ static void tick_all_effects(GameState *gs) {
|
|||
continue;
|
||||
int enemy_effect_dmg = combat_tick_enemy_effects(e);
|
||||
if (enemy_effect_dmg > 0) {
|
||||
spawn_floating_text(gs, e->x * TILE_SIZE + 8, e->y * TILE_SIZE, enemy_effect_dmg, 0);
|
||||
spawn_floating_text(gs, e->position.x * TILE_SIZE + 8, e->position.y * TILE_SIZE, enemy_effect_dmg, 0);
|
||||
}
|
||||
if (!e->alive) {
|
||||
add_log(gs, "Enemy died from effects!");
|
||||
|
|
@ -173,7 +175,7 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) {
|
|||
return;
|
||||
|
||||
// Check if stepped on stairs
|
||||
if (gs->map.tiles[gs->player.y][gs->player.x] == TILE_STAIRS) {
|
||||
if (gs->map.tiles[gs->player.position.y][gs->player.position.x] == TILE_STAIRS) {
|
||||
gs->awaiting_descend = 1;
|
||||
gs->last_message = "Descend to next floor? (Y/N)";
|
||||
gs->message_timer = 120;
|
||||
|
|
@ -182,8 +184,8 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) {
|
|||
|
||||
// combat feedback - player attacked an enemy this turn
|
||||
if (attacked_enemy != NULL) {
|
||||
int ex = attacked_enemy->x * TILE_SIZE + 8;
|
||||
int ey = attacked_enemy->y * TILE_SIZE;
|
||||
int ex = attacked_enemy->position.x * TILE_SIZE + 8;
|
||||
int ey = attacked_enemy->position.y * TILE_SIZE;
|
||||
|
||||
if (combat_was_dodged()) {
|
||||
spawn_floating_label(gs, ex, ey, LABEL_DODGE, EFFECT_NONE);
|
||||
|
|
@ -224,8 +226,8 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) {
|
|||
gs->screen_shake = SHAKE_PLAYER_DAMAGE_DURATION;
|
||||
gs->damage_taken += combat_get_last_damage();
|
||||
gs->times_hit++;
|
||||
spawn_floating_text(gs, gs->player.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, combat_get_last_damage(),
|
||||
combat_was_critical());
|
||||
spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE,
|
||||
combat_get_last_damage(), combat_was_critical());
|
||||
}
|
||||
|
||||
// Set message and check game over
|
||||
|
|
@ -388,7 +390,7 @@ static int handle_movement_input(GameState *gs) {
|
|||
|
||||
// Check for manual item pickup (G key)
|
||||
if (IsKeyPressed(KEY_G)) {
|
||||
Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.x, gs->player.y);
|
||||
Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.position.x, gs->player.position.y);
|
||||
if (item != NULL) {
|
||||
if (player_pickup(&gs->player, item)) {
|
||||
gs->items_collected++;
|
||||
|
|
@ -417,38 +419,45 @@ static int handle_movement_input(GameState *gs) {
|
|||
}
|
||||
}
|
||||
|
||||
// Movement: use IsKeyDown for held-key repeat
|
||||
int dx = 0, dy = 0;
|
||||
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))
|
||||
dy = -1;
|
||||
else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))
|
||||
dy = 1;
|
||||
else if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))
|
||||
dx = -1;
|
||||
else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))
|
||||
dx = 1;
|
||||
|
||||
if (dx == 0 && dy == 0)
|
||||
Vec2 direction = {0, 0};
|
||||
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))
|
||||
direction.y = -1;
|
||||
else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))
|
||||
direction.y = 1;
|
||||
else if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))
|
||||
direction.x = -1;
|
||||
else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))
|
||||
direction.x = 1;
|
||||
|
||||
if (direction.x == 0 && direction.y == 0)
|
||||
return 0;
|
||||
|
||||
// Reset combat event before player acts
|
||||
combat_reset_event();
|
||||
|
||||
int new_x = gs->player.x + dx;
|
||||
int new_y = gs->player.y + dy;
|
||||
|
||||
int new_x = gs->player.position.x + direction.x;
|
||||
int new_y = gs->player.position.y + direction.y;
|
||||
|
||||
Enemy *target = NULL;
|
||||
int action = 0;
|
||||
|
||||
// Attack enemy at target tile, or move into it
|
||||
Enemy *target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y);
|
||||
if (target != NULL) {
|
||||
player_attack(&gs->player, target);
|
||||
MoveResult result =
|
||||
try_move_entity(&gs->player.position, direction, &gs->map, &gs->player, gs->enemies, gs->enemy_count, true);
|
||||
if (result == MOVE_RESULT_MOVED) {
|
||||
player_on_move(&gs->player);
|
||||
action = 1;
|
||||
} else {
|
||||
action = player_move(&gs->player, dx, dy, &gs->map);
|
||||
} else if (result == MOVE_RESULT_BLOCKED_ENEMY) {
|
||||
target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y);
|
||||
if (target != NULL) {
|
||||
player_attack(&gs->player, target);
|
||||
action = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (action)
|
||||
post_action(gs, target); // target is NULL on move, enemy ptr on attack
|
||||
post_action(gs, target);
|
||||
|
||||
return action;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue