More to come...
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

178 строки
6.3KB

  1. #include "../chapter/chapter_0.c"
  2. #include "../chapter/chapter_1.c"
  3. #include "../chapter/chapter_2.c"
  4. static int entity_define (char * name, int character, int colour, int effect, int value, int logic, void (* trigger) (void));
  5. static void entity_create (int entity, int amount);
  6. static void entity_render (int entity);
  7. static void entity_clean_up (void);
  8. static void entity_update_logic (void);
  9. static int player_x = 0;
  10. static int player_y = 0;
  11. static int player_coins = 0;
  12. static int entity_count = 0;
  13. static char * * entity_name = NULL;
  14. static char * entity_character = '\0';
  15. static int * entity_colour = 0;
  16. static int * entity_effect = 0;
  17. static int * entity_value = 0;
  18. static int * entity_logic = 0;
  19. static void (* * entity_trigger) (void) = NULL;
  20. static int entity_usage = 0;
  21. static int * entity_index = 0;
  22. static int * entity_x = 0;
  23. static int * entity_y = 0;
  24. static void player_move_up (void) { player_y -= 1; limit (& player_y, 0, curses_screen_height - 1); }
  25. static void player_move_down (void) { player_y += 1; limit (& player_y, 0, curses_screen_height - 1); }
  26. static void player_move_left (void) { player_x -= 1; limit (& player_x, 0, curses_screen_width - 1); }
  27. static void player_move_right (void) { player_x += 1; limit (& player_x, 0, curses_screen_width - 1); }
  28. static void player_take_coin (void) { ++player_coins; }
  29. static void player_died (void) { curses_active = 0; }
  30. int main (void) {
  31. terminal_show_cursor (FALSE);
  32. curses_configure ();
  33. curses_bind (SIGNAL_W, player_move_up);
  34. curses_bind (SIGNAL_S, player_move_down);
  35. curses_bind (SIGNAL_A, player_move_left);
  36. curses_bind (SIGNAL_D, player_move_right);
  37. int coin = entity_define ("Coin", '+', COLOUR_YELLOW, EFFECT_BOLD, 1, FALSE, player_take_coin);
  38. int goblin = entity_define ("Goblin", 'g', COLOUR_RED, EFFECT_NORMAL, 3, TRUE, player_died);
  39. int hob_goblin = entity_define ("Hob Goblin", 'G', COLOUR_RED, EFFECT_BOLD, 7, TRUE, player_died);
  40. entity_create (coin, 11);
  41. entity_create (goblin, 7);
  42. entity_create (hob_goblin, 3);
  43. while (curses_active) {
  44. curses_render_background ('.', COLOUR_GREY, EFFECT_BOLD);
  45. curses_render_rectangle_fill (',', COLOUR_GREEN, EFFECT_NORMAL, 10, 10, 80, 24);
  46. curses_render_rectangle_line ('#', COLOUR_WHITE, EFFECT_NORMAL, 10, 10, 80, 24);
  47. for (int entity = 0; entity < entity_usage; ++entity) {
  48. entity_render (entity);
  49. }
  50. curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player_x, player_y);
  51. curses_render_number (player_coins, COLOUR_YELLOW, EFFECT_BOLD, 0, 0);
  52. entity_update_logic ();
  53. curses_synchronize ();
  54. }
  55. entity_clean_up ();
  56. terminal_show_cursor (TRUE);
  57. return (EXIT_SUCCESS);
  58. }
  59. static int entity_define (char * name, int character, int colour, int effect, int value, int logic, void (* trigger) (void)) {
  60. int entity = entity_count;
  61. ++entity_count;
  62. entity_name = reallocate (entity_name, entity_count * (int) sizeof (* entity_name));
  63. entity_character = reallocate (entity_character, entity_count * (int) sizeof (* entity_character));
  64. entity_colour = reallocate (entity_colour, entity_count * (int) sizeof (* entity_colour));
  65. entity_effect = reallocate (entity_effect, entity_count * (int) sizeof (* entity_effect));
  66. entity_value = reallocate (entity_value, entity_count * (int) sizeof (* entity_value));
  67. entity_logic = reallocate (entity_logic, entity_count * (int) sizeof (* entity_logic));
  68. entity_trigger = reallocate (entity_trigger, entity_count * (int) sizeof (* entity_trigger));
  69. string_copy ((entity_name [entity] = allocate (string_length (name) + 1)), name);
  70. entity_character [entity] = character;
  71. entity_colour [entity] = colour;
  72. entity_effect [entity] = effect;
  73. entity_value [entity] = value;
  74. entity_logic [entity] = logic;
  75. entity_trigger [entity] = trigger;
  76. return (entity);
  77. }
  78. static void entity_create (int entity, int amount) {
  79. int index;
  80. for (index = 0; index < amount; ++index) {
  81. ++entity_usage;
  82. entity_index = reallocate (entity_index, entity_usage * (int) sizeof (* entity_index));
  83. entity_x = reallocate (entity_x, entity_usage * (int) sizeof (* entity_x));
  84. entity_y = reallocate (entity_y, entity_usage * (int) sizeof (* entity_y));
  85. entity_index [entity_usage - 1] = entity;
  86. entity_x [entity_usage - 1] = randomize (0, curses_screen_width - 1);
  87. entity_y [entity_usage - 1] = randomize (0, curses_screen_height - 1);
  88. }
  89. }
  90. static void entity_render (int entity) {
  91. if (entity_index [entity] == -1) {
  92. return;
  93. }
  94. curses_render_character (entity_character [entity_index [entity]],
  95. entity_colour [entity_index [entity]],
  96. entity_effect [entity_index [entity]],
  97. entity_x [entity],
  98. entity_y [entity]);
  99. }
  100. void entity_clean_up (void) {
  101. for (int entity = 0; entity < entity_count; ++entity) {
  102. entity_name [entity] = deallocate (entity_name [entity]);
  103. }
  104. entity_name = deallocate (entity_name);
  105. entity_character = deallocate (entity_character);
  106. entity_colour = deallocate (entity_colour);
  107. entity_effect = deallocate (entity_effect);
  108. entity_value = deallocate (entity_value);
  109. entity_logic = deallocate (entity_logic);
  110. entity_trigger = deallocate (entity_trigger);
  111. entity_index = deallocate (entity_index);
  112. entity_x = deallocate (entity_x);
  113. entity_y = deallocate (entity_y);
  114. entity_count = 0;
  115. entity_usage = 0;
  116. }
  117. void entity_update_logic (void) {
  118. for (int entity = 0; entity < entity_usage; ++entity) {
  119. if (entity_index [entity] == -1) {
  120. continue;
  121. }
  122. if (entity_logic [entity_index [entity]] == TRUE) {
  123. entity_x [entity] += randomize (0, 1) * ((randomize (0, 1) == 0) ? +1 : -1);
  124. entity_y [entity] += randomize (0, 1) * ((randomize (0, 1) == 0) ? +1 : -1);
  125. limit (& entity_x [entity], 0, curses_screen_width - 1);
  126. limit (& entity_y [entity], 0, curses_screen_height - 1);
  127. }
  128. if ((player_x == entity_x [entity]) && (player_y == entity_y [entity])) {
  129. if (entity_trigger [entity_index [entity]] != NULL) {
  130. entity_trigger [entity_index [entity]] ();
  131. }
  132. entity_index [entity] = -1;
  133. }
  134. }
  135. }