-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcftWaypoints.cxs
1082 lines (969 loc) · 32.6 KB
/
cftWaypoints.cxs
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
Strict
#rem
Title: fantomCX
Description: A 2D game framework for the Cerberus X programming language
Author: Michael Hartlef
Contact: [email protected]
Website: http://www.fantomgl.com
License: MIT
#End
Import fantomCX
'nav:<blockquote><nav><b>fantomCX documentation</b> | <a href="index.html">Home</a> | <a href="classes.html">Classes</a> | <a href="3rdpartytools.html">3rd party tools</a> | <a href="examples.html">Examples</a> | <a href="changes.html">Changes</a></nav></blockquote>
#Rem
'header:The waypoint module lets you create paths of waypoints by hand. Then you can create a marker that you can let run along the path. Also you can connect an object to that marker. Another functionality is A* path finding.
#End
'***************************************
'summery:The ftWaypoint class defines a single waypoint in a path.
Class ftWaypoint
'#DOCOFF#
Field xPos:Float = 0
Field yPos:Float = 0
Field xDist:Float = 0
Field yDist:Float = 0
Field pathNode:list.Node<ftWaypoint> = Null
Field path:ftPath = Null
'#DOCON#
'------------------------------------------
'summery:Returns the path index of a waypoint. Index start with 1
Method GetIndex:Int()
Local ci:Int = 0
For Local waypoint := Eachin path.wpList
ci = ci + 1
If waypoint = Self Then Return ci
Next
Return 0
End Method
'------------------------------------------
'summery:Returns the X/Y position of a waypoint.
Method GetPos:Float[]()
Local p:Float[2]
p[0] = xPos
p[1] = yPos
Return p
End
'------------------------------------------
'summery:Removes a waypoint from its path.
Method Remove:Void()
Self.pathNode.Remove()
Self.path = Null
End
'------------------------------------------
#Rem
'summery:Renders a waypoint at its current position.
'A renderType of 0 renders a box, every other value a circle.
#End
Method Render:Void(renderType:Int=0)
If renderType = 0 Then
Self.path.engine.currentCanvas.PushMatrix
Self.path.engine.currentCanvas.Translate (Self.xPos, Self.yPos)
Self.path.engine.currentCanvas.DrawRect -5, -5, 10, 10
Self.path.engine.currentCanvas.PopMatrix
Else
Self.path.engine.currentCanvas.DrawCircle Self.xPos, Self.yPos, 5
Endif
End
'------------------------------------------
'summery:Sets the position of a waypoint inside a path.
Method SetPos:Void(xpos:Float, ypos:Float, relative:Bool = False)
If relative = True Then
self.xDist = xpos
self.yDist = ypos
self.xPos = self.xDist + Self.path.xPos
self.yPos = self.yDist + Self.path.yPos
Else
self.xDist = xpos - Self.path.xPos
self.yDist = ypos - Self.path.yPos
Endif
'Scale and rotate all waypoints of the path
Self.path.SetScale(0.0, 0.0, True)
Self.path.SetAngle(0.0, True)
End
End
'***************************************
'summery:The ftPath class defines a path created by hand.
Class ftPath
'#DOCOFF#
Field wpList:= New List<ftWaypoint>
Field markerList:= New List<ftMarker>
Field engine:ftEngine = Null
Field xPos:Float = 0
Field yPos:Float = 0
Field angle:Float = 0.0
Field scaleX:Float = 1.0
Field scaleY:Float = 1.0
Field inUpdate:Bool = False
'#DOCON#
'----- Marker direction -----
Const mdForward:Int = 1
Const mdBackwards:Int = 2
'----- Marker move mode -----
Const mmStop:Int = 0
Const mmBounce:Int = 1 'Goes back and forth
Const mmWarp:Int = 2 'Once it hits the end waypoint, it starts right away at the start waypoint.
Const mmCircle:Int = 3 'Once it hits the end waypoint, it moves towards the start waypoint.
'----- Interpolation mode for moving along the path -----
Const imLinear:Int=0
Const imCatmull:Int=1
'------------------------------------------
'summery:This method adds a new waypoint to an existing path and returns it.
Method AddWP:ftWaypoint(xpos:Float, ypos:Float, relative:Bool = False)
Local waypoint := New ftWaypoint
If relative = True Then
waypoint.xDist = xpos
waypoint.yDist = ypos
waypoint.xPos = waypoint.xDist + Self.xPos
waypoint.yPos = waypoint.yDist + Self.yPos
Else
waypoint.xPos = xpos
waypoint.yPos = ypos
waypoint.xDist = xpos - Self.xPos
waypoint.yDist = ypos - Self.yPos
Endif
waypoint.pathNode = Self.wpList.AddLast(waypoint)
waypoint.path = Self
'Scale and rotate all waypoints of the path
Self.SetScale(0.0, 0.0, True)
Self.SetAngle(0.0, True)
Return waypoint
End
'-----------------------------------------------------------------------------
Method CleanupLists:Void()
For Local marker := Eachin Self.markerList
If marker.isDeleted = True Then
marker.pathNode.Remove()
marker.path = Null
For Local tmpObj := Eachin marker.objList
tmpObj.markerNode.Remove()
tmpObj.marker = Null
Next
Endif
Next
End
'------------------------------------------
'summery:This method creates a new marker for an existing path.
Method CreateMarker:ftMarker(speed:Float= 1.0, dir:Int = mdForward, mode:Int = mmStop)
Local nc:Int = Self.wpList.Count()
#If CONFIG="debug"
If nc < 1 Then Error ("~n~nError in file fantomCX.cftWaypoints, Method ftPath.CreateMarker():~n~nPath has no waypoints!")
#End
Local marker := New ftMarker
marker._Init(Self, speed, dir, mode)
marker.pathNode = Self.markerList.AddLast(marker)
marker.path = Self
Return marker
End
'------------------------------------------
'summery:To find the closest waypoint of a path to a given position, use this method.
Method FindClosestWP:ftWaypoint(xpos:Float, ypos:Float)
Local cdist:Float
Local dist:Float
Local dX:Float
Local dY:Float
Local tmpWP:ftWaypoint = Null
cdist = 999999.9
For Local waypoint := Eachin wpList
dX = xpos - waypoint.xPos
dY = ypos - waypoint.yPos
dist = Sqrt(dX*dX + dY*dY)
If dist < cdist Then
cdist = dist
tmpWP = waypoint
Endif
Next
Return tmpWP
End
'------------------------------------------
'summery:Get a the waypoint with the given index. Index starts with 1.
Method GetWP:ftWaypoint(index:Int)
Local ci:Int = 0
Local cc:Int = Self.wpList.Count()
#If CONFIG="debug"
If index < 1 Or index > cc Then Error ("~n~nError in file fantomCX.cftWaypoints, Method ftPath.GetWP():~n~nUsed index ("+index+") is out of bounds (1-"+cc+")")
#End
For Local waypoint := Eachin Self.wpList
ci = ci + 1
If ci = index Then
Return waypoint
Endif
Next
Return Null
End
'------------------------------------------
'summery:Returns the waypoint count of the path.
Method GetWPCount:Int()
Return Self.wpList.Count()
End
'------------------------------------------
'summery:Returns the angle of the path.
Method GetAngle:Float()
Return Self.angle
End
'------------------------------------------
'summery:Returns the position of the path inside an array.
Method GetPos:Float[]()
Local p:Float[2]
p[0] = Self.xPos
p[1] = Self.yPos
Return p
End
'------------------------------------------
'summery:Returns the scale factors of the path inside an array.
Method GetScale:Float[]()
Local s:Float[2]
s[0] = Self.scaleX
s[1] = Self.scaleY
Return s
End
'------------------------------------------
'summery:Load a path from a string.
Method LoadFromString:Void(ps:String)
Local entryCount:Int = 0
Local nx:Float = 0.0
Local ny:Float = 0.0
Local _angle:Float = 0.0
Local _xp:Float = 0.0
Local _yp:Float = 0.0
Local _sx:Float = 0.0
Local _sy:Float = 0.0
Local cw:Float=0
Local ch:Float=0
Self.RemoveAllMarkers()
Self.RemoveAllWP()
Local lines := ps.Split(String.FromChar(10))
For Local line:= Eachin lines
If line = "" Then Continue
If line.StartsWith("waypointCount=") Then
Local ct$[] = line.Split("=")
entryCount = Int(ct[1])
Elseif line.StartsWith("canvasWidth=") Then
Local ct$[] = line.Split("=")
cw = Float(ct[1])
Elseif line.StartsWith("canvasHeight=") Then
Local ct$[] = line.Split("=")
ch = Float(ct[1])
Elseif line.StartsWith("angle=") Then
Local ct$[] = line.Split("=")
_angle = Float(ct[1])
Elseif line.StartsWith("xPos=") Then
Local ct$[] = line.Split("=")
_xp = Float(ct[1])
Elseif line.StartsWith("yPos=") Then
Local ct$[] = line.Split("=")
_yp = Float(ct[1])
Elseif line.StartsWith("scaleX=") Then
Local ct$[] = line.Split("=")
_sx = Float(ct[1])
Elseif line.StartsWith("scaleY=") Then
Local ct$[] = line.Split("=")
_sy = Float(ct[1])
Elseif line.StartsWith("waypoint=") Then
Local ct$[] = line.Split("=")
Local dt$[] = ct[1].Split(";")
nx = (Float(dt[0])/cw)*engine.GetCanvasWidth()
ny = (Float(dt[1])/ch)*engine.GetCanvasHeight()
Self.AddWP(nx, ny)
Endif
Next
Self.SetPos( (_xp/cw) * engine.GetCanvasWidth(), (_yp/ch) * engine.GetCanvasHeight () )
Self.SetScale(_sx, _sy)
Self.SetAngle(_angle)
'Return 0
End
'------------------------------------------
'summery:Remove a path and its content.
Method Remove:Void()
RemoveAllMarkers()
RemoveAllWP()
End
'------------------------------------------
'summery:Removes all markers of the path.
Method RemoveAllMarkers:Void()
For Local marker := Eachin Self.markerList.Backwards()
If marker.isDeleted = false Then marker.Remove()
Next
End
'------------------------------------------
'summery:Removes all waypoints of the path.
Method RemoveAllWP:Void()
For Local waypoint := Eachin Self.wpList.Backwards()
waypoint.Remove()
Next
End
'------------------------------------------
'summery:Returns a waypoint with the given index. Index starts with 1.
Method RemoveWP:Void(index:Int)
Local ci:Int = -1
Local cc:Int = Self.wpList.Count()
#If CONFIG="debug"
If index < 1 Or index > cc Then Error ("~n~nError in file fantomCX.cftWaypoints, Method ftPath.RemoveWP(index:Int):~n~nUsed index ("+index+") is out of bounds (1-"+cc+")")
#End
Local tmpWP:ftWaypoint = Null
For Local waypoint := Eachin Self.wpList.Backwards()
ci = ci + 1
If ci = index Then
waypoint.Remove()
Exit
Endif
Next
End
'------------------------------------------
#Rem
'summery:Render all markers of a path. You can use this for debugging.
A type value of 0 renders a square, any other value renders a circle.
#End
Method RenderAllMarker:Void(type:Int=0)
Self.engine.currentCanvas.PushMatrix
Self.engine.currentCanvas.Translate(engine.autofitX, engine.autofitY)
Self.engine.currentCanvas.Scale(engine.scaleX, engine.scaleY)
Self.engine.currentCanvas.SetScissor( engine.autofitX, engine.autofitY, engine.canvasWidth*engine.scaleX, engine.canvasHeight*engine.scaleY )
For Local marker := Eachin Self.markerList
marker.Render(type)
Next
Self.engine.currentCanvas.PopMatrix
End
'------------------------------------------
#Rem
'summery:Render all waypoints of a path. You can use this for debugging.
A type value of 0 renders a square, any other value renders a circle.
#End
Method RenderAllWP:Void(type:Int=0)
Self.engine.currentCanvas.PushMatrix
Self.engine.currentCanvas.Translate(engine.autofitX, engine.autofitY)
Self.engine.currentCanvas.Scale(engine.scaleX, engine.scaleY)
Self.engine.currentCanvas.SetScissor( engine.autofitX, engine.autofitY, engine.canvasWidth*engine.scaleX, engine.canvasHeight*engine.scaleY )
For Local waypoint := Eachin Self.wpList
waypoint.Render(type)
Next
Self.engine.currentCanvas.PopMatrix
End
'------------------------------------------
'summery:Save a path to a string.
Method SaveToString:String()
Local ps:String
ps = "waypointCount="+Self.wpList.Count() + String.FromChar(10)
ps = ps + "canvasWidth=" + engine.GetCanvasWidth() + String.FromChar(10)
ps = ps + "canvasHeight=" + engine.GetCanvasHeight() + String.FromChar(10)
ps = ps + "angle=" + Self.angle + String.FromChar(10)
ps = ps + "xPos=" + Self.xPos + String.FromChar(10)
ps = ps + "yPos=" + Self.yPos + String.FromChar(10)
ps = ps + "scaleX=" + Self.scaleX + String.FromChar(10)
ps = ps + "scaleY=" + Self.scaleY + String.FromChar(10)
For Local waypoint := Eachin Self.wpList
ps = ps + "waypoint="+waypoint.xDist + ";" + waypoint.yDist + String.FromChar(10)
Next
Return ps
End
'------------------------------------------
'summery:Sets the angle of a path and position its waypoints regarding the angle.
Method SetAngle:Void(a:Float, relative:Bool = False)
Local waypointDist:Float = 0.0
Local distX:Float = 0.0
Local distY:Float = 0.0
Local waypointAngle:Float = 0.0
If relative = True Then
Self.angle += a
Else
Self.angle = a
Endif
For Local waypoint := Eachin Self.wpList
distX = waypoint.xDist * Self.scaleX
distY = waypoint.yDist * Self.scaleY
' WP distance to path center
waypointDist = Sqrt(distX * distX + distY * distY)
' WP angle to path center
waypointAngle = ATan2(distY, distX) + 90.0
waypointAngle += Self.angle
If waypointAngle < 0 Then
waypointAngle += 360.0
Elseif waypointAngle > 360.0 Then
waypointAngle -= 360.0
Endif
' Calculate new position of the waypoint
waypoint.xPos = (Sin(waypointAngle) * waypointDist) + Self.xPos
waypoint.yPos = (-Cos(waypointAngle) * waypointDist) + Self.yPos
Next
End
'------------------------------------------
'summery:Sets the position of a path and its waypoints.
Method SetPos:Void(x:Float, y:Float, relative:Bool = False)
If relative = True Then
Self.xPos += x
Self.yPos += y
Else
Self.xPos = x
Self.yPos = y
Endif
For Local waypoint := Eachin Self.wpList
waypoint.xPos = waypoint.xDist + Self.xPos
waypoint.yPos = waypoint.yDist + Self.yPos
Next
'Scale and rotate all waypoints accordning to the scale factors and the angle of the path
Self.SetScale(0.0, 0.0, True)
Self.SetAngle(0.0, True)
End
'------------------------------------------
'summery:Sets the X position of a path and its waypoints.
Method SetPosX:Void(x:Float, relative:Bool = False)
If relative = True Then
Self.xPos += x
Else
Self.xPos = x
Endif
For Local waypoint := Eachin Self.wpList
waypoint.xPos = waypoint.xDist + Self.xPos
Next
'Scale and rotate all waypoints accordning to the scale factors and the angle of the path
Self.SetScale(0.0, 0.0, True)
Self.SetAngle(0.0, True)
End
'------------------------------------------
'summery:Sets the Y position of a path and its waypoints.
Method SetPosY:Void(y:Float, relative:Bool = False)
If relative = True Then
Self.yPos += y
Else
Self.yPos = y
Endif
For Local waypoint := Eachin Self.wpList
waypoint.yPos = waypoint.yDist + Self.yPos
Next
'Scale and rotate all waypoints accordning to the scale factors and the angle of the path
Self.SetScale(0.0, 0.0, True)
Self.SetAngle(0.0, True)
End
'------------------------------------------
'summery:Sets the scale of a path and position its waypoints regarding the angle.
Method SetScale:Void(x:Float, y:Float, relative:Bool = False)
If relative = True Then
Self.scaleX += x
Self.scaleY += y
Else
Self.scaleX = x
Self.scaleY = y
Endif
For Local waypoint := Eachin Self.wpList
waypoint.xPos = (waypoint.xDist * Self.scaleX) + Self.xPos
waypoint.yPos = (waypoint.yDist * Self.scaleY) + Self.yPos
Next
'Rotate all waypoints according to the angle of the path
Self.SetAngle(0.0, True)
End
'------------------------------------------
#Rem
'summery:Updates all markers of a path.
#End
Method UpdateAllMarker:Void(speedFactor:Float = 1.0)
Self.inUpdate = True
For Local marker := Eachin Self.markerList
If marker.isDeleted = false Then marker.Update(speedFactor)
Next
CleanupLists()
Self.inUpdate = False
End
End
'***************************************
'summery:The ftMarker class defines a marker that runs along a path.
Class ftMarker
'#DOCOFF#
Field objList:= New List<ftObject>
'Field obj:ftObject = Null
'Field objPathUpdAngle:Bool=True
Field speed:Float = 1.0
Field sensivity:Float = 0.1
Field currDist:Float = 0.0
Field currAngle:Float = 0.0
Field currXpos:Float = 0.0
Field currYpos:Float = 0.0
Field prevIndex:Int = -1
Field srcIndex:Int = -1
Field destIndex:Int = -1
Field nextIndex:Int = -1
Field prevWP:ftWaypoint = Null
Field srcWP:ftWaypoint = Null
Field destWP:ftWaypoint = Null
Field nextWP:ftWaypoint = Null
Field destXpos:Float = 0.0
Field destYpos:Float = 0.0
Field sourceXpos:Float = 0.0
Field sourceYpos:Float = 0.0
Field diffX:Float = 0.0
Field diffY:Float = 0.0
Field distance:Float = 0.0
Field pathNode:list.Node<ftMarker> = Null
Field path:ftPath = Null
Field direction:Int = ftPath.mdForward
Field mode:Int = 0
Field _final:Bool = False
Field ipMode:Int = ftPath.imLinear
Field cmPos:ftVec2D = Null
Field cmP1:ftVec2D = Null
Field cmP2:ftVec2D = Null
Field cmP3:ftVec2D = Null
Field cmP4:ftVec2D = Null
Field isDeleted:Bool = False
'------------------------------------------
Method _Calc:Void()
Self.prevWP = Self.path.GetWP(Self.prevIndex)
Self.srcWP = Self.path.GetWP(Self.srcIndex)
Self.destWP = Self.path.GetWP(Self.destIndex)
Self.nextWP = Self.path.GetWP(Self.nextIndex)
Self.diffX = Self.destWP.xPos - Self.srcWP.xPos
Self.diffY = Self.destWP.yPos - Self.srcWP.yPos
Self.currXpos = Self.srcWP.xPos
Self.currYpos = Self.srcWP.yPos
Self.sourceXpos = Self.srcWP.xPos
Self.sourceYpos = Self.srcWP.yPos
Self.destXpos = Self.destWP.xPos
Self.destYpos = Self.destWP.yPos
Self.distance = Sqrt(Self.diffX * Self.diffX + Self.diffY * Self.diffY)
Self.currDist = Self.distance
Self.currAngle = ATan2( Self.diffY, Self.diffX )+90.0
While Self.currAngle < 0
Self.currAngle += 360.0
End
_UpdateObjects()
End
'------------------------------------------
Method _OnMarkerBounce:Void()
For Local tmpObj := Eachin Self.objList
Self.path.engine.OnMarkerBounce(Self, tmpObj)
Next
End
'------------------------------------------
Method _OnMarkerCircle:Void()
For Local tmpObj := Eachin Self.objList
Self.path.engine.OnMarkerCircle(Self, tmpObj)
Next
end
'------------------------------------------
Method _OnMarkerWP:Void()
For Local tmpObj := Eachin Self.objList
Self.path.engine.OnMarkerWP(Self, tmpObj)
Next
end
'------------------------------------------
Method _OnMarkerStop:Void()
For Local tmpObj := Eachin Self.objList
Self.path.engine.OnMarkerStop(Self, tmpObj)
Next
End
'------------------------------------------
Method _OnMarkerWarp:Void()
For Local tmpObj := Eachin Self.objList
Self.path.engine.OnMarkerWarp(Self, tmpObj)
Next
End
'------------------------------------------
Method _GetIndex:Int(si:Int, off:Int)
Local ret:Int
ret = si
Local nCount:Int = Self.path.GetWPCount()
ret = ret + off
If off > 0 Then
If ret > nCount Then ret = ret - nCount
Endif
If off < 0 Then
If ret < 1 Then ret = ret + nCount
Endif
Return ret
End
'------------------------------------------
Method _NextWP:Void()
Local nCount:Int = Self.path.GetWPCount()
If Self.direction = ftPath.mdForward Then
Select Self.mode
Case ftPath.mmStop
If Self.destIndex = nCount Then
_OnMarkerStop()
Self._final = True
Else
Self.srcIndex = Self.destIndex
Self.destIndex = Min(Self.srcIndex + 1, nCount)
Self.nextIndex = Min(Self.destIndex + 1, nCount)
Self.prevIndex = Max(Self.srcIndex - 1, 1)
Endif
Case ftPath.mmBounce
If Self.destIndex = nCount Then
_OnMarkerBounce()
Self.direction = ftPath.mdBackwards
Self.srcIndex = nCount
Self.destIndex = Max(Self.destIndex - 1, 1)
Self.nextIndex = Max(Self.destIndex - 1, 1)
Self.prevIndex = Self.srcIndex
Else
Self.srcIndex = Self.destIndex
Self.destIndex = Min(Self.srcIndex + 1, nCount)
Self.nextIndex = Min(Self.destIndex + 1, nCount)
Self.prevIndex = Max(Self.srcIndex - 1, 1)
Endif
Case ftPath.mmWarp
If Self.destIndex = nCount Then
_OnMarkerWarp()
Self.srcIndex = 1
Self.destIndex = Min(Self.srcIndex + 1, nCount)
Self.nextIndex = Min(Self.destIndex + 1, nCount)
Self.prevIndex = Self.srcIndex
Else
Self.srcIndex = Self.destIndex
Self.destIndex = Min(Self.srcIndex + 1, nCount)
Self.nextIndex = Min(Self.destIndex + 1, nCount)
Self.prevIndex = Max(Self.srcIndex - 1, 1)
Endif
Case ftPath.mmCircle
If Self.destIndex = nCount Then
_OnMarkerCircle()
Endif
Self.prevIndex = Self.srcIndex
Self.srcIndex = Self._GetIndex(Self.srcIndex, 1)
Self.destIndex = Self._GetIndex(Self.srcIndex, 1)
Self.nextIndex = Self._GetIndex(Self.destIndex, 1)
End
Else 'direction = Backwards
Select Self.mode
Case ftPath.mmStop
If Self.destIndex = 1 Then
_OnMarkerStop()
Self._final = True
Else
Self.srcIndex = Self.srcIndex - 1
Self.destIndex = Max(Self.srcIndex - 1, 1)
Self.nextIndex = Max(Self.destIndex - 1, 1)
Self.prevIndex = Min(Self.srcIndex + 1, nCount)
Endif
Case ftPath.mmBounce
If Self.destIndex = 1 Then
_OnMarkerBounce()
Self.direction = ftPath.mdForward
Self.srcIndex = 1
Self.destIndex = Min(Self.srcIndex + 1, nCount)
Self.nextIndex = Min(Self.destIndex + 1, nCount)
Self.prevIndex = Self.srcIndex
Else
Self.srcIndex = Self.srcIndex - 1
Self.destIndex = Max(Self.srcIndex - 1, 1)
Self.nextIndex = Max(Self.destIndex - 1, 1)
Self.prevIndex = Min(Self.srcIndex + 1, nCount)
Endif
Case ftPath.mmWarp
If Self.destIndex = 1 Then
_OnMarkerWarp()
Self.srcIndex = nCount
Self.destIndex = Max(Self.srcIndex - 1, 1)
Self.nextIndex = Max(Self.destIndex - 1, 1)
Self.prevIndex = Self.srcIndex
Else
Self.srcIndex = Self.destIndex
Self.destIndex = Max(Self.srcIndex - 1, 1)
Self.nextIndex = Max(Self.destIndex - 1, 1)
Self.prevIndex = Min(Self.srcIndex + 1, nCount)
Endif
Case ftPath.mmCircle
If Self.destIndex = 1 Then
_OnMarkerCircle()
Endif
Self.prevIndex = Self.srcIndex
Self.srcIndex = Self._GetIndex(Self.srcIndex,-1)
Self.destIndex = Self._GetIndex(Self.srcIndex,-1)
Self.nextIndex = Self._GetIndex(Self.destIndex,-1)
End
Endif
If Self._final <> True Then Self._Calc()
End
'------------------------------------------
Method _UpdateObjects:Void(obj:ftObject = Null)
If obj= Null Then
For Local tmpObj := Eachin Self.objList
If tmpObj.deleted = False Then
tmpObj.SetPos(Self.currXpos, Self.currYpos)
If tmpObj.objPathUpdAngle = True Then tmpObj.SetAngle(Self.currAngle)
Endif
Next
Else
If obj.deleted = False Then
obj.SetPos(Self.currXpos, Self.currYpos)
If obj.objPathUpdAngle = True Then obj.SetAngle(Self.currAngle)
Endif
Endif
End
'------------------------------------------
Method _Init:Void(path:ftPath, speed:Float = 1.0, dir:Int = ftPath.mdForward, mode:Int = ftPath.mmStop)
Self.path = path
Self.direction = dir
Self.mode = mode
Self.speed = speed
Local nCount:Int = path.GetWPCount()
If dir = ftPath.mdForward Then
Self.prevIndex = 1
Self.srcIndex = 1
Self.destIndex = Min(2,nCount)
Self.nextIndex = Min(3,nCount)
Else
Self.prevIndex = nCount
Self.srcIndex = nCount
Self.destIndex = Max(0,nCount-1)
Self.nextIndex = Max(0,nCount-2)
Endif
Self._Calc()
Self._final = False
End
'#DOCON#
'------------------------------------------
'summery:Connects an object to an existing marker .
Method ConnectObj:Void(obj:ftObject, flag:Bool = True)
obj.markerNode = Self.objList.AddLast(obj)
obj.marker = Self
obj.objPathUpdAngle = flag
_UpdateObjects(obj)
End
'------------------------------------------
'summery:Returns the current angle of a marker in degree.
Method GetCurrAngle:Float()
Return Self.currAngle
End
'------------------------------------------
'summery:Returns the current destination waypoint.
Method GetCurrDestWP:ftWaypoint()
Return Self.destWP
End
'------------------------------------------
'summery:Returns the current destination waypoint X/Y/Z position.
Method GetCurrDestPos:Float[]()
Local pos:Float[2]
pos[0] = Self.destWP.xPos
pos[1] = Self.destWP.yPos
Return pos
End
'------------------------------------------
'summery:Returns the current distance of the marker.
Method GetCurrDist:Float()
Return Self.currDist
End
'------------------------------------------
'summery:Returns the current X/Y/Z position of the marker.
Method GetCurrPos:Float[]()
Local pos:Float[2]
pos[0] = Self.currXpos
pos[1] = Self.currYpos
Return pos
End
'------------------------------------------
'summery:Returns the source destination waypoint.
Method GetCurrSrcWP:ftWaypoint()
Return Self.srcWP
End
'------------------------------------------
'summery:Returns if the marker reached the end of a path.
Method GetFinal:Bool()
Return Self._final
End
'------------------------------------------
'summery:Moves a marker to a waypoint with a given index. Index starts with 1.
Method MoveToWP:Void(index:Int=1)
Local nCount:Int = Self.path.wpList.Count()
#If CONFIG="debug"
If index < 1 Or index > nCount Then Error ("~n~nError in file fantomCX.cftWaypoints, Method ftMarker.MoveTpWP(index:Int):~n~nUsed index ("+index+") is out of bounds (1-"+nCount+")")
#End
Self.srcIndex = index
Select Self.mode
Case ftPath.mmCircle
If Self.direction = ftPath.mdForward
'If Self.srcIndex = nCount Then Self.srcIndex = 1
Self.destIndex = Self._GetIndex(Self.srcIndex, 1)
Self.nextIndex = Self._GetIndex(Self.destIndex, 1)
Self.prevIndex = Self._GetIndex(Self.srcIndex, -1)
Else
'If Self.srcIndex = 1 Then Self.srcIndex = nCount
Self.destIndex = Self._GetIndex(Self.srcIndex, -1)
Self.nextIndex = Self._GetIndex(Self.destIndex,-1)
Self.prevIndex = Self._GetIndex(Self.srcIndex, 1)
Endif
Default
If Self.direction = ftPath.mdForward
'If Self.srcIndex = nCount Then Self.srcIndex = 1
Self.destIndex = Min(Self.srcIndex+1, nCount)
Self.nextIndex = Min(Self.destIndex+1, nCount)
Self.prevIndex = Max(Self.srcIndex - 1, 1)
Else
'If Self.srcIndex = 1 Then Self.srcIndex = nCount
Self.destIndex = Max(Self.srcIndex-1, 1)
Self.nextIndex = Max(Self.destIndex-1, 1)
Self.prevIndex = Min(Self.srcIndex+1, nCount)
Endif
End
Self._Calc()
End
'------------------------------------------
'summery:Moves a marker to a specific waypoint.
Method MoveToWP:Void(waypoint:ftWaypoint)
Self.MoveToWP(waypoint.GetIndex())
End
'#DOCOFF#
'------------------------------------------
Method New()
Self.prevWP = Null
Self.srcWP = Null
Self.destWP = Null
Self.nextWP = Null
Self.diffX = 0.0
Self.diffY = 0.0
Self.currDist = 0.0
Self.distance = 0.0
Self.currAngle = 0.0
Self.prevIndex = -1
Self.srcIndex = -1
Self.destIndex = -1
Self.nextIndex = -1
Self.cmPos = New ftVec2D(0,0)
Self.cmP1 = New ftVec2D(0,0)
Self.cmP2 = New ftVec2D(0,0)
Self.cmP3 = New ftVec2D(0,0)
Self.cmP4 = New ftVec2D(0,0)
End
'#DOCON#
'------------------------------------------
'summery:Removes a marker.
Method Remove:Void()
If Self.path.inUpdate = False Then
Self.pathNode.Remove()
Self.path = Null
For Local tmpObj := Eachin Self.objList
tmpObj.markerNode.Remove()
tmpObj.marker = Null
Next
Else
Self.isDeleted = True
Endif
End
'------------------------------------------
#Rem
'summery:Renders a marker at its current position.
'A renderType of 0 renders a box, every other value a circle.
#End
Method Render:Void(renderType:Int=0)
If renderType = 0 Then
Self.path.engine.currentCanvas.PushMatrix
Self.path.engine.currentCanvas.Translate (Self.currXpos, Self.currYpos)
Self.path.engine.currentCanvas.Rotate 360.0-Self.currAngle
Self.path.engine.currentCanvas.DrawRect -5, -10, 10, 20
Self.path.engine.currentCanvas.PopMatrix
Else
Self.path.engine.currentCanvas.DrawCircle Self.currXpos, Self.currYpos, 3
Endif
End
'------------------------------------------
'summery:Sets the direction of a marker.
Method SetDirection:Void(dir:Int = ftPath.mdForward)
Local nCount:Int = Self.path.GetWPCount()
Self.direction = dir
Select Self.mode
Case ftPath.mmCircle
If Self.direction = ftPath.mdForward
If Self.srcIndex = nCount Then Self.srcIndex = 1
Self.destIndex = Self._GetIndex(Self.srcIndex, 1)
Self.nextIndex = Self._GetIndex(Self.destIndex, 1)
Self.prevIndex = Self._GetIndex(Self.srcIndex, -1)
Else
If Self.srcIndex = 1 Then Self.srcIndex = nCount
Self.destIndex = Self._GetIndex(Self.srcIndex, -1)
Self.nextIndex = Self._GetIndex(Self.destIndex,-1)
Self.prevIndex = Self._GetIndex(Self.srcIndex, 1)
Endif
Default
If Self.direction = ftPath.mdForward
If Self.srcIndex = nCount Then Self.srcIndex = 1
Self.destIndex = Min(Self.srcIndex+1, nCount)
Self.nextIndex = Min(Self.destIndex+1, nCount)
Self.prevIndex = Max(Self.srcIndex - 1, 1)
Else
If Self.srcIndex = 1 Then Self.srcIndex = nCount
Self.destIndex = Max(Self.srcIndex-1, 1)
Self.nextIndex = Max(Self.destIndex-1, 1)
Self.prevIndex = Min(Self.srcIndex+1, nCount)
Endif
'Case ftPath.mmStop
'Case ftPath.mmBounce
'Case ftPath.mmWarp
End
Self._Calc()
End
'------------------------------------------
'summery:Sets the interpolation mode of a marker.
Method SetInterpolationMode:Void(mode:Int = ftPath.imLinear)
Self.ipMode = mode
End
'------------------------------------------
'summery:Sets the move mode of a marker.
Method SetMoveMode:Void(mode:Int = ftPath.mmStop)
Self.mode = mode
End
'------------------------------------------
#Rem
'summery:Sets the sensivity of a marker.
'The sensivity controls when a marker reaches its next waypoint. It is the minimum distance in pixel.