-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame9.monkey
1901 lines (1690 loc) · 52.4 KB
/
game9.monkey
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
header:
[quote]
[b]File Name :[/b] Game9Screen
[b]Author :[/b] Shane "Samah" Woolcock
[b]About :[/b] Music by SouljahdeShiva: http://opengameart.org/content/upbeat-techno-spacenomad-2
Touhou Clone
[/quote]
#End
Strict
Public
Import main
#rem
summary:Title Screen Class.
Used to manage and deal with all Title Page stuff.
#End
Class Game9Screen Extends Screen
Field loaded:Bool = False
Method New()
name = "DanMonkey"
Local gameid:Int = 9
GameList[gameid - 1] = New miniGame
GameList[gameid - 1].id = gameid - 1
GameList[gameid - 1].name = "DanMonkey"
GameList[gameid - 1].iconname = "game" + gameid + "_icon"
GameList[gameid - 1].thumbnail = "game" + gameid + "_thumb"
GameList[gameid - 1].author = "Shane Woolcock"
GameList[gameid - 1].authorurl = "????"
GameList[gameid - 1].info = "A bullet hell diddyGame... in Monkey ;)"
End
#rem
summary:Start Screen
Start the Title Screen.
#End
Method Start:Void()
If Not loaded Then LoadMedia()
'diddyGame.images.LoadAnim("Ship1.png", 64, 64, 7, tmpImage)
smhGameScreen = New Smh_GameScreen
End
#rem
summary:Render Title Screen
Renders all the Screen Elements.
#End
Method Render:Void()
Cls
TitleFont.DrawText(Self.name, SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 2)
End
#rem
summary:Update Title Screen
Will update all screen objects, handles mouse, keys
And all use input.
#End
Method Update:Void()
If KeyHit(KEY_ESCAPE) Then
FadeToScreen(TitleScr)
End
If KeyHit(KEY_SPACE) Or MouseHit() Then
FadeToScreen(smhGameScreen)
End
End
Method LoadMedia:Void()
loaded = True
diddyGame.images.LoadAtlas("game9/game9_bullets1.txt", ImageBank.LIBGDX_ATLAS, True)
diddyGame.images.LoadAtlas("game9/game9_bullets2.txt", ImageBank.LIBGDX_ATLAS, True)
diddyGame.images.LoadAtlas("game9/game9_ships.txt", ImageBank.LIBGDX_ATLAS, True)
Local tmpImage:GameImage = Null
diddyGame.images.Load("game9/game9_ship2.png",,False).image.SetHandle(9, 12)
diddyGame.images.Load("game9/game9_explode1.png")
diddyGame.images.LoadAnim("game9/game9_ship6.png", 39, 39, 3, Null)
diddyGame.images.LoadAnim("game9/game9_cube.png", 24, 28, 21, Null)
diddyGame.images.Load("game9/game9_starfield.jpg",,False)
diddyGame.images.Load("game9/game9_bg.png",,False)
diddyGame.sounds.Load("game9_death")
diddyGame.sounds.Load("game9_explosion1")
diddyGame.sounds.Load("game9_explosion2")
diddyGame.sounds.Load("game9_explosion3")
diddyGame.sounds.Load("game9_laser1")
diddyGame.sounds.Load("game9_laser2")
diddyGame.sounds.Load("game9_laser3")
diddyGame.sounds.Load("game9_laser4")
diddyGame.sounds.Load("game9_pickup1")
End
End
Class Smh_GameOverScreen Extends Screen
Method Start:Void()
End
Method Render:Void()
Cls
TitleFont.DrawText("Game Over!", SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 2)
End
Method Update:Void()
If KeyHit(KEY_ESCAPE) Or KeyHit(KEY_SPACE) Or MouseHit() Then
FadeToScreen(Game9Scr)
End
End
End
Private
Global smhGameScreen:Smh_GameScreen
Global psys:ParticleSystem
Global explosions:ParticleGroup
Global sparks:ParticleGroup
Global explode1:Emitter
Global grazeEmitter:Emitter
Global playarea:Smh_PlayArea
Global enemyBullets:Smh_BulletPool
Global playerBullets:Smh_BulletPool
Global powerups:Smh_PowerupPool
Global enemies:Smh_EnemyPool
Global player:Smh_Player
Global boss:Smh_Boss
Global currentStage:Smh_Stage
Class Smh_GameScreen Extends Screen
Const GRAZE_INTERVAL:Int = 200
Field nextGrazeBonus:Int = -1
Method Start:Void()
CreateParticleSystem()
playarea = New Smh_PlayArea("graphics/game9/game9_background1.tmx")
playarea.x = 0
playarea.y = 0
playarea.height = SCREEN_HEIGHT' - playarea.y*2
playarea.width = 380
playarea.boundsLeft = 0
playarea.boundsTop = 0
playarea.boundsRight = playarea.width
playarea.boundsBottom = playarea.height
enemyBullets = New Smh_BulletPool
enemyBullets.parent = playarea
playerBullets = New Smh_BulletPool
playerBullets.parent = playarea
powerups = New Smh_PowerupPool
powerups.parent = playarea
player = New Smh_Player
player.parent = playarea
player.x = playarea.x + playarea.width * 0.5
player.y = playarea.y + playarea.height * 0.8
enemies = New Smh_EnemyPool
enemies.parent = playarea
currentStage = New Smh_Stage1
diddyGame.MusicPlay("game9_bgm.mp3")
End
Method Kill:Void()
StopMusic()
playarea = Null
enemyBullets = Null
playerBullets = Null
player = Null
enemies = Null
boss = Null
currentStage = Null
End
Method Update:Void()
If KeyHit(KEY_ESCAPE) Then
FadeToScreen(Game9Scr)
End
' update everything
currentStage.Update(dt.frametime)
playarea.DoUpdate(dt.frametime)
enemies.DoUpdate(dt.frametime)
If boss Then boss.DoUpdate(dt.frametime)
enemyBullets.DoUpdate(dt.frametime)
playerBullets.DoUpdate(dt.frametime)
powerups.DoUpdate(dt.frametime)
player.DoUpdate(dt.frametime)
psys.Update(dt.frametime)
ResolveCollisions()
End
Method Render:Void()
' clear the screen
Cls
' draw the main background image
DrawImage(diddyGame.images.Find("game9_bg").image, 0, 0)
' draw the gui
DrawGUI()
' draw the play area
playarea.DoRender()
' draw the timer
If boss Then
SetAlpha(0.8)
SetColor(255,255,255)
DrawRect(playarea.x+15, playarea.y+15, (playarea.width-60) * Float(boss.currentHP)/Float(boss.maxHP), 10)
SetAlpha(1)
TitleFont.DrawText(""+Int(Max(0.0,Ceil(boss.timeRemainingMillis/1000.0))), playarea.x + playarea.width - 10, 10, eDrawAlign.RIGHT)
End
SetColor 255,255,255
DrawLine(playarea.x+playarea.width, playarea.y, playarea.x+playarea.width, playarea.y+playarea.height)
If player.godMode Then DrawText("God Mode", 0, 0)
End
Method DrawGUI:Void()
' labels
TitleFont.DrawText("Player", playarea.x+playarea.width+20, 100)
TitleFont.DrawText("Bomb", playarea.x+playarea.width+20, 150)
TitleFont.DrawText("Score", playarea.x+playarea.width+20, 200)
TitleFont.DrawText("Graze", playarea.x+playarea.width+20, 250)
' draw player/bomb counters
Local markerX:Float = playarea.x+playarea.width+100
Local cubes:GameImage = diddyGame.images.Find("game9_cube")
For Local i:Int = 0 Until Smh_Player.MAX_LIVES
If (i+1) * Smh_Player.POWER_PER_LIFE <= player.lifePower
SetAlpha(1)
cubes.Draw(markerX+(cubes.image.Width()+2)*i, 110,,,,20)
Else
SetAlpha(0.5)
cubes.Draw(markerX+(cubes.image.Width()+2)*i, 110,,,,18)
If i = Int(player.lifePower / Smh_Player.POWER_PER_LIFE) Then
SetAlpha(1)
Local ratio:Float = (player.lifePower Mod Smh_Player.POWER_PER_LIFE) / Smh_Player.POWER_PER_LIFE
cubes.DrawSubImage(markerX+(cubes.image.Width()+2)*i, 110, 0, 0, cubes.image.Width()*ratio, cubes.image.Height(),,,,20)
End
End
Next
For Local i:Int = 0 Until Smh_Player.MAX_BOMBS
If (i+1) * Smh_Player.POWER_PER_BOMB <= player.bombPower
SetAlpha(1)
cubes.Draw(markerX+(cubes.image.Width()+2)*i, 160,,,,17)
Else
SetAlpha(0.5)
cubes.Draw(markerX+(cubes.image.Width()+2)*i, 160,,,,15)
If i = Int(player.bombPower / Smh_Player.POWER_PER_BOMB) Then
SetAlpha(1)
Local ratio:Float = (player.bombPower Mod Smh_Player.POWER_PER_BOMB) / Smh_Player.POWER_PER_BOMB
cubes.DrawSubImage(markerX+(cubes.image.Width()+2)*i, 160, 0, 0, cubes.image.Width()*ratio, cubes.image.Height(),,,,17)
End
End
Next
End
Method ResolveCollisions:Void()
Local bullet:Smh_Bullet = Null
Local enemy:Smh_Enemy = Null
' resolve collisions of boss with player
If boss Then
Local dx:Float = player.x-boss.x
Local dy:Float = player.y-boss.y
If dx*dx + dy*dy <= boss.radius + player.radius Then
player.currentHP = 0
End
End
' resolve collisions of enemies with player
enemy = enemies.FindFirstCollision(player)
If enemy Then
player.currentHP = 0
End
' resolve collisions of enemy bullets with player
bullet = enemyBullets.FindFirstCollision(player)
If bullet Then
player.currentHP = 0
End
' resolve collisions of player bullets with boss
If boss Then
bullet = playerBullets.FindFirstCollision(boss)
If bullet Then
' damage
bullet.alive = False
bullet.active = False
boss.currentHP -= 5
If boss.currentHP < 0 Then boss.currentHP = 0
End
End
' resolve collisions of player bullets with enemies
For Local i:Int = 0 Until enemies.aliveCount
bullet = playerBullets.FindFirstCollision(enemies.children[i])
If bullet Then
' damage
bullet.alive = False
bullet.active = False
enemies.children[i].currentHP -= 5
If enemies.children[i].currentHP < 0 Then
enemies.children[i].currentHP = 0
End
End
Next
' find graze count
If nextGrazeBonus < dt.currentticks Then
Local grazeCount% = enemyBullets.GetGrazeCount(player, 15)
player.AddBombPower(grazeCount)
nextGrazeBonus = dt.currentticks + GRAZE_INTERVAL
If grazeCount > 0 Then grazeEmitter.EmitAt(5*grazeCount, player.x, player.y)
End
End
Method CreateParticleSystem:Void()
Local parser:XMLParser = New XMLParser
Local doc:XMLDocument = parser.ParseFile("graphics/game9/psystem.xml")
psys = New ParticleSystem(doc)
explosions = psys.GetGroup("explosions")
sparks = psys.GetGroup("sparks")
explode1 = psys.GetEmitter("explode1")
explode1.ParticleImage = diddyGame.images.Find("game9_explode1").image
grazeEmitter = psys.GetEmitter("graze")
grazeEmitter.ParticleImage = diddyGame.images.Find("game9_bullet1").image
End
End
Class Smh_Entity
' used for HSL conversion
Global rgbArray:Int[] = New Int[3]
' misc
Field parent:Smh_Entity
Field alive:Bool = False ' used for pooling
Field active:Bool = True
Field activeDelayMillis:Int = 0
Field logicHandler:Smh_EntityLogicHandler = Null
Field activeTimeMillis:Int = 0 ' the game time that this entity became active
Field entityTypeId:Int = 0 ' arbitrary value
' custom fields for the logic handler
Field logicVar1:Int
Field logicVar2:Int
Field logicVar3:Int
' position/velocities (velocity in units per second)
Field x#, y# ' position
Field width#, height# ' size
Field dx#, dy# ' cartesian velocity if usePolar = False
Field accelX#, accelY# ' cartesian acceleration if usePolar = False
Field polarAngle#, polarVelocity# ' polar velocity if usePolar = True
Field polarAccel# ' polar acceleration if usePolar = True
Field terminalVelocity# ' can never move faster than this (if not interping)
Field usePolar?
Field recalcPolar?
Field boundsRestrict? = False
Field boundsPurge? = False
Field useParentBounds? = True
Field boundsLeft#, boundsRight#, boundsTop#, boundsBottom#
Field boundsInset# = 0
Field radius# = 0
' interpolation
Field sourceX#, sourceY# ' starting point
Field targetX#, targetY# ' ending point
Field interpTotalMillis% ' total interp time millis
Field interpRemainingMillis% ' interp time remaining millis
Field interpSmooth%
Field interping?
' visual
Field image:GameImage ' for a simple single image
Field anim:Smh_AnimStrip
Field animFrame:Int
Field animDelayMillis:Int
Field animDirection:Int
Field animForcedDirection:Int
Field rotation# = 0
Field rotationSpeed# = 0
Field rotateWithHeading? = True
Field scaleX# = 1
Field scaleY# = 1
Field hue# = 0
Field saturation# = 1
Field luminance# = 0.5
Field red% = 255
Field green% = 255
Field blue% = 255
Field useHSL? = False
Field recalcHSL?
Field alpha# = 1
Field visible? = True
Field visibleWhileInactive? = False
Field fadeInTimeMillis% = 0
Field blendMode# = AlphaBlend
Field scissor? = False
Method CalcBoundsLeft:Float()
Local current:Smh_Entity = Self
While current.parent And current.useParentBounds
current = current.parent
End
Return current.boundsLeft + boundsInset
End
Method CalcBoundsRight:Float()
Local current:Smh_Entity = Self
While current.parent And current.useParentBounds
current = current.parent
End
Return current.boundsRight - boundsInset
End
Method CalcBoundsTop:Float()
Local current:Smh_Entity = Self
While current.parent And current.useParentBounds
current = current.parent
End
Return current.boundsTop + boundsInset
End
Method CalcBoundsBottom:Float()
Local current:Smh_Entity = Self
While current.parent And current.useParentBounds
current = current.parent
End
Return current.boundsBottom - boundsInset
End
Method SetVelocityCartesian:Void(dx#, dy#)
Self.dx = dx
Self.dy = dy
Self.usePolar = False
Self.recalcPolar = False
Self.interping = False
End
Method SetVelocityPolar:Void(polarAngle#, polarVelocity#)
Self.polarAngle = polarAngle
Self.polarVelocity = polarVelocity
Self.usePolar = True
Self.interping = False
RecalcPolar()
End
Method SetInterpOverTime:Void(sourceX#, sourceY#, targetX#, targetY#, millis%, smooth% = 0)
Self.sourceX = sourceX
Self.sourceY = sourceY
Self.targetX = targetX
Self.targetY = targetY
Self.interpTotalMillis = millis
Self.interpRemainingMillis = millis
Self.interpSmooth = smooth
Self.interping = True
End
Method SetInterpWithVelocity:Void(sourceX#, sourceY#, targetX#, targetY#, velocity#, smooth% = 0)
Local distance# = Sqrt((targetX-sourceX)*(targetX-sourceX)+(targetY-sourceY)*(targetY-sourceY))
Local time# = distance/velocity
SetInterpOverTime(sourceX, sourceY, targetX, targetY, Int(time*1000), smooth)
End
' Warning: don't call this with parameters that would give an impossible movement, or it'll infinite loop!
Method SetInterpRandomOverTime:Void(sourceX#, sourceY#, minDistance#, maxDistance#, millis%, smooth% = 0)
Local rndX# = 0
Local rndY# = 0
Local calc# = 0
Repeat
rndX = Rnd(boundsLeft+boundsInset, boundsRight-boundsInset)
rndY = Rnd(boundsTop+boundsInset, boundsBottom-boundsInset)
calc = (sourceX-rndX)*(sourceX-rndX)+(sourceY-rndY)*(sourceY-rndY)
Until calc >= minDistance*minDistance And calc <= maxDistance*maxDistance
SetInterpOverTime(sourceX, sourceY, rndX, rndY, millis, smooth)
End
Method RecalcPolar:Void()
Self.recalcPolar = False
Self.dx = Cos(polarAngle) * polarVelocity
Self.dy = Sin(polarAngle) * polarVelocity
End
Method RecalcHSL:Void()
Self.recalcHSL = False
HSLtoRGB(hue, saturation, luminance, rgbArray)
red = rgbArray[0]; green = rgbArray[1]; blue = rgbArray[2]
End
Method PreUpdate:Float(millis%)
If Not active And activeDelayMillis > 0 Then
activeDelayMillis -= millis
If activeDelayMillis <= 0 Then
millis = -activeDelayMillis
activeDelayMillis = 0
activeTimeMillis = dt.currentticks
active = True
End
End
If Not active Then Return 0
Return millis
End
Method PostUpdate:Void(millis%)
' update position based on interp or velocity
If interping Then
interpRemainingMillis -= millis
If interpRemainingMillis <= 0 Then
interping = False
x = targetX
y = targetY
interpRemainingMillis = 0
Else
Local alpha# = 1 - Float(interpRemainingMillis)/Float(interpTotalMillis)
For Local i% = 0 Until interpSmooth
alpha *= alpha * (3 - 2 * alpha)
Next
x = sourceX + (targetX-sourceX)*alpha
y = sourceY + (targetY-sourceY)*alpha
If usePolar Then polarAngle = ATan2(targetY-y, targetX-x)
End
Else
' update polar accel
If usePolar Then
If polarAccel <> 0 Then
polarVelocity += polarAccel * millis / 1000.0
recalcPolar = True
End
If recalcPolar Then RecalcPolar()
' update cartesian accel
Else
dx += accelX * millis / 1000.0
dy += accelY * millis / 1000.0
End
' do terminal velocity
If terminalVelocity > 0 And dx*dx + dy*dy > terminalVelocity*terminalVelocity Then
Local length:Float = Sqrt(dx*dx + dy*dy)
dx *= terminalVelocity / length
dy *= terminalVelocity / length
polarVelocity = terminalVelocity
End
' update position
x += dx * millis / 1000.0
y += dy * millis / 1000.0
End
' update rotation
If rotationSpeed <> 0 Then
rotation += rotationSpeed * millis / 1000.0
While rotation < 0 rotation += 360 End
While rotation >= 360 rotation -= 360 End
End
' get bounds
Local current:Smh_Entity = Self
While current.parent And current.useParentBounds
current = current.parent
End
Local bl:Float = current.boundsLeft + boundsInset
Local br:Float = current.boundsRight - boundsInset
Local bt:Float = current.boundsTop + boundsInset
Local bb:Float = current.boundsBottom - boundsInset
' restrict
If boundsRestrict Then
If x < bl Then x = bl
If x > br Then x = br
If y < bt Then y = bt
If y > bb Then y = bb
End
' mark for purging
If boundsPurge Then
If x < bl Or x > br Or y < bt Or y > bb Then alive = False
End
End
' This should be called in OnUpdate() or by a parent entity
Method DoUpdate:Void(millis%)
millis = PreUpdate(millis)
If millis > 0 Then
Update(millis)
PostUpdate(millis)
End
End
' Entities should implement this method
Method Update:Void(millis%)
If logicHandler Then logicHandler.UpdateLogic(Self, millis)
End
Method PreRender:Bool()
If Not active And Not visibleWhileInactive Then Return False
If fadeInTimeMillis > 0 Then alpha = Max(0.0,Min(1.0,1.0-(Float(activeDelayMillis) / Float(fadeInTimeMillis))))
Local rot:Float = rotation
If rotateWithHeading And usePolar Then rot -= polarAngle
PushMatrix
Translate x, y
Scale scaleX, scaleY
Rotate rot
If scissor Then SetScissor(x+boundsLeft, y+boundsTop, boundsRight-boundsLeft, boundsBottom-boundsTop)
Return True
End
Method PostRender:Void()
' we have to use device width/height rather than screen width/height as the scissor is not affected by scaling
If scissor Then SetScissor(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT)
PopMatrix
End
' This should be called in OnRender() or by a parent entity
Method DoRender:Void()
If PreRender() Then
Render()
PostRender()
End
End
' Entities should override this method if they want anything special
Method Render:Void()
If image Or anim Then
Local oldalpha:Float = GetAlpha()
Local oldblend:Int = GetBlend()
SetBlend(blendMode)
If useHSL And recalcHSL Then RecalcHSL()
If blendMode = AlphaBlend Then
SetAlpha(alpha)
SetColor(red, green, blue)
Else
SetAlpha(1)
SetColor(red*alpha, green*alpha, blue*alpha)
End
If image Then
DrawImage(image.image, 0, 0)
Elseif anim Then
anim.Render(Self, 0, 0)
End
SetBlend(oldblend)
If blendMode = AlphaBlend Then
SetAlpha(oldalpha)
Else
SetColor(red, green, blue)
End
End
End
Method AbsoluteX:Float() Property
If parent = Null Then Return x
Return x + parent.AbsoluteX
End
Method AbsoluteY:Float() Property
If parent = Null Then Return y
Return y + parent.AbsoluteY
End
' summary: Copies specific fields from another entity to this one.
' This is so we can reuse entities and populate them with templates.
Method CopyFrom:Smh_Entity(source:Smh_Entity)
parent = source.parent
'active = source.active
'activeDelayMillis = source.activeDelayMillis
'alive = source.alive
entityTypeId = source.entityTypeId
logicHandler = source.logicHandler
logicVar1 = source.logicVar1
logicVar2 = source.logicVar2
logicVar3 = source.logicVar3
x = source.x
y = source.y
width = source.width
height = source.height
dx = source.dx
dy = source.dy
accelX = source.accelX
accelY = source.accelY
polarAngle = source.polarAngle
polarVelocity = source.polarVelocity
polarAccel = source.polarAccel
terminalVelocity = source.terminalVelocity
usePolar = source.usePolar
recalcPolar = source.recalcPolar
boundsLeft = source.boundsLeft
boundsRight = source.boundsRight
boundsTop = source.boundsTop
boundsBottom = source.boundsBottom
boundsPurge = source.boundsPurge
boundsRestrict = source.boundsRestrict
boundsInset = source.boundsInset
radius = source.radius
sourceX = source.sourceX
sourceY = source.sourceY
targetX = source.targetX
targetY = source.targetY
interpTotalMillis = source.interpTotalMillis
interpRemainingMillis = source.interpRemainingMillis
interpSmooth = source.interpSmooth
interping = source.interping
image = source.image
rotation = source.rotation
rotationSpeed = source.rotationSpeed
rotateWithHeading = source.rotateWithHeading
scaleX = source.scaleX
scaleY = source.scaleY
hue = source.hue
saturation = source.saturation
luminance = source.luminance
red = source.red
green = source.green
blue = source.blue
useHSL = source.useHSL
recalcHSL = source.recalcHSL
alpha = source.alpha
blendMode = source.blendMode
visible = source.visible
visibleWhileInactive = source.visibleWhileInactive
fadeInTimeMillis = source.fadeInTimeMillis
scissor = source.scissor
anim = source.anim
animFrame = source.animFrame
animDelayMillis = source.animDelayMillis
animDirection = source.animDirection
animForcedDirection = source.animForcedDirection
Return Self
End
Method SuckToPlayer:Void(template:Smh_Entity, millis:Int)
Local oldX:Int = x
Local oldY:Int = y
If template Then CopyFrom(template)
SetInterpOverTime(oldX, oldY, player.x, player.y, millis, 1)
End
End
' Implement this interface if you want to provide logic shared amongst entities without having to extend them.
' This is useful for applying different functionality to pooled objects (like bullets).
Interface Smh_EntityLogicHandler
Method UpdateLogic:Void(entity:Smh_Entity, millis%)
End
Class Smh_AnimStrip
Field image:GameImage
Field frameCount:Int
Field frameDelays:Int[]
Field frameOffsets:Int[]
Field frameDirections:Int[]
Method New(name:String, frameCount:Int)
Self.image = diddyGame.images.Find(name)
Self.frameCount = frameCount
frameDelays = New Int[frameCount]
frameOffsets = New Int[frameCount]
frameDirections = New Int[frameCount]
For Local i:Int = 0 Until frameCount
frameOffsets[i] = 1
frameDirections[i] = 0
Next
End
Method Update:Void(entity:Smh_Entity, millis%)
' die if paused and not forced
If entity.animDirection = 0 And entity.animForcedDirection = 0 Then Return
' loop while we have millis
While millis > 0
' update the delay
entity.animDelay -= millis
If entity.animDelay >= 0 Then
millis = 0
Else
millis = -entity.animDelay
End
' if we should move to the next frame
If entity.animDelay <= 0 Then
' if we're forcing a direction, use that
If entity.animForcedDirection <> 0 Then
entity.animFrame += entity.animForcedDirection
Else
entity.animFrame += frameOffsets[entity.animFrame]*entity.animDirection
End
' clip it
If entity.animFrame < 0 Then entity.animFrame = 0
If entity.animFrame >= frameCount Then entity.animFrame = frameCount-1
' update the direction if not forced
If entity.animForcedDirection = 0 And frameDirections[entity.animFrame] <> 0 Then
entity.animDirection = frameDirections[entity.animFrame]
End
entity.animDelay = frameDelays[entity.animFrame]
End
End
End
Method Render:Void(entity:Smh_Entity, x#, y#)
DrawImage(image.image, x, y, entity.animFrame)
End
End
Class Smh_EntityGroup Extends Smh_Entity
Field children:ArrayList<Smh_Entity> = New ArrayList<Smh_Entity>(100)
Method Update:Void(millis%)
For Local i:Int = 0 Until children.Size
Local child:Smh_Entity = children.Get(i)
If child.active Then child.DoUpdate(millis)
Next
End
Method Render:Void()
For Local i:Int = 0 Until children.Size
Local child:Smh_Entity = children.Get(i)
If child.active Then child.DoRender()
Next
End
End
Class Smh_Pool<T> Extends Smh_Entity
Field children:T[]
Field aliveCount:Int = 0
Field autoPurgeMillis:Int = 500
Field nextPurgeMillis:Int = -1
Method New(capacity:Int)
active = True
PopulatePool(capacity)
End
Method PopulatePool:Void(capacity:Int)
If children.Length > 0 Then Return ' throw exception?
If capacity <= 0 Then Return ' throw exception?
children = New T[capacity]
For Local i:Int = 0 Until capacity
children[i] = New T
Next
End
Method GetEntity:T(template:Smh_Entity=Null)
If aliveCount = children.Length Then Purge()
If aliveCount = children.Length Then Return Null
aliveCount += 1
children[aliveCount-1].alive = True
If template Then children[aliveCount-1].CopyFrom(template)
Return children[aliveCount-1]
End
Method ReleaseEntity:Void(entity:T)
' TODO: improve performance with a reverse pointer lookup?
For Local i:Int = 0 Until aliveCount
If children[i] = entity Then
entity.alive = False
Exit
End
Next
Purge()
End
Method Purge:Void()
Local current:Int = 0
While current < aliveCount
' if the low index is alive
If children[current].alive Then
' move to the next index
current += 1
Else
' else, reduce the alive count until we find one to swap with
While current < aliveCount And Not children[aliveCount-1].alive
aliveCount -= 1
End
' if we have an alive to swap, do it
If current < aliveCount Then
Local oldChild:T = children[current]
children[current] = children[aliveCount-1]
children[aliveCount-1] = oldChild
aliveCount -= 1
End
End
End
End
Method Clear:Void()
For Local i% = 0 Until aliveCount
children[i].alive = False
Next
Purge()
End
Method FindFirstCollision:T(other:Smh_Entity)
For Local i% = 0 Until aliveCount
If children[i].alive Then
Local dx# = other.x-children[i].x
Local dy# = other.y-children[i].y
Local dist# = other.radius+children[i].radius
If dx*dx + dy*dy < dist*dist Then Return children[i]
End
Next
Return Null
End
Method GetGrazeCount:Int(other:Smh_Entity, grazeProximity:Float)
Local count:Int = 0
For Local i% = 0 Until aliveCount
If children[i].alive Then
Local dx# = other.x-children[i].x
Local dy# = other.y-children[i].y
Local dist# = other.radius+children[i].radius+grazeProximity
If dx*dx + dy*dy < dist*dist Then count += 1
End
Next
Return count
End
Method Update:Void(millis%)
For Local i:Int = 0 Until aliveCount
If children[i].alive Then children[i].DoUpdate(millis)
Next
If autoPurgeMillis > 0 Then
If nextPurgeMillis < dt.frametime Then
Purge()
nextPurgeMillis = dt.frametime + autoPurgeMillis
End
End
End
Method Render:Void()
For Local i:Int = 0 Until aliveCount
If children[i].alive Then children[i].DoRender()
Next
End
End
Class Smh_PlayArea Extends Smh_Entity
Field scrollX:Float
Field scrollY:Float
Field scrollSpeed:Float = 50
Field tilemap:TileMap
Method New(mapfile:String)
Local reader:MyTiledTileMapReader = New MyTiledTileMapReader
Local tm:TileMap = reader.LoadMap(mapfile)
tilemap = MyTileMap(tm)
active = True
scissor = True
End
Method Render:Void()
SetAlpha(1)
'SetColor(128, 128, 128)
' render starfield
Local starfield:GameImage = diddyGame.images.Find("game9_starfield")
Local y:Float = scrollY/2
While y > 0
y -= starfield.h
End
While y < height
DrawImage(starfield.image, 0, y)
y += starfield.h
End
tilemap.RenderMap(scrollX, (tilemap.height*tilemap.tileHeight)-scrollY, SCREEN_WIDTH, SCREEN_HEIGHT)'boundsRight, boundsBottom)
' temporary
SetColor(0,0,0)
SetAlpha(0.5)
DrawRect(x, Self.y, width, height)
SetAlpha(1)
SetColor(255, 255, 255)
enemies.DoRender()
If boss Then boss.DoRender()
playerBullets.DoRender()
powerups.DoRender()
player.DoRender()
psys.Render()
enemyBullets.DoRender()
End
Method Update:Void(millis:Int)
scrollY += scrollSpeed * (Float(millis)/1000)
End
End