This repository was 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
Expand file tree
/
Copy pathflxsprite.monkey
More file actions
652 lines (485 loc) · 13.4 KB
/
flxsprite.monkey
File metadata and controls
652 lines (485 loc) · 13.4 KB
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
Strict
Import mojo
Import flxextern
Import flxpoint
Import flxobject
Import flxcamera
Import flxg
Import system.flxanim
Import system.flxcolor
Import "data/default_flx.png"
Class FlxSprite Extends FlxObject
Global __CLASS__:Object
Field origin:FlxPoint
Field offset:FlxPoint
Field scale:FlxPoint
Field blend:Int
Field finished:Bool
Field frameWidth:Int
Field frameHeight:Int
Field frames:Int
Field dirty:Bool
Field _color:FlxColor
Field _mixedColor:FlxColor
Field _camera:FlxCamera
Private
Global _Matrix:Float[6]
Field _animations:StringMap<FlxAnim>
Field _flipped:Bool
Field _flipNeeded:Bool
Field _curAnim:FlxAnim
Field _curFrame:Int
Field _curIndex:Int
Field _frameTimer:Float
Field _paused:Bool
Field _callback:FlxAnimationListener
Field _facing:Int
Field _alpha:Float
Field _bakedRotation:Float
Field _pixels:Image
Field _halfWidth:Float
Field _halfHeight:Float
Field _renderer:FlxSpriteRenderer
Public
Method New(x:Float = 0, y:Float = 0, simpleGraphic:String = "")
Super.New(x, y)
offset = New FlxPoint()
origin = New FlxPoint()
scale = New FlxPoint(1.0, 1.0)
_alpha = 1
_color = New FlxColor(FlxG.WHITE)
blend = AlphaBlend
_cameras = Null
finished = False
_facing = RIGHT
_animations = New StringMap<FlxAnim>
_flipped = False
_curAnim = Null
_curFrame = 0
_curIndex = 0
_frameTimer = 0
_paused = False
_callback = Null
_mixedColor = New FlxColor(FlxG.WHITE)
If (simpleGraphic.Length() = 0)
simpleGraphic = "default" + FlxG.DATA_SUFFIX
End If
LoadGraphic(simpleGraphic)
End Method
Method Destroy:Void()
If (_animations <> Null) Then
_animations.Clear()
_animations = Null
End If
offset = Null
origin = Null
scale = Null
_curAnim = Null
_callback = Null
_color = Null
_mixedColor = Null
_camera = Null
_renderer = Null
Super.Destroy()
End Method
Method LoadGraphic:FlxSprite(graphic:String, animated:Bool = False, reverse:Bool = False, width:Int = 0, height:Int = 0, unique:Bool = False)
_bakedRotation = 0
_flipped = reverse
_pixels = FlxG.AddBitmap(New ImageResource(graphic, animated, width, height), unique)
Self.width = _pixels.Width()
frameWidth = Self.width
Self.height = _pixels.Height()
frameHeight = Self.height
ResetHelpers()
Return Self
End Method
Method LoadRotatedGraphic:FlxSprite(graphic:String, rotations:Int = 16, frame:Int = -1)
Error "LoadRotatedGraphic not currenlty support in " + FlxG.LIBRARY_NAME
'note: TODO:
Return Null
End Method
Method MakeGraphic:FlxSprite(width:Int, height:Int, color:Int = FlxG.WHITE, unique:Bool = False, key:String = "")
_pixels = FlxG.CreateBitmap(width, height, color, unique, key)
_bakedRotation = 0
Self.width = width
frameWidth = width
Self.height = height
frameHeight = height
ResetHelpers()
Return Self
End Method
Method PostUpdate:Void()
Super.PostUpdate()
_UpdateAnimation()
End Method
Method Draw:Void()
If (_flickerTimer <> 0) Then
_flicker = Not _flicker
If (_flicker) Return
End If
_camera = FlxG._CurrentCamera
If (_cameras <> Null And Not _cameras.Contains(_camera)) Return
If (Not OnScreen(_camera)) Return
If (dirty) _CalcFrame()
If (FlxG._LastDrawingBlend <> blend) Then
SetBlend(blend)
FlxG._LastDrawingBlend = blend
End If
_point.x = x - int(_camera.scroll.x * scrollFactor.x) - offset.x
_point.y = y - int(_camera.scroll.y * scrollFactor.y) - offset.y
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
If (_camera.Color <> FlxG.WHITE) Then
_mixedColor.MixRGB(_color, _camera._color)
If (FlxG._LastDrawingColor <> _mixedColor.argb) Then
SetColor(_mixedColor.r, _mixedColor.g, _mixedColor.b)
FlxG._LastDrawingColor = _mixedColor.argb
End If
Else
If (FlxG._LastDrawingColor <> _color.argb) Then
SetColor(_color.r, _color.g, _color.b)
FlxG._LastDrawingColor = _color.argb
End If
End If
If (_camera.Alpha < 1) Then
Local _mixedAlpha:Float = _camera.Alpha * _alpha
If (FlxG._LastDrawingAlpha <> _mixedAlpha) Then
SetAlpha(_mixedAlpha)
FlxG._LastDrawingAlpha = _mixedAlpha
End If
Else
If (FlxG._LastDrawingAlpha <> _alpha) Then
SetAlpha(_alpha)
FlxG._LastDrawingAlpha = _alpha
End If
End If
If ((angle = 0 Or _bakedRotation > 0) And scale.x = 1 And scale.y = 1) Then
If ( Not _flipNeeded) Then
If (_renderer = Null) Then
DrawImage(_pixels, _point.x, _point.y, _curIndex)
Else
_renderer.OnSpriteRender(_point.x, _point.y)
End If
Else
PushMatrix()
Transform(-1, 0, 0, 1, _point.x + _halfWidth, _point.y + _halfHeight)
If (_renderer = Null) Then
DrawImage(_pixels, -_halfWidth, -_halfHeight, _curIndex)
Else
_renderer.OnSpriteRender(-_halfWidth, -_halfHeight)
End If
PopMatrix()
End If
Else
PushMatrix()
'Translate
_Matrix[4] = _point.x + origin.x
_Matrix[5] = _point.y + origin.y
'Scale
_Matrix[0] = scale.x
_Matrix[3] = scale.y
'Rotate
If (angle <> 0 And _bakedRotation = 0) Then
Local sin:Float = Sin(angle)
Local cos:Float = Cos(angle)
_Matrix[1] = sin * _Matrix[0]
_Matrix[2] = -sin * _Matrix[3]
_Matrix[0] *= cos
_Matrix[3] *= cos
Else
_Matrix[1] = 0
_Matrix[2] = 0
End If
Transform(_Matrix[0], _Matrix[1], _Matrix[2], _Matrix[3], _Matrix[4], _Matrix[5])
If (_flipNeeded) Then
Transform(-1, 0, 0, 1, _halfWidth - origin.x, _halfHeight - origin.y)
End If
If (_renderer = Null) Then
DrawImage(_pixels, -origin.x, -origin.y, _curIndex)
Else
_renderer.OnSpriteRender(-origin.x, -origin.y)
End If
PopMatrix()
End If
#If FLX_DEBUG_ENABLED
_VisibleCount += 1
If (FlxG.VisualDebug And Not ignoreDrawDebug) DrawDebug(_camera)
#End
End Method
Method DrawFrame:Void(force:Bool = False)
If (force Or dirty) _CalcFrame()
End Method
Method AddAnimation:FlxAnim(name:String, frames:Int[], frameRate:Float = 0, looped:Bool = True)
Local anim:FlxAnim = New FlxAnim(name, frames, frameRate, looped)
_animations.Set(name, anim)
Return anim
End Method
Method AddAnimation:FlxAnim(name:String, startFrame:Int, endFrame:Int, frameRate:Float = 0, looped:Bool = True)
Local i:Int = 0
Local l:Int = Abs(endFrame - startFrame) + 1
Local frames:Int[] = New Int[l]
Local dir:Int = 1
If (endFrame < startFrame) dir = -1
While (i < l)
frames[i] = startFrame + dir * i
i += 1
Wend
Return AddAnimation(name, frames, frameRate, looped)
End Method
Method AddAnimation:FlxAnim(name:String, frameRate:Float = 0, looped:Bool = True)
If (_pixels = Null) Return Null
Return AddAnimation(name, 0, _pixels.Frames() -1, frameRate, looped)
End Method
Method AddAnimationCallback:Void(animationCallback:FlxAnimationListener)
_callback = animationCallback
End Method
Method ClearAnimationCallback:Void()
_callback = Null
End Method
Method GetAnimation:FlxAnim(animName:String)
Return _animations.Get(animName)
End Method
Method GetAnimation:FlxAnim()
Return _curAnim
End Method
Method Play:Void(animName:String, force:Bool = False)
If (Not force And _curAnim <> Null And animName = _curAnim.name And( _curAnim.looped Or Not finished )) Return
_curFrame = 0
_curIndex = 0
_frameTimer = 0
_paused = False
Local anim:FlxAnim = _animations.Get(animName)
If (anim <> Null) Then
_curAnim = anim
If (_curAnim.delay <= 0) Then
finished = True
Else
finished = False
End If
_curIndex = _curAnim.frames[_curFrame]
dirty = True
Return
End If
#If FLX_DEBUG_ENABLED
FlxG.Log("WARNING: No animation called ~q" + animName + "~q")
#End
End Method
Method Stop:Void()
Frame = 0
End Method
Method Pause:Void()
_paused = True
End Method
Method Resume:Void()
_paused = False
End Method
Method RandomFrame:Void()
_curAnim = Null
If (_pixels <> Null) Then
_curIndex = Int(FlxG.Random() * (_pixels.Frames() - 1))
Else
_curIndex = 0
End If
dirty = True
End Method
Method SetOriginToCorner:Void()
origin.x = 0
origin.y = 0
End Method
Method CenterOffsets:Void(adjustPosition:Bool = False)
offset.x = (frameWidth - width) * .5
offset.y = (frameHeight - height) * .5
If (adjustPosition) Then
x += offset.x
y += offset.y
End If
End Method
Method Pixels:Image() Property
Return _pixels
End Method
Method Pixels:Void(pixels:Image) Property
_pixels = pixels
Local width:Int = 0
Local height:Int = 0
If (_pixels <> Null) Then
width = pixels.Width()
height = pixels.Height()
End If
If (_pixels <> Null Or _renderer = Null) Then
Self.width = width
Self.height = height
frameWidth = width
frameHeight = height
ResetHelpers()
End If
End Method
Method Facing:Int() Property
Return _facing
End Method
Method Facing:Void(direction:Int) Property
If (_facing <> direction) dirty = True
_facing = direction
_flipNeeded = (_flipped And _facing = LEFT)
End Method
Method Alpha:Float() Property
Return _alpha
End Method
Method Alpha:Void(alpha:Float) Property
If (alpha < 0) alpha = 0
_alpha = alpha
End Method
Method Color:Int() Property
Return _color.argb
End Method
Method Color:Void(color:Int) Property
_color.SetRGB(color)
End Method
Method Frame:Int() Property
Return _curIndex
End Method
Method Frame:Void(frame:Int) Property
_curAnim = Null
_curIndex = frame
_paused = False
dirty = True
End Method
Method OnScreen:Bool(camera:FlxCamera = Null)
If (camera = Null) camera = FlxG.Camera
GetScreenXY(_point, camera)
_point.x = _point.x - offset.x
_point.y = _point.y - offset.y
If ((angle = 0 Or _bakedRotation > 0) And scale.x = 1 And scale.y = 1) Then
Return _point.x + frameWidth > 0 And _point.x < camera.Width And _point.y + frameHeight > 0 And _point.y < camera.Height
End If
Local radius:Float = Sqrt(_halfWidth * _halfWidth + _halfHeight * _halfHeight) * Max(Abs(scale.x), Abs(scale.y))
_point.x += _halfWidth
_point.y += _halfHeight
Return _point.x + radius > 0 And _point.x - radius < camera.Width And _point.y + radius > 0 And _point.y - radius < camera.Height
End Method
Method SetRenderer:Void(render:FlxSpriteRenderer)
_renderer = render
_renderer.OnSpriteBind(Self)
frameWidth = width
frameHeight = height
ResetHelpers()
End Method
Method ClearRenderer:Void()
Local renderer:FlxSpriteRenderer = _renderer
_renderer = Null
renderer.OnSpriteUnbind()
frameWidth = width
frameHeight = height
ResetHelpers()
End Method
Method ResetHelpers:Void()
_halfWidth = frameWidth * .5
_halfHeight = frameHeight * .5
origin.Make(_halfWidth, _halfHeight)
If (_pixels <> Null) Then
frames = _pixels.Frames()
Else
frames = 1
End If
_curIndex = 0
_mixedColor.SetARGB(FlxG.WHITE)
End Method
Private
Method _UpdateAnimation:Void()
If (_bakedRotation > 0) Then
Local oldIndex:Int = _curIndex
Local angleHelper:Int = angle Mod 360
If (angleHelper < 0) angleHelper += 360
_curIndex = angleHelper / _bakedRotation + 0.5
If (oldIndex <> _curIndex) _CalcFrame()
Return
End If
If (_paused) Return
If (_curAnim <> Null And _curAnim.delay > 0 And (_curAnim.looped Or Not finished))
_frameTimer += FlxG.Elapsed
While (_frameTimer > _curAnim.delay)
_frameTimer -= _curAnim.delay
If (_curFrame = _curAnim.frames.Length() - 1) Then
If (_curAnim.looped) _curFrame = 0
finished = True
Else
_curFrame += 1
End If
_curIndex = _curAnim.frames[_curFrame]
dirty = True
Wend
End If
If (dirty) _CalcFrame()
End Method
Method _CalcFrame:Void()
If (_callback <> Null) Then
If (_curAnim <> Null) Then
_callback.OnAnimationFrame(_curAnim, _curFrame, _curIndex)
Else
_callback.OnAnimationFrame(Null, _curFrame, _curIndex)
End If
End If
dirty = False
End Method
End Class
Interface FlxSpriteRenderer
Method OnSpriteBind:Void(sprite:FlxSprite)
Method OnSpriteUnbind:Void()
Method OnSpriteRender:Void(x:Float, y:Float)
End Interface
Private
Class ImageResource Extends FlxResource<Image>
Field animated:Bool
Field width:Float
Field height:Float
Field atlas:Image
Field image:Image
Method New(name:String, animated:Bool, width:Float, height:Float)
Super.New(name)
Self.animated = animated
Self.width = width
Self.height = height
End Method
Method Load:Image()
image = LoadImage(FlxAssetsManager.GetImagePath(name))
If (Not animated) Then
Return image
Else
Local frames:Int
If (width = 0 And height = 0) Then
width = image.Height()
ElseIf (width = 0) Then
width = height
ElseIf (height = 0) Then
height = width
End If
If (height = 0) Then
frames = Ceil(image.Width() / width)
height = width
Else
frames = Ceil((image.Width() * image.Height()) / (width * height))
End If
atlas = image
image = atlas.GrabImage(0, 0, width, height, frames)
Return image
End If
End Method
Method Use:Image()
Return image
End Method
Method Discard:Void()
If (atlas <> Null) Then
atlas.Discard()
atlas = Null
End If
image.Discard()
image = Null
End Method
End Class