xhads/source/world.adb

141 lines
5.3 KiB
Ada
Raw Normal View History

-- Copyright (c) 2024 - Ognjen 'xolatile' Milan Robovic
--
-- GNU General Public Licence (version 3 or later)
with core, resource, item, unit, construction, world;
2024-02-15 21:03:09 -05:00
package body world is
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
type sprite_array is array (natural range <>) of core.sprite;
type world_array is array (natural range <>) of access information;
type limit_array is array (enumeration) of integer;
2024-02-15 21:03:09 -05:00
type landmark_sprite_array is array (enumeration) of access sprite_array;
2024-02-15 21:03:09 -05:00
------------------------------------------------------------------------------------------
blocks : core.sprite;
landmarks : landmark_sprite_array := (others => null);
limit : constant limit_array := (2, 2, 2, 2, 2, 2);
2024-02-15 21:03:09 -05:00
2024-02-16 07:53:36 -05:00
landmark_limit : constant integer := 140;
2024-02-26 18:00:15 -05:00
construction_limit : constant integer := 40;
item_limit : constant integer := 40;
2024-02-16 07:53:36 -05:00
2024-02-15 21:03:09 -05:00
------------------------------------------------------------------------------------------
procedure configure is
begin
2024-03-11 08:42:25 -04:00
core.echo (core.comment, "Configuring world components...");
--
blocks := core.import_sprite ("./sprite/world/terrain/terrain.png", 1, 1);
2024-02-15 21:03:09 -05:00
--
for index in enumeration
2024-02-15 21:03:09 -05:00
loop
2024-04-25 04:14:45 -04:00
landmarks (index) := new sprite_array (0 .. limit (index) - 1);
--
for value in 0 .. limit (index) - 1
2024-02-15 21:03:09 -05:00
loop
declare
folder : constant string := core.lowercase (enumeration'image (index));
file : constant string := value'image;
2024-02-15 21:03:09 -05:00
begin
landmarks (index) (value) := core.import_sprite ("./sprite/world/landmark/" & folder & "/" & file & ".png", 1, 1);
2024-02-15 21:03:09 -05:00
end;
end loop;
end loop;
end configure;
------------------------------------------------------------------------------------------
procedure make (index : in enumeration; width, height : in natural) is
2024-02-15 21:03:09 -05:00
begin
core.echo (core.comment, "-- Procedurally generating new map...");
2024-04-25 04:14:45 -04:00
--
core.echo (core.comment, "-- -- Map type : " & index'image);
core.echo (core.comment, "-- -- Map width :" & width'image);
core.echo (core.comment, "-- -- Map height :" & height'image);
core.echo (core.comment, "-- -- Landmark count :" & landmark_limit'image);
core.echo (core.comment, "-- -- Construction count :" & construction_limit'image);
core.echo (core.comment, "-- -- Item count :" & item_limit'image);
--
2024-02-15 21:03:09 -05:00
map.terrain := index;
map.width := width;
map.height := height;
map.block := new block_array (0 .. width - 1, 0 .. height - 1);
2024-02-16 07:53:36 -05:00
map.landmark := new entity_array (0 .. landmark_limit);
map.construction := new entity_array (0 .. construction_limit);
2024-02-26 18:00:15 -05:00
map.item := new entity_array (0 .. item_limit);
2024-02-15 21:03:09 -05:00
--
for x in 0 .. width - 1
loop
for y in 0 .. height - 1
loop
map.block (x, y) := (x * x + x * y + y * y) mod 24;
2024-02-15 21:03:09 -05:00
end loop;
end loop;
--
2024-02-16 07:53:36 -05:00
for object in 0 .. landmark_limit
2024-02-15 21:03:09 -05:00
loop
2024-04-26 11:05:19 -04:00
map.landmark (object).index := core.random (0, limit (index) - 1);
map.landmark (object).x := core.base * core.random (1, map.width - 1);
map.landmark (object).y := core.base * core.random (1, map.height - 1);
2024-02-15 21:03:09 -05:00
end loop;
--
core.echo (core.success, "Finished procedurally generating new map.");
2024-02-15 21:03:09 -05:00
end make;
------------------------------------------------------------------------------------------
procedure draw (x, y, width, height : in integer) is
2024-02-16 07:53:36 -05:00
crop_width : integer := width mod core.base;
crop_height : integer := height mod core.base;
u, v : integer;
2024-02-15 21:03:09 -05:00
begin
for move_y in 0 .. height / core.base - 1
loop
for move_x in 0 .. width / core.base - 1
2024-02-15 21:03:09 -05:00
loop
u := core.base * enumeration'pos (map.terrain) * 4;
v := core.base * map.block (core.camera.x + move_x, core.camera.y + move_y);
2024-02-15 21:03:09 -05:00
--
core.draw (blocks, x + move_x * core.base, y + move_y * core.base, u, v, core.base, core.base);
2024-02-15 21:03:09 -05:00
end loop;
--
u := core.base * enumeration'pos (map.terrain) * 4;
v := core.base * map.block (width / core.base, core.camera.y + move_y);
2024-02-15 21:03:09 -05:00
--
core.draw (blocks, x + width - crop_width, y + move_y * core.base, u, v, crop_width, core.base);
2024-02-15 21:03:09 -05:00
end loop;
--
for move_x in 0 .. width / core.base - 1
loop
u := core.base * enumeration'pos (map.terrain) * 4;
v := core.base * map.block (core.camera.x + move_x, height / core.base);
2024-02-15 21:03:09 -05:00
--
core.draw (blocks, x + move_x * core.base, y + height - crop_height, u, v, core.base, crop_height);
2024-02-15 21:03:09 -05:00
end loop;
--
u := core.base * enumeration'pos (map.terrain) * 4;
2024-02-15 21:03:09 -05:00
v := core.base * map.block (width / core.base, height / core.base);
--
core.draw (blocks, x + width - crop_width, y + height - crop_height, u, v, crop_width, crop_height);
2024-02-15 21:03:09 -05:00
--
2024-02-16 07:53:36 -05:00
for object in 0 .. landmark_limit
2024-02-15 21:03:09 -05:00
loop
core.draw (landmarks (map.terrain) (map.landmark (object).index),
map.landmark (object).x - core.camera.x * core.base,
map.landmark (object).y - core.camera.y * core.base,
x, y, width, height);
2024-02-15 21:03:09 -05:00
end loop;
end draw;
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
end world;