Prototype game engine for Heroes of Might & Magic, featuring a gameplay plot-twist...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
7.5KB

  1. -- Copyright (c) 2024 - Ognjen 'xolatile' Milan Robovic
  2. --
  3. -- GNU General Public Licence (version 3 or later)
  4. with core, ui, resource, item, unit, construction, world;
  5. package body world is
  6. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7. procedure configure is
  8. begin
  9. core.echo (core.comment, "Configuring world components...");
  10. --
  11. tiles := core.import_sprite ("./sprite/world/terrain/terrain.png", 1, 1);
  12. --
  13. for index in landmark_index loop
  14. declare file : constant string := core.lowercase (index'image);
  15. begin
  16. landmarks (index) := core.import_sprite ("./sprite/world/landmark/" & file & ".png", trait (index).frames, 1);
  17. end;
  18. end loop;
  19. end configure;
  20. ------------------------------------------------------------------------------------------
  21. procedure make (index : in biome; width, height : in natural) is
  22. begin
  23. core.echo (core.comment, "-- Procedurally generating new map...");
  24. --
  25. core.echo (core.comment, "-- -- Map type : " & index'image);
  26. core.echo (core.comment, "-- -- Map width :" & width'image);
  27. core.echo (core.comment, "-- -- Map height :" & height'image);
  28. core.echo (core.comment, "-- -- Landmark count :" & landmark_limit'image);
  29. --
  30. map.kind := index;
  31. map.width := width;
  32. map.height := height;
  33. --
  34. map.tiles := new tile_array (0 .. map.width - 1, 0 .. map.height - 1);
  35. map.clips := new clip_array (0 .. map.width - 1, 0 .. map.height - 1);
  36. map.views := new view_array (0 .. map.width - 1, 0 .. map.height - 1);
  37. map.landmarks := new entity_array (1 .. landmark_limit);
  38. map.constructions := new entity_array (1 .. 30);
  39. map.items := new entity_array (1 .. 60);
  40. --
  41. for x in 0 .. width - 1 loop
  42. for y in 0 .. height - 1 loop
  43. map.tiles (x, y) := (core.random (0, 23) * core.random (0, 23)) mod 24;
  44. map.clips (x, y) := false;
  45. map.views (x, y) := false;
  46. end loop;
  47. end loop;
  48. --
  49. for index in 1 .. landmark_limit loop
  50. map.landmarks (index).index := core.random (0, 8);
  51. map.landmarks (index).x := core.random (0, map.width - 1);
  52. map.landmarks (index).y := core.random (0, map.height - 1);
  53. --
  54. if trait (landmark_index'val (map.landmarks (index).index)).clip then
  55. map.clips (map.landmarks (index).x, map.landmarks (index).y) := true;
  56. end if;
  57. end loop;
  58. --
  59. for index in 1 .. 30 loop
  60. map.constructions (index).index := core.random (0, construction.count - 1);
  61. map.constructions (index).x := core.random (0, map.width - 1);
  62. map.constructions (index).y := core.random (0, map.height - 1);
  63. end loop;
  64. --
  65. for index in 1 .. 60 loop
  66. map.items (index).index := core.random (0, item.count - 1);
  67. map.items (index).x := core.random (0, map.width - 1);
  68. map.items (index).y := core.random (0, map.height - 1);
  69. end loop;
  70. --
  71. core.echo (core.success, "Finished procedurally generating new map.");
  72. end make;
  73. ------------------------------------------------------------------------------------------
  74. procedure draw is
  75. u : integer := 0;
  76. v : integer := 0;
  77. --
  78. offset : core.vector := ((core.window_width - core.base) / 2,
  79. (core.window_height - core.base) / 2);
  80. --
  81. render : core.vector := (map.width - 1,
  82. map.height - 1);
  83. begin
  84. view;
  85. --
  86. for vertical in 0 .. map.height - 1 loop
  87. exit when offset.y + (vertical - core.camera.y) * core.base * core.zoom > core.window_height;
  88. --
  89. for horizontal in 0 .. map.width - 1 loop
  90. exit when offset.x + (horizontal - core.camera.x) * core.base * core.zoom > core.window_width;
  91. --
  92. if map.views (horizontal, vertical) then
  93. u := core.base * biome'pos (map.kind) * 4;
  94. v := core.base * map.tiles (horizontal, vertical);
  95. --
  96. core.draw (data => tiles,
  97. x => offset.x + (horizontal - core.camera.x) * core.base * core.zoom,
  98. y => offset.y + (vertical - core.camera.y) * core.base * core.zoom,
  99. u => u,
  100. v => v,
  101. width => core.base,
  102. height => core.base);
  103. --~--MOVE PLAYER TO TILE WHERE YOU CLICKED
  104. --~if core.cursor.x > offset.x + (horizontal - core.camera.x ) * core.base * core.zoom
  105. --~and core.cursor.x < offset.x + (horizontal - core.camera.x + 1) * core.base * core.zoom
  106. --~and core.cursor.y > offset.y + (vertical - core.camera.y ) * core.base * core.zoom
  107. --~and core.cursor.y < offset.y + (vertical - core.camera.y + 1) * core.base * core.zoom
  108. --~and core.cursor_mode = 1 then
  109. --~core.camera.x := horizontal;
  110. --~core.camera.y := vertical;
  111. --~core.cursor_mode := 0;
  112. --~end if;
  113. end if;
  114. end loop;
  115. end loop;
  116. --
  117. for index in 1 .. landmark_limit loop
  118. if map.views (map.landmarks (index).x, map.landmarks (index).y) then
  119. core.draw (data => landmarks (landmark_index'val (map.landmarks (index).index)),
  120. x => offset.x + (map.landmarks (index).x - core.camera.x) * core.base * core.zoom,
  121. y => offset.y + (map.landmarks (index).y - core.camera.y) * core.base * core.zoom);
  122. end if;
  123. end loop;
  124. --
  125. for index in 1 .. 30 loop
  126. if map.views (map.constructions (index).x, map.constructions (index).y) then
  127. construction.draw (construction.enumeration'val (map.constructions (index).index),
  128. offset.x + (map.constructions (index).x - core.camera.x) * core.base * core.zoom,
  129. offset.y + (map.constructions (index).y - core.camera.y) * core.base * core.zoom);
  130. end if;
  131. end loop;
  132. --
  133. for index in 1 .. 60 loop
  134. if map.views (map.items (index).x, map.items (index).y) then
  135. item.draw (item.enumeration'val (map.items (index).index),
  136. offset.x + (map.items (index).x - core.camera.x) * core.base * core.zoom,
  137. offset.y + (map.items (index).y - core.camera.y) * core.base * core.zoom);
  138. end if;
  139. end loop;
  140. --
  141. --~for vertical in 0 .. map.height - 1 loop
  142. --~exit when offset.y + (vertical - core.camera.y) * core.base * core.zoom > core.window_height;
  143. --~--
  144. --~for horizontal in 0 .. map.width - 1 loop
  145. --~exit when offset.x + (horizontal - core.camera.x) * core.base * core.zoom > core.window_width;
  146. --~--
  147. --~u := core.base * biome'pos (map.kind) * 4;
  148. --~v := core.base * map.tiles (horizontal, vertical);
  149. --~--
  150. --~core.draw (data => tiles,
  151. --~x => offset.x + (horizontal - core.camera.x) * core.base * core.zoom,
  152. --~y => offset.y + (vertical - core.camera.y) * core.base * core.zoom,
  153. --~u => u,
  154. --~v => v,
  155. --~width => core.base,
  156. --~height => core.base);
  157. --~end loop;
  158. --~end loop;
  159. end draw;
  160. ------------------------------------------------------------------------------------------
  161. procedure view is
  162. view_reach : constant integer := 12;
  163. begin
  164. for x in 0 .. view_reach loop
  165. for y in 0 .. view_reach loop
  166. map.views (x + core.camera.x - view_reach / 2, y + core.camera.y - view_reach / 2) := true;
  167. end loop;
  168. end loop;
  169. end view;
  170. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  171. end world;