-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFunkinSprite.hx
More file actions
262 lines (217 loc) · 6.29 KB
/
FunkinSprite.hx
File metadata and controls
262 lines (217 loc) · 6.29 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
package funkin.objects;
import animate.FlxAnimate;
import animate.FlxAnimateFrames;
import flixel.animation.FlxAnimation;
import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup;
import flixel.util.FlxSignal.FlxTypedSignal;
import funkin.structures.ObjectStructure;
using funkin.util.FlxAnimateUtil;
/**
* This is a sprite class that adds on to the already existing FlxSprite.
*/
class FunkinSprite extends FlxAnimate
{
/**
* Draws this `FunkinSprite`, but invisible.
* This is basically visible/alpha, but it doesn't lag when you make it visible again.
*/
public var doInvisibleDraw:Bool = false;
/**
* Settings to use when initializing texture atlases.
*/
public var atlasSettings:FlxAnimateSettings = {};
public function new(x:Float = 0, y:Float = 0)
{
super(x, y);
}
override function initVars():Void
{
super.initVars();
}
/**
* Loads or creates a texture and applies it to this sprite.
* @param path The asset path. (If `path` starts with a **#** then a color will be made instead and rectWidth + rectHeight will determine its size.)
* @param rectWidth If the path is a color then how big should it's width be?
* @param rectHeight If the path is a color then how big should it's height be?
* @return This `FunkinSprite` instance (nice for chaining stuff together, if you're into that).
*/
public function loadTexture(path:String = '#000000', rectWidth:Int = 1, rectHeight:Int = 1):FunkinSprite
{
// Regex Check for colors?
if (path.startsWith('#'))
{
loadGraphic(FlxG.bitmap.create(1, 1, FlxColor.fromString(path), false));
scale.set(rectWidth, rectHeight);
updateHitbox();
}
else
{
loadGraphic(Paths.content.imageGraphic(path));
}
return this;
}
/**
* Loads frames and applies it to this sprite.
* @param path The path of where frames should load from.
* @param forcedType Which type to force. If null, it will be determined automatically.
* @return This `FunkinSprite` instance (nice for chaining stuff together, if you're into that).
*/
public function loadFrames(path:String, ?forcedType:Null<String>):FunkinSprite
{
if (Paths.location.exists(path + '.xml'))
{
frames = Paths.content.sparrowAtlas(path);
}
else if (Paths.location.exists(path + '/Animation.json'))
{
frames = Paths.content.animateAtlas(path, atlasSettings);
}
return this;
}
override public function draw():Void
{
var oldAlpha:Float = alpha;
if (doInvisibleDraw)
alpha = 0.0001;
super.draw();
if (doInvisibleDraw)
{
alpha = oldAlpha;
}
}
#if FLX_DEBUG
override public function drawDebug():Void
{
if (doInvisibleDraw)
return;
super.drawDebug();
}
#end
// ANIMATION BINDINGS
/**
* The current playing animation.
*/
public var currentAnim(default, null):String = '';
var animationStunned:Bool = false;
/**
* Plays an animation.
* @param name The name of the animation to play.
* @param restart Should the animation restart if it's already playing?
* @param stunAnimations Should the animations be "stunned" until this one is finished?
* @param reversed Should the animation be reversed?
*/
public function playAnimation(name:String, ?restart:Bool = false, ?stunAnimations:Bool = false, ?reversed:Bool = false):Void
{
if (animationStunned)
return;
animation.play(name, restart, reversed);
animationStunned = stunAnimations;
currentAnim = name;
}
/**
* Adds an Animation to the sprite.
* @param name The name of the animation to add.
* @param anim The actual animation name.
* @param indices The frame indices to use. (Optional)
* @param frameRate The Frame Rate of the animation. (Optional)
* @param looped Should the animation loop? (Optional)
*/
public function addAnimation(name:String, anim:String, ?indices:Array<Int> = null, ?frameRate:Float = 24, ?looped:Bool = true):Void
{
var atlasAnimList:Array<String> = super.getAnimateAnimations();
if (atlasAnimList.contains(anim))
{
super.addAnimateAtlasAnimation(name, anim, indices, frameRate, looped);
return;
}
if (indices != null && indices.length > 0)
animation.addByIndices(name, anim + '0', indices, '', frameRate, looped);
else
animation.addByPrefix(name, anim + '0', frameRate, looped);
}
/**
* Is the current animation null?
*/
public var animationIsNull(get, never):Bool;
function get_animationIsNull():Bool
{
return animation.curAnim == null;
}
/**
* Is the current animation finished?
*/
public var animFinished(get, never):Bool;
function get_animFinished():Bool
{
return animation?.curAnim?.finished ?? false;
}
/**
* Finishes the current animation playing.
*/
public function finishAnimation():Void
{
if (animationIsNull)
return;
animation.curAnim.finish();
}
/**
* Is the current animation paused?
*/
public var animPaused(get, set):Bool;
function get_animPaused():Bool
{
if (animationIsNull)
return false;
return animation?.curAnim?.paused ?? false;
}
function set_animPaused(value:Bool):Bool
{
if (animationIsNull)
return value;
if (value)
animation.curAnim.pause();
else
animation.curAnim.resume();
return value;
}
/**
* Checks if the animation specified exists.
* @param name The animation name to check for.
* @return If the animation exists.
*/
public function animationExists(name:String):Bool
{
var atlasAnimList:Array<String> = super.getAnimateAnimations();
if (atlasAnimList.contains(name))
return true;
return animation?.exists(name) ?? false;
}
/**
* Called when an animation is finished.
*/
public var onAnimFinished(get, never):FlxTypedSignal<String->Void>;
var _onAnimFinished:FlxTypedSignal<String->Void>;
function get_onAnimFinished():FlxTypedSignal<String->Void>
{
if (_onAnimFinished == null)
{
_onAnimFinished = new FlxTypedSignal<String->Void>();
animation.onFinish.add((_) -> _onAnimFinished.dispatch(currentAnim));
}
return _onAnimFinished;
}
/**
* @param id The animation ID to check.
* @return Whether the animation is dynamic (has multiple frames). `false` for static, one-frame animations.
*/
public function isAnimationDynamic(id:String):Bool
{
if (animationIsNull)
return false;
var animData:Null<FlxAnimation> = animation.getByName(id);
if (animData == null)
return false;
return animData.numFrames > 1;
}
}
typedef FunkinSpriteGroup = FlxTypedSpriteGroup<FunkinSprite>