Skip to content

Commit 5d78a2e

Browse files
Add new custom tweening functionality
1 parent d074b63 commit 5d78a2e

File tree

9 files changed

+466
-58
lines changed

9 files changed

+466
-58
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ www-test/
8888

8989

9090
## NetBeans:
91+
9192
/nbproject/private/
9293
/android/nbproject/private/
9394
/core/nbproject/private/
@@ -144,7 +145,7 @@ nbactions.xml
144145
nb-configuration.xml
145146

146147
# VS Code
147-
/.vscode
148+
.vscode/
148149

149150
## OS-Specific:
150151
.DS_Store

core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies {
1010
api "org.mini2Dx:universal-tween-engine:$universalTweenVersion"
1111

1212
implementation "org.luaj:luaj-jse:3.0.1"
13+
implementation 'org.jetbrains:annotations:15.0'
1314

1415
if(enableGraalNative == 'true') {
1516
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"

core/src/main/java/me/stringfromjava/funkin/game/menus/TitleScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class TitleScreen extends FunkinScreen {
1616
@Override
1717
public void show() {
1818
super.show();
19-
tickleFight = Funkin.playSound("shared/sounds/tickleFight.ogg");
19+
tickleFight = new FunkinSound("shared/sounds/tickleFight.ogg");
2020
Funkin.playMusic("preload/music/freakyMenu/freakyMenu.ogg", 0.5f);
2121
}
2222

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,60 @@
11
package me.stringfromjava.funkin.tween;
22

3+
import me.stringfromjava.funkin.tween.settings.FunkinTweenSettings;
4+
5+
import java.lang.reflect.Field;
6+
import java.util.ArrayList;
7+
38
/**
4-
* Core manager class for creating new tweens.
9+
* Core class for creating new tweens to add nice and smooth animations to visual objects.
10+
* <p>
11+
* Note that this doesn't have to be used on sprites, it can be used on just about anything!
512
*/
6-
public final class FunkinTween {
13+
public class FunkinTween {
14+
15+
/**
16+
* The global tween manager for the entire game.
17+
*/
18+
public static FunkinTweenManager globalManager = new FunkinTweenManager();
19+
20+
/**
21+
* The object to tween.
22+
*/
23+
protected Object object;
24+
25+
/**
26+
* The settings used for how the tween is handled and calculated (aka how it looks and animates).
27+
*/
28+
protected FunkinTweenSettings tweenSettings;
29+
30+
private final ArrayList<Field> cachedFields;
31+
32+
/**
33+
* @param object The object to tween values.
34+
* @param settings The settings that configure and determine how the tween should animate and last for.
35+
*/
36+
public FunkinTween(Object object, FunkinTweenSettings settings) {
37+
this.tweenSettings = settings;
38+
this.cachedFields = new ArrayList<>();
39+
40+
Field[] allFields = object.getClass().getFields();
41+
var neededFields = settings.getGoalFields();
42+
43+
// Due to how costly it is using Java reflect, we cache the fields we need
44+
// before we actually do the real tweening functionality. This is so it
45+
// doesn't cause lag while multiple tweens are being active.
46+
for (Field field : allFields) {
47+
String fName = field.getName();
48+
49+
if (!field.trySetAccessible() && !neededFields.contains(fName)) {
50+
continue;
51+
}
52+
53+
cachedFields.add(field);
54+
}
55+
}
756

8-
private FunkinTween() {
57+
public FunkinTweenSettings getTweenSettings() {
58+
return tweenSettings;
959
}
1060
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package me.stringfromjava.funkin.tween;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Core manager class for handling all {@link FunkinTween}s that are currently active.
7+
*/
8+
public final class FunkinTweenManager {
9+
10+
/**
11+
* A list where all current active tweens are stored.
12+
*/
13+
public final ArrayList<FunkinTween> activeTweens = new ArrayList<>();
14+
15+
public void update(float delta) {
16+
// TODO: Iterate through every active tween and update them!
17+
}
18+
}

core/src/main/java/me/stringfromjava/funkin/tween/accessors/SpriteAccessor.java

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package me.stringfromjava.funkin.tween.settings;
2+
3+
import me.stringfromjava.funkin.tween.FunkinTween;
4+
5+
/**
6+
* Class where all easer functions are stored, mostly used for tweening.
7+
*/
8+
public final class FunkinTweenEase {
9+
10+
// Easing constants for specific functions.
11+
private static final float PI2 = (float) Math.PI / 2;
12+
private static final float EL = (float) ((float) 2 * Math.PI / .45);
13+
private static final float B1 = (float) ((float) 1 / 2.75);
14+
private static final float B2 = (float) ((float) 2 / 2.75);
15+
private static final float B3 = (float) ((float) 1.5 / 2.75);
16+
private static final float B4 = (float) ((float) 2.5 / 2.75);
17+
private static final float B5 = (float) ((float) 2.25 / 2.75);
18+
private static final float B6 = (float) ((float) 2.625 / 2.75);
19+
private static final float ELASTIC_AMPLITUDE = 1;
20+
private static final float ELASTIC_PERIOD = 0.4f;
21+
22+
private FunkinTweenEase() {
23+
}
24+
25+
public static float linear(float t) {
26+
return t;
27+
}
28+
29+
public static float quadIn(float t) {
30+
return t * t;
31+
}
32+
33+
public static float quadOut(float t) {
34+
return -t * (t - 2);
35+
}
36+
37+
public static float quadInOut(float t) {
38+
return t <= .5 ? t * t * 2 : 1 - (--t) * t * 2;
39+
}
40+
41+
public static float cubeIn(float t) {
42+
return t * t * t;
43+
}
44+
45+
public static float cubeOut(float t) {
46+
return 1 + (--t) * t * t;
47+
}
48+
49+
public static float cubeInOut(float t) {
50+
return t <= .5 ? t * t * t * 4 : 1 + (--t) * t * t * 4;
51+
}
52+
53+
public static float quartIn(float t) {
54+
return t * t * t * t;
55+
}
56+
57+
public static float quartOut(float t) {
58+
return 1 - (t -= 1) * t * t * t;
59+
}
60+
61+
public static float quartInOut(float t) {
62+
return t <= .5 ? t * t * t * t * 8 : (float) ((1 - (t = t * 2 - 2) * t * t * t) / 2 + .5);
63+
}
64+
65+
public static float quintIn(float t) {
66+
return t * t * t * t * t;
67+
}
68+
69+
public static float quintOut(float t) {
70+
return (t = t - 1) * t * t * t * t + 1;
71+
}
72+
73+
public static float quintInOut(float t) {
74+
return ((t *= 2) < 1) ? (t * t * t * t * t) / 2 : ((t -= 2) * t * t * t * t + 2) / 2;
75+
}
76+
77+
public static float smoothStepIn(float t) {
78+
return 2 * smoothStepInOut(t / 2);
79+
}
80+
81+
public static float smoothStepOut(float t) {
82+
return 2 * smoothStepInOut((float) (t / 2 + 0.5)) - 1;
83+
}
84+
85+
public static float smoothStepInOut(float t) {
86+
return t * t * (t * -2 + 3);
87+
}
88+
89+
public static float smootherStepIn(float t) {
90+
return 2 * smootherStepInOut(t / 2);
91+
}
92+
93+
public static float smootherStepOut(float t) {
94+
return 2 * smootherStepInOut((float) (t / 2 + 0.5)) - 1;
95+
}
96+
97+
public static float smootherStepInOut(float t) {
98+
return t * t * t * (t * (t * 6 - 15) + 10);
99+
}
100+
101+
public static float sineIn(float t) {
102+
return (float) (-Math.cos(PI2 * t) + 1);
103+
}
104+
105+
public static float sineOut(float t) {
106+
return (float) Math.sin(PI2 * t);
107+
}
108+
109+
public static float sineInOut(float t) {
110+
return (float) (-Math.cos(Math.PI * t) / 2 + .5);
111+
}
112+
113+
public static float bounceIn(float t) {
114+
return 1 - bounceOut(1 - t);
115+
}
116+
117+
public static float bounceOut(float t) {
118+
if (t < B1)
119+
return (float) (7.5625 * t * t);
120+
if (t < B2)
121+
return (float) (7.5625 * (t - B3) * (t - B3) + .75);
122+
if (t < B4)
123+
return (float) (7.5625 * (t - B5) * (t - B5) + .9375);
124+
return (float) (7.5625 * (t - B6) * (t - B6) + .984375);
125+
}
126+
127+
public static float bounceInOut(float t) {
128+
return t < 0.5
129+
? (1 - bounceOut(1 - 2 * t)) / 2
130+
: (1 + bounceOut(2 * t - 1)) / 2;
131+
}
132+
133+
public static float circIn(float t) {
134+
return (float) -(Math.sqrt(1 - t * t) - 1);
135+
}
136+
137+
public static float circOut(float t) {
138+
return (float) Math.sqrt(1 - (t - 1) * (t - 1));
139+
}
140+
141+
public static float circInOut(float t) {
142+
return (float) (t <= .5 ? (Math.sqrt(1 - t * t * 4) - 1) / -2 : (Math.sqrt(1 - (t * 2 - 2) * (t * 2 - 2)) + 1) / 2);
143+
}
144+
145+
public static float expoIn(float t) {
146+
return (float) Math.pow(2, 10 * (t - 1));
147+
}
148+
149+
public static float expoOut(float t) {
150+
return (float) (-Math.pow(2, -10 * t) + 1);
151+
}
152+
153+
public static float expoInOut(float t) {
154+
return (float) (t < .5 ? Math.pow(2, 10 * (t * 2 - 1)) / 2 : (-Math.pow(2, -10 * (t * 2 - 1)) + 2) / 2);
155+
}
156+
157+
public static float backIn(float t) {
158+
return (float) (t * t * (2.70158 * t - 1.70158));
159+
}
160+
161+
public static float backOut(float t) {
162+
return (float) (1 - (--t) * (t) * (-2.70158 * t - 1.70158));
163+
}
164+
165+
public static float backInOut(float t) {
166+
t *= 2;
167+
if (t < 1)
168+
return (float) (t * t * (2.70158 * t - 1.70158) / 2);
169+
t--;
170+
return (float) ((1 - (--t) * (t) * (-2.70158 * t - 1.70158)) / 2 + .5);
171+
}
172+
173+
public static float elasticIn(float t) {
174+
return (float) -(ELASTIC_AMPLITUDE * Math.pow(2,
175+
10 * (t -= 1)) * Math.sin((t - (ELASTIC_PERIOD / (2 * Math.PI) * Math.asin(1 / ELASTIC_AMPLITUDE))) * (2 * Math.PI) / ELASTIC_PERIOD));
176+
}
177+
178+
public static float elasticOut(float t) {
179+
return (float) (ELASTIC_AMPLITUDE * Math.pow(2,
180+
-10 * t) * Math.sin((t - (ELASTIC_PERIOD / (2 * Math.PI) * Math.asin(1 / ELASTIC_AMPLITUDE))) * (2 * Math.PI) / ELASTIC_PERIOD)
181+
+ 1);
182+
}
183+
184+
public static float elasticInOut(float t) {
185+
if (t < 0.5) {
186+
return (float) (-0.5 * (Math.pow(2, 10 * (t -= 0.5f)) * Math.sin((t - (ELASTIC_PERIOD / 4)) * (2 * Math.PI) / ELASTIC_PERIOD)));
187+
}
188+
return (float) (Math.pow(2, -10 * (t -= 0.5f)) * Math.sin((t - (ELASTIC_PERIOD / 4)) * (2 * Math.PI) / ELASTIC_PERIOD) * 0.5 + 1);
189+
}
190+
191+
@FunctionalInterface
192+
public interface FunkinTweenEaseFunction {
193+
float compute(float t);
194+
}
195+
196+
@FunctionalInterface
197+
public interface FunkinTweenEaseStartCallback {
198+
void run(FunkinTween tween);
199+
}
200+
201+
@FunctionalInterface
202+
public interface FunkinTweenEaseUpdateCallback {
203+
void run(FunkinTween tween);
204+
}
205+
206+
@FunctionalInterface
207+
public interface FunkinTweenEaseCompleteCallback {
208+
void run(FunkinTween tween);
209+
}
210+
}

0 commit comments

Comments
 (0)