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
49
src/enemy.c
49
src/enemy.c
|
|
@ -2,6 +2,7 @@
|
|||
#include "combat.h"
|
||||
#include "common.h"
|
||||
#include "map.h"
|
||||
#include "movement.h"
|
||||
#include "rng.h"
|
||||
#include <string.h>
|
||||
|
||||
|
|
@ -29,7 +30,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
|
|||
get_random_floor_tile(map, &ex, &ey, 50);
|
||||
|
||||
// Don't spawn on player position
|
||||
if (ex == p->x && ey == p->y) {
|
||||
if (ex == p->position.x && ey == p->position.y) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -41,8 +42,8 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
|
|||
// Create enemy
|
||||
Enemy e;
|
||||
memset(&e, 0, sizeof(Enemy));
|
||||
e.x = ex;
|
||||
e.y = ey;
|
||||
e.position.x = ex;
|
||||
e.position.y = ey;
|
||||
e.alive = 1;
|
||||
e.type = rng_int(ENEMY_GOBLIN, max_type);
|
||||
e.effect_count = 0;
|
||||
|
|
@ -127,7 +128,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
|
|||
// Check if position has an enemy
|
||||
int is_enemy_at(const Enemy *enemies, int count, int x, int y) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y) {
|
||||
if (enemies[i].alive && enemies[i].position.x == x && enemies[i].position.y == y) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -136,45 +137,37 @@ int is_enemy_at(const Enemy *enemies, int count, int x, int y) {
|
|||
|
||||
// Check if enemy can see player (adjacent)
|
||||
static int can_see_player(Enemy *e, Player *p) {
|
||||
int dx = p->x - e->x;
|
||||
int dy = p->y - e->y;
|
||||
int dx = p->position.x - e->position.x;
|
||||
int dy = p->position.y - e->position.y;
|
||||
return (dx >= -1 && dx <= 1 && dy >= -1 && dy <= 1);
|
||||
}
|
||||
|
||||
// Check if position is occupied by player
|
||||
static int is_player_at(Player *p, int x, int y) {
|
||||
return (p->x == x && p->y == y);
|
||||
}
|
||||
|
||||
// Move enemy toward player
|
||||
static void enemy_move_toward_player(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) {
|
||||
int dx = 0, dy = 0;
|
||||
|
||||
if (p->x > e->x)
|
||||
if (p->position.x > e->position.x)
|
||||
dx = 1;
|
||||
else if (p->x < e->x)
|
||||
else if (p->position.x < e->position.x)
|
||||
dx = -1;
|
||||
|
||||
if (p->y > e->y)
|
||||
if (p->position.y > e->position.y)
|
||||
dy = 1;
|
||||
else if (p->y < e->y)
|
||||
else if (p->position.y < e->position.y)
|
||||
dy = -1;
|
||||
|
||||
// Try horizontal first, then vertical
|
||||
int new_x = e->x + dx;
|
||||
int new_y = e->y;
|
||||
Vec2 dir = {dx, 0};
|
||||
if (dx != 0) {
|
||||
MoveResult r = try_move_entity(&e->position, dir, map, p, all_enemies, enemy_count, false);
|
||||
if (r == MOVE_RESULT_MOVED)
|
||||
return;
|
||||
}
|
||||
|
||||
if (dx != 0 && is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y) &&
|
||||
!is_player_at(p, new_x, new_y)) {
|
||||
e->x = new_x;
|
||||
} else if (dy != 0) {
|
||||
new_x = e->x;
|
||||
new_y = e->y + dy;
|
||||
if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y) &&
|
||||
!is_player_at(p, new_x, new_y)) {
|
||||
e->x = new_x;
|
||||
e->y = new_y;
|
||||
}
|
||||
dir.x = 0;
|
||||
dir.y = dy;
|
||||
if (dy != 0) {
|
||||
try_move_entity(&e->position, dir, map, p, all_enemies, enemy_count, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue