-
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathCountdown.hx
More file actions
152 lines (124 loc) · 3.51 KB
/
Countdown.hx
File metadata and controls
152 lines (124 loc) · 3.51 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
package funkin.game;
import flixel.tweens.misc.VarTween;
import flixel.tweens.FlxTween;
import flixel.sound.FlxSound;
import funkin.backend.scripting.events.gameplay.CountdownEvent;
enum CountdownAnimationPreset {
/**
* The default animation for Codename Engine's countdown.
*/
DEFAULT;
/**
* The classic animation, similar to Funkin's countdown.
*/
CLASSIC;
/**
* A more enhanced version of Funkin's countdown animation.
*/
BEATING;
}
typedef CountdownParams = {
/**
* The CountdownEvent to be used.
*/
var event:CountdownEvent;
/**
* Whether the countdown should be visible or not.
*/
var enabled:Bool;
/**
* Whether each tick from the countdown should play a sound.
*/
var playSound:Bool;
/**
* The animation preset to be used for the countdown.
*/
var animationPreset:CountdownAnimationPreset;
/**
* The duration of the countdown's animation.
*/
var duration:Float;
/**
* The speed of the countdown's animation. The lower it is, the slower it goes and vice-versa.
*/
var speed:Float;
}
class Countdown extends FlxTypedSpriteGroup<FlxSprite> {
public var event:CountdownEvent;
public var enabled:Bool;
public var playSound:Bool;
public var animationPreset:CountdownAnimationPreset;
public var duration:Float;
public var speed:Float;
/**
* Create a new Countdown component.
* @param params
*/
public function new(params:CountdownParams) {
super();
this.event = params.event;
this.enabled = params.enabled;
this.playSound = params.playSound;
this.animationPreset = params.animationPreset;
this.duration = params.duration;
this.speed = params.speed;
this.__createSprite();
}
@:noPrivateAccess
private function __createSprite():Void {
if (!this.enabled) {
return;
}
var sprite:FlxSprite = null;
var sound:FlxSound = null;
var tween:FlxTween = null;
if (!this.event.cancelled) {
if (this.event.spritePath != null) {
// Add 1.0 to defaultSize if animationPreset is BEATING.
var defaultSize:Float = this.event.scale;
var isBeatingPreset:Bool = (this.animationPreset == BEATING);
var targetSize:Float = defaultSize + ((!isBeatingPreset) ? 0.0 : 0.15);
var spr = this.event.spritePath;
if (!Assets.exists(spr)) {
spr = Paths.image('$spr');
}
sprite = new FunkinSprite().loadAnimatedGraphic(spr);
sprite.scale.set(targetSize, targetSize);
sprite.scrollFactor.set();
sprite.antialiasing = this.event.antialiasing;
sprite.updateHitbox();
sprite.screenCenter();
add(sprite);
switch(this.animationPreset) {
case CLASSIC:
tween = __createTween(sprite, {alpha: 0}, FlxEase.cubeInOut);
case BEATING:
tween = __createTween(sprite, {alpha: 0, "scale.x": defaultSize, "scale.y": defaultSize}, FlxEase.expoOut);
default: // DEFAULT
tween = __createTween(sprite, {y: sprite.y + 100, alpha: 0}, FlxEase.cubeInOut);
}
}
if (this.event.soundPath != null && this.playSound) {
var sfx = this.event.soundPath;
if (!Assets.exists(sfx)) {
sfx = Paths.sound(sfx);
}
sound = FlxG.sound.play(sfx, this.event.volume);
}
this.event.sprite = sprite;
this.event.sound = sound;
this.event.spriteTween = tween;
this.event.cancelled = false;
}
}
@:noPrivateAccess
private function __createTween(sprite:FlxSprite, values:Dynamic, easing:EaseFunction):VarTween {
return FlxTween.tween(sprite, values, (this.duration / this.speed), {
ease: easing,
onComplete: function(twn:FlxTween) {
sprite.destroy();
remove(sprite, true);
}
});
}
}