-
Notifications
You must be signed in to change notification settings - Fork 7
/
base.framework.entity.bmx
executable file
·456 lines (323 loc) · 11.3 KB
/
base.framework.entity.bmx
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
Rem
====================================================================
Basic entity class
====================================================================
Entities are objects in your game / app.
The collection contains various entity-types depending on whether
the object needs to move or is a static one.
====================================================================
If not otherwise stated, the following code is available under the
following licence:
LICENCE: zlib/libpng
Copyright (C) 2002-2018 Ronny Otto, digidea.de
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
====================================================================
EndRem
SuperStrict
Import "base.framework.entity.base.bmx"
Import "base.util.rectangle.bmx"
Import "base.util.deltatimer.bmx"
Type TRenderableEntity extends TEntityBase
Field area:TRectangle = new TRectangle
Field _screenRect:TRectangle = new TRectangle {nosave}
Field _screenArea:TRectangle = new TRectangle {nosave}
Field name:string
Field visible:int = True
Field parent:TRenderableEntity = null
Field childEntities:TRenderableEntity[]
Field childOffsets:TVec2D[]
Field _entityOptions:int = 0
Const OPTION_IGNORE_PARENT_SCREENLIMIT:int = 1
Const OPTION_IGNORE_PARENT_SCREENCOORDS:int = 2
Method New()
name = "TRenderableEntity"
End Method
Method GenerateGUID:string()
return "renderableentity-"+id
End Method
Method hasEntityOption:Int(option:Int)
Return (_entityOptions & option) <> 0
End Method
Method setEntityOption(option:Int, enable:Int=True)
If enable
_entityOptions :| option
Else
_entityOptions :& ~option
EndIf
End Method
Method HasChild:int(child:TRenderableEntity)
for local c:TRenderableEntity = EachIn childEntities
if child = c then return True
next
return False
End Method
Method AddChild(child:TRenderableEntity, childOffset:TVec2D = null, index:int = -1)
if not child then return
if not childEntities then childEntities = new TRenderableEntity[0]
if not childOffsets then childOffsets = new TVec2D[0]
if not childOffset then childOffset = new TVec2D.Init()
if index < 0 then index = childEntities.length
if index >= childEntities.length
childEntities :+ [child]
childOffsets :+ [childOffset]
else
childEntities = childEntities[.. index] + [child] + childEntities[index ..]
childOffsets = childOffsets[.. index] + [childOffset] + childOffsets[index ..]
endif
'set self as parent
childEntities[index].SetParent(self)
End Method
Method RemoveChild(child:TRenderableEntity)
if not child then return
if not childEntities or childEntities.length = 0 then return
local index:int = 0
while index < childEntities.length
if childEntities[index] = child
'skip increasing index, the next child might be also the
'searched one
RemoveChildAtIndex(index)
else
index :+ 1
endif
Wend
End Method
Method RemoveChildAtIndex:Int(index:int = -1)
if not childEntities or childEntities.length = 0 then return False
if index < 0 then index = childEntities.length - 1
'remove parent association (strong reference)
'this makes it garbage-collectable
childEntities[index].SetParent(null)
if index <= 0
childEntities = childEntities[1 ..]
childOffsets = childOffsets[1 ..]
elseif index >= childEntities.length - 1
childEntities = childEntities[.. childEntities.length-1]
childOffsets = childOffsets[.. childOffsets.length-1]
else
childEntities = childEntities[.. index] + childEntities[index+1 ..]
childOffsets = childOffsets[.. index] + childOffsets[index+1 ..]
endif
return True
End Method
Method Render:Int(xOffset:Float = 0, yOffset:Float = 0, alignment:TVec2D = Null)
RenderChildren(xOffset, yOffset, alignment)
End Method
Method RenderAt:Int(x:Float = 0, y:Float = 0, alignment:TVec2D = Null)
local oldPos:SVec2F = New SVec2F(area.position.x, area.position.y)
area.position.SetXY(x,y)
Render(0, 0, alignment)
area.position.SetXY(oldPos.x, oldPos.y)
End Method
Method Update:Int()
UpdateChildren()
End Method
Method RenderChildren:Int(xOffset:Float = 0, yOffset:Float = 0, alignment:TVec2D = Null)
For local i:int = 0 until childEntities.length
if not childEntities[i] then continue
childEntities[i].Render(xOffset + childOffsets[i].GetX(), yOffset + childOffsets[i].GetY(), alignment)
Next
End Method
Method UpdateChildren:Int()
For local child:TRenderableEntity = EachIn childEntities
child.Update()
Next
End Method
'returns whether two (Render)Entities overlap eachother visually
Method OverlapsVisually:int(other:TRenderableEntity)
if not other then return False
return GetScreenBoundingBox().intersects(other.GetScreenBoundingBox())
End Method
Method GetX:Float()
return area.GetX()
End Method
Method GetY:Float()
return area.GetY()
End Method
Method GetWidth:Float()
return area.GetW()
End Method
Method GetHeight:Float()
return area.GetH()
End Method
Method GetBoundingBox:TRectangle()
return area
End Method
Method GetScreenBoundingBox:TRectangle()
return GetScreenArea()
End Method
Method GetScreenArea:TRectangle()
return _screenArea.Init(GetScreenX(), GetScreenY(), GetScreenWidth(), GetScreenHeight() )
End Method
Method GetScreenX:Float()
if parent and not (_entityOptions & OPTION_IGNORE_PARENT_SCREENCOORDS)
return parent.GetScreenX() + parent.GetChildX(self) + area.GetX()
else
return area.GetX()
endif
End Method
Method GetScreenY:Float()
if parent and not (_entityOptions & OPTION_IGNORE_PARENT_SCREENCOORDS)
return parent.GetScreenY() + parent.GetChildY(self) + area.GetY()
else
return area.GetY()
endif
End Method
Method GetScreenWidth:Float()
if parent and not (_entityOptions & OPTION_IGNORE_PARENT_SCREENLIMIT)
'parent has auto-size wont limit the entity, so use full size
if parent.area.GetW() < 0 then return Max(0, area.GetW())
'variant a:
'entity has auto-size -> cannot calculate occupied space
'just occupy all potential space
'if area.GetW() < 0 then return Max(0, parent.GetScreenWidth() - parent.GetChildX(self))
'variant b:
'just return 0 if we cannot calculate the size
if area.GetW() < 0 then return 0
'calculate available space of parent, or if enough left,
'occupy all space entity wants
local x:Int = GetScreenX() 'parent.GetScreenX() + parent.GetChildX(self)
local x2:Int = Min(GetScreenX() + area.GetW(), parent.GetScreenX() + parent.GetScreenWidth())
return Max(0, (x2 - x))
endif
return Max(0, area.GetW())
End Method
Method GetScreenHeight:Float()
if parent and not (_entityOptions & OPTION_IGNORE_PARENT_SCREENLIMIT)
'parent has auto-size wont limit the entity, so use full size
if parent.area.GetH() < 0 then return Max(0, area.GetH())
'variant a:
'entity has auto-size -> cannot calculate occupied space
'just occupy all potential space
'if area.GetH() < 0 then return Max(0, parent.GetScreenHeight() - parent.GetChildY(self))
'variant b:
'just return 0 if we cannot calculate the size
if area.GetH() < 0 then return 0
'calculate available space of parent, or if enough left,
'occupy all space entity wants
local y:Int = GetScreenY() 'parent.GetScreenY() + parent.GetChildY(self)
local y2:Int = Min(GetScreenY() + area.GetH(), parent.GetScreenY() + parent.GetScreenHeight())
return Max(0, (y2 - y))
endif
return Max(0, area.GetH())
End Method
'get a rectangle describing the objects area on the screen
Method GetScreenRect:TRectangle()
return _screenRect.Init(GetScreenX(), GetScreenY(), GetScreenWidth(), GetScreenHeight() )
End Method
'get a vector describing the objects position on the screen
Method GetScreenPos:TVec2D()
Return new TVec2D.Init(GetScreenX(), GetScreenY())
End Method
'return at which x position a child (or all) start
' returns an offset, not a screen coordinate!
Method GetChildX:Float(child:TRenderableEntity = Null)
return 0
End Method
'return at which y position a child (or all) start
' returns an offset, not a screen coordinate!
Method GetChildY:Float(child:TRenderableEntity = Null)
return 0
End Method
Method SetParent:Int(parent:TRenderableEntity)
self.parent = parent
End Method
Method SetVisible:int(bool:int=True)
visible = bool
End Method
Method SetSize(width:Float, height:Float)
area.dimension.SetXY(width, height)
End Method
Method SetPosition(x:Float, y:Float)
area.position.SetXY(x, y)
End Method
Method IsVisible:int()
return visible
End Method
'returns if the size of the entity was given
Method HasSize:int()
return area.GetW() > 0 and area.GetH() > 0
End Method
Method ToString:String()
return name
End Method
End Type
'a moveable and drawable entity
Type TEntity extends TRenderableEntity
'for tweening
Field oldPosition:TVec2D = new TVec2D
'moving direction
Field velocity:TVec2D = new TVec2D
'entity specific speedFactor. If < 0 then worldSpeedFactor gets used
Field worldSpeedFactor:Float = -1.0
'a world speed factor of 1.0 means realtime, 2.0 = fast forward
Global globalWorldSpeedFactor:Float = 1.0
Global globalWorldSpeedFactorMod:Float = 1.0
Method New()
name = "TEntity"
End Method
Method GenerateGUID:string()
return "entity-"+id
End Method
Function GetGlobalWorldSpeedFactor:float()
return globalWorldSpeedFactor * globalWorldSpeedFactorMod
End Function
'until BMX-NG allows for returned function pointers
'Method GetCustomSpeedFactorFunc:Float()()
' return Null
'End Method
Method HasCustomSpeedFactorFunc:int()
return False
End Method
Method RunCustomSpeedFactorFunc:float()
return 0
End Method
Method GetWorldSpeedFactor:float()
if worldSpeedFactor < 0
'call the individual function if needed
if HasCustomSpeedFactorFunc()
return RunCustomSpeedFactorFunc() * globalWorldSpeedFactorMod
else
return GetGlobalWorldSpeedFactor()
endif
endif
return worldSpeedFactor * globalWorldSpeedFactorMod
End Method
Method SetVelocity(dx:float, dy:float)
velocity.SetXY(dx, dy)
End Method
Method GetVelocity:TVec2D()
return velocity
End Method
Method GetSpeed:Float()
if not velocity then return 0
return sqr(velocity.x^2 + velocity.y^2)
End Method
Method GetDeltaTime:Float()
return GetDeltaTimer().GetDelta() * GetWorldSpeedFactor()
End Method
Method Move:int()
'=== UPDATE MOVEMENT ===
'backup for tweening
oldPosition.SetXY(area.position.x, area.position.y)
'set new position
local deltaTime:Float = GetDeltaTime()
area.position.AddXY( deltaTime * GetVelocity().x, deltaTime * GetVelocity().y )
End Method
Method Update:Int()
Super.Update()
Move()
End Method
End Type