Prototype game engine for Heroes of Might & Magic, featuring a gameplay plot-twist...
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

175 行
7.2KB

  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 (6, map.width - 6);
  62. map.constructions (index).y := core.random (6, map.height - 6);
  63. --
  64. declare reach_x : constant natural := construction.sprite (construction.enumeration'val (map.constructions (index).index)).width / core.base;
  65. reach_y : constant natural := construction.sprite (construction.enumeration'val (map.constructions (index).index)).height / core.base;
  66. begin
  67. for x in 0 .. reach_x - 1 loop
  68. for y in 0 .. reach_y - 1 loop
  69. map.clips (map.constructions (index).x + x, map.constructions (index).y + y) := true;
  70. end loop;
  71. end loop;
  72. end;
  73. end loop;
  74. --
  75. for index in 1 .. 60 loop
  76. map.items (index).index := core.random (0, item.count - 1);
  77. map.items (index).x := core.random (0, map.width - 1);
  78. map.items (index).y := core.random (0, map.height - 1);
  79. end loop;
  80. --
  81. core.echo (core.success, "Finished procedurally generating new map.");
  82. end make;
  83. ------------------------------------------------------------------------------------------
  84. procedure draw is
  85. u : integer := 0;
  86. v : integer := 0;
  87. --
  88. offset : core.vector := ((core.window_width - core.base) / 2,
  89. (core.window_height - core.base) / 2);
  90. --
  91. render : core.vector := (map.width - 1,
  92. map.height - 1);
  93. begin
  94. view;
  95. --
  96. for vertical in 0 .. map.height - 1 loop
  97. exit when offset.y + (vertical - core.camera.y) * core.base * core.zoom > core.window_height;
  98. --
  99. for horizontal in 0 .. map.width - 1 loop
  100. exit when offset.x + (horizontal - core.camera.x) * core.base * core.zoom > core.window_width;
  101. --
  102. if map.views (horizontal, vertical) then
  103. u := core.base * biome'pos (map.kind) * 4;
  104. v := core.base * map.tiles (horizontal, vertical);
  105. --
  106. core.draw (data => tiles,
  107. x => offset.x + (horizontal - core.camera.x) * core.base * core.zoom,
  108. y => offset.y + (vertical - core.camera.y) * core.base * core.zoom,
  109. u => u,
  110. v => v,
  111. width => core.base,
  112. height => core.base);
  113. --~--MOVE PLAYER TO TILE WHERE YOU CLICKED
  114. --~if core.cursor.x > offset.x + (horizontal - core.camera.x ) * core.base * core.zoom
  115. --~and core.cursor.x < offset.x + (horizontal - core.camera.x + 1) * core.base * core.zoom
  116. --~and core.cursor.y > offset.y + (vertical - core.camera.y ) * core.base * core.zoom
  117. --~and core.cursor.y < offset.y + (vertical - core.camera.y + 1) * core.base * core.zoom
  118. --~and core.cursor_mode = 1 then
  119. --~core.camera.x := horizontal;
  120. --~core.camera.y := vertical;
  121. --~core.cursor_mode := 0;
  122. --~end if;
  123. end if;
  124. end loop;
  125. end loop;
  126. --
  127. for index in 1 .. landmark_limit loop
  128. if map.views (map.landmarks (index).x, map.landmarks (index).y) then
  129. core.draw (data => landmarks (landmark_index'val (map.landmarks (index).index)),
  130. x => offset.x + (map.landmarks (index).x - core.camera.x) * core.base * core.zoom,
  131. y => offset.y + (map.landmarks (index).y - core.camera.y) * core.base * core.zoom);
  132. end if;
  133. end loop;
  134. --
  135. for index in 1 .. 30 loop
  136. if map.views (map.constructions (index).x, map.constructions (index).y) then
  137. construction.draw (construction.enumeration'val (map.constructions (index).index),
  138. offset.x + (map.constructions (index).x - core.camera.x) * core.base * core.zoom,
  139. offset.y + (map.constructions (index).y - core.camera.y) * core.base * core.zoom);
  140. end if;
  141. end loop;
  142. --
  143. for index in 1 .. 60 loop
  144. if map.views (map.items (index).x, map.items (index).y) then
  145. item.draw (item.enumeration'val (map.items (index).index),
  146. offset.x + (map.items (index).x - core.camera.x) * core.base * core.zoom,
  147. offset.y + (map.items (index).y - core.camera.y) * core.base * core.zoom);
  148. end if;
  149. end loop;
  150. end draw;
  151. ------------------------------------------------------------------------------------------
  152. procedure view is
  153. view_reach : constant integer := 12;
  154. begin
  155. for x in 0 .. view_reach loop
  156. for y in 0 .. view_reach loop
  157. map.views (x + core.camera.x - view_reach / 2, y + core.camera.y - view_reach / 2) := true;
  158. end loop;
  159. end loop;
  160. end view;
  161. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  162. end world;