-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworlds.adb
254 lines (229 loc) · 7.47 KB
/
worlds.adb
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
with Ada.Unchecked_Deallocation;
with Physics; use Physics;
with Materials;
with Circles;
package body Worlds is
-- init world
procedure Init(This : in out World; dt : in Float; MaxEnts : Natural := 32)
is
VecZero : constant Vec2D := (0.0, 0.0);
begin
This.MaxEntities := MaxEnts;
This.dt := dt;
This.Invdt := 1.0 / dt;
This.Entities := new EntsList.List;
This.Environments := new EntsList.List;
This.Links := new LinksList.List;
This.Cols := new ColsList.List;
This.InvalidChecker := null;
This.MaxSpeed := VecZero;
This.StaticEnt :=
Circles.Create(VecZero, VecZero, VecZero, 1.0,
Materials.STATIC.SetFriction.SetRestitution(LinkTypesFactors(LTRope)));
end Init;
procedure IncreaseMaxEntities(This : in out World; Count : Positive)
is
begin
This.MaxEntities := This.MaxEntities + Count;
end IncreaseMaxEntities;
procedure Step(This : in out World; Mode : StepModes := Step_Normal)
is
begin
if Mode = Step_LowRAM then
StepLowRAM(This);
else
StepNormal(This);
end if;
end Step;
-- Add entity to the world
procedure AddEntity(This : in out World; Ent : not null EntityClassAcc)
is
begin
if This.MaxEntities = 0
or else Integer(This.Entities.Length)
+ Integer(This.Environments.Length)
+ Integer(This.Links.Length) < This.MaxEntities
then
This.Entities.Append(Ent);
end if;
end AddEntity;
procedure SetMaxSpeed(This : in out World; Speed : Vec2D) is
begin
This.MaxSpeed := Speed;
end SetMaxSpeed;
-- Add env to the world
procedure AddEnvironment(This : in out World; Ent : not null EntityClassAcc)
is
begin
if This.MaxEntities = 0
or else Integer(This.Entities.Length)
+ Integer(This.Environments.Length)
+ Integer(This.Links.Length) < This.MaxEntities
then
This.Environments.Append(Ent);
end if;
end AddEnvironment;
procedure LinkEntities(This : in out World; A, B : EntityClassAcc; LinkType : LinkTypes; Factor : Float := 0.0) is
begin
if This.MaxEntities = 0
or else Integer(This.Entities.Length)
+ Integer(This.Environments.Length)
+ Integer(This.Links.Length) < This.MaxEntities
then
This.Links.Append(CreateLink(A, B, LinkType, Factor));
end if;
end LinkEntities;
procedure UnlinkEntity(This : in out World; E : EntityClassAcc) is
CurLink : LinkAcc;
Edited : Boolean := False;
begin
loop
Edited := False;
declare
use LinksList;
Curs : LinksList.Cursor := This.Links.First;
begin
while Curs /= LinksList.No_Element loop
CurLink := LinksList.Element(Curs);
if CurLink.A = E or CurLink.B = E then
This.Links.Delete(Curs);
FreeLink(CurLink);
Edited := True;
end if;
exit when Edited;
Curs := LinksList.Next(Curs);
end loop;
end;
exit when not Edited;
end loop;
end UnlinkEntity;
-- clear the world (deep free)
procedure Free(This : in out World)
is
use EntsList; use LinksList;
procedure FreeEntList is new Ada.Unchecked_Deallocation(EntsList.List, EntsListAcc);
procedure FreeLinkList is new Ada.Unchecked_Deallocation(LinksList.List, LinksListAcc);
procedure FreeColsList is new Ada.Unchecked_Deallocation(ColsList.List, ColsListAcc);
Curs : EntsList.Cursor := This.Entities.First;
CursL : LinksList.Cursor := This.Links.First;
TmpLink : LinkAcc;
begin
while CursL /= LinksList.No_Element loop
TmpLink := LinksList.Element(CursL);
FreeLink(TmpLink);
CursL := LinksList.Next(CursL);
end loop;
while Curs /= EntsList.No_Element loop
FreeEnt(EntsList.Element(Curs));
Curs := EntsList.Next(Curs);
end loop;
Curs := This.Environments.First;
while Curs /= EntsList.No_Element loop
FreeEnt(EntsList.Element(Curs));
Curs := EntsList.Next(Curs);
end loop;
FreeEnt(This.StaticEnt);
This.Entities.Clear;
This.Environments.Clear;
This.Links.Clear;
This.Cols.Clear;
FreeEntList(This.Entities);
FreeEntList(This.Environments);
FreeLinkList(This.Links);
FreeColsList(This.Cols);
end Free;
-- Gives the world a function to check if entities are valid or not
procedure SetInvalidChecker(This : in out World; Invalider : EntCheckerAcc)
is
begin
if Invalider /= null then
This.InvalidChecker := Invalider;
end if;
end SetInvalidChecker;
-- Remove entity from the world
procedure RemoveEntity(This : in out World; Ent : EntityClassAcc; Destroy : Boolean)
is
Curs : EntsList.Cursor := This.Entities.Find(Ent);
begin
This.Entities.Delete(Curs);
This.UnlinkEntity(Ent);
if Destroy then
FreeEnt(Ent);
end if;
end RemoveEntity;
-- Remove entity from the world
procedure RemoveEnvironment(This : in out World; Ent : not null EntityClassAcc; Destroy : Boolean)
is
Curs : EntsList.Cursor := This.Environments.Find(Ent);
begin
This.Environments.Delete(Curs);
if Destroy then
FreeEnt(Ent);
end if;
end RemoveEnvironment;
function GetEntities(This : in World) return EntsListAcc
is
begin
return This.Entities;
end GetEntities;
function GetClosest(This : in out World; Pos : Vec2D; SearchMode : SearchModes := SM_All) return EntityClassAcc
is
use EntsList;
EntList : constant EntsListAcc := (if SearchMode = SM_All or SearchMode = SM_Entity
then This.Entities
else This.Environments);
Curs : EntsList.Cursor := EntList.First;
Ent : EntityClassAcc;
begin
while Curs /= EntsList.No_Element loop
Ent := EntsList.Element(Curs);
if IsInside(Pos, Ent) then
return Ent;
end if;
Curs := EntsList.Next(Curs);
end loop;
if SearchMode = SM_All then
return This.GetClosest(Pos, SM_Environment);
end if;
return null;
end GetClosest;
function GetEnvironments(This : in World) return EntsListAcc
is
begin
return This.Environments;
end GetEnvironments;
function GetLinks(This : in World) return LinksListAcc
is
begin
return This.Links;
end GetLinks;
procedure CheckEntities(This : in out World)
is
begin
if This.InvalidChecker /= null then
declare
E : EntityClassAcc;
Edited : Boolean := False;
begin
loop
Edited := False;
declare
use EntsList;
Curs : EntsList.Cursor := This.Entities.First;
begin
while Curs /= EntsList.No_Element loop
E := EntsList.Element(Curs);
if This.InvalidChecker.all(E) then
This.RemoveEntity(E, True);
Edited := True;
end if;
exit when Edited;
Curs := EntsList.Next(Curs);
end loop;
end;
exit when not Edited;
end loop;
end;
end if;
end CheckEntities;
end Worlds;