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
29
src/movement.c
Normal file
29
src/movement.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "movement.h"
|
||||
#include "enemy.h"
|
||||
#include "map.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
// Check if position is occupied by player
|
||||
static int is_player_at(Player *p, int x, int y) {
|
||||
return (p->position.x == x && p->position.y == y);
|
||||
}
|
||||
|
||||
MoveResult try_move_entity(Vec2 *p, Vec2 direction, Map *map, Player *player, Enemy *enemies, int enemy_count,
|
||||
bool moving_is_player) {
|
||||
int new_x = p->x + direction.x;
|
||||
int new_y = p->y + direction.y;
|
||||
|
||||
if (!is_floor(map, new_x, new_y))
|
||||
return MOVE_RESULT_BLOCKED_WALL;
|
||||
|
||||
if (is_enemy_at(enemies, enemy_count, new_x, new_y))
|
||||
return MOVE_RESULT_BLOCKED_ENEMY;
|
||||
if (!moving_is_player) {
|
||||
if (is_player_at(player, new_x, new_y))
|
||||
return MOVE_RESULT_BLOCKED_PLAYER;
|
||||
}
|
||||
|
||||
p->x = new_x;
|
||||
p->y = new_y;
|
||||
return MOVE_RESULT_MOVED;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue