Extended program 2, added randomize to chapter 1...
This commit is contained in:
parent
b208ea10df
commit
9283711899
@ -307,4 +307,8 @@ char * format_to_string (int number, int sign, int base, int amount, char charac
|
||||
return (string);
|
||||
}
|
||||
|
||||
int randomize (int minimum, int maximum) {
|
||||
return (rand () % (maximum - minimum + 1) + minimum);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -361,4 +361,6 @@ extern void * file_record (char * name); // We store an enti
|
||||
extern char * number_to_string (int number);
|
||||
extern char * format_to_string (int number, int sign, int base, int amount, char character);
|
||||
|
||||
extern int randomize (int minimum, int maximum);
|
||||
|
||||
#endif
|
||||
|
@ -36,10 +36,6 @@ procedure_t game_clean_up (procedure_t) {
|
||||
game_memory = deallocate (game_memory);
|
||||
}
|
||||
|
||||
number_t randomize (number_t lower, number_t upper) {
|
||||
return ((number_t) rand () % (upper - lower + 1) + lower);
|
||||
}
|
||||
|
||||
bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current) {
|
||||
bundle_t * bundle = allocate ((number_t) sizeof (* bundle));
|
||||
|
||||
|
@ -125,8 +125,6 @@ typedef struct generator_t {
|
||||
|
||||
extern procedure_t game_clean_up (procedure_t);
|
||||
|
||||
extern number_t randomize (number_t lower, number_t upper);
|
||||
|
||||
extern bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current);
|
||||
extern symbol_t * game_symbol (number_t character, number_t colour, number_t effect);
|
||||
extern sprite_t * game_sprite (number_t width, number_t height, memory_t data);
|
||||
|
@ -13,6 +13,25 @@ It is distributed in the hope that it will be useful or harmful, it really depen
|
||||
static int player_x = 0;
|
||||
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_render (int entity);
|
||||
|
||||
static void entity_clean_up (void);
|
||||
|
||||
static int entity_count = 0;
|
||||
static char * * entity_name = NULL;
|
||||
static char * entity_character = '\0';
|
||||
static int * entity_colour = 0;
|
||||
static int * entity_effect = 0;
|
||||
static int * entity_value = 0;
|
||||
static int * entity_logic = 0;
|
||||
static int * entity_menu = 0;
|
||||
static int entity_usage = 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_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); }
|
||||
@ -23,28 +42,104 @@ int main (void) {
|
||||
|
||||
curses_configure ();
|
||||
|
||||
curses_bind (SIGNAL_ARROW_UP, player_move_up);
|
||||
curses_bind (SIGNAL_ARROW_DOWN, player_move_down);
|
||||
curses_bind (SIGNAL_ARROW_LEFT, player_move_left);
|
||||
curses_bind (SIGNAL_ARROW_RIGHT, player_move_right);
|
||||
|
||||
curses_bind (SIGNAL_W, player_move_up);
|
||||
curses_bind (SIGNAL_S, player_move_down);
|
||||
curses_bind (SIGNAL_A, player_move_left);
|
||||
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, FALSE, FALSE, 0);
|
||||
|
||||
entity_create (goblin, 3);
|
||||
entity_create (coin, 3);
|
||||
|
||||
while (curses_active) {
|
||||
curses_render_background ('.', COLOUR_GREY, EFFECT_BOLD);
|
||||
|
||||
curses_render_rectangle_fill (',', COLOUR_GREEN, EFFECT_NORMAL, 10, 10, 80, 24);
|
||||
curses_render_rectangle_line ('#', COLOUR_WHITE, EFFECT_NORMAL, 10, 10, 80, 24);
|
||||
|
||||
for (int entity = 0; entity < entity_usage; ++entity) {
|
||||
entity_render (entity);
|
||||
}
|
||||
|
||||
curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player_x, player_y);
|
||||
|
||||
curses_synchronize ();
|
||||
}
|
||||
|
||||
entity_clean_up ();
|
||||
|
||||
terminal_show_cursor (TRUE);
|
||||
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static int entity_define (char * name, int character, int colour, int effect, int value, int logic, int menu) {
|
||||
int entity_index = entity_count;
|
||||
|
||||
++entity_count;
|
||||
|
||||
entity_name = reallocate (entity_name, entity_count * (int) sizeof (* entity_name));
|
||||
entity_character = reallocate (entity_character, entity_count * (int) sizeof (* entity_character));
|
||||
entity_colour = reallocate (entity_colour, entity_count * (int) sizeof (* entity_colour));
|
||||
entity_effect = reallocate (entity_effect, entity_count * (int) sizeof (* entity_effect));
|
||||
entity_value = reallocate (entity_value, entity_count * (int) sizeof (* entity_value));
|
||||
entity_logic = reallocate (entity_logic, entity_count * (int) sizeof (* entity_logic));
|
||||
entity_menu = reallocate (entity_menu, entity_count * (int) sizeof (* entity_menu));
|
||||
|
||||
string_copy ((entity_name [entity_index] = allocate (string_length (name) + 1)), name);
|
||||
|
||||
entity_character [entity_index] = character;
|
||||
entity_colour [entity_index] = colour;
|
||||
entity_effect [entity_index] = effect;
|
||||
entity_value [entity_index] = value;
|
||||
entity_logic [entity_index] = logic;
|
||||
entity_menu [entity_index] = menu;
|
||||
|
||||
return (entity_index);
|
||||
}
|
||||
|
||||
static void entity_create (int entity, int amount) {
|
||||
int index;
|
||||
|
||||
for (index = 0; index < amount; ++index) {
|
||||
++entity_usage;
|
||||
|
||||
entity_index = reallocate (entity_index, entity_usage * (int) sizeof (* entity_index));
|
||||
entity_x = reallocate (entity_x, entity_usage * (int) sizeof (* entity_x));
|
||||
entity_y = reallocate (entity_y, entity_usage * (int) sizeof (* entity_y));
|
||||
|
||||
entity_index [entity_usage - 1] = entity;
|
||||
entity_x [entity_usage - 1] = randomize (0, curses_screen_width - 1);
|
||||
entity_y [entity_usage - 1] = randomize (0, curses_screen_height - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void entity_render (int entity) {
|
||||
curses_render_character (entity_character [entity_index [entity]],
|
||||
entity_colour [entity_index [entity]],
|
||||
entity_effect [entity_index [entity]],
|
||||
entity_x [entity],
|
||||
entity_y [entity]);
|
||||
}
|
||||
|
||||
void entity_clean_up (void) {
|
||||
for (int entity = 0; entity < entity_count; ++entity) {
|
||||
entity_name [entity] = deallocate (entity_name [entity]);
|
||||
}
|
||||
|
||||
entity_name = deallocate (entity_name);
|
||||
entity_character = deallocate (entity_character);
|
||||
entity_colour = deallocate (entity_colour);
|
||||
entity_effect = deallocate (entity_effect);
|
||||
entity_value = deallocate (entity_value);
|
||||
entity_logic = deallocate (entity_logic);
|
||||
entity_menu = deallocate (entity_menu);
|
||||
entity_index = deallocate (entity_index);
|
||||
entity_x = deallocate (entity_x);
|
||||
entity_y = deallocate (entity_y);
|
||||
|
||||
entity_count = 0;
|
||||
entity_usage = 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user