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
/
flxcamera.monkey
535 lines (399 loc) · 11.9 KB
/
flxcamera.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
Strict
Import flxextern
Import flxpoint
Import flxrect
Import flxbasic
Import flxobject
Import flxg
Import system.flxcolor
Class FlxCamera Extends FlxBasic
Global __CLASS__:Object
Const STYLE_LOCKON:Int = 0
Const STYLE_PLATFORMER:Int = 1
Const STYLE_TOPDOWN:Int = 2
Const STYLE_TOPDOWN_TIGHT:Int = 3
Const SHAKE_BOTH_AXES:Int = 0
Const SHAKE_HORIZONTAL_ONLY:Int = 1
Const SHAKE_VERTICAL_ONLY:Int = 2
Global DefaultZoom:Float
Field target:FlxObject
Field deadzone:FlxRect
Field bounds:FlxRect
Field scroll:FlxPoint
Field _color:FlxColor
Private
Const _MIN_FLOAT_VALUE:Float = .0000001
Field _x:Float
Field _y:Float
Field _realX:Float
Field _realY:Float
Field _width:Float
Field _height:Float
Field _realWidth:Float
Field _realHeight:Float
Field _zoom:Float
Field _point:FlxPoint
Field _alpha:Float
Field _scaleX:Float
Field _scaleY:Float
Field _angle:Float
Field _clipped:Bool
Field _bgColor:FlxColor
Field _fxFlashColor:Int
Field _fxFlashDuration:Float
Field _fxFlashComplete:FlxCameraFlashListener
Field _fxFlashAlpha:Float
Field _fxFadeColor:Int
Field _fxFadeDuration:Float
Field _fxFadeComplete:FlxCameraFadeListener
Field _fxFadeAlpha:Float
Field _fxShakeIntensity:Float
Field _fxShakeDuration:Float
Field _fxShakeComplete:FlxCameraShakeListener
Field _fxShakeOffset:FlxPoint
Field _fxShakeDirection:Int
Field _fill:FlxColor
Public
Method New(x:Int, y:Int, width:Int, height:Int, zoom:Float = 0)
Zoom = zoom 'must be first for init
X = x
Y = y
Width = width
Height = height
target = Null
deadzone = Null
scroll = New FlxPoint()
_point = New FlxPoint()
bounds = Null
_bgColor = New FlxColor(FlxG.BgColor())
_color = New FlxColor()
_alpha = 1
_fxFlashColor = 0
_fxFlashDuration = 0
_fxFlashComplete = null
_fxFlashAlpha = 0
_fxFadeColor = 0
_fxFadeDuration = 0
_fxFadeComplete = Null
_fxFadeAlpha = 0
_fxShakeIntensity = 0
_fxShakeDuration = 0
_fxShakeComplete = Null
_fxShakeOffset = New FlxPoint()
_fxShakeDirection = 0
active = True
_fill = New FlxColor(0)
End Method
Method Destroy:Void()
target = Null
scroll = Null
deadzone = Null
bounds = Null
_color = Null
_bgColor = Null
_fxFlashComplete = Null
_fxFadeComplete = Null
_fxShakeComplete = Null
_fxShakeOffset = Null
_fill = Null
Super.Destroy()
End Method
Method Lock:Void()
If (_clipped) Then
If (_fxShakeOffset.x <> 0 Or _fxShakeOffset.y <> 0) Then
SetScissor(Ceil(_realX + _fxShakeOffset.x * FlxG._DeviceScaleFactorX), Ceil(_realY + _fxShakeOffset.y * FlxG._DeviceScaleFactorY), _realWidth, _realHeight)
Else
SetScissor(_realX, _realY, _realWidth, _realHeight)
End If
End If
PushMatrix()
Translate(Ceil(_x + _fxShakeOffset.x), Ceil(_y + _fxShakeOffset.y))
Scale(_scaleX, _scaleY)
If (_clipped Or _bgColor.argb <> FlxG._BgColor.argb)
SetColor(_bgColor.r, _bgColor.g, _bgColor.b)
DrawRect(0, 0, _width, _height)
FlxG._LastDrawingColor = _bgColor.argb
End If
End Method
Method Unlock:Void()
If (_fill.argb <> 0) Then
If (_fill.argb <> FlxG._LastDrawingColor) Then
SetColor(_fill.r, _fill.g, _fill.b)
FlxG._LastDrawingColor = _fill.argb
End if
If (FlxG._LastDrawingAlpha <> _fill.a) Then
SetAlpha(_fill.a)
FlxG._LastDrawingAlpha = _fill.a
End If
DrawRect( -1, - 1, _width + 2, _height + 2)
End If
PopMatrix()
_fill.SetARGB(0)
If (_clipped) Then
SetScissor(0, 0, FlxG.DeviceWidth, FlxG.DeviceHeight)
End If
End Method
Method Update:Void()
If (target <> Null) Then
If (deadzone = Null) Then
FocusOn(target.GetMidpoint(_point))
Else
Local edge:Float
Local targetX:Float
Local targetY:Float
If (target.x > 0) Then
targetX = target.x + 0.0000001
Else
targetX = target.x - 0.0000001
End If
If (target.y > 0) Then
targetY = target.y + 0.0000001
Else
targetY = target.y - 0.0000001
End If
edge = targetX - deadzone.x
If (scroll.x > edge) scroll.x = edge
edge = targetX + target.width - deadzone.x - deadzone.width
If (scroll.x < edge) scroll.x = edge
edge = targetY - deadzone.y
If (scroll.y > edge) scroll.y = edge
edge = targetY + target.height - deadzone.y - deadzone.height
If (scroll.y < edge) scroll.y = edge
End If
End If
If (bounds <> Null) Then
If (scroll.x < bounds.Left) scroll.x = bounds.Left
If (scroll.x > bounds.Right - _width) scroll.x = bounds.Right - _width
If (scroll.y < bounds.Top) scroll.y = bounds.Top
If (scroll.y > bounds.Bottom - _height) scroll.y = bounds.Bottom - _height
End If
If (_fxFlashAlpha > 0) Then
_fxFlashAlpha -= FlxG.Elapsed / _fxFlashDuration
If (_fxFlashAlpha <= 0 And _fxFlashComplete <> Null) Then
_fxFlashComplete.OnCameraFlashComplete(Self)
_fxFlashComplete = Null
End If
End If
If (_fxFadeAlpha > 0 And _fxFadeAlpha < 1) Then
_fxFadeAlpha += FlxG.Elapsed / _fxFadeDuration
If (_fxFadeAlpha >= 1) Then
_fxFadeAlpha = 1
If (_fxFadeComplete <> Null) Then
_fxFadeComplete.OnCameraFadeComplete(Self)
_fxFadeComplete = Null
End If
End If
End If
If (_fxShakeDuration > 0) Then
_fxShakeDuration -= FlxG.Elapsed
If (_fxShakeDuration <= 0) Then
_fxShakeOffset.Make()
If (_fxShakeComplete <> Null) Then
_fxShakeComplete.OnCameraShakeComplete(Self)
_fxShakeComplete = Null
End If
Else
If (_fxShakeDirection = SHAKE_BOTH_AXES Or _fxShakeDirection = SHAKE_HORIZONTAL_ONLY) Then
_fxShakeOffset.x = (FlxG.Random() * _fxShakeIntensity * _width * 2 - _fxShakeIntensity * _width) * _zoom
End If
If (_fxShakeDirection = SHAKE_BOTH_AXES Or _fxShakeDirection = SHAKE_VERTICAL_ONLY) Then
_fxShakeOffset.y = (FlxG.Random() * _fxShakeIntensity * _height * 2 - _fxShakeIntensity * _height) * _zoom
End If
End If
End If
End Method
Method Follow:Void(target:FlxObject, style:Int = STYLE_LOCKON)
Self.target = target
Local helper:Float
Select (style)
Case STYLE_PLATFORMER
Local w:Float = _width / 8
Local h:Float = _height / 3
deadzone = New FlxRect((_width - w) / 2, (_height - h) / 2 - h * .25, w, h)
Case STYLE_TOPDOWN
helper = Max(_width, _height) / 4
deadzone = New FlxRect((_width - helper) / 2, (_height - helper) / 2, helper, helper)
Case STYLE_TOPDOWN_TIGHT
helper = Max(_width, _height) / 8
deadzone = New FlxRect((_width - helper) / 2, (_height - helper) / 2, helper, helper)
Default
deadzone = Null
End Select
End Method
Method FocusOn:Void(point:FlxPoint)
If (point.x > 0) Then
point.x += 0.0000001
Else
point.x -= 0.0000001
End If
If (point.y > 0) Then
point.y += 0.0000001
Else
point.y -= 0.0000001
End If
scroll.Make(point.x - _width * .5, point.y - _height * .5)
End Method
Method SetBounds:Void(x:Float = 0, y:Float = 0, width:Float = 0, height:Float = 0, updateWorld:Bool = False)
If (bounds = Null) bounds = New FlxRect()
bounds.Make(x, y, width, height)
If (updateWorld) FlxG.WorldBounds.CopyFrom(bounds)
Update()
End Method
Method Flash:Void(color:Int = FlxG.WHITE, duration:Float = 1, onComplete:FlxCameraFlashListener = Null, force:Bool = False)
If (Not force And _fxFlashAlpha > 0) Return
_fxFlashColor = color
If (duration <= 0) duration = _MIN_FLOAT_VALUE
_fxFlashDuration = duration
_fxFlashComplete = onComplete
_fxFlashAlpha = 1
End Method
Method Fade:Void(color:Int = FlxG.BLACK, duration:Float = 1, onComplete:FlxCameraFadeListener = Null, force:Bool = False)
If (Not force And _fxFadeAlpha > 0) Return
_fxFadeColor = color
If (duration <= 0) duration = _MIN_FLOAT_VALUE
_fxFadeDuration = duration
_fxFadeComplete = onComplete
_fxFadeAlpha = _MIN_FLOAT_VALUE
End Method
Method Shake:Void(intensity:Float = 0.05, duration:Float = 0.5, onComplete:FlxCameraShakeListener = Null, force:Bool = True, direction:Int = SHAKE_BOTH_AXES)
If (Not force And (_fxShakeOffset.x <> 0 Or _fxShakeOffset.y <> 0)) Return
_fxShakeIntensity = intensity
_fxShakeDuration = duration
_fxShakeComplete = onComplete
_fxShakeDirection = direction
_fxShakeOffset.Make()
End Method
Method StopFX:Void()
_fxFlashAlpha = 0
_fxFadeAlpha = 0
_fxShakeDuration = 0
_fxShakeOffset.x = 0
_fxShakeOffset.y = 0
End Method
Method CopyFrom:FlxCamera(camera:FlxCamera)
If (camera.bounds = Null) Then
bounds = camera.bounds
Else
If (bounds = Null) bounds = New FlxRect()
bounds.CopyFrom(camera.bounds)
End If
target = camera.target
If (target <> Null) Then
If (camera.deadzone = Null) Then
deadzone = Null
Else
If (deadzone = Null) deadzone = New FlxRect()
deadzone.CopyFrom(camera.deadzone)
End If
End If
Return Self
End Method
Method X:Float() Property
Return _x
End Method
Method X:Void(x:Float) Property
_x = x
_realX = Ceil(FlxG._DeviceOffsetX + _x * FlxG._DeviceScaleFactorX)
_clipped = _IsClipped()
End Method
Method Y:Float() Property
Return _y
End Method
Method Y:Void(y:Float) Property
_y = y
_realY = Ceil(FlxG._DeviceOffsetY + _y * FlxG._DeviceScaleFactorY)
_clipped = _IsClipped()
End Method
Method Width:Float() Property
Return _width
End Method
Method Width:Void(width:Float) Property
_width = width
_realWidth = Floor(_width * _scaleX * FlxG._DeviceScaleFactorX)
_clipped = _IsClipped()
End Method
Method Height:Float() Property
Return _height
End Method
Method Height:Void(height:Float) Property
_height = height
_realHeight = Floor(_height * _scaleY * FlxG._DeviceScaleFactorY)
_clipped = _IsClipped()
End Method
Method Zoom:Float() Property
Return _zoom
End Method
Method Zoom:Void(zoom:Float) Property
If (zoom = 0) Then
_zoom = FlxCamera.DefaultZoom
Else
_zoom = zoom
End If
SetScale(_zoom, _zoom)
End Method
Method Alpha:Float() Property
Return _alpha
End Method
Method Alpha:Void(alpha:Float) Property
_alpha = alpha
End Method
Method Angle:Float() Property
Return _angle
End Method
Method Angle:Void(angle:Float) Property
_angle = angle
End Method
Method Color:Int() Property
Return _color.argb
End Method
Method Color:Void(color:Int) Property
_color.SetRGB(color)
End Method
Method BgColor:Int() Property
Return _bgColor.argb
End Method
Method BgColor:Void(color:Int) Property
_bgColor.SetARGB(color)
End Method
Method GetScale:FlxPoint()
Return _point.Make(_scaleX, _scaleY)
End Method
Method SetScale:Void(x:Float, y:Float)
_scaleX = x
_scaleY = y
_realWidth = Floor(_width * _scaleX * FlxG._DeviceScaleFactorX)
_realHeight = Floor(_height * _scaleY * FlxG._DeviceScaleFactorY)
End Method
Method Fill:Void(color:Int, blendAlpha:Bool = True)
If (blendAlpha) Then
_fill.SetARGB(color)
Else
_fill.SetRGB(color)
End If
End Method
Method DrawFX:Void()
If (_fxFlashAlpha > 0) Then
Local alphaComponent:Float = _fxFlashColor Shr 24
If (alphaComponent <= 0) alphaComponent = $FF
Fill(((alphaComponent * _fxFlashAlpha) Shl 24) + (_fxFlashColor & $00FFFFFF))
End If
If (_fxFadeAlpha > 0) Then
Local alphaComponent:Float = _fxFadeColor Shr 24
If (alphaComponent <= 0) alphaComponent = $FF
Fill(((alphaComponent * _fxFadeAlpha) Shl 24) + (_fxFadeColor & $00FFFFFF))
End If
End Method
Private
Method _IsClipped:Bool()
Return _realX <> 0 Or _realY <> 0 Or _realWidth <> FlxG.DeviceWidth Or _realHeight <> FlxG.DeviceHeight
End Method
End Class
Interface FlxCameraFlashListener
Method OnCameraFlashComplete:Void(camera:FlxCamera)
End Interface
Interface FlxCameraFadeListener
Method OnCameraFadeComplete:Void(camera:FlxCamera)
End Interface
Interface FlxCameraShakeListener
Method OnCameraShakeComplete:Void(camera:FlxCamera)
End Interface