forked from peterigz/timelinefx.monkey2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameobject.monkey2
1697 lines (1477 loc) · 45.7 KB
/
gameobject.monkey2
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Rem
Copyright (c) 2017 Peter J Rigby
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#End
Namespace timelinefx
Using timelinefx..
#Rem monkeydoc @hidden
#End
Const tlCAPTURE_SELF:Int = 0
#Rem monkeydoc @hidden
#End
Const tlCAPTURE_ALL:Int = 1
#Rem monkeydoc @hidden
#End
Const tlCAPTURE_NONE:Int = 2
#Rem monkeydoc The base class for all particle objects, can also be used for general game entities
This is the main type for storing entity information such as coordinates, colour and other information. This type is designed to be a base
type that you can use to extend and create other classes, for example Player or Bullet.
Entities can be parented to other entities and maintain and update a list of children. These children can be relative to their parents therefore
minimising the work needed to calculate where entities should be drawn to the screen. If a parent rotates, then it's children will rotate with it and
likewise, if the parent moves then its children will move with it as well. Children don't have to be relative however, set realative to false using [[Relative]]
to make the children move independantly, however they will still be updated by their parent
If you have an entity with children, and those children in turn have children, in order to update all of those entities only one call to the parent
[[Update]] method is required to see all of the children updated too. This also applies to rendering the entities on screen - by calling [[Render]] on the parent entity
all children entities will also be rendered. See the code below for an example
Entities draw to the screen using [[tlShape]], a class that allows you to easily draw single or animated images. To set the image use [[Image]]
and [[LoadShape]]. You can adjust the appearence of the entity such as colour and scale using commands such as [[Red]], [[Green]], [[Blue]], [[Alpha]],
[[SetScale]] and [[Angle]].
When entities are rendered they can be tweened so that their animation and movement on the screen is smoothed out using fixed rate timing. You pass the tween value when you call [[Render]] but it's
only optional.
tlGameObject os component based, meaning it uses [[tlComponent]]s to add functionality. [[tlComponent]] is an abstract class you can extend and override the update method to add whatever functionality you
need to the object, be it a control system or movement. You can create as many components as you need for an object and they can be re-used as much as you need. See [[tlComponent]] for more info.
@example
Namespace myapp
#Import "<std>"
#Import "<mojo>"
#Import "<timelinefx>"
#Import "assets/smoke.png"
Using std..
Using mojo..
Using timelinefx..
Class MyWindow Extends Window
Field GameObject:tlGameObject
Field ChildObject:tlGameObject
Method New( title:String="tlGameObject example",width:Int=640,height:Int=480,flags:WindowFlags=Null )
Super.New( title,width,height,flags )
'Create a couple of new game objects
GameObject = New tlGameObject
ChildObject = New tlGameObject
'Assign images using LoadShape
GameObject.Image = LoadShape("asset::smoke.png")
ChildObject.Image = LoadShape("asset::smoke.png")
'Position the child object. This will be relative to the parent object.
ChildObject.SetPosition(0, 200)
'Scale it down a bit
ChildObject.SetScale(0.5)
'Add the child object to the parent object
GameObject.AddChild(ChildObject)
'Set the parent position
GameObject.SetPosition(width/2, height/2)
'Update the gameobject to make sure everything is initialised in the correct positions
GameObject.Update()
End
Method OnRender( canvas:Canvas ) Override
App.RequestRender()
canvas.Clear( New Color(0,0,0,1) )
'Rotate the parent object to show that the child stays relative
GameObject.Rotate(0.05)
'Update the game object
GameObject.Update()
'Render the game object
GameObject.Render(canvas)
End
End
Function Main()
New AppInstance
New MyWindow
App.Run()
End
@end
#End
Class tlGameObject Virtual
Private
Field name:string
'vectors
Field local_vec:tlVector2
Field world_vec:tlVector2
Field tiedvector:tlVector2
Field scale_vec:tlVector2
Field world_scale_vec:tlVector2
Field zoom:Float = 1
'matrices
Field matrix:tlMatrix2 'A matrix to calculate entity rotation relative to the parent
Field scale_matrix:tlMatrix2
'colour
Field red:Float = 1
Field green:Float = 1
Field blue:Float = 1
Field alpha:Float = 1
'image
Field image:tlShape
Field currentframe:Float
'Field currentanimseq:tlAnimationSequence
Field framerate:Float
Field animating:Int
Field handle_vec:tlVector2
Field autocenter:Int = True
Field local_rotation:Float
Field world_rotation:Float
Field frames:Int
Field iterations:Int
Field iterationcount:Int
Field blendmode:BlendMode
Field donotrender:Int
'hierarchy
Field parent:tlGameObject
Field rootparent:tlGameObject
Field children:Stack<tlGameObject>
Field childcount:Int
Field runchildren:Int
Field relative:Int = True
Field attached:Int
Field rotate_vec:tlVector2 'Vector formed between the parent and the children
'tween values
Field oldworld_vec:tlVector2
Field oldworldscale_vec:tlVector2
Field oldlocal_rotation:Float
Field oldworld_rotation:Float
Field oldcurrentframe:Float
Field updatetime:Float = 30
Field oldzoom:Float
Field capturemode:Float
'collision
Field imagebox:tlBox
Field imageboxtype:Int = tlBOX_COLLISION
Field collisionbox:tlBox
Field collisionboxtype:Int = tlBOX_COLLISION
Field containingbox:tlBox
Field rendercollisionbox:Int = False
Field listenlayers:Int[]
Field isstatic:Int
Field maxtlx:Float = $7fffffff
Field maxtly:Float = $7fffffff
Field maxbrx:Float = -$7fffffff
Field maxbry:Float = -$7fffffff
Field updatecontainingbox:Int
Field collisionhandler:void(ReturnedObject:Object, Data:Object)
'Age and status flags
Field dob:Float
Field age:Float
Field dead:Int
Field dying:Int
Field destroyed:Int
field removed:int
'components
Field components:Stack<tlComponent> = New Stack<tlComponent>
Field components_map:StringMap<tlComponent>
Public
#Rem monkeydoc Has [[Destory]] been called on the gameobject?
@return Int True or False
#End
Property IsDestroyed:Int()
Return destroyed
End
#Rem monkeydoc @hidden
#End
Property Dying:Int()
Return dying
Setter (v:Int)
dying = v
End
#Rem monkeydoc @hidden
#End
Property Dead:Int()
Return dead
Setter(v:Int)
dead = v
End
#Rem monkeydoc This flag tells you if the child has been removed from the parent's child list.
Mainly used internally to manage the child list.
@return Int True or False
#End
Property Removed:Int()
Return removed
Setter(v:Int)
removed = v
End
#Rem monkeydoc @hidden
#End
Property DoB:Int()
Return dob
Setter(v:Int)
dob = v
End
#Rem monkeydoc Get or set the destroyed flag
Mainly used internally
#End
Property Destroyed:Int()
Return destroyed
Setter(v:Int)
destroyed = v
End
#Rem monkeydoc Set to true to render the collision box of the object for debugging and testing purposes
#End
Property RenderCollisionBox:Bool()
Return rendercollisionbox
Setter(v:Bool)
rendercollisionbox = v
End
#Rem monkeydoc Get/Set the name of the object
@param String Name of the object
@return String Name of the object
#End
Property Name:String()
Return name
Setter(v:String)
name = v
End
#Rem monkeydoc Get/Set the Zoom level of the object. This helps with features where you can zoom in and out of the game world
@param float the zoom level
@return float the zoom level
#End
Property Zoom:Float()
Return zoom
Setter(v:float)
zoom = v
End
#Rem monkeydoc @hidden
#End
Property OldZoom:Float()
Return oldzoom
Setter(v:float)
oldzoom = v
End
#Rem monkeydoc @hidden
#End
Property Matrix:tlMatrix2()
Return matrix
Setter(v:tlMatrix2)
matrix = v
End
#Rem monkeydoc @hidden
#End
Property ScaleMatrix:tlMatrix2()
Return scale_matrix
End
#Rem monkeydoc Get/Set the local vector of the object
If the object is a child of another object then the local vector will the coordinates relative to the parent
@param [[tlVector2]]
@return [[tlVector2]]
#End
Property LocalVector:tlVector2()
Return local_vec
Setter(v:tlVector2)
local_vec = v
End
#Rem monkeydoc Get/Set the World Vector of the object.
The world vector is the coordinates of the object relative to the origin of the game world
@param [[tlVector2]]
@return [[tlVector2]]
#End
Property WorldVector:tlVector2()
Return world_vec
Setter (v:tlVector2)
world_vec = v
End
#Rem monkeydoc @hidden
#End
Property OldWorldVector:tlVector2()
Return oldworld_vec
Setter (v:tlVector2)
oldworld_vec = v
End
#Rem monkeydoc Get/Set the scale vector of the object reltive to the parent
@param [[tlVector2]]
@return [[tlVector2]]
#End
Property ScaleVector:tlVector2()
Return scale_vec
Setter (v:tlVector2)
scale_vec = v
End
#Rem monkeydoc Get/Set the scale vector of the object reltive to the world
@param [[tlVector2]]
@return [[tlVector2]]
#End
Property WorldScaleVector:tlVector2()
Return world_scale_vec
Setter (v:tlVector2)
world_scale_vec = v
End
#Rem monkeydoc @hidden
#End
Property OldWorldScaleVector:tlVector2()
Return oldworldscale_vec
Setter (v:tlVector2)
oldworldscale_vec = v
End
#Rem monkeydoc Get/Set the handle of the object. Handle is the origin of the object.
This can be used to dictate where the object is drawn from and also the point at which it is rotated around. By default the handle is set to the center of the object.
@param [[tlVector2]]
@return [[tlVector2]]
#End
Property HandleVector:tlVector2()
Return handle_vec
Setter(v:tlVector2)
handle_vec = v
End
#Rem monkeydoc Get/Set the local rotation of the object. See [[Angle]]
@param [[tlVector2]]
@return [[tlVector2]]
#End
Property LocalRotation:Float()
Return local_rotation
Setter(v:Float)
local_rotation = v
End
#Rem monkeydoc Get/Set the local rotation of the object.
@param [[tlVector2]]
@return [[tlVector2]]
#End
Property Angle:Float()
Return local_rotation
Setter(v:Float)
local_rotation = v
End
#Rem monkeydoc Get/Set the world rotation of the object.
@param float
@return float
#End
Property WorldRotation:Float()
Return world_rotation
Setter(v:Float)
world_rotation = v
End
#Rem monkeydoc @hidden
#End
Property OldLocalRotation:Float()
Return oldlocal_rotation
Setter(v:Float)
oldlocal_rotation = v
End
#Rem monkeydoc @hidden
#End
Property OldWorldRotation:Float()
Return oldworld_rotation
Setter(v:Float)
oldworld_rotation = v
End
#Rem monkeydoc @hidden
#End
Property RotateVector:tlVector2()
Return rotate_vec
Setter(v:tlVector2)
rotate_vec = v
End
#Rem monkeydoc Set this to true if you don't want the object to be rendered.
@param int
@return int
#End
Property DoNotRender:Int()
Return donotrender
Setter(v:Int)
donotrender = v
End
#Rem monkeydoc Set to true if you don't want the container box to be updated.
The container box is the box that encloses the whole object including any children and is used for of screen culling amongst other things.
@param int
@return int
#End
Property UpdateContainerBox:Int()
Return updatecontainingbox
Setter(v:Int)
updatecontainingbox = v
End
#Rem monkeydoc Get the components of the object.
@param Stack<tlComponent>
#End
Property Components:Stack<tlComponent>()
Return components
End
#Rem monkeydoc Get/Set the imagebox of the object.
The image box is the [[tlBox]] that represents the image size of the object. It is used for adding to a [[tlQuadtree]] so you can make use of off screen culling (can also use [[ContainingBox]])
@param [[tlBox]]
@return [[tlBox]]
#End
Property ImageBox:tlBox()
Return imagebox
Setter(v:tlBox)
imagebox = v
End
#Rem monkeydoc Get/Set the containingbox of the object.
The image box is the [[tlBox]] that represents the box that envelops the object object including children.
It is used for adding to a [[tlQuadtree]] so you can make use of off screen culling.
@param [[tlBox]]
@return [[tlBox]]
#End
Property ContainingBox:tlBox()
Return containingbox
Setter(v:tlBox)
containingbox = v
End
#Rem monkeydoc Get/Set the collisionbox of the object.
The collisionbox is the [[tlBox]] that represents a more accurate collision of the object for more precise collision checks. for this reason it could be [[tlPolygon]] or tlCircle as well.
It is used for adding to a [[tlQuadtree]] so you can make use of off screen culling.
@param [[tlBox]]
@return [[tlBox]]
#End
Property CollisionBox:tlBox()
Return collisionbox
Setter(v:tlBox)
collisionbox = v
End
#Rem monkeydoc Get/Set the listen layers of the object.
When using a [[tlQuadTree]] to manage all your game objects, you can define which layers of the quad tree the object should listen for collisions.
You then define a callback using [[SetCollisionHandler]] where you can use a function to process the collisions.
@param [[tlBox]]
@return [[tlBox]]
#End
Property ListenLayers:Int[]()
Return listenlayers
Setter(v:Int[])
listenlayers = v
End
#Rem monkeydoc @hidden
#End
Property Age:Int()
Return age
Setter (v:Int)
age = v
End
#Rem monkeydoc Get/Set the whether the objects handle is set to auto center
@param Int
@return Int
#End
Property AutoCenter:Int()
Return autocenter
Setter(v:Int)
autocenter = v
End
#Rem monkeydoc Get/Set the Blendmode of the object for rendering
@param [[mojo.BlendMode]]
@return [[mojo.BlendMode]]
#End
Property BlendMode:BlendMode()
Return blendmode
Setter(v:BlendMode)
blendmode = v
End
#Rem monkeydoc Get/Set the Red value of the object
@param Float Between 0 and 1
@return Float Between 0 and 1
#End
Property Red:Float()
Return red
Setter(v:Float)
red = v
End
#Rem monkeydoc Get/Set the Green value of the object
@param Float Between 0 and 1
@return Float Between 0 and 1
#End
Property Green:Float()
Return green
Setter(v:Float)
green = v
End
#Rem monkeydoc Get/Set the Blue value of the object
@param Float Between 0 and 1
@return Float Between 0 and 1
#End
Property Blue:Float()
Return blue
Setter(v:Float)
blue = v
End
#Rem monkeydoc Get/Set the Alpha value of the object. Alpha is the opacity of the object when rendered
@param Float Between 0 and 1
@return Float Between 0 and 1
#End
Property Alpha:Float()
Return alpha
Setter (v:Float)
alpha = v
End
#Rem monkeydoc Get/Set the [[tlShape]] of the object.
This is the image that is drawn when the object is rendered to screen. Use [[timelinefx.LoadShape]] to set this value.
@param [[tlShape]]
@return [[tlShape]]
#End
Property Image:tlShape()
Return image
Setter(v:tlShape)
image = v
End
#Rem monkeydoc Get/Set the number of animation frames.
@param Int
@return Int
#End
Property Frames:Int()
Return frames
Setter(v:Int)
frames = v
End
#Rem monkeydoc Get/Set the framerate of the image.
This allows you to set how fast the image animates when rendering.
@param Float
@return Float
#End
Property FrameRate:Float()
Return framerate
Setter(v:Float)
framerate = v
End
#Rem monkeydoc Get/Set the the updatetime of the object.
This lets you sync the object with how fast your app is updating so that the framerate can be accurate.
@param Int
@return Int
#End
Property UpdateTime:Float()
Return updatetime
Setter(v:Float)
updatetime = v
End
#Rem monkeydoc Get/Set whether the object is animating or not.
@param Int
@return Int
#End
Property Animating:Int()
Return animating
Setter(v:Int)
animating = v
End
#Rem monkeydoc Get/Set current animation frame of the object
@param float
@return float
#End
Property CurrentFrame:Float()
Return currentframe
Setter(v:Float)
currentframe = v
End
#Rem monkeydoc @hidden
#End
Property OldCurrentFrame:Float()
Return oldcurrentframe
Setter(v:Float)
oldcurrentframe = v
End
#Rem monkeydoc Get/Set whether the object is relative to it's parent.
If an object is relative then it will move and rotate with it's parent. Otherwise it will be independent. However, note that if it's not relative it will still
be updated when the parent object is updated.
@param Int
@return Int
#End
Property Relative:Int()
Return relative
Setter(v:Int)
relative = v
End
#Rem monkeydoc Get/Set the parent of the object.
This will be set automatically when you call [[AddChild]]/[[RemoveChild]]
@param Int
@return Int
#End
Property Parent:tlGameObject()
Return parent
Setter(v:tlGameObject)
parent = v
End
#Rem monkeydoc Get/Set the root parent at the top of the hierarchy
This will be set automatically when you call [[AddChild]]/[[RemoveChild]]
@param Int
@return Int
#End
Property RootParent:tlGameObject()
Return rootparent
Setter(v:tlGameObject)
rootparent = v
End
#Rem monkeydoc Get the number of children in the object.
This will be set automatically when you call [[AddChild]]/[[RemoveChild]]
@param Int
@return Int
#End
Property ChildCount:Int()
Return childcount
Setter(v:Int)
childcount = v
End
#Rem monkeydoc New construct for creating a [[tlGameObject]] and setting defaults
@return [[tlGameObject]]
#End
Method New()
local_vec = New tlVector2(0, 0)
world_vec = New tlVector2(0, 0)
oldworld_vec = New tlVector2(0, 0)
oldworldscale_vec = New tlVector2(0, 0)
scale_vec = New tlVector2(1, 1)
world_scale_vec = New tlVector2(1, 1)
handle_vec = New tlVector2(0, 0)
children = New Stack<tlGameObject>
tiedvector = New tlVector2(0, 0, true)
'matrices
matrix = New tlMatrix2
scale_matrix = New tlMatrix2
BlendMode = BlendMode.Alpha
'compoents
'components = New List<tlComponent>
Capture()
End
'setters
#Rem monkeydoc Set the position of the #tlGameObject for static objects
use this instead of SetPostion if the object is static, otherwise the objects collision box will not be updated as well.
#End
Method SetStaticPosition(x:Float, y:Float)
LocalVector = New tlVector2(x, y)
SetStatic()
Update()
CaptureAll()
UpdateStaticCollisionBox()
End Method
#Rem
monkeydoc Set the current state of the object, whether it's static or not.
If an object is static then its collision box's position is not updated every update. This means that it won't bother syncing itself with
the gameobject or checking every update whether or not it should move within the quadtree. If you set an object as static then you must
ensure you call [[UpdateStaticCollisionBox]] after using [[SetPosition]] to ensure it's put in the right place to initialise it.
@return True or False
#End
Method SetStatic(_isstatic:Int = True)
isstatic = _isstatic
End Method
#Rem monkeydoc Is the object a static object? See [[SetStatic]]
@param Int
@return Int
#End
Property IsStatic:Int()
Return isstatic
Setter (s:Int)
isstatic = s
End
#Rem monkeydoc Set the handle of the object. See [[HandleVector]]
@param x Int
@param y Int
#End
Method SetHandle(x:Int, y:Int)
handle_vec = New tlVector2(x, y)
End
#Rem monkeydoc Set the current frame on the game object image
@param frame
#End
Method SetCurrentFrame(frame:Int)
currentframe = frame
oldcurrentframe = frame
End Method
#Rem monkeydoc Set the layer that the object's image box resides on.
All of your game objects' image boxes should ideally be kept on the same layer, or atleast on a different layer to the one that you use to test for collisions.
#End
Method SetImageLayer(layer:Int)
imagebox.CollisionLayer = layer
End Method
#Rem monkeydoc Set the image of the object.
This will also define a [[tlBox]] that you can use for adding to a quad tree and for doing of screen culling and such.
@param image [[tlShape]]
@param collisiontype Int or either [[tlBOX_COLLISION]], [[tlPOLY_COLLISION]] or [[tlCIRCLE_COLLISION]]
@param layer Int of the collision layer the Image box should go on (for use with [[tlQuadTree]])
#End
Method SetImage(image:tlShape, collisiontype:Int = tlBOX_COLLISION, layer:Int = 0)
Self.image = image
frames = Self.image.Frames
Select collisiontype
case -1
'no collision
Case tlPOLY_COLLISION
SetImagePoly(GetWorldX(), GetWorldX(),New Float[](Float(-Self.image.Width) / 2, Float(-Self.image.Height) / 2,
Float(-Self.image.Width) / 2, Float(Self.image.Height) / 2,
Float(Self.image.Width) / 2, Float(Self.image.Height) / 2,
Float(Self.image.Width) / 2, Float(-Self.image.Height) / 2), layer)
Case tlCIRCLE_COLLISION
SetImageCircle(GetWorldX(), GetWorldX(), Max(Self.image.Width / 2, Self.image.Height / 2), layer)
Default
SetImageBox(GetWorldX(), GetWorldX(), Self.image.Width, Self.image.Height, layer)
End Select
End
#Rem monkeydoc Set the collision box of the object to the imagebox
You can use the collision box for checking collisions and quadtree queries.
@param layer Int of the collision layer the collision box should go on (for use with [[tlQuadTree]])
#End
Method SetCollisionBoxtoImage(layer:Int)
Local qtree:tlQuadTree
If collisionbox
If collisionbox.quadtree
qtree = collisionbox.quadtree
collisionbox.RemoveFromQuadTree()
End If
End If
Select imageboxtype
Case tlBOX_COLLISION
SetCollisionBox(0, 0, imagebox.Width, imagebox.Height, layer)
Case tlCIRCLE_COLLISION
SetCollisionCircle(imagebox.WorldX(), imagebox.WorldY(), Cast<tlCircle>(imagebox).Radius, layer)
Case tlPOLY_COLLISION
Local verts:=New Float[imagebox.Vertices.Length * 2]
For Local v:Int = 0 To verts.Length - 1 Step 2
verts[v] = imagebox.Vertices[v / 2].x
verts[v + 1] = imagebox.Vertices[v / 2].y
Next
SetCollisionPoly(imagebox.WorldX(), imagebox.WorldY(), verts, layer)
End Select
If qtree qtree.AddBox(collisionbox)
End Method
#Rem monkeydoc Set the dimensions of the objects collision box
This will create a new box with the given dimensions and set the collision type of the object to tlBOX_COLLISION,
removing the current one from the world quadtree if it's in there. Set the layer to put the object on a specific collision layer.
@param x Float
@param y Float
@param w Float
@param h Float
@param layer Int
#End
Method SetCollisionBox(x:Float, y:Float, w:Float, h:Float, layer:Int = 0)
Local qtree:tlQuadTree
If collisionbox
If collisionbox.quadtree
qtree = collisionbox.quadtree
collisionbox.RemoveFromQuadTree()
End If
End If
collisionbox = CreateBox(x, y, w, h, layer)
collisionbox.handle = New tlVector2(x, y)
collisionbox.Data = Self
collisionboxtype = tlBOX_COLLISION
If qtree qtree.AddBox(collisionbox)
End Method
#Rem monkeydoc Set the dimensions of the objects collision circle
This will create a new circle collision with the given radius and set the collision type of the object to tlCIRCLE_COLLISION,
removing the current one from the world quadtree if it's in there. Set the layer to put the object on a specific collision layer.
@param x
@param y
@param r
@param layer
#End
Method SetCollisionCircle(x:Float, y:Float, r:Float, layer:Int = 0)
Local qtree:tlQuadTree
If collisionbox
If collisionbox.quadtree
qtree = collisionbox.quadtree
collisionbox.RemoveFromQuadTree()
End If
End If
collisionbox = CreateCircle(x, y, r, layer)
collisionbox.handle = New tlVector2(x, y)
collisionbox.Data = Self
collisionboxtype = tlCIRCLE_COLLISION
If qtree qtree.AddBox(collisionbox)
End Method
#Rem monkeydoc Set the dimensions of the objects collisionbox
This will create a new poly collision with the given verts[] and set the collision type of the object to tlPOLY_COLLISION,
removing the current one from the world quadtree if it's in there. Set the layer to put the object on a specific collision layer.
@param x
@param y
@param verts
@param layer
#End
Method SetCollisionPoly(x:Float, y:Float, verts:Float[], layer:Int = 0)
Local qtree:tlQuadTree
If collisionbox
If collisionbox.quadtree
qtree = collisionbox.quadtree
collisionbox.RemoveFromQuadTree()
End If
End If
collisionbox = CreatePolygon(x, y, verts, layer)
collisionbox.Data = Self
collisionboxtype = tlPOLY_COLLISION
If qtree qtree.AddBox(collisionbox)
End Method
#Rem monkeydoc @hidden
#End
Method SetImageBox(x:Float, y:Float, w:Float, h:Float, layer:Int = 0)
imagebox = CreateBox(x, y, w, h, layer)
imagebox.Data = Self
imageboxtype = tlBOX_COLLISION
End
#Rem monkeydoc @hidden
#End
Method SetImageCircle(x:Float, y:Float, r:Float, layer:Int = 0)
imagebox = CreateCircle(x, y, r, layer)
imagebox.Data = Self
imageboxtype = tlCIRCLE_COLLISION
End
#Rem monkeydoc @hidden
#End
Method SetImagePoly(x:Float, y:Float, verts:Float[], layer:Int = 0)
imagebox = CreatePolygon(x, y, verts, layer)
imagebox.Data = Self
imageboxtype = tlPOLY_COLLISION
End
#Rem monkeydoc Set the position of the object
@param x Float
@param y Float
#End
Method SetPosition(x:Float, y:Float)
local_vec = New tlVector2(x, y)
End
#Rem monkeydoc Set the position of the object using a [[tlVector2]]
@param position [[tlVector2]]
#End
Method SetPositionVector(position:tlVector2)
local_vec = New tlVector2(position.x, position.y)
End
#Rem monkeydoc move the object relative to it's current position
@param x Float
@param y Float
#End
Method Move(x:Float, y:Float)
local_vec = local_vec.Move(x, y)
End
#Rem monkeydoc move the object relative to it's current position using a [[tlVector2]]
@param v [[tlVector]]
#End
Method MoveVector(v:tlVector2)
local_vec = local_vec.Move(v.x, v.y)
End
#Rem monkeydoc Tie the object to a vector so that whatever the position of the vector, will be the position of the object.
@param vector [[tlVector]]
#End
Method TieToVector(vector:tlVector2)
tiedvector = vector
End
#Rem monkeydoc Untie the object from the vector that it's tied to.
#End
Method UnTie()
tiedvector.Invalid = True
End
#Rem monkeydoc Set the scale of the object
@param x Float
@param x Float
#End
Method SetScale( x:Float, y:Float )
scale_vec = New tlVector2(x, y)
End
#Rem monkeydoc Set the scale of the object
@param vector [[tlVector]]
#End
Method SetScale( v:Float )
scale_vec = New tlVector2(v, v)
End
#Rem monkeydoc Rotate the object relative to it's current rotation
@param v Float (radians)
#End
Method Rotate(v:float)
local_rotation += v