1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 | ------------------------------------------------------------------------------
-- --
-- tiled-code-gen --
-- --
-- Copyright (C) 2018 Fabien Chouteau --
-- --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
-- met: --
-- 1. Redistributions of source code must retain the above copyright --
-- notice, this list of conditions and the following disclaimer. --
-- 2. Redistributions in binary form must reproduce the above copyright --
-- notice, this list of conditions and the following disclaimer in --
-- the documentation and/or other materials provided with the --
-- distribution. --
-- 3. Neither the name of the copyright holder nor the names of its --
-- contributors may be used to endorse or promote products derived --
-- from this software without specific prior written permission. --
-- --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
-- --
------------------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Maps; use Ada.Strings.Maps;
with Ada.Strings.Maps.Constants;
with DOM.Core; use DOM.Core;
with DOM.Core.Nodes; use DOM.Core.Nodes;
with DOM.Core.Elements; use DOM.Core.Elements;
with TCG.Utils; use TCG.Utils;
with TCG.Tilesets; use TCG.Tilesets;
package body TCG.Tile_Layers is
function Create (N : Node) return Tile_Layer;
procedure Load_Data (L : Tile_Layer; N : Node)
with Pre => L /= No_Layer;
Whitespace : constant Ada.Strings.Maps.Character_Set :=
not Ada.Strings.Maps.Constants.Decimal_Digit_Set;
------------
-- Create --
------------
function Create (N : Node) return Tile_Layer is
Id : Natural;
Width : constant Natural := Item_As_Natural (N, "width");
Height : constant Natural := Item_As_Natural (N, "height");
Name : constant String := Item_As_String (N, "name");
L : constant Tile_Layer := new Layer_Data (Width, Height);
begin
if Item_Exists (N, "id") then
Id := Item_As_Natural (N, "id");
else
-- When there is not ID it means that there is only one layer
Id := 0;
end if;
L.Id := Tile_Layer_Id (Id);
L.Name := new String'(Name);
return L;
end Create;
---------------
-- Load_Data --
---------------
procedure Load_Data (L : Tile_Layer; N : Node) is
Data : constant String := Node_Value (First_Child (N));
Cursor : Integer := Data'First;
function Next_Tile return Map_Tile_Id;
---------------
-- Next_Tile --
---------------
function Next_Tile return Map_Tile_Id is
From : constant Integer := Cursor;
To : Integer := Cursor;
begin
while To < Data'Last and then Data (To) /= ',' loop
To := To + 1;
end loop;
Cursor := To + 1;
return Map_Tile_Id'Value
(Trim (Data (From .. To - 1), Whitespace, Whitespace));
end Next_Tile;
Encoding : constant String := Item_As_String (N, "encoding");
begin
if Encoding /= "csv" then
raise Program_Error with "Unsupported layer encoding: " & Encoding;
end if;
for Y in L.Map'Range (2) loop
for X in L.Map'Range (1) loop
L.Map (X, Y) := Next_Tile;
end loop;
end loop;
end Load_Data;
----------
-- Load --
----------
function Load (Root : Node) return Tile_Layer is
L : constant Tile_Layer := Create (Root);
List : Node_List;
begin
List := Get_Elements_By_Tag_Name (Root, "data");
if Length (List) > 1 then
raise Program_Error with "Too many data elements";
end if;
Load_Data (L, Item (List, 0));
Free (List);
return L;
end Load;
----------
-- Name --
----------
function Name (This : Tile_Layer) return String
is (if This.Name /= null then This.Name.all else "");
--------
-- Id --
--------
function Id (This : Tile_Layer) return Tile_Layer_Id
is (This.Id);
-----------
-- Width --
-----------
function Width (This : Tile_Layer) return Natural
is (This.Width);
------------
-- Height --
------------
function Height (This : Tile_Layer) return Natural
is (This.Height);
----------
-- Tile --
----------
function Tile
(This : Tile_Layer;
X, Y : Natural)
return TCG.Tilesets.Map_Tile_Id
is (This.Map (X, Y));
---------
-- Put --
---------
procedure Put (This : Tile_Layer) is
begin
Put_Line ("Layer: " & Name (This) & " Id:" & This.Id'Img);
for Y in This.Map'Range (2) loop
for X in This.Map'Range (1) loop
Put (This.Map (X, Y)'Img &
(if X = This.Map'Last (1) then "," else ", "));
end loop;
New_Line;
end loop;
end Put;
end TCG.Tile_Layers;
|