forked from NotAShelf/rogged
Compare commits
2 commits
main
...
notashelf/
| Author | SHA1 | Date | |
|---|---|---|---|
|
dbf8d4886c |
|||
|
c2412ac4b1 |
9 changed files with 113 additions and 148 deletions
|
|
@ -25,7 +25,6 @@ pub fn build(b: *std.Build) void {
|
||||||
"src/main.c",
|
"src/main.c",
|
||||||
"src/map.c",
|
"src/map.c",
|
||||||
"src/player.c",
|
"src/player.c",
|
||||||
"src/movement.c",
|
|
||||||
"src/render.c",
|
"src/render.c",
|
||||||
"src/rng.c",
|
"src/rng.c",
|
||||||
"src/settings.c",
|
"src/settings.c",
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,6 @@
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int x, y;
|
|
||||||
} Vec2;
|
|
||||||
|
|
||||||
// Tile types
|
// Tile types
|
||||||
typedef enum { TILE_WALL, TILE_FLOOR, TILE_STAIRS } TileType;
|
typedef enum { TILE_WALL, TILE_FLOOR, TILE_STAIRS } TileType;
|
||||||
|
|
||||||
|
|
@ -63,7 +59,7 @@ typedef struct {
|
||||||
|
|
||||||
// Player
|
// Player
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Vec2 position;
|
int x, y;
|
||||||
int hp, max_hp;
|
int hp, max_hp;
|
||||||
int attack;
|
int attack;
|
||||||
int defense;
|
int defense;
|
||||||
|
|
@ -89,7 +85,7 @@ typedef enum { ENEMY_GOBLIN, ENEMY_SKELETON, ENEMY_ORC } EnemyType;
|
||||||
|
|
||||||
// Enemy
|
// Enemy
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Vec2 position;
|
int x, y;
|
||||||
int hp;
|
int hp;
|
||||||
int max_hp;
|
int max_hp;
|
||||||
int attack;
|
int attack;
|
||||||
|
|
|
||||||
91
src/enemy.c
91
src/enemy.c
|
|
@ -2,7 +2,6 @@
|
||||||
#include "combat.h"
|
#include "combat.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "movement.h"
|
|
||||||
#include "rng.h"
|
#include "rng.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
@ -31,7 +30,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
|
||||||
get_random_floor_tile(map, &ex, &ey, 50);
|
get_random_floor_tile(map, &ex, &ey, 50);
|
||||||
|
|
||||||
// Don't spawn on player position
|
// Don't spawn on player position
|
||||||
if (ex == p->position.x && ey == p->position.y) {
|
if (ex == p->x && ey == p->y) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,8 +42,8 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
|
||||||
// Create enemy
|
// Create enemy
|
||||||
Enemy e;
|
Enemy e;
|
||||||
memset(&e, 0, sizeof(Enemy));
|
memset(&e, 0, sizeof(Enemy));
|
||||||
e.position.x = ex;
|
e.x = ex;
|
||||||
e.position.y = ey;
|
e.y = ey;
|
||||||
e.alive = 1;
|
e.alive = 1;
|
||||||
e.type = rng_int(ENEMY_GOBLIN, max_type);
|
e.type = rng_int(ENEMY_GOBLIN, max_type);
|
||||||
e.effect_count = 0;
|
e.effect_count = 0;
|
||||||
|
|
@ -133,7 +132,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
|
||||||
// Check if position has an enemy
|
// Check if position has an enemy
|
||||||
int is_enemy_at(const Enemy *enemies, int count, int x, int y) {
|
int is_enemy_at(const Enemy *enemies, int count, int x, int y) {
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (enemies[i].alive && enemies[i].position.x == x && enemies[i].position.y == y) {
|
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -142,35 +141,43 @@ int is_enemy_at(const Enemy *enemies, int count, int x, int y) {
|
||||||
|
|
||||||
// Check if enemy can see player (within view range and line of sight)
|
// Check if enemy can see player (within view range and line of sight)
|
||||||
static int can_see_player(Enemy *e, Player *p, Map *map) {
|
static int can_see_player(Enemy *e, Player *p, Map *map) {
|
||||||
return can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, e->vision_range);
|
return can_see_entity(map, e->x, e->y, p->x, p->y, e->vision_range);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// Move enemy toward player
|
||||||
static void enemy_move_toward_player(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) {
|
static void enemy_move_toward_player(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) {
|
||||||
int dx = 0, dy = 0;
|
int dx = 0, dy = 0;
|
||||||
|
|
||||||
if (p->position.x > e->position.x)
|
if (p->x > e->x)
|
||||||
dx = 1;
|
dx = 1;
|
||||||
else if (p->position.x < e->position.x)
|
else if (p->x < e->x)
|
||||||
dx = -1;
|
dx = -1;
|
||||||
|
|
||||||
if (p->position.y > e->position.y)
|
if (p->y > e->y)
|
||||||
dy = 1;
|
dy = 1;
|
||||||
else if (p->position.y < e->position.y)
|
else if (p->y < e->y)
|
||||||
dy = -1;
|
dy = -1;
|
||||||
|
|
||||||
Vec2 dir = {dx, 0};
|
// Try horizontal first, then vertical
|
||||||
if (dx != 0) {
|
int new_x = e->x + dx;
|
||||||
MoveResult r = try_move_entity(&e->position, dir, map, p, all_enemies, enemy_count, false);
|
int new_y = e->y;
|
||||||
if (r == MOVE_RESULT_MOVED)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dir.x = 0;
|
if (dx != 0 && is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y) &&
|
||||||
dir.y = dy;
|
!is_player_at(p, new_x, new_y)) {
|
||||||
if (dy != 0) {
|
e->x = new_x;
|
||||||
try_move_entity(&e->position, dir, map, p, all_enemies, enemy_count, false);
|
} 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -185,12 +192,12 @@ static void enemy_patrol(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count
|
||||||
if (dx == 0 && dy == 0)
|
if (dx == 0 && dy == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int new_x = e->position.x + dx;
|
int new_x = e->x + dx;
|
||||||
int new_y = e->position.y + dy;
|
int new_y = e->y + dy;
|
||||||
|
|
||||||
if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
|
if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
|
||||||
e->position.x = new_x;
|
e->x = new_x;
|
||||||
e->position.y = new_y;
|
e->y = new_y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -198,31 +205,31 @@ static void enemy_patrol(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count
|
||||||
static void enemy_move_to_last_known(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count) {
|
static void enemy_move_to_last_known(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count) {
|
||||||
int dx = 0, dy = 0;
|
int dx = 0, dy = 0;
|
||||||
|
|
||||||
if (e->last_known_x > e->position.x)
|
if (e->last_known_x > e->x)
|
||||||
dx = 1;
|
dx = 1;
|
||||||
else if (e->last_known_x < e->position.x)
|
else if (e->last_known_x < e->x)
|
||||||
dx = -1;
|
dx = -1;
|
||||||
|
|
||||||
if (e->last_known_y > e->position.y)
|
if (e->last_known_y > e->y)
|
||||||
dy = 1;
|
dy = 1;
|
||||||
else if (e->last_known_y < e->position.y)
|
else if (e->last_known_y < e->y)
|
||||||
dy = -1;
|
dy = -1;
|
||||||
|
|
||||||
int new_x = e->position.x + dx;
|
int new_x = e->x + dx;
|
||||||
int new_y = e->position.y;
|
int new_y = e->y;
|
||||||
|
|
||||||
if (dx != 0 && is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
|
if (dx != 0 && is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
|
||||||
e->position.x = new_x;
|
e->x = new_x;
|
||||||
} else if (dy != 0) {
|
} else if (dy != 0) {
|
||||||
new_x = e->position.x;
|
new_x = e->x;
|
||||||
new_y = e->position.y + dy;
|
new_y = e->y + dy;
|
||||||
if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
|
if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
|
||||||
e->position.x = new_x;
|
e->x = new_x;
|
||||||
e->position.y = new_y;
|
e->y = new_y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e->position.x == e->last_known_x && e->position.y == e->last_known_y)
|
if (e->x == e->last_known_x && e->y == e->last_known_y)
|
||||||
e->alert = 0;
|
e->alert = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -231,8 +238,8 @@ static int is_nearby_enemy(const Enemy *enemies, int count, int x, int y, int ra
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (!enemies[i].alive)
|
if (!enemies[i].alive)
|
||||||
continue;
|
continue;
|
||||||
int dx = enemies[i].position.x - x;
|
int dx = enemies[i].x - x;
|
||||||
int dy = enemies[i].position.y - y;
|
int dy = enemies[i].y - y;
|
||||||
if (dx * dx + dy * dy <= radius * radius)
|
if (dx * dx + dy * dy <= radius * radius)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
@ -247,7 +254,7 @@ static void propagate_alert(Enemy *trigger_enemy, Enemy *all_enemies, int enemy_
|
||||||
continue;
|
continue;
|
||||||
if (e->alert)
|
if (e->alert)
|
||||||
continue;
|
continue;
|
||||||
if (is_nearby_enemy(all_enemies, enemy_count, e->position.x, e->position.y, 5)) {
|
if (is_nearby_enemy(all_enemies, enemy_count, e->x, e->y, 5)) {
|
||||||
e->alert = 1;
|
e->alert = 1;
|
||||||
e->last_known_x = trigger_enemy->last_known_x;
|
e->last_known_x = trigger_enemy->last_known_x;
|
||||||
e->last_known_y = trigger_enemy->last_known_y;
|
e->last_known_y = trigger_enemy->last_known_y;
|
||||||
|
|
@ -269,12 +276,12 @@ void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_coun
|
||||||
// If we can see the player, update alert state and last known position
|
// If we can see the player, update alert state and last known position
|
||||||
if (can_see) {
|
if (can_see) {
|
||||||
e->alert = 1;
|
e->alert = 1;
|
||||||
e->last_known_x = p->position.x;
|
e->last_known_x = p->x;
|
||||||
e->last_known_y = p->position.y;
|
e->last_known_y = p->y;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attack if adjacent to player
|
// Attack if adjacent to player
|
||||||
if (can_see && can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, 1)) {
|
if (can_see && can_see_entity(map, e->x, e->y, p->x, p->y, 1)) {
|
||||||
combat_enemy_attack(e, p);
|
combat_enemy_attack(e, p);
|
||||||
propagate_alert(e, all_enemies, enemy_count);
|
propagate_alert(e, all_enemies, enemy_count);
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
67
src/main.c
67
src/main.c
|
|
@ -4,7 +4,6 @@
|
||||||
#include "enemy.h"
|
#include "enemy.h"
|
||||||
#include "items.h"
|
#include "items.h"
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "movement.h"
|
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "render.h"
|
#include "render.h"
|
||||||
|
|
@ -118,13 +117,13 @@ static void init_floor(GameState *gs, int floor_num) {
|
||||||
gs->floors_reached = 1;
|
gs->floors_reached = 1;
|
||||||
} else {
|
} else {
|
||||||
// Move player to new floor position
|
// Move player to new floor position
|
||||||
gs->player.position.x = start_x;
|
gs->player.x = start_x;
|
||||||
gs->player.position.y = start_y;
|
gs->player.y = start_y;
|
||||||
}
|
}
|
||||||
gs->player.floor = floor_num;
|
gs->player.floor = floor_num;
|
||||||
|
|
||||||
// Calculate initial visibility
|
// Calculate initial visibility
|
||||||
calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y);
|
calculate_visibility(&gs->map, gs->player.x, gs->player.y);
|
||||||
|
|
||||||
// Spawn enemies
|
// Spawn enemies
|
||||||
enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num);
|
enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num);
|
||||||
|
|
@ -141,8 +140,7 @@ static void tick_all_effects(GameState *gs) {
|
||||||
// Player effects
|
// Player effects
|
||||||
int player_effect_dmg = combat_tick_effects(&gs->player);
|
int player_effect_dmg = combat_tick_effects(&gs->player);
|
||||||
if (player_effect_dmg > 0) {
|
if (player_effect_dmg > 0) {
|
||||||
spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE, player_effect_dmg,
|
spawn_floating_text(gs, gs->player.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, player_effect_dmg, 0);
|
||||||
0);
|
|
||||||
gs->screen_shake = SHAKE_EFFECT_DURATION;
|
gs->screen_shake = SHAKE_EFFECT_DURATION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -160,7 +158,7 @@ static void tick_all_effects(GameState *gs) {
|
||||||
continue;
|
continue;
|
||||||
int enemy_effect_dmg = combat_tick_enemy_effects(e);
|
int enemy_effect_dmg = combat_tick_enemy_effects(e);
|
||||||
if (enemy_effect_dmg > 0) {
|
if (enemy_effect_dmg > 0) {
|
||||||
spawn_floating_text(gs, e->position.x * TILE_SIZE + 8, e->position.y * TILE_SIZE, enemy_effect_dmg, 0);
|
spawn_floating_text(gs, e->x * TILE_SIZE + 8, e->y * TILE_SIZE, enemy_effect_dmg, 0);
|
||||||
}
|
}
|
||||||
if (!e->alive) {
|
if (!e->alive) {
|
||||||
add_log(gs, "Enemy died from effects!");
|
add_log(gs, "Enemy died from effects!");
|
||||||
|
|
@ -178,7 +176,7 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Check if stepped on stairs
|
// Check if stepped on stairs
|
||||||
if (gs->map.tiles[gs->player.position.y][gs->player.position.x] == TILE_STAIRS) {
|
if (gs->map.tiles[gs->player.y][gs->player.x] == TILE_STAIRS) {
|
||||||
gs->awaiting_descend = 1;
|
gs->awaiting_descend = 1;
|
||||||
gs->last_message = "Descend to next floor? (Y/N)";
|
gs->last_message = "Descend to next floor? (Y/N)";
|
||||||
gs->message_timer = 120;
|
gs->message_timer = 120;
|
||||||
|
|
@ -187,8 +185,8 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) {
|
||||||
|
|
||||||
// combat feedback - player attacked an enemy this turn
|
// combat feedback - player attacked an enemy this turn
|
||||||
if (attacked_enemy != NULL) {
|
if (attacked_enemy != NULL) {
|
||||||
int ex = attacked_enemy->position.x * TILE_SIZE + 8;
|
int ex = attacked_enemy->x * TILE_SIZE + 8;
|
||||||
int ey = attacked_enemy->position.y * TILE_SIZE;
|
int ey = attacked_enemy->y * TILE_SIZE;
|
||||||
|
|
||||||
if (combat_was_dodged()) {
|
if (combat_was_dodged()) {
|
||||||
spawn_floating_label(gs, ex, ey, LABEL_DODGE, EFFECT_NONE);
|
spawn_floating_label(gs, ex, ey, LABEL_DODGE, EFFECT_NONE);
|
||||||
|
|
@ -221,7 +219,7 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update visibility based on player's new position
|
// Update visibility based on player's new position
|
||||||
calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y);
|
calculate_visibility(&gs->map, gs->player.x, gs->player.y);
|
||||||
|
|
||||||
// Enemy turns - uses speed/cooldown system
|
// Enemy turns - uses speed/cooldown system
|
||||||
enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map);
|
enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map);
|
||||||
|
|
@ -232,8 +230,8 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) {
|
||||||
gs->screen_shake = SHAKE_PLAYER_DAMAGE_DURATION;
|
gs->screen_shake = SHAKE_PLAYER_DAMAGE_DURATION;
|
||||||
gs->damage_taken += combat_get_last_damage();
|
gs->damage_taken += combat_get_last_damage();
|
||||||
gs->times_hit++;
|
gs->times_hit++;
|
||||||
spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE,
|
spawn_floating_text(gs, gs->player.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, combat_get_last_damage(),
|
||||||
combat_get_last_damage(), combat_was_critical());
|
combat_was_critical());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set message and check game over
|
// Set message and check game over
|
||||||
|
|
@ -254,7 +252,7 @@ static int handle_stun_turn(GameState *gs) {
|
||||||
if (gs->game_over)
|
if (gs->game_over)
|
||||||
return 1;
|
return 1;
|
||||||
enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map);
|
enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map);
|
||||||
calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y);
|
calculate_visibility(&gs->map, gs->player.x, gs->player.y);
|
||||||
if (gs->player.hp <= 0)
|
if (gs->player.hp <= 0)
|
||||||
gs->game_over = 1;
|
gs->game_over = 1;
|
||||||
gs->last_message = "You are stunned!";
|
gs->last_message = "You are stunned!";
|
||||||
|
|
@ -397,7 +395,7 @@ static int handle_movement_input(GameState *gs) {
|
||||||
|
|
||||||
// Check for manual item pickup (G key)
|
// Check for manual item pickup (G key)
|
||||||
if (IsKeyPressed(KEY_G)) {
|
if (IsKeyPressed(KEY_G)) {
|
||||||
Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.position.x, gs->player.position.y);
|
Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.x, gs->player.y);
|
||||||
if (item != NULL) {
|
if (item != NULL) {
|
||||||
if (player_pickup(&gs->player, item)) {
|
if (player_pickup(&gs->player, item)) {
|
||||||
gs->items_collected++;
|
gs->items_collected++;
|
||||||
|
|
@ -426,45 +424,38 @@ static int handle_movement_input(GameState *gs) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Movement: use IsKeyDown for held-key repeat
|
||||||
Vec2 direction = {0, 0};
|
int dx = 0, dy = 0;
|
||||||
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))
|
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))
|
||||||
direction.y = -1;
|
dy = -1;
|
||||||
else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))
|
else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))
|
||||||
direction.y = 1;
|
dy = 1;
|
||||||
else if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))
|
else if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))
|
||||||
direction.x = -1;
|
dx = -1;
|
||||||
else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))
|
else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))
|
||||||
direction.x = 1;
|
dx = 1;
|
||||||
|
|
||||||
if (direction.x == 0 && direction.y == 0)
|
if (dx == 0 && dy == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// Reset combat event before player acts
|
// Reset combat event before player acts
|
||||||
combat_reset_event();
|
combat_reset_event();
|
||||||
|
|
||||||
|
int new_x = gs->player.x + dx;
|
||||||
int new_x = gs->player.position.x + direction.x;
|
int new_y = gs->player.y + dy;
|
||||||
int new_y = gs->player.position.y + direction.y;
|
|
||||||
|
|
||||||
Enemy *target = NULL;
|
|
||||||
int action = 0;
|
int action = 0;
|
||||||
|
|
||||||
MoveResult result =
|
// Attack enemy at target tile, or move into it
|
||||||
try_move_entity(&gs->player.position, direction, &gs->map, &gs->player, gs->enemies, gs->enemy_count, true);
|
Enemy *target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y);
|
||||||
if (result == MOVE_RESULT_MOVED) {
|
if (target != NULL) {
|
||||||
player_on_move(&gs->player);
|
player_attack(&gs->player, target);
|
||||||
action = 1;
|
action = 1;
|
||||||
} else if (result == MOVE_RESULT_BLOCKED_ENEMY) {
|
} else {
|
||||||
target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y);
|
action = player_move(&gs->player, dx, dy, &gs->map);
|
||||||
if (target != NULL) {
|
|
||||||
player_attack(&gs->player, target);
|
|
||||||
action = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action)
|
if (action)
|
||||||
post_action(gs, target);
|
post_action(gs, target); // target is NULL on move, enemy ptr on attack
|
||||||
|
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
#ifndef MOVEMENT_H
|
|
||||||
#define MOVEMENT_H
|
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
MOVE_RESULT_MOVED,
|
|
||||||
MOVE_RESULT_BLOCKED_WALL,
|
|
||||||
MOVE_RESULT_BLOCKED_PLAYER,
|
|
||||||
MOVE_RESULT_BLOCKED_ENEMY
|
|
||||||
} MoveResult;
|
|
||||||
|
|
||||||
// Attempts to move entity in a given direction. Returns outcome of action.
|
|
||||||
MoveResult try_move_entity(Vec2 *p, Vec2 direction, Map *map, Player *player, Enemy *enemies, int enemy_count,
|
|
||||||
bool moving_is_player);
|
|
||||||
|
|
||||||
#endif // MOVEMENT_H
|
|
||||||
31
src/player.c
31
src/player.c
|
|
@ -2,12 +2,15 @@
|
||||||
#include "combat.h"
|
#include "combat.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "items.h"
|
#include "items.h"
|
||||||
|
#include "map.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void player_init(Player *p, int x, int y) {
|
void player_init(Player *p, int x, int y) {
|
||||||
p->position.x = x;
|
p->x = x;
|
||||||
p->position.y = y;
|
p->y = y;
|
||||||
p->hp = PLAYER_BASE_HP;
|
p->hp = PLAYER_BASE_HP;
|
||||||
p->max_hp = PLAYER_BASE_HP;
|
p->max_hp = PLAYER_BASE_HP;
|
||||||
p->attack = PLAYER_BASE_ATTACK;
|
p->attack = PLAYER_BASE_ATTACK;
|
||||||
|
|
@ -40,20 +43,36 @@ Enemy *player_find_enemy_at(Enemy *enemies, int count, int x, int y) {
|
||||||
if (count > MAX_ENEMIES)
|
if (count > MAX_ENEMIES)
|
||||||
count = MAX_ENEMIES;
|
count = MAX_ENEMIES;
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (enemies[i].alive && enemies[i].position.x == x && enemies[i].position.y == y)
|
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y)
|
||||||
return &enemies[i];
|
return &enemies[i];
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_on_move(Player *p) {
|
int player_move(Player *p, int dx, int dy, Map *map) {
|
||||||
|
int new_x = p->x + dx;
|
||||||
|
int new_y = p->y + dy;
|
||||||
|
|
||||||
|
// Check bounds
|
||||||
|
if (!in_bounds(new_x, new_y, MAP_WIDTH, MAP_HEIGHT))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Check if walkable
|
||||||
|
if (!is_floor(map, new_x, new_y))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Move player
|
||||||
|
p->x = new_x;
|
||||||
|
p->y = new_y;
|
||||||
p->step_count += 1;
|
p->step_count += 1;
|
||||||
|
// Regen suppressed while poisoned, bleeding, or burning
|
||||||
if (p->step_count % REGEN_STEP_INTERVAL == 0 && p->hp < p->max_hp &&
|
if (p->step_count % REGEN_STEP_INTERVAL == 0 && p->hp < p->max_hp &&
|
||||||
!combat_has_effect(p->effects, p->effect_count, EFFECT_POISON) &&
|
!combat_has_effect(p->effects, p->effect_count, EFFECT_POISON) &&
|
||||||
!combat_has_effect(p->effects, p->effect_count, EFFECT_BLEED) &&
|
!combat_has_effect(p->effects, p->effect_count, EFFECT_BLEED) &&
|
||||||
!combat_has_effect(p->effects, p->effect_count, EFFECT_BURN)) {
|
!combat_has_effect(p->effects, p->effect_count, EFFECT_BURN)) {
|
||||||
p->hp += 1;
|
p->hp += 1;
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_attack(Player *p, Enemy *e) {
|
void player_attack(Player *p, Enemy *e) {
|
||||||
|
|
@ -209,8 +228,8 @@ int player_drop_item(Player *p, int inv_index, Item *items, int item_count) {
|
||||||
if (items[i].picked_up) {
|
if (items[i].picked_up) {
|
||||||
// Place dropped item at this position
|
// Place dropped item at this position
|
||||||
items[i] = *item;
|
items[i] = *item;
|
||||||
items[i].x = p->position.x;
|
items[i].x = p->x;
|
||||||
items[i].y = p->position.y;
|
items[i].y = p->y;
|
||||||
items[i].picked_up = 0;
|
items[i].picked_up = 0;
|
||||||
// Remove from inventory
|
// Remove from inventory
|
||||||
player_remove_inventory_item(p, inv_index);
|
player_remove_inventory_item(p, inv_index);
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@
|
||||||
// Initialize player at position
|
// Initialize player at position
|
||||||
void player_init(Player *p, int x, int y);
|
void player_init(Player *p, int x, int y);
|
||||||
|
|
||||||
// Apply status effects, healing, etc
|
// Move player to (x+dx, y+dy); returns 1 if moved, 0 if blocked
|
||||||
void player_on_move(Player *p);
|
int player_move(Player *p, int dx, int dy, Map *map);
|
||||||
|
|
||||||
// Find a living enemy at tile (x, y); returns NULL if none
|
// Find a living enemy at tile (x, y); returns NULL if none
|
||||||
Enemy *player_find_enemy_at(Enemy *enemies, int count, int x, int y);
|
Enemy *player_find_enemy_at(Enemy *enemies, int count, int x, int y);
|
||||||
|
|
|
||||||
13
src/render.c
13
src/render.c
|
|
@ -43,8 +43,7 @@ void render_map(const Map *map) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void render_player(const Player *p) {
|
void render_player(const Player *p) {
|
||||||
Rectangle rect = {(float)(p->position.x * TILE_SIZE), (float)(p->position.y * TILE_SIZE), (float)TILE_SIZE,
|
Rectangle rect = {(float)(p->x * TILE_SIZE), (float)(p->y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE};
|
||||||
(float)TILE_SIZE};
|
|
||||||
DrawRectangleRec(rect, BLUE);
|
DrawRectangleRec(rect, BLUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,11 +51,11 @@ void render_enemies(const Enemy *enemies, int count, const unsigned char visible
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (!enemies[i].alive)
|
if (!enemies[i].alive)
|
||||||
continue;
|
continue;
|
||||||
if (!visible[enemies[i].position.y][enemies[i].position.x])
|
if (!visible[enemies[i].y][enemies[i].x])
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Rectangle rect = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE),
|
Rectangle rect = {(float)(enemies[i].x * TILE_SIZE), (float)(enemies[i].y * TILE_SIZE), (float)TILE_SIZE,
|
||||||
(float)TILE_SIZE, (float)TILE_SIZE};
|
(float)TILE_SIZE};
|
||||||
|
|
||||||
// Different colors based on enemy type
|
// Different colors based on enemy type
|
||||||
Color enemy_color;
|
Color enemy_color;
|
||||||
|
|
@ -88,8 +87,8 @@ void render_enemies(const Enemy *enemies, int count, const unsigned char visible
|
||||||
bar_color = (Color){200, 180, 40, 255}; // yellow
|
bar_color = (Color){200, 180, 40, 255}; // yellow
|
||||||
else
|
else
|
||||||
bar_color = (Color){200, 60, 60, 255}; // red
|
bar_color = (Color){200, 60, 60, 255}; // red
|
||||||
Rectangle hp_bar = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE - 4),
|
Rectangle hp_bar = {(float)(enemies[i].x * TILE_SIZE), (float)(enemies[i].y * TILE_SIZE - 4), (float)hp_pixels,
|
||||||
(float)hp_pixels, 3};
|
3};
|
||||||
DrawRectangleRec(hp_bar, bar_color);
|
DrawRectangleRec(hp_bar, bar_color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue