-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutilEntitySeeker.angelscript
More file actions
248 lines (232 loc) · 5.88 KB
/
Copy pathutilEntitySeeker.angelscript
File metadata and controls
248 lines (232 loc) · 5.88 KB
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
/// Creates an array containing every entity within thisEntity's bucket and the buckets around it, including itself
void getSurroundingEntities(ETHEntity @thisEntity, ETHEntityArray @outEntities)
{
GetEntitiesAroundBucket(thisEntity.GetCurrentBucket(), outEntities);
}
/// Finds an entity named 'entityName' among all thisEntity's surrounding entities.
ETHEntity @findAmongNeighbourEntities(ETHEntity @thisEntity, const string &in entityName)
{
ETHEntityArray outEntities;
GetEntitiesAroundBucket(thisEntity.GetCurrentBucket(), outEntities, entityName);
if (outEntities.size() > 0)
return outEntities[0];
else
return null;
}
/// Finds all entities named 'entityName' among all thisEntity's surrounding entities.
void findAllAmongNeighbourEntities(ETHEntity @thisEntity, const string &in entityName, ETHEntityArray @outEntities)
{
findAllAmongNeighbourEntities(thisEntity, ByNameChooser(entityName), @outEntities);
}
void findAllAmongNeighbourEntities(ETHEntity @thisEntity, EntityChooser@ chooser, ETHEntityArray @outEntities)
{
ETHEntityArray entityArray;
getSurroundingEntities(thisEntity, entityArray);
uint size = entityArray.size();
for (uint t=0; t<size; t++)
{
if (chooser.choose(entityArray[t]))
{
ETHEntity@ temp = entityArray[t];
outEntities.push_back(temp);
}
}
}
/// Scans the screen for an entity named 'name' and returns a handle to it if found.
ETHEntity @findEntityInScreen(const string &in name)
{
ETHEntityArray entities;
GetVisibleEntities(entities);
for (uint t=0; t<entities.size(); t++)
{
if (entities[t].GetEntityName() == name)
{
return entities[t];
}
}
return null;
}
void getEntitiesFromNeighbourBuckets(const vector2 &in centerBucket, uint count, ETHEntityArray@ targets)
{
int _count = int(count);
for (int i = _count * -1; i <= _count; i++)
{
for (int k = _count * -1; k <= _count; k++)
{
GetEntitiesFromBucket(vector2(centerBucket.x - i, centerBucket.y - k), targets);
}
}
}
interface EntityChooser
{
bool choose(ETHEntity@ entity);
}
class DefaultChooser : EntityChooser { bool choose(ETHEntity@ entity) { return true; } }
class DynamicChooser : EntityChooser { bool choose(ETHEntity@ entity) { return !entity.IsStatic(); } }
class ByNameChooser : EntityChooser
{
private string m_name;
ByNameChooser(const string &in name)
{
m_name = name;
}
bool choose(ETHEntity@ entity)
{
return entity.GetEntityName() == m_name;
}
}
class ByIDChooser : EntityChooser
{
private int m_id;
ByIDChooser(const int id)
{
m_id = id;
}
bool choose(ETHEntity@ entity)
{
return entity.GetID() == m_id;
}
}
DefaultChooser g_defaultChooser;
DynamicChooser g_dynamicChooser;
class DynamicBodyChooser : EntityChooser
{
bool choose(ETHEntity@ entity)
{
if (entity.IsStatic())
return false;
if (entity.GetPhysicsController() !is null)
return true;
else
return false;
}
}
vector2[] __neighbourBucketSearchPriority;
ETHEntity@ seekNeighbourEntity(const vector2 &in centerBucket, EntityChooser@ chooser = @g_defaultChooser)
{
if (__neighbourBucketSearchPriority.length() == 0)
{
__neighbourBucketSearchPriority.insertLast(vector2(0,0));
__neighbourBucketSearchPriority.insertLast(vector2(1,0));
__neighbourBucketSearchPriority.insertLast(vector2(-1,0));
__neighbourBucketSearchPriority.insertLast(vector2(0,1));
__neighbourBucketSearchPriority.insertLast(vector2(0,-1));
__neighbourBucketSearchPriority.insertLast(vector2(1,1));
__neighbourBucketSearchPriority.insertLast(vector2(-1,1));
__neighbourBucketSearchPriority.insertLast(vector2(1,-1));
__neighbourBucketSearchPriority.insertLast(vector2(-1,-1));
}
ETHEntityArray ents;
for (uint i = 0; i < __neighbourBucketSearchPriority.length(); i++)
{
GetEntitiesFromBucket(centerBucket + __neighbourBucketSearchPriority[i], ents);
for (uint t = 0; t < ents.size(); t++)
{
if (chooser.choose(@ents[t]))
{
return ents[t];
}
}
ents.clear();
}
return null;
}
ETHEntity@ seekEntity(const string &in entityName, EntityChooser@ chooser = @g_defaultChooser)
{
ETHEntityArray ents;
GetAllEntitiesInScene(ents);
for (uint t = 0; t < ents.size(); t++)
{
if (ents[t].GetEntityName() == entityName && chooser.choose(@ents[t]))
{
return ents[t];
}
}
return null;
}
ETHEntity@ seekEntity(EntityChooser@ chooser = @g_defaultChooser)
{
ETHEntityArray ents;
GetAllEntitiesInScene(ents);
for (uint t = 0; t < ents.size(); t++)
{
if (chooser.choose(@ents[t]))
{
return ents[t];
}
}
return null;
}
funcdef bool ENTITY_CHOOSER (ETHEntity@ entity);
ETHEntity@ seekEntity(ENTITY_CHOOSER@ chooser)
{
ETHEntityArray ents;
GetAllEntitiesInScene(ents);
for (uint t = 0; t < ents.size(); t++)
{
if (chooser(@ents[t]))
{
return ents[t];
}
}
return null;
}
ETHEntity@ seekEntityFromBucket(const vector2 &in bucket, const int entityID)
{
ETHEntityArray ents;
GetEntitiesFromBucket(bucket, ents);
for (uint t = 0; t < ents.size(); t++)
{
if (ents[t].GetID() == entityID)
{
return ents[t];
}
}
return null;
}
ETHEntity@ seekEntityFromBucket(const vector2 &in bucket, const string &in name)
{
ETHEntityArray ents;
GetEntitiesFromBucket(bucket, ents);
for (uint t = 0; t < ents.size(); t++)
{
if (ents[t].GetEntityName() == name)
{
return ents[t];
}
}
return null;
}
ETHEntity@ chooseClosestContact(const vector2 &in a, const vector2 &in b, EntityChooser@ chooser)
{
ETHEntityArray entities;
float squaredDist = squaredDistance(a, b);
GetContactEntities(a, b, entities);
ETHEntity@ r = null;
for (uint t = 0; t < entities.size(); t++)
{
if (chooser.choose(entities[t]))
{
const float currentDistance = squaredDistance(a, entities[t].GetPositionXY());
if (currentDistance < squaredDist)
{
@r = @(entities[t]);
squaredDist = currentDistance;
}
}
}
return r;
}
bool deleteEntity(const int id)
{
ETHEntity@ thisEntity = SeekEntity(id);
if (thisEntity !is null)
{
DeleteEntity(thisEntity);
return true;
}
else
{
return false;
}
}