Skip to content

Commit

Permalink
Make hostile actors move towards player
Browse files Browse the repository at this point in the history
- Add game over
- Add wait 'movement'

Close #31
  • Loading branch information
arbiter0xf committed Dec 21, 2021
1 parent e77d8d1 commit 27a20d4
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 17 deletions.
12 changes: 12 additions & 0 deletions src/actor.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,15 @@ void player_equip_item(struct item* const item_to_equip)
strcat(g_new_announcement, player->equipment[slot]->name);
announce(g_new_announcement);
}

struct actor* spawn_actor_skeleton(int row, int col)
{
return spawn_actor(
"skeleton",
row, col,
ICON_SKELETON,
despawn_actor,
do_combat,
25,
1);
}
6 changes: 2 additions & 4 deletions src/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#include "interact.h"
#include "item.h"

#define ALL_ACTORS_SIZE 32
#define ACTOR_INVENTORY_SIZE 64
#define ACTOR_EQUIPMENT_SIZE 6

enum equipment_slot {
EQUIPMENT_SLOT_NONE,
EQUIPMENT_SLOT_HEAD,
Expand Down Expand Up @@ -63,6 +59,7 @@ extern int g_all_actors_player_index;

struct actor* get_player(void);
struct actor** get_all_actors(void);
struct actor** get_all_hostile_actors(void);
struct item** get_actor_inventory(struct actor* const a);
struct item** get_actor_equipment(struct actor* const a);
struct item* get_actor_item(struct actor* const a, int index);
Expand Down Expand Up @@ -118,5 +115,6 @@ void spawn_player(
const int col,
struct actor ** const all_actors);
void player_equip_item(struct item* const item_to_equip);
struct actor* spawn_actor_skeleton(int row, int col);

#endif
1 change: 1 addition & 0 deletions src/actor.h.const
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define ACTOR_H_CONST_DEFINED

#define ALL_ACTORS_SIZE 32
#define ALL_HOSTILE_ACTORS_SIZE ALL_ACTORS_SIZE
#define ACTOR_INVENTORY_SIZE 64
#define ACTOR_EQUIPMENT_SIZE 6

Expand Down
14 changes: 14 additions & 0 deletions src/draw.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
#include "draw.h"
#include "log.h"
#include "util_poet.h"

enum hud_draw g_hud_to_draw = DRAW_HIDE;
char g_hud_heading[HUD_HEADING_SIZE] = {0};
int g_cursor_index = 0;

static void undraw_tile(int row, int col);
static void draw_tile(int row, int col);
static void draw_game_over();

void draw(struct actor ** const all_actors)
{
if (is_game_over()) {
draw_game_over();
goto do_refresh;
}

set_stage_slice_around_player();
draw_stage();
draw_hud();

do_refresh:
refresh();
}

static void draw_game_over()
{
LOG_DEBUG("%s\n", "draw_game_over()");
}

void draw_stage(void)
{
move(ROW_DRAW_STAGE_ZERO, COL_DRAW_STAGE_ZERO);
Expand Down
1 change: 1 addition & 0 deletions src/interact.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ void do_combat(struct actor* const defender, struct actor* const attacker)
strcat(g_new_announcement, " damage");
announce(g_new_announcement);

LOG_DEBUG("%s\n", "next actor_take_damage()");
actor_take_damage(defender, damage_attacker_reduced);
}
2 changes: 0 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#define ROW_DEBUG_ZERO 45
#define COL_DEBUG_ZERO 5

#define BUTTON_QUIT 'q'

#define DEBUG_MESSAGE_SIZE 128

#define DEBUG_PRINT_ENABLE 0
Expand Down
9 changes: 1 addition & 8 deletions src/stage.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,7 @@ static void load_stage_dungeon(void)

spawn_player(2, 2, get_all_actors());

a = spawn_actor(
"skeleton",
5, 8,
ICON_SKELETON,
despawn_actor,
do_combat,
25,
1);
a = spawn_actor_skeleton(5, 8);
actor_set_base_damage_unarmed(a, 3);
actor_calculate_damage(a);

Expand Down
38 changes: 37 additions & 1 deletion src/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "item.h"
#include "log.h"
#include "update.h"
#include "user_input.h"
#include "util_poet.h"

static void update_position(
enum position_update update,
Expand All @@ -19,17 +21,28 @@ static void toggle_hud(const enum hud_toggle toggle);
static void move_cursor(const enum cursor_movement movement);
static void update_hud(int* const pressed_key);
static void update_player(int* const pressed_key);
static void update_all_hostile_actors();
static void update_hostile_actor();

void update(int* const pressed_key)
{
if (BUTTON_QUIT == *pressed_key) {
return;
}

// Player and UI related updates dependent on user input.
update_hud(pressed_key);

if (is_game_over()) {
return;
}

if (player_has_spawned()) {
update_player(pressed_key);
}

// Enemy updates independent of user input.
update_all_hostile_actors();
}

static void update_hud(int* const pressed_key)
Expand Down Expand Up @@ -162,7 +175,7 @@ static void apply_operation_to_item(struct item* const selected_item, struct ite

static void update_player(int* const pressed_key)
{
if (is_direction_button(pressed_key)) {
if (is_direction_button(pressed_key) || is_wait_button(pressed_key)) {
update_position(key_to_position_update(pressed_key), get_player());
*pressed_key = 0;
}
Expand All @@ -175,6 +188,10 @@ static void update_position(
int new_position_row = actor->row;
int new_position_col = actor->col;

if (POSITION_UPDATE_WAIT == update) {
return;
}

if (POSITION_UPDATE_DOWN == update) {
new_position_row = actor->row + 1;
}
Expand Down Expand Up @@ -205,3 +222,22 @@ static void update_position(
actor->col = new_position_col;
g_stage[actor->row][actor->col].occupant = actor;
}

static void update_all_hostile_actors()
{
struct actor** const hostile_actors = get_all_hostile_actors();

for (int i = 0; i < ALL_HOSTILE_ACTORS_SIZE; i++) {
if (0 != hostile_actors[i]) {
update_hostile_actor(hostile_actors[i]);
}
}
}

static void update_hostile_actor(struct actor* const ha)
{
enum position_update next_movement;

next_movement = movement_towards_player(ha);
update_position(next_movement, ha);
}
9 changes: 9 additions & 0 deletions src/user_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ int is_hud_toggle_button(int* const pressed_key) {
return 0;
}

int is_wait_button(int* const pressed_key) {
if ('w' == *pressed_key) {
return 1;
}

return 0;
}

enum cursor_movement key_to_cursor_movement(int* const pressed_key)
{
if (KEY_UP == *pressed_key) { return CURSOR_UP; }
Expand All @@ -81,6 +89,7 @@ enum position_update key_to_position_update(int* const pressed_key)
if (KEY_DOWN == *pressed_key) { return POSITION_UPDATE_DOWN; }
if (KEY_LEFT == *pressed_key) { return POSITION_UPDATE_LEFT; }
if (KEY_RIGHT == *pressed_key) { return POSITION_UPDATE_RIGHT; }
if ('w' == *pressed_key) { return POSITION_UPDATE_WAIT; }

return POSITION_UPDATE_NONE;
}
4 changes: 4 additions & 0 deletions src/user_input.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef USER_INPUT_H_DEFINED
#define USER_INPUT_H_DEFINED

#define BUTTON_QUIT 'q'

enum cursor_movement {
CURSOR_NONE,
CURSOR_UP,
Expand All @@ -20,12 +22,14 @@ enum position_update {
POSITION_UPDATE_DOWN,
POSITION_UPDATE_LEFT,
POSITION_UPDATE_RIGHT,
POSITION_UPDATE_WAIT,
};

int is_direction_button(int* const pressed_key);
int is_hud_select_button(int* const pressed_key);
int is_cursor_movement_button(int* const pressed_key);
int is_hud_toggle_button(int* const pressed_key);
int is_wait_button(int* const pressed_key);
enum cursor_movement key_to_cursor_movement(int* const pressed_key);
enum hud_toggle key_to_hud_toggle(int* const pressed_key);
enum position_update key_to_position_update(int* const pressed_key);
Expand Down
63 changes: 61 additions & 2 deletions src/util_poet.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#include <assert.h>
#include <stdlib.h>

#include "util_poet.h"
#include "log.h"
#include "user_input.h"
#include "util_poet.h"

int g_game_over = 0;

static enum position_update vertical_movement_towards_player(int row, int player_row);
static enum position_update horizontal_movement_towards_player(int col, int player_col);

int get_first_free_inventory_slot(struct item** const inventory)
{
Expand Down Expand Up @@ -163,6 +169,59 @@ void unpopulate_structures(void)

void game_over(void)
{
strcat(g_new_announcement, "Game Over");
g_game_over = 1;
strcpy(g_new_announcement, "Game Over");
announce(g_new_announcement);
}

int is_game_over(void)
{
return g_game_over;
}

enum position_update movement_towards_player(struct actor* const ha)
{
struct actor* const p = get_player();

int ha_row = get_actor_row(ha);
int ha_col = get_actor_col(ha);
int p_row = get_actor_row(p);
int p_col = get_actor_col(p);

if (ha_row != p_row) {
return vertical_movement_towards_player(ha_row, p_row);
}

if (ha_col != p_col) {
return horizontal_movement_towards_player(ha_col, p_col);
}

// Player and hostile actor have same row and column
assert(0 == 1);
}

static enum position_update vertical_movement_towards_player(int row, int player_row)
{
if (row < player_row) {
return POSITION_UPDATE_DOWN;
}

if (row > player_row) {
return POSITION_UPDATE_UP;
}

return POSITION_UPDATE_WAIT;
}

static enum position_update horizontal_movement_towards_player(int col, int player_col)
{
if (col < player_col) {
return POSITION_UPDATE_RIGHT;
}

if (col > player_col) {
return POSITION_UPDATE_LEFT;
}

return POSITION_UPDATE_WAIT;
}
4 changes: 4 additions & 0 deletions src/util_poet.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ void teardown_io(void);
void populate_structures(void);
void unpopulate_structures(void);
void game_over(void);
int is_game_over(void);
enum position_update movement_towards_player(struct actor* const ha);

extern int g_game_over;

#endif
44 changes: 44 additions & 0 deletions test/utest_util_poet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <criterion/criterion.h>

#include "../src/actor.h"
#include "../src/user_input.h"
#include "../src/util_poet.h"

#if 0
cr_log_warn("message");
#endif

Test(util_poet, get_movement_leading_from_actors_position_towards_player)
{
struct actor* player;
struct actor* skeleton;
enum position_update movement;

spawn_player(2, 2, get_all_actors());
player = get_player();
skeleton = spawn_actor_skeleton(3, 3);

movement = movement_towards_player(skeleton);
cr_expect(POSITION_UPDATE_UP == movement);

set_actor_row(skeleton, 2);
set_actor_col(skeleton, 4);

movement = movement_towards_player(skeleton);
cr_expect(POSITION_UPDATE_LEFT == movement);

set_actor_row(skeleton, 2);
set_actor_col(skeleton, 0);

movement = movement_towards_player(skeleton);
cr_expect(POSITION_UPDATE_RIGHT == movement);

set_actor_row(skeleton, 0);
set_actor_col(skeleton, 0);

movement = movement_towards_player(skeleton);
cr_expect(POSITION_UPDATE_DOWN == movement);

player->op_despawn(player);
skeleton->op_despawn(skeleton);
}

0 comments on commit 27a20d4

Please sign in to comment.