11package funkin .objects ;
22
3+ import animate .FlxAnimate ;
4+ import animate .FlxAnimateFrames ;
5+ import flixel .animation .FlxAnimation ;
36import flixel .group .FlxSpriteGroup .FlxTypedSpriteGroup ;
47import flixel .util .FlxSignal .FlxTypedSignal ;
5- import flxanimate .FlxAnimate ;
68import funkin .structures .ObjectStructure ;
79
10+ using funkin .util .FlxAnimateUtil ;
11+
812/**
913 * This is a sprite class that adds on to the already existing FlxSprite.
1014 */
11- class FunkinSprite extends FlxSprite
15+ class FunkinSprite extends FlxAnimate
1216{
1317 /**
1418 * Draws this `FunkinSprite`, but invisible.
@@ -17,15 +21,20 @@ class FunkinSprite extends FlxSprite
1721 public var doInvisibleDraw : Bool = false ;
1822
1923 /**
20- * The Animate Atlas Object if the animation is one .
24+ * Settings to use when initializing texture atlases .
2125 */
22- public var atlas : FlxAnimate ;
26+ public var atlasSettings : FlxAnimateSettings = {} ;
2327
2428 public function new (x : Float = 0 , y : Float = 0 )
2529 {
2630 super (x , y );
2731 }
2832
33+ override function initVars (): Void
34+ {
35+ super .initVars ();
36+ }
37+
2938 /**
3039 * Loads or creates a texture and applies it to this sprite.
3140 * @param path The asset path. (If `path` starts with a **#** then a color will be made instead and rectWidth + rectHeight will determine its size.)
@@ -57,58 +66,29 @@ class FunkinSprite extends FlxSprite
5766 */
5867 public function loadFrames (path : String , ? forcedType : Null <String >): FunkinSprite
5968 {
60- if (atlas != null )
61- {
62- atlas .destroy ();
63- atlas = null ;
64- }
65-
66- // TODO: make forcedType work, im too lazy
67-
6869 if (Paths .location .exists (path + ' .xml' ))
6970 {
7071 frames = Paths .content .sparrowAtlas (path );
7172 }
7273 else if (Paths .location .exists (path + ' /Animation.json' ))
7374 {
74- atlas = new FlxAnimate (0 , 0 , Paths .location .get (path ), {
75- ShowPivot : false
76- });
75+ frames = Paths .content .animateAtlas (path , atlasSettings );
7776 }
7877
7978 return this ;
8079 }
8180
82- override public function destroy (): Void
83- {
84- if (atlas != null )
85- atlas .destroy ();
86-
87- super .destroy ();
88- }
89-
9081 override public function draw (): Void
9182 {
9283 var oldAlpha : Float = alpha ;
9384 if (doInvisibleDraw )
9485 alpha = 0.0001 ;
9586
96- if (atlas != null )
97- {
98- updateAtlasDummy ();
99- atlas .draw ();
100- }
101- else
102- {
103- super .draw ();
104- }
87+ super .draw ();
10588
10689 if (doInvisibleDraw )
10790 {
10891 alpha = oldAlpha ;
109-
110- if (atlas != null )
111- atlas .alpha = alpha ;
11292 }
11393 }
11494
@@ -122,37 +102,6 @@ class FunkinSprite extends FlxSprite
122102 }
123103 #end
124104
125- override public function update (elapsed : Float ): Void
126- {
127- if (atlas != null )
128- atlas .update (elapsed );
129-
130- super .update (elapsed );
131- }
132-
133- /**
134- * Updates the Atlas dummy's values, so it looks like it belongs to this sprite.
135- * @see The Original Code: https://github.com/CodenameCrew/CodenameEngine/blob/f6deda2c84984202effdfc5f6b577c2d956aa7b5/source/funkin/backend/FunkinSprite.hx#L209C2-L232C3
136- */
137- @:privateAccess
138- public function updateAtlasDummy (): Void
139- {
140- atlas .cameras = cameras ;
141- atlas .scrollFactor = scrollFactor ;
142- atlas .scale = scale ;
143- atlas .offset = offset ;
144- atlas .x = x ;
145- atlas .y = y ;
146- atlas .angle = angle ;
147- atlas .alpha = alpha ;
148- atlas .visible = visible ;
149- atlas .flipX = flipX ;
150- atlas .flipY = flipY ;
151- atlas .shader = shader ;
152- atlas .antialiasing = antialiasing ;
153- atlas .colorTransform = colorTransform ;
154- }
155-
156105 // ANIMATION BINDINGS
157106
158107 /**
@@ -174,11 +123,7 @@ class FunkinSprite extends FlxSprite
174123 if (animationStunned )
175124 return ;
176125
177- if (atlas != null )
178- atlas .anim .play (name , restart , reversed );
179- else
180- animation .play (name , restart , reversed );
181-
126+ animation .play (name , restart , reversed );
182127 animationStunned = stunAnimations ;
183128 currentAnim = name ;
184129 }
@@ -193,20 +138,18 @@ class FunkinSprite extends FlxSprite
193138 */
194139 public function addAnimation (name : String , anim : String , ? indices : Array <Int > = null , ? frameRate : Float = 24 , ? looped : Bool = true ): Void
195140 {
196- if (atlas != null )
141+ var atlasAnimList : Array <String > = super .getAnimateAnimations ();
142+
143+ if (atlasAnimList .contains (anim ))
197144 {
198- if (indices != null && indices .length > 0 )
199- atlas .anim .addBySymbolIndices (name , anim + ' \\ ' , indices , frameRate , looped );
200- else
201- atlas .anim .addBySymbol (name , anim + ' \\ ' , frameRate , looped );
145+ super .addAnimateAtlasAnimation (name , anim , indices , frameRate , looped );
146+ return ;
202147 }
148+
149+ if (indices != null && indices .length > 0 )
150+ animation .addByIndices (name , anim + ' 0' , indices , ' ' , frameRate , looped );
203151 else
204- {
205- if (indices != null && indices .length > 0 )
206- animation .addByIndices (name , anim + ' 0' , indices , ' ' , frameRate , looped );
207- else
208- animation .addByPrefix (name , anim + ' 0' , frameRate , looped );
209- }
152+ animation .addByPrefix (name , anim + ' 0' , frameRate , looped );
210153 }
211154
212155 /**
@@ -216,7 +159,7 @@ class FunkinSprite extends FlxSprite
216159
217160 function get_animationIsNull (): Bool
218161 {
219- return ( atlas != null ) ? atlas . anim . curSymbol == null : animation .curAnim == null ;
162+ return animation .curAnim == null ;
220163 }
221164
222165 /**
@@ -226,10 +169,7 @@ class FunkinSprite extends FlxSprite
226169
227170 function get_animFinished (): Bool
228171 {
229- if (animationIsNull )
230- return false ;
231-
232- return ((atlas != null ) ? atlas .anim .finished : animation .curAnim .finished ) ?? false ;
172+ return animation ?. curAnim ?. finished ?? false ;
233173 }
234174
235175 /**
@@ -240,10 +180,7 @@ class FunkinSprite extends FlxSprite
240180 if (animationIsNull )
241181 return ;
242182
243- if (atlas != null )
244- atlas .anim .curFrame = atlas .anim .length - 1 ;
245- else
246- animation .curAnim .finish ();
183+ animation .curAnim .finish ();
247184 }
248185
249186 /**
@@ -256,28 +193,18 @@ class FunkinSprite extends FlxSprite
256193 if (animationIsNull )
257194 return false ;
258195
259- return (( atlas != null ) ? atlas . anim . isPlaying : animation . curAnim .paused ) ?? false ;
196+ return animation ?. curAnim ? .paused ?? false ;
260197 }
261198
262199 function set_animPaused (value : Bool ): Bool
263200 {
264201 if (animationIsNull )
265202 return value ;
266203
267- if (atlas != null )
268- {
269- if (value )
270- atlas .anim .pause ();
271- else
272- atlas .anim .resume ();
273- }
204+ if (value )
205+ animation .curAnim .pause ();
274206 else
275- {
276- if (value )
277- animation .curAnim .pause ();
278- else
279- animation .curAnim .resume ();
280- }
207+ animation .curAnim .resume ();
281208
282209 return value ;
283210 }
@@ -287,13 +214,13 @@ class FunkinSprite extends FlxSprite
287214 * @param name The animation name to check for.
288215 * @return If the animation exists.
289216 */
290- @:privateAccess
291217 public function animationExists (name : String ): Bool
292218 {
293- if (atlas != null )
294- return atlas .anim .symbolDictionary .get (name ) != null ;
295- else
296- return animation ?. exists (name ) ?? false ;
219+ var atlasAnimList : Array <String > = super .getAnimateAnimations ();
220+ if (atlasAnimList .contains (name ))
221+ return true ;
222+
223+ return animation ?. exists (name ) ?? false ;
297224 }
298225
299226 /**
@@ -308,21 +235,7 @@ class FunkinSprite extends FlxSprite
308235 if (_onAnimFinished == null )
309236 {
310237 _onAnimFinished = new FlxTypedSignal <String -> Void >();
311-
312- if (atlas != null )
313- {
314- atlas .anim .onComplete .add (() ->
315- {
316- _onAnimFinished .dispatch (currentAnim );
317- });
318- }
319- else
320- {
321- animation .onFinish .add ((_ ) ->
322- {
323- _onAnimFinished .dispatch (currentAnim );
324- });
325- }
238+ animation .onFinish .add ((_ ) -> _onAnimFinished .dispatch (currentAnim ));
326239 }
327240
328241 return _onAnimFinished ;
@@ -337,65 +250,11 @@ class FunkinSprite extends FlxSprite
337250 if (animationIsNull )
338251 return false ;
339252
340- if (atlas != null )
341- {
342- var animData = atlas .anim .symbolDictionary .get (id );
343- if (animData == null )
344- return false ;
345- return animData .length > 1 ;
346- }
347- else
348- {
349- var animData = animation .getByName (id );
350- if (animData == null )
351- return false ;
352- return animData .numFrames > 1 ;
353- }
354- }
355-
356- @:noCompletion
357- override function set_width (value : Float ): Float
358- {
359- if (atlas != null )
360- {
361- atlas .width = value ;
362- return atlas .width ;
363- }
364-
365- return super .set_width (value );
366- }
367-
368- @:noCompletion
369- override function get_width (): Float
370- {
371- if (atlas != null )
372- {
373- return atlas .width ;
374- }
375-
376- return super .get_width ();
377- }
378-
379- @:noCompletion
380- override function set_height (value : Float ): Float
381- {
382- if (atlas != null )
383- {
384- atlas .height = value ;
385- return atlas .height ;
386- }
387-
388- return super .set_height (value );
389- }
390-
391- override function get_height (): Float
392- {
393- if (atlas != null )
394- {
395- return atlas .height ;
396- }
253+ var animData : Null <FlxAnimation > = animation .getByName (id );
254+ if (animData == null )
255+ return false ;
397256
398- return super . get_height () ;
257+ return animData . numFrames > 1 ;
399258 }
400259}
401260
0 commit comments