Updated program 2, bot logic and triggers...
This commit is contained in:
parent
9283711899
commit
f328f734d3
@ -10,14 +10,16 @@ It is distributed in the hope that it will be useful or harmful, it really depen
|
|||||||
#include "../chapter/chapter_1.c"
|
#include "../chapter/chapter_1.c"
|
||||||
#include "../chapter/chapter_2.c"
|
#include "../chapter/chapter_2.c"
|
||||||
|
|
||||||
static int player_x = 0;
|
static int entity_define (char * name, int character, int colour, int effect, int value, int logic, void (* trigger) (void));
|
||||||
static int player_y = 0;
|
|
||||||
|
|
||||||
static int entity_define (char * name, int character, int colour, int effect, int value, int logic, int menu);
|
|
||||||
static void entity_create (int entity, int amount);
|
static void entity_create (int entity, int amount);
|
||||||
static void entity_render (int entity);
|
static void entity_render (int entity);
|
||||||
|
|
||||||
static void entity_clean_up (void);
|
static void entity_clean_up (void);
|
||||||
|
static void entity_update_logic (void);
|
||||||
|
|
||||||
|
static int player_x = 0;
|
||||||
|
static int player_y = 0;
|
||||||
|
static int player_coins = 0;
|
||||||
|
|
||||||
static int entity_count = 0;
|
static int entity_count = 0;
|
||||||
static char * * entity_name = NULL;
|
static char * * entity_name = NULL;
|
||||||
@ -26,16 +28,20 @@ static int * entity_colour = 0;
|
|||||||
static int * entity_effect = 0;
|
static int * entity_effect = 0;
|
||||||
static int * entity_value = 0;
|
static int * entity_value = 0;
|
||||||
static int * entity_logic = 0;
|
static int * entity_logic = 0;
|
||||||
static int * entity_menu = 0;
|
|
||||||
static int entity_usage = 0;
|
static void (* * entity_trigger) (void) = NULL;
|
||||||
static int * entity_index = 0;
|
|
||||||
static int * entity_x = 0;
|
static int entity_usage = 0;
|
||||||
static int * entity_y = 0;
|
static int * entity_index = 0;
|
||||||
|
static int * entity_x = 0;
|
||||||
|
static int * entity_y = 0;
|
||||||
|
|
||||||
static void player_move_up (void) { player_y -= 1; limit (& player_y, 0, curses_screen_height - 1); }
|
static void player_move_up (void) { player_y -= 1; limit (& player_y, 0, curses_screen_height - 1); }
|
||||||
static void player_move_down (void) { player_y += 1; limit (& player_y, 0, curses_screen_height - 1); }
|
static void player_move_down (void) { player_y += 1; limit (& player_y, 0, curses_screen_height - 1); }
|
||||||
static void player_move_left (void) { player_x -= 1; limit (& player_x, 0, curses_screen_width - 1); }
|
static void player_move_left (void) { player_x -= 1; limit (& player_x, 0, curses_screen_width - 1); }
|
||||||
static void player_move_right (void) { player_x += 1; limit (& player_x, 0, curses_screen_width - 1); }
|
static void player_move_right (void) { player_x += 1; limit (& player_x, 0, curses_screen_width - 1); }
|
||||||
|
static void player_take_coin (void) { ++player_coins; }
|
||||||
|
static void player_died (void) { curses_active = 0; }
|
||||||
|
|
||||||
int main (void) {
|
int main (void) {
|
||||||
terminal_show_cursor (FALSE);
|
terminal_show_cursor (FALSE);
|
||||||
@ -47,11 +53,13 @@ int main (void) {
|
|||||||
curses_bind (SIGNAL_A, player_move_left);
|
curses_bind (SIGNAL_A, player_move_left);
|
||||||
curses_bind (SIGNAL_D, player_move_right);
|
curses_bind (SIGNAL_D, player_move_right);
|
||||||
|
|
||||||
int goblin = entity_define ("Goblin", 'G', COLOUR_RED, EFFECT_NORMAL, FALSE, FALSE, 0);
|
int coin = entity_define ("Coin", '+', COLOUR_YELLOW, EFFECT_BOLD, 1, FALSE, player_take_coin);
|
||||||
int coin = entity_define ("Coin", '+', COLOUR_YELLOW, EFFECT_BOLD, FALSE, FALSE, 0);
|
int goblin = entity_define ("Goblin", 'g', COLOUR_RED, EFFECT_NORMAL, 3, TRUE, player_died);
|
||||||
|
int hob_goblin = entity_define ("Hob Goblin", 'G', COLOUR_RED, EFFECT_BOLD, 7, TRUE, player_died);
|
||||||
|
|
||||||
entity_create (goblin, 3);
|
entity_create (coin, 11);
|
||||||
entity_create (coin, 3);
|
entity_create (goblin, 7);
|
||||||
|
entity_create (hob_goblin, 3);
|
||||||
|
|
||||||
while (curses_active) {
|
while (curses_active) {
|
||||||
curses_render_background ('.', COLOUR_GREY, EFFECT_BOLD);
|
curses_render_background ('.', COLOUR_GREY, EFFECT_BOLD);
|
||||||
@ -65,6 +73,10 @@ int main (void) {
|
|||||||
|
|
||||||
curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player_x, player_y);
|
curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player_x, player_y);
|
||||||
|
|
||||||
|
curses_render_number (player_coins, COLOUR_YELLOW, EFFECT_BOLD, 0, 0);
|
||||||
|
|
||||||
|
entity_update_logic ();
|
||||||
|
|
||||||
curses_synchronize ();
|
curses_synchronize ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,8 +87,8 @@ int main (void) {
|
|||||||
return (EXIT_SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int entity_define (char * name, int character, int colour, int effect, int value, int logic, int menu) {
|
static int entity_define (char * name, int character, int colour, int effect, int value, int logic, void (* trigger) (void)) {
|
||||||
int entity_index = entity_count;
|
int entity = entity_count;
|
||||||
|
|
||||||
++entity_count;
|
++entity_count;
|
||||||
|
|
||||||
@ -86,18 +98,18 @@ static int entity_define (char * name, int character, int colour, int effect, in
|
|||||||
entity_effect = reallocate (entity_effect, entity_count * (int) sizeof (* entity_effect));
|
entity_effect = reallocate (entity_effect, entity_count * (int) sizeof (* entity_effect));
|
||||||
entity_value = reallocate (entity_value, entity_count * (int) sizeof (* entity_value));
|
entity_value = reallocate (entity_value, entity_count * (int) sizeof (* entity_value));
|
||||||
entity_logic = reallocate (entity_logic, entity_count * (int) sizeof (* entity_logic));
|
entity_logic = reallocate (entity_logic, entity_count * (int) sizeof (* entity_logic));
|
||||||
entity_menu = reallocate (entity_menu, entity_count * (int) sizeof (* entity_menu));
|
entity_trigger = reallocate (entity_trigger, entity_count * (int) sizeof (* entity_trigger));
|
||||||
|
|
||||||
string_copy ((entity_name [entity_index] = allocate (string_length (name) + 1)), name);
|
string_copy ((entity_name [entity] = allocate (string_length (name) + 1)), name);
|
||||||
|
|
||||||
entity_character [entity_index] = character;
|
entity_character [entity] = character;
|
||||||
entity_colour [entity_index] = colour;
|
entity_colour [entity] = colour;
|
||||||
entity_effect [entity_index] = effect;
|
entity_effect [entity] = effect;
|
||||||
entity_value [entity_index] = value;
|
entity_value [entity] = value;
|
||||||
entity_logic [entity_index] = logic;
|
entity_logic [entity] = logic;
|
||||||
entity_menu [entity_index] = menu;
|
entity_trigger [entity] = trigger;
|
||||||
|
|
||||||
return (entity_index);
|
return (entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void entity_create (int entity, int amount) {
|
static void entity_create (int entity, int amount) {
|
||||||
@ -117,6 +129,10 @@ static void entity_create (int entity, int amount) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void entity_render (int entity) {
|
static void entity_render (int entity) {
|
||||||
|
if (entity_index [entity] == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
curses_render_character (entity_character [entity_index [entity]],
|
curses_render_character (entity_character [entity_index [entity]],
|
||||||
entity_colour [entity_index [entity]],
|
entity_colour [entity_index [entity]],
|
||||||
entity_effect [entity_index [entity]],
|
entity_effect [entity_index [entity]],
|
||||||
@ -135,7 +151,7 @@ void entity_clean_up (void) {
|
|||||||
entity_effect = deallocate (entity_effect);
|
entity_effect = deallocate (entity_effect);
|
||||||
entity_value = deallocate (entity_value);
|
entity_value = deallocate (entity_value);
|
||||||
entity_logic = deallocate (entity_logic);
|
entity_logic = deallocate (entity_logic);
|
||||||
entity_menu = deallocate (entity_menu);
|
entity_trigger = deallocate (entity_trigger);
|
||||||
entity_index = deallocate (entity_index);
|
entity_index = deallocate (entity_index);
|
||||||
entity_x = deallocate (entity_x);
|
entity_x = deallocate (entity_x);
|
||||||
entity_y = deallocate (entity_y);
|
entity_y = deallocate (entity_y);
|
||||||
@ -143,3 +159,27 @@ void entity_clean_up (void) {
|
|||||||
entity_count = 0;
|
entity_count = 0;
|
||||||
entity_usage = 0;
|
entity_usage = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void entity_update_logic (void) {
|
||||||
|
for (int entity = 0; entity < entity_usage; ++entity) {
|
||||||
|
if (entity_index [entity] == -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entity_logic [entity_index [entity]] == TRUE) {
|
||||||
|
entity_x [entity] += randomize (0, 1) * ((randomize (0, 1) == 0) ? +1 : -1);
|
||||||
|
entity_y [entity] += randomize (0, 1) * ((randomize (0, 1) == 0) ? +1 : -1);
|
||||||
|
|
||||||
|
limit (& entity_x [entity], 0, curses_screen_width - 1);
|
||||||
|
limit (& entity_y [entity], 0, curses_screen_height - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((player_x == entity_x [entity]) && (player_y == entity_y [entity])) {
|
||||||
|
if (entity_trigger [entity_index [entity]] != NULL) {
|
||||||
|
entity_trigger [entity_index [entity]] ();
|
||||||
|
}
|
||||||
|
|
||||||
|
entity_index [entity] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user