This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
flxgame.monkey
625 lines (461 loc) · 12.7 KB
/
flxgame.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
Strict
Import mojo
Import reflection
Import flxextern
Import flxbasic
Import flxstate
Import flxcamera
Import flxtimer
Import flxtext
Import flxg
Import flxu
Import flxtilemap
Import system.flxassetsmanager
Import system.flxfont
Import system.flxdebugger
Import system.flxreplay
Import plugin.timermanager
#If FLX_SOUND_EXTENSION = "unknown"
Import "data/beep_flx.wav"
Import "data/beep_flx.ogg"
Import "data/beep_flx.mp3"
#Else
Import "data/beep_flx.${FLX_SOUND_EXTENSION}"
#End
Class FlxGame extends App
Field useSoundHotKeys:Bool
Field useSystemCursor:Bool
Field forceDebugger:Bool
Field _state:FlxState
Field _requestedState:FlxState
Field _requestedReset:Bool
Field _debugger:FlxDebugger
Field _debuggerUp:Bool
Field _replay:FlxReplay
Field _replayRequested:Bool
Field _recordingRequested:Bool
Field _replaying:Bool
Field _recording:Bool
Field _replayCancelKeys:Int[]
Field _replayTimer:Float
Field _replayCallback:FlxReplayListener
Private
Field _iState:ClassInfo
Field _step:Int
Field _soundTrayTimer:Float
Field _soundTrayWidth:Float
Field _soundTrayHeight:Float
Field _soundTrayX:Float
Field _soundTrayY:Float
Field _soundTrayVisible:Bool
Field _soundTrayLabel:FlxText
Field _updaterate:Int
Field _switchStateListener:FlxSwitchStateListener
'note: BUG: It seems we have bug in HTML5 vesion fo mojo
#If TARGET = "html5" And Not FLX_WEBGL_ENABLED
Field _stateIsReady:Bool
#End
Public
Method New(gameSizeX:Int, gameSizeY:Int, initialState:ClassInfo, zoom:Float = 1, updaterate:Int = 60, useSystemCursor:Bool = False)
Local classObject:GlobalInfo
For Local classInfo:ClassInfo = EachIn GetClasses()
classObject = classInfo.GetGlobal("__CLASS__", False)
If (classObject <> Null) Then
classObject.SetValue(classInfo)
End If
Next
_soundTrayTimer = 0
_soundTrayWidth = 160
_soundTrayHeight = 45
_soundTrayX = 0
_soundTrayY = -_soundTrayHeight
_soundTrayVisible = False
FlxG.Init(Self, gameSizeX, gameSizeY, zoom)
FlxG.Updaterate = updaterate
_state = Null
useSoundHotKeys = Not FlxG.Mobile
Self.useSystemCursor = useSystemCursor
forceDebugger = False
_debuggerUp = False
_replay = New FlxReplay()
_replayRequested = False
_recordingRequested = False
_replaying = False
_recording = False
_iState = initialState
_requestedState = Null
_requestedReset = True
End Method
Method OnCreate:Int()
If ( Not useSystemCursor) HideMouse()
_InitData()
_Reset()
#Rem
_soundTrayLabel = New FlxText(10, 32, _soundTrayWidth, "VOLUME")
_soundTrayLabel.SetFormat(FlxText.SYSTEM_FONT, 16, FlxG.WHITE, FlxText.ALIGN_CENTER)
#End
Return 0
End Method
Method OnUpdate:Int()
If (FlxG.Updaterate <> _updaterate) Then
_ResetFramerate()
End If
#If FLX_ASYNC_EVENTS_ENABLED
UpdateAsyncEvents()
#End
FlxG.UpdateDevice()
_Step()
#If TARGET <> "ios" And TARGET <> "android" And TARGET <> "psm"
If (useSoundHotKeys) Then
If (KeyHit(KEY_0)) Then
FlxG.Mute = Not FlxG.Mute
If (FlxG.VolumeHandler <> Null) Then
If (FlxG.Mute) Then
FlxG.VolumeHandler.OnVolumeChange(0)
Else
FlxG.VolumeHandler.OnVolumeChange(FlxG.Volume())
End If
End If
_ShowSoundTray()
End If
If (KeyHit(KEY_MINUS)) Then
FlxG.Mute = False
FlxG.Volume(FlxG.Volume() - .1)
_ShowSoundTray()
End If
If (KeyHit(KEY_EQUALS)) Then
FlxG.Mute = False
FlxG.Volume(FlxG.Volume() + .1)
_ShowSoundTray()
End If
End If
_UpdateSoundTray(_step)
#End
Return 0
End Method
Method OnRender:Int()
#If FLX_DEBUG_ENABLED
FlxBasic._VisibleCount = 0
#End
#If TARGET = "html5" And Not FLX_WEBGL_ENABLED
If ( Not _stateIsReady And _state) Then
Cls(FlxG._BgColor.r, FlxG._BgColor.g, FlxG._BgColor.b)
_state.Create()
_stateIsReady = True
Cls(FlxG._BgColor.r, FlxG._BgColor.g, FlxG._BgColor.b)
Return 0
End If
#End
_Draw()
Return 0
End Method
Method OnSuspend:Int()
ShowMouse()
FlxG.PauseSounds()
Return 0
End Method
Method OnResume:Int()
#If FLX_DEBUG_ENABLED
If (Not _debuggerUp And Not useSystemCursor) Then
HideMouse()
End If
#Else
If ( Not useSystemCursor) Then
HideMouse()
End If
#End
FlxG.ResetInput()
FlxG.ResumeSounds()
Return 0
End Method
Method OnBack:Int()
If (_state = Null Or _state.DoBack()) Then
Return Super.OnBack()
End If
Return 0
End Method
Method OnClose:Int()
If (_state = Null Or _state.DoClose()) Then
Return Super.OnClose()
End If
Return 0
End Method
Method SetSwitchStateListener:Void(listener:FlxSwitchStateListener)
_switchStateListener = listener
End Method
Method OnContentInit:Void()
End Method
Private
Method _ShowSoundTray:Void(silent:Bool = False)
If (Not silent) Then
FlxG.Play("beep" + FlxG.DATA_SUFFIX)
End If
_soundTrayTimer = 1
_soundTrayY = 0
_soundTrayVisible = True
End Method
Method _SwitchState:Void()
FlxG.ResetCameras()
FlxG.ResetInput()
FlxG.DestroySounds()
#If FLX_DEBUG_ENABLED
If (_debugger <> Null) Then
'note: TODO:!
End If
#End
Local timeManager:TimerManager = FlxTimer.Manager()
If (timeManager <> Null) timeManager.Clear()
If (_state <> Null) _state.Destroy()
If (_switchStateListener <> Null) _switchStateListener.OnSwitchState(_state, _requestedState)
_state = _requestedState
#If TARGET = "html5" And Not FLX_WEBGL_ENABLED
_stateIsReady = False
#Else
BeginRender()
Cls(FlxG._BgColor.r, FlxG._BgColor.g, FlxG._BgColor.b)
_state.Create()
Cls(FlxG._BgColor.r, FlxG._BgColor.g, FlxG._BgColor.b)
EndRender()
#End
If (FlxG.Updaterate <> _updaterate) Then
_ResetFramerate()
End If
End Method
Method _Step:Void()
If (_requestedReset) Then
_Reset()
End If
If (_recordingRequested) Then
_recordingRequested = False
_replay.Create(FlxG.GlobalSeed)
_recording = True
#If FLX_DEBUG_ENABLED
If (_debugger <> Null) Then
'note: TODO:
FlxG.Log("FLIXEL: starting new flixel gameplay record.")
End If
#End
ElseIf (_replayRequested)
_replayRequested = False
_replay.Rewind()
FlxG.GlobalSeed = _replay.seed
#If FLX_DEBUG_ENABLED
If (_debugger <> Null) Then
'note: TODO:
End If
#End
_replaying = True
End If
#If TARGET = "html5" And Not FLX_WEBGL_ENABLED
If (_state <> _requestedState) Then
_SwitchState()
Return
End If
If ( Not _stateIsReady) Return
#Else
If (_state <> _requestedState) _SwitchState()
#end
#If FLX_DEBUG_ENABLED
FlxBasic._ActiveCount = 0
#End
If (_replaying) Then
If (_replayCancelKeys.Length() > 0 And Not KeyDown(192) And Not KeyDown(220)) Then
Local i:Int = 0
Local l:Int = _replayCancelKeys.Length()
While (i < l)
If (KeyDown(_replayCancelKeys[i])) Then
If (_replayCallback <> Null) Then
_replayCallback.OnReplayComplete()
_replayCallback = Null
Else
FlxG.StopReplay()
End If
Exit
End If
i += 1
Wend
End If
_replay.PlayNextFrame()
If (_replayTimer > 0) Then
_replayTimer -= _step
If (_replayTimer <= 0) Then
If (_replayCallback <> Null) Then
_replayCallback.OnReplayComplete()
_replayCallback = Null
Else
FlxG.StopReplay()
End If
End If
End If
If (_replaying And _replay.finished) Then
FlxG.StopReplay()
If (_replayCallback <> Null) Then
_replayCallback.OnReplayComplete()
_replayCallback = Null
End If
End If
#If FLX_DEBUG_ENABLED
If (_debugger <> Null) Then
'note: TODO:
End If
#End
Else
FlxG.UpdateInput()
End If
If (_recording) Then
_replay.RecordFrame()
#If FLX_DEBUG_ENABLED
If (_debugger <> Null) Then
'note: TODO:
End If
#End
End If
_Update()
#If FLX_DEBUG_ENABLED
If (_debugger <> Null) Then
'note: TODO:
End If
#End
End Method
Method _Update:Void()
FlxG.Elapsed = FlxG.TimeScale * (_step / 1000.0)
FlxG.UpdateSounds()
FlxG.UpdatePlugins()
_state.DoUpdate()
If (FlxG.Tweener.active And FlxG.Tweener.HasTween) Then
FlxG.Tweener.UpdateTweens()
End If
FlxG.UpdateCameras()
#If FLX_DEBUG_ENABLED
If (_debuggerUp) Then
'note: TODO:!
End If
#End
End Method
Method _UpdateSoundTray:Void(ms:Int)
If (Not _soundTrayVisible) Return
If (_soundTrayTimer > 0) Then
_soundTrayTimer -= ms / 1000.0
ElseIf (_soundTrayY > -_soundTrayHeight)
_soundTrayY =_soundTrayY - (ms / 1000.0 * FlxG.Height * 2)
If (_soundTrayY <= -_soundTrayHeight) Then
_soundTrayVisible = False
'note: TODO: Save sound settings
End If
End If
End Method
Method _Draw:Void()
Cls(FlxG._BgColor.r, FlxG._BgColor.g, FlxG._BgColor.b)
#If TARGET <> "ios" And TARGET <> "android" And TARGET <> "psm" And TARGET <> "winrt"
If( Not FlxG.Mobile) Then
PushMatrix()
End If
#End
Translate(FlxG._DeviceOffsetX, FlxG._DeviceOffsetY)
Scale(FlxG._DeviceScaleFactorX, FlxG._DeviceScaleFactorY)
FlxG._LastDrawingColor = FlxG.WHITE
FlxG._LastDrawingBlend = GetBlend()
FlxG._LastDrawingAlpha = GetAlpha()
FlxG._CurrentCamera = Null
Local i:Int = 0
Local l:Int = FlxG.Cameras.Length()
While(i < l)
FlxG._CurrentCamera = FlxG.Cameras.Get(i)
If (FlxG._CurrentCamera <> Null And FlxG._CurrentCamera.active And FlxG._CurrentCamera.exists And FlxG._CurrentCamera.visible) Then
FlxG._CurrentCamera.DrawFX() 'not really draw. Only calculation
FlxG._CurrentCamera.Lock()
_state.DoDraw()
FlxG.DrawPlugins()
FlxG._CurrentCamera.Unlock()
End If
i+=1
Wend
#If TARGET <> "ios" And TARGET <> "android" And TARGET <> "psm" And TARGET <> "winrt"
If( Not FlxG.Mobile) Then
PopMatrix()
_DrawSoundTray()
FlxG.Mouse.Draw()
End If
#End
End Method
Method _DrawSoundTray:Void()
If (_soundTrayVisible) Then
Local globalVolume:Int = FlxU.Round(FlxG.Volume() * 10)
If (FlxG.Mute) globalVolume = 0
PushMatrix()
Transform(FlxG._DeviceScaleFactorX, 0, 0, FlxG._DeviceScaleFactorY, ( (FlxG.DeviceWidth - _soundTrayWidth * FlxG._DeviceScaleFactorX) * 0.5), _soundTrayY)
If (FlxG._LastDrawingAlpha <> .5) Then
SetAlpha(.5)
FlxG._LastDrawingAlpha = .5
End If
If (FlxG._LastDrawingColor <> FlxG.BLACK) Then
SetColor(0, 0, 0)
End If
DrawRect(0, 0, _soundTrayWidth, _soundTrayHeight)
SetColor(255, 255, 255)
FlxG._LastDrawingColor = FlxG.WHITE
Local bx:Int = 20
Local by:Int = 28
Local i:Int = 0
While (i < 10)
If (i < globalVolume) Then
If (FlxG._LastDrawingAlpha <> 1) Then
SetAlpha(1)
FlxG._LastDrawingAlpha = 1
End If
Else
If (FlxG._LastDrawingAlpha <> .5) Then
SetAlpha(.5)
FlxG._LastDrawingAlpha = .5
End If
End If
DrawRect(bx, by, 8, i * 2)
bx += 12
by -= 2
i += 1
Wend
'_soundTrayLabel.Draw()
PopMatrix()
End If
End Method
Method _Reset:Void()
_requestedReset = False
_requestedState = FlxState(_iState.NewInstance())
_replayTimer = 0
_replayCancelKeys =[]
If (FlxG.Updaterate <> _updaterate) Then
_ResetFramerate()
End If
Local date:Int[] = GetDate()
Seed = (date[3] * 3600 + date[4] * 60 + date[5]) * 1000 + date[6]
FlxG.UpdateDevice()
FlxG.Reset()
End Method
Method _ResetFramerate:Void()
SetUpdateRate(FlxG.Updaterate)
_step = 1000 / FlxG.Updaterate
_updaterate = FlxG.Updaterate
End Method
Method _InitData:Void()
FlxAssetsManager.Init()
Local minSystemFontSize:Int = 8
Local maxSystemFontSize:Int = 24
Local fontPathPrefix:String = FlxText.SYSTEM_FONT + "_font_"
Local system:FlxFont = FlxAssetsManager.AddFont(FlxText.SYSTEM_FONT)
For Local size:Int = minSystemFontSize To maxSystemFontSize
system.SetPath(size, fontPathPrefix + size + FlxG.DATA_SUFFIX)
Next
FlxAssetsManager.AddImage("default" + FlxG.DATA_SUFFIX, "default" + FlxG.DATA_SUFFIX + ".png")
FlxAssetsManager.AddImage("button" + FlxG.DATA_SUFFIX, "button" + FlxG.DATA_SUFFIX + ".png")
FlxAssetsManager.AddImage(FlxTilemap.AUTOTILES, FlxTilemap.AUTOTILES + ".png")
FlxAssetsManager.AddImage(FlxTilemap.AUTOTILES_ALT, FlxTilemap.AUTOTILES_ALT + ".png")
FlxAssetsManager.AddCursor("cursor" + FlxG.DATA_SUFFIX, "cursor" + FlxG.DATA_SUFFIX + ".png")
FlxAssetsManager.AddSound("beep" + FlxG.DATA_SUFFIX, "beep" + FlxG.DATA_SUFFIX + "." + FlxSound.GetValidExt())
Local contentInitTrigger:FunctionInfo
For Local classInfo:ClassInfo = EachIn GetClasses()
contentInitTrigger = classInfo.GetFunction("OnContentInit",[], False)
If (contentInitTrigger <> Null) contentInitTrigger.Invoke([])
Next
Self.OnContentInit()
End Method
End Class