> getAnimations() {
return animations;
}
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/graphics/text/FlixelText.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/graphics/text/FlixelText.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/graphics/text/FlixelText.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/graphics/text/FlixelText.java
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/signal/FlixelSignal.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/signal/FlixelSignal.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/signal/FlixelSignal.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/signal/FlixelSignal.java
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/signal/FlixelSignalData.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/signal/FlixelSignalData.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/signal/FlixelSignalData.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/signal/FlixelSignalData.java
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelEase.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelEase.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelEase.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelEase.java
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTween.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTween.java
similarity index 51%
rename from core/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTween.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTween.java
index 3c6652e..36e0430 100644
--- a/core/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTween.java
+++ b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTween.java
@@ -1,32 +1,25 @@
package me.stringdotjar.flixelgdx.tween;
import com.badlogic.gdx.utils.Pool;
-import me.stringdotjar.flixelgdx.Flixel;
import me.stringdotjar.flixelgdx.tween.settings.FlixelTweenSettings;
-import me.stringdotjar.flixelgdx.util.FlixelConstants;
-import me.stringdotjar.flixelgdx.util.FlixelReflectUtil;
+import me.stringdotjar.flixelgdx.tween.type.FlixelNumTween;
+import me.stringdotjar.flixelgdx.tween.type.FlixelVarTween;
import org.jetbrains.annotations.NotNull;
-import java.lang.reflect.Field;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
-
/**
- * Core class for creating new tweens to add nice and smooth animations to visual objects.
+ * Core class for creating new tweens to add nice and smooth animations.
+ *
+ * Please note that while this isn't an abstract class, it is advised to NOT create instances
+ * of this class, since it does not implement the actual tweening logic. Instead, you should use one of
+ * its subclasses, such as {@link FlixelVarTween}, or create your own subclass and add your own functionality.
*
- *
Note that this doesn't have to be used on sprites, it can be used on just about anything!
+ *
The only reason this class is not abstract is to allow pooling of generic tweens when needed to save memory.
*/
public class FlixelTween implements Pool.Poolable {
/** The global tween manager for the entire game. */
protected static FlixelTweenManager globalManager = new FlixelTweenManager();
- /** The object to tween. */
- protected Object object;
-
/** The settings used for how the tween is handled and calculated (aka how it looks and animates). */
protected FlixelTweenSettings tweenSettings;
@@ -36,9 +29,6 @@ public class FlixelTween implements Pool.Poolable {
/** How far the tween is tweening itself. This is what's used to actually tween the object! */
protected float scale = 0.0f;
- /** The update callback for {@code this} tween to change the objects values every update. */
- protected FlixelTween.FunkinTweenUpdateCallback updateCallback;
-
/** How many seconds has elapsed since {@code this} tween started. */
protected float secondsSinceStart = 0.0f;
@@ -57,69 +47,63 @@ public class FlixelTween implements Pool.Poolable {
/** Is {@code this} tween tweening backwards? */
protected boolean backward = false;
- /** The initial values of the fields being tweened. */
- protected final Map initialValues = new HashMap<>();
-
- /**
- * Cache of the fields being tweened for faster access so they aren't accessed every time the
- * {@link #start()} method is called.
- */
- protected Field[] fieldsCache = null;
-
/** Default constructor for pooling purposes. */
protected FlixelTween() {}
+ protected FlixelTween(FlixelTweenSettings tweenSettings) {
+ this.tweenSettings = tweenSettings;
+ }
+
/**
- * Constructs a new tween.
- *
- * Note that this does NOT add the tween to the global manager, it just assigns its main
- * values. That's it. If you wish to create a tween to automatically start working, you might want
- * to see {@link FlixelTween#tween(Object object, FlixelTweenSettings tweenSettings,
- * FunkinTweenUpdateCallback updateCallback)}.
+ * Creates a new tween with the provided settings and starts it in the global tween manager.
*
- * @param object The object to tween values.
- * @param settings The settings that configure and determine how the tween should animate and last
- * for.
+ * @param object The object to tween its values.
+ * @param tweenSettings The settings that configure and determine how the tween should animate.
* @param updateCallback Callback function for updating the objects values when the tween updates.
+ * @return The newly created and started tween.
*/
- protected FlixelTween(
- Object object, FlixelTweenSettings settings, FunkinTweenUpdateCallback updateCallback) {
- this.object = object;
- this.tweenSettings = settings;
- this.updateCallback = updateCallback;
+ public static FlixelTween tween(Object object, FlixelTweenSettings tweenSettings, FlixelVarTween.FunkinVarTweenUpdateCallback updateCallback) {
+ return new FlixelVarTween(object, tweenSettings, updateCallback)
+ .setManager(globalManager)
+ .start();
+ }
+
+ public static FlixelTween num(float from, float to, FlixelTweenSettings tweenSettings, FlixelNumTween.FlixelNumTweenUpdateCallback updateCallback) {
+ return new FlixelNumTween(from, to, tweenSettings, updateCallback)
+ .setManager(globalManager)
+ .start();
}
/**
- * Creates a new tween with the provided settings and starts it in the global tween manager.
+ * Starts {@code this} tween and resets every value to its initial state.
*
- * @param object The object to tween its values.
- * @param tweenSettings The settings that configure and determine how the tween should animate.
- * @param updateCallback Callback function for updating the objects values when the tween updates.
- * @return The newly created and started tween.
+ * @return {@code this} tween.
*/
- public static FlixelTween tween(
- Object object, FlixelTweenSettings tweenSettings, FunkinTweenUpdateCallback updateCallback) {
- return new FlixelTween(object, tweenSettings, updateCallback).setManager(globalManager).start();
+ public FlixelTween start() {
+ resetBasic();
+ running = true;
+ finished = false;
+ return this;
}
+ /**
+ * Updates {@code this} tween by the given delta time.
+ *
+ * @param delta How much time has passed since the last update.
+ */
public void update(float delta) {
- if (paused || finished) {
- return;
- }
- if (object == null) {
+ if (paused || finished || !running) {
return;
}
if (tweenSettings == null) {
return;
}
- if (updateCallback == null) {
- return;
- }
var ease = tweenSettings.getEase();
var duration = tweenSettings.getDuration();
var onStart = tweenSettings.getOnStart();
var onUpdate = tweenSettings.getOnUpdate();
+ var onComplete = tweenSettings.getOnComplete();
var framerate = tweenSettings.getFramerate();
float preTick = secondsSinceStart;
@@ -150,33 +134,13 @@ public void update(float delta) {
}
}
- // Update the object's fields based on the tween progress.
- var newValues = new HashMap();
- var goals = tweenSettings.getGoalFields();
- for (String field : goals) {
- FlixelTweenSettings.FunkinTweenGoal goal = tweenSettings.getGoal(field);
- if (goal == null) {
- continue;
- }
- if (initialValues.isEmpty()) {
- continue;
- }
- if (!initialValues.containsKey(field)) {
- continue;
- }
- float startValue = initialValues.get(field);
- float goalValue = goal.value();
- float newValue = startValue + (goalValue - startValue) * scale;
- newValues.put(field, newValue);
- }
- if (!newValues.isEmpty() && initialValues.keySet().containsAll(newValues.keySet())) {
- updateCallback.update(newValues);
- }
-
// Check if the tween has finished.
if (secondsSinceStart >= duration + delay) {
scale = (backward) ? 0 : 1;
finished = true;
+ if (onComplete != null) {
+ onComplete.run(this);
+ }
} else {
if (postTick > preTick && onUpdate != null) {
onUpdate.run(this);
@@ -184,63 +148,6 @@ public void update(float delta) {
}
}
- /**
- * Starts {@code this} tween and resets every value to its initial state.
- *
- * @return {@code this} tween.
- */
- public FlixelTween start() {
- resetBasic();
- running = true;
- finished = false;
-
- if (tweenSettings == null) {
- Flixel.warn("FlixelTween", "No tween settings were provided for the tween.");
- return this;
- }
- var neededFields = tweenSettings.getGoalFields();
- if (neededFields == null || neededFields.isEmpty()) {
- Flixel.warn("FlixelTween", "No fields were provided to tween on the object.");
- return this;
- }
-
- if (fieldsCache == null) {
- fieldsCache = FlixelReflectUtil.getAllFieldsAsArray(object.getClass());
- }
-
- // Ensure that the fields provided actually exist on the object and are floating point values.
- // If there are fields that the tween is trying to tween that don't exist, then throw an error.
- Set fieldIds =
- Arrays.stream(fieldsCache).map(Field::getName).collect(Collectors.toSet());
- for (String neededField : neededFields) {
- if (!fieldIds.contains(neededField)) {
- String message = "Field \"" + neededField + "\" does not exist on the given object.";
- Flixel.error("FlixelTween", message);
- throw new RuntimeException(message);
- }
- }
-
- for (Field field : fieldsCache) {
- try {
- String fName = field.getName();
- if (!field.trySetAccessible()) {
- continue;
- }
- if (!neededFields.contains(fName)) {
- continue;
- }
- if (field.getType() != float.class) {
- continue;
- }
- initialValues.put(fName, field.getFloat(object));
- } catch (IllegalAccessException e) {
- Flixel.error(
- FlixelConstants.System.LOG_TAG, "Could not access field \"" + field.getName() + "\".", e);
- }
- }
- return this;
- }
-
/**
* Resumes {@code this} tween if it was previously paused.
*
@@ -270,13 +177,11 @@ public FlixelTween pause() {
*/
public FlixelTween stop() {
running = false;
- finished = true;
return this;
}
/**
- * Cancels {@code this} tween and automatically defaults its values, removing it from the manager
- * by default.
+ * Cancels {@code this} tween and automatically defaults its values, removing it from its manager by default.
*
* @return {@code this} tween.
*/
@@ -302,7 +207,6 @@ public FlixelTween cancel(boolean remove) {
public void reset() {
resetBasic();
manager = null;
- fieldsCache = null;
}
/**
@@ -317,7 +221,6 @@ public void resetBasic() {
running = false;
finished = false;
backward = false;
- initialValues.clear();
}
public FlixelTweenSettings getTweenSettings() {
@@ -333,6 +236,10 @@ public static FlixelTweenManager getGlobalManager() {
return globalManager;
}
+ public FlixelTweenManager getManager() {
+ return manager;
+ }
+
public FlixelTween setManager(FlixelTweenManager newManager) {
if (newManager != null) {
if (manager != null) {
@@ -345,17 +252,4 @@ public FlixelTween setManager(FlixelTweenManager newManager) {
}
return this;
}
-
- /** Callback interface for changing an objects values when a tween updates its values. */
- @FunctionalInterface
- public interface FunkinTweenUpdateCallback {
-
- /**
- * A callback method that is called when the tween updates its values during its tweening (or
- * animating) process.
- *
- * @param values The new current values of the fields being tweened during the animation.
- */
- void update(Map values);
- }
}
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTweenManager.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTweenManager.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTweenManager.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/FlixelTweenManager.java
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenSettings.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenSettings.java
similarity index 89%
rename from core/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenSettings.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenSettings.java
index 70f22dd..1f202bb 100644
--- a/core/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenSettings.java
+++ b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenSettings.java
@@ -59,6 +59,8 @@ public FlixelTweenSettings(
/**
* Adds a new goal to tween an objects value to.
*
+ * Note that this is only used on a {@link me.stringdotjar.flixelgdx.tween.type.FlixelVarTween}.
+ *
* @param field The field to tween.
* @param value The value to tween the field to.
* @return {@code this} tween settings object for chaining.
@@ -165,6 +167,21 @@ public FlixelTweenSettings setType(@NotNull FlixelTweenType type) {
return this;
}
+ public FlixelTweenSettings setOnStart(FlixelEase.FunkinEaseStartCallback onStart) {
+ this.onStart = onStart;
+ return this;
+ }
+
+ public FlixelTweenSettings setOnUpdate(FlixelEase.FunkinEaseUpdateCallback onUpdate) {
+ this.onUpdate = onUpdate;
+ return this;
+ }
+
+ public FlixelTweenSettings setOnComplete(FlixelEase.FunkinEaseCompleteCallback onComplete) {
+ this.onComplete = onComplete;
+ return this;
+ }
+
/**
* A record containing basic info for a tween goal (aka a field to tween a numeric value to).
*
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenType.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenType.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenType.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/settings/FlixelTweenType.java
diff --git a/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/type/FlixelNumTween.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/type/FlixelNumTween.java
new file mode 100644
index 0000000..40cc7d1
--- /dev/null
+++ b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/type/FlixelNumTween.java
@@ -0,0 +1,73 @@
+package me.stringdotjar.flixelgdx.tween.type;
+
+import me.stringdotjar.flixelgdx.tween.FlixelTween;
+import me.stringdotjar.flixelgdx.tween.settings.FlixelTweenSettings;
+
+/**
+ * Tween type that tweens one numerical value to another.
+ */
+public class FlixelNumTween extends FlixelTween {
+
+ /** The starting value of the tween. */
+ protected float start;
+
+ /** The target value of the tween. */
+ protected float end;
+
+ /** The current value of the tween. */
+ protected float value;
+
+ /** The range between the start and end values. */
+ protected float range;
+
+ /** Callback function for updating the value when the tween updates. */
+ protected FlixelNumTweenUpdateCallback updateCallback;
+
+ /**
+ * Constructs a new numerical tween, which will tween a simple starting number to an ending value.
+ *
+ * @param start The starting value.
+ * @param end The ending value.
+ * @param settings The settings that configure and determine how the tween should animate.
+ * @param updateCallback Callback function for updating any variable that needs the current value when the tween updates.
+ */
+ public FlixelNumTween(float start, float end, FlixelTweenSettings settings, FlixelNumTweenUpdateCallback updateCallback) {
+ super(settings);
+ this.start = start;
+ this.end = end;
+ this.value = start;
+ this.range = end - start;
+ this.updateCallback = updateCallback;
+ }
+
+ @Override
+ public void update(float delta) {
+ super.update(delta);
+
+ if (paused || finished || !running) {
+ return;
+ }
+ if (updateCallback == null) {
+ return;
+ }
+
+ value = start + range * scale;
+
+ updateCallback.update(value);
+ }
+
+ /**
+ * Functional interface for updating the numerical value when the tween updates. This is for updating any
+ * variable that needs the current value of the tween.
+ */
+ @FunctionalInterface
+ public interface FlixelNumTweenUpdateCallback {
+
+ /**
+ * A callback method that is called when the tween updates its value during its tweening (or animating) process.
+ *
+ * @param value The new current value of the numerical tween during the animation.
+ */
+ void update(float value);
+ }
+}
diff --git a/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/type/FlixelVarTween.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/type/FlixelVarTween.java
new file mode 100644
index 0000000..3f14829
--- /dev/null
+++ b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/tween/type/FlixelVarTween.java
@@ -0,0 +1,165 @@
+package me.stringdotjar.flixelgdx.tween.type;
+
+import me.stringdotjar.flixelgdx.Flixel;
+import me.stringdotjar.flixelgdx.tween.FlixelTween;
+import me.stringdotjar.flixelgdx.tween.settings.FlixelTweenSettings;
+import me.stringdotjar.flixelgdx.util.FlixelConstants;
+import me.stringdotjar.flixelgdx.util.FlixelReflectUtil;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Tween type for tweening specific fields on an object using reflection.
+ */
+public class FlixelVarTween extends FlixelTween {
+
+ /** The object to tween. */
+ protected Object object;
+
+ /** The initial values of the fields being tweened. */
+ protected final Map initialValues = new HashMap<>();
+
+ /**
+ * Cache of the fields being tweened for faster access so they aren't accessed every time the
+ * {@link #start()} method is called.
+ */
+ protected Field[] fieldsCache = null;
+
+ /** The update callback for {@code this} tween to change the objects values every update. */
+ protected FlixelVarTween.FunkinVarTweenUpdateCallback updateCallback;
+
+ /**
+ * Constructs a new object tween using reflection.
+ *
+ * Note that this does NOT add the tween to the global manager, it just assigns its main
+ * values. That's it. If you wish to create a tween to automatically start working, you might want
+ * to see {@link FlixelTween#tween(Object object, FlixelTweenSettings tweenSettings,
+ * FunkinVarTweenUpdateCallback updateCallback)}.
+ *
+ * @param object The object to tween values.
+ * @param settings The settings that configure and determine how the tween should animate and last for.
+ * @param updateCallback Callback function for updating the objects values when the tween updates.
+ */
+ public FlixelVarTween(Object object, FlixelTweenSettings settings, FunkinVarTweenUpdateCallback updateCallback) {
+ super(settings);
+ this.object = object;
+ this.updateCallback = updateCallback;
+ }
+
+ @Override
+ public FlixelTween start() {
+ super.start();
+
+ if (tweenSettings == null) {
+ Flixel.warn("FlixelTween", "No tween settings were provided for the tween.");
+ return this;
+ }
+ var neededFields = tweenSettings.getGoalFields();
+ if (neededFields == null || neededFields.isEmpty()) {
+ Flixel.warn("FlixelTween", "No fields were provided to tween on the object.");
+ return this;
+ }
+
+ if (fieldsCache == null) {
+ fieldsCache = FlixelReflectUtil.getAllFieldsAsArray(object.getClass());
+ }
+
+ // Ensure that the fields provided actually exist on the object and are floating point values.
+ // If there are fields that the tween is trying to tween that don't exist, then throw an error.
+ Set fieldIds = Arrays.stream(fieldsCache).map(Field::getName).collect(Collectors.toSet());
+ for (String neededField : neededFields) {
+ if (!fieldIds.contains(neededField)) {
+ String message = "Field \"" + neededField + "\" does not exist on the given object.";
+ Flixel.error("FlixelTween", message);
+ throw new RuntimeException(message);
+ }
+ }
+
+ for (Field field : fieldsCache) {
+ try {
+ String fName = field.getName();
+ if (!field.trySetAccessible()) {
+ continue;
+ }
+ if (!neededFields.contains(fName)) {
+ continue;
+ }
+ if (field.getType() != float.class) {
+ continue;
+ }
+ initialValues.put(fName, field.getFloat(object));
+ } catch (IllegalAccessException e) {
+ Flixel.error(FlixelConstants.System.LOG_TAG, "Could not access field \"" + field.getName() + "\".", e);
+ }
+ }
+ return this;
+ }
+
+ @Override
+ public void update(float delta) {
+ super.update(delta);
+
+ if (object == null) {
+ return;
+ }
+ if (paused || finished) {
+ return;
+ }
+ if (updateCallback == null) {
+ return;
+ }
+
+ // Update the object's fields based on the tween progress.
+ var newValues = new HashMap();
+ var goals = tweenSettings.getGoalFields();
+ for (String field : goals) {
+ FlixelTweenSettings.FunkinTweenGoal goal = tweenSettings.getGoal(field);
+ if (goal == null) {
+ continue;
+ }
+ if (initialValues.isEmpty()) {
+ continue;
+ }
+ if (!initialValues.containsKey(field)) {
+ continue;
+ }
+ float startValue = initialValues.get(field);
+ float goalValue = goal.value();
+ float newValue = startValue + (goalValue - startValue) * scale;
+ newValues.put(field, newValue);
+ }
+ if (!newValues.isEmpty() && initialValues.keySet().containsAll(newValues.keySet())) {
+ updateCallback.update(newValues);
+ }
+ }
+
+ @Override
+ public void reset() {
+ super.reset();
+ fieldsCache = null;
+ }
+
+ @Override
+ public void resetBasic() {
+ super.resetBasic();
+ initialValues.clear();
+ }
+
+ /** Callback interface for changing an objects values when a var tween updates its values. */
+ @FunctionalInterface
+ public interface FunkinVarTweenUpdateCallback {
+
+ /**
+ * A callback method that is called when the tween updates its values during its tweening (or
+ * animating) process.
+ *
+ * @param values The new current values of the fields being tweened during the animation.
+ */
+ void update(Map values);
+ }
+}
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/util/FlixelConstants.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelConstants.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/util/FlixelConstants.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelConstants.java
diff --git a/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelMathUtil.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelMathUtil.java
new file mode 100644
index 0000000..bd28994
--- /dev/null
+++ b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelMathUtil.java
@@ -0,0 +1,21 @@
+package me.stringdotjar.flixelgdx.util;
+
+/**
+ * Utility class for various math related functions used in FlixelGDX.
+ */
+public final class FlixelMathUtil {
+
+ /**
+ * Rounds a float value to a specified number of decimal places.
+ *
+ * @param value The float value to round.
+ * @param decimalPlaces The number of decimal places to round to.
+ * @return The rounded float value.
+ */
+ public static float round(float value, int decimalPlaces) {
+ float scale = (float) Math.pow(10, decimalPlaces);
+ return Math.round(value * scale) / scale;
+ }
+
+ private FlixelMathUtil() {}
+}
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/util/FlixelReflectUtil.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelReflectUtil.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/util/FlixelReflectUtil.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelReflectUtil.java
diff --git a/core/src/main/java/me/stringdotjar/flixelgdx/util/FlixelRuntimeUtil.java b/flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelRuntimeUtil.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/flixelgdx/util/FlixelRuntimeUtil.java
rename to flixelgdx/src/main/java/me/stringdotjar/flixelgdx/util/FlixelRuntimeUtil.java
diff --git a/core/build.gradle b/funkin/build.gradle
similarity index 91%
rename from core/build.gradle
rename to funkin/build.gradle
index 4dd5eed..b1991df 100644
--- a/core/build.gradle
+++ b/funkin/build.gradle
@@ -1,17 +1,16 @@
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
-eclipse.project.name = appName + '-core'
+eclipse.project.name = appName + '-funkin'
dependencies {
- // libGDX.
api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.github.tommyettinger:anim8-gdx:$anim8Version"
api "com.github.tommyettinger:libgdx-utils:$utilsVersion"
api "io.github.libktx:ktx-freetype:$ktxVersion"
api "games.rednblack.miniaudio:miniaudio:$miniaudioVersion"
+ api project(":flixelgdx")
+ api project(":polyverse")
- // FNF:JE.
- implementation "org.apache.groovy:groovy:5.0.3"
implementation "org.jetbrains:annotations:26.0.2-1"
if (enableGraalNative == 'true') {
diff --git a/core/src/main/java/me/stringdotjar/funkin/FunkinGame.java b/funkin/src/main/java/me/stringdotjar/funkin/FunkinGame.java
similarity index 59%
rename from core/src/main/java/me/stringdotjar/funkin/FunkinGame.java
rename to funkin/src/main/java/me/stringdotjar/funkin/FunkinGame.java
index 0040de4..cfdce50 100644
--- a/core/src/main/java/me/stringdotjar/funkin/FunkinGame.java
+++ b/funkin/src/main/java/me/stringdotjar/funkin/FunkinGame.java
@@ -13,6 +13,8 @@
*/
public class FunkinGame extends FlixelGame {
+ private float lastVolume = 1.0f;
+
public FunkinGame(String title, int width, int height, FlixelScreen initialScreen) {
super(title, width, height, initialScreen);
}
@@ -26,10 +28,38 @@ public void create() {
@Override
public void render() {
super.render();
-
Polyverse.forAllScripts(script -> script.onRender(Flixel.getDelta()));
}
+ @Override
+ public void dispose() {
+ super.dispose();
+ Polyverse.forAllScripts(Script::onDispose);
+ }
+
+ @Override
+ public void onWindowFocused() {
+ super.onWindowFocused();
+ Flixel.setMasterVolume(lastVolume);
+ Polyverse.forEachScript(SystemScript.class, SystemScript::onWindowFocused);
+ }
+
+ @Override
+ public void onWindowUnfocused() {
+ super.onWindowUnfocused();
+ lastVolume = Flixel.getMasterVolume();
+ Flixel.setMasterVolume(0.008f);
+ Polyverse.forEachScript(SystemScript.class, SystemScript::onWindowUnfocused);
+ }
+
+ @Override
+ public void onWindowMinimized(boolean iconified) {
+ super.onWindowMinimized(iconified);
+ lastVolume = Flixel.getMasterVolume();
+ Flixel.setMasterVolume(0);
+ Polyverse.forEachScript(SystemScript.class, script -> script.onWindowMinimized(iconified));
+ }
+
private void configurePolyverse() {
// Register Polyverse script types.
Polyverse.registerScriptType(Script.class); // Master type, DO NOT REMOVE THIS!
diff --git a/core/src/main/java/me/stringdotjar/funkin/init/InitScreen.java b/funkin/src/main/java/me/stringdotjar/funkin/InitScreen.java
similarity index 89%
rename from core/src/main/java/me/stringdotjar/funkin/init/InitScreen.java
rename to funkin/src/main/java/me/stringdotjar/funkin/InitScreen.java
index f73bf8c..e90d619 100644
--- a/core/src/main/java/me/stringdotjar/funkin/init/InitScreen.java
+++ b/funkin/src/main/java/me/stringdotjar/funkin/InitScreen.java
@@ -1,4 +1,4 @@
-package me.stringdotjar.funkin.init;
+package me.stringdotjar.funkin;
import me.stringdotjar.flixelgdx.graphics.screen.FlixelScreen;
import me.stringdotjar.flixelgdx.Flixel;
diff --git a/core/src/main/java/me/stringdotjar/funkin/menus/TitleScreen.java b/funkin/src/main/java/me/stringdotjar/funkin/menus/TitleScreen.java
similarity index 94%
rename from core/src/main/java/me/stringdotjar/funkin/menus/TitleScreen.java
rename to funkin/src/main/java/me/stringdotjar/funkin/menus/TitleScreen.java
index 5597818..99c78e2 100644
--- a/core/src/main/java/me/stringdotjar/funkin/menus/TitleScreen.java
+++ b/funkin/src/main/java/me/stringdotjar/funkin/menus/TitleScreen.java
@@ -51,16 +51,16 @@ public void render(float elapsed) {
float speed = 500 * elapsed;
if (Flixel.keyPressed(Input.Keys.W)) {
- logo.setY(logo.getY() + speed);
+ logo.changeY(speed);
}
if (Flixel.keyPressed(Input.Keys.S)) {
- logo.setY(logo.getY() - speed);
+ logo.changeY(-speed);
}
if (Flixel.keyPressed(Input.Keys.A)) {
- logo.setX(logo.getX() - speed);
+ logo.changeX(-speed);
}
if (Flixel.keyPressed(Input.Keys.D)) {
- logo.setX(logo.getX() + speed);
+ logo.changeX(speed);
}
if (Flixel.keyJustPressed(Input.Keys.SPACE)) {
diff --git a/core/src/main/java/me/stringdotjar/funkin/util/FunkinConstants.java b/funkin/src/main/java/me/stringdotjar/funkin/util/FunkinConstants.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/funkin/util/FunkinConstants.java
rename to funkin/src/main/java/me/stringdotjar/funkin/util/FunkinConstants.java
diff --git a/lwjgl3/build.gradle b/lwjgl3/build.gradle
index aedb396..2038bf9 100644
--- a/lwjgl3/build.gradle
+++ b/lwjgl3/build.gradle
@@ -29,7 +29,8 @@ dependencies {
implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
implementation "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
- implementation project(':core')
+ implementation project(':funkin')
+ implementation project(":flixelgdx")
if (enableGraalNative == 'true') {
implementation "io.github.berstanio:gdx-svmhelper-backend-lwjgl3:$graalHelperVersion"
diff --git a/lwjgl3/src/main/java/me/stringdotjar/funkin/lwjgl3/Lwjgl3Launcher.java b/lwjgl3/src/main/java/me/stringdotjar/funkin/lwjgl3/Lwjgl3Launcher.java
index 6a84013..7222c22 100644
--- a/lwjgl3/src/main/java/me/stringdotjar/funkin/lwjgl3/Lwjgl3Launcher.java
+++ b/lwjgl3/src/main/java/me/stringdotjar/funkin/lwjgl3/Lwjgl3Launcher.java
@@ -5,7 +5,7 @@
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3WindowAdapter;
import me.stringdotjar.flixelgdx.Flixel;
import me.stringdotjar.funkin.FunkinGame;
-import me.stringdotjar.funkin.init.InitScreen;
+import me.stringdotjar.funkin.InitScreen;
import me.stringdotjar.funkin.util.FunkinConstants;
/** Launches the desktop (LWJGL3) application. */
@@ -26,12 +26,13 @@ private static void createApplication() {
new InitScreen()
);
Flixel.initialize(game); // This is VERY important to do before creating the application!
- new Lwjgl3Application(game, createWindowConfiguration());
+ var size = game.getWindowSize();
+ new Lwjgl3Application(game, createWindowConfiguration(game.getTitle(), (int) size.x, (int) size.y));
}
- private static Lwjgl3ApplicationConfiguration createWindowConfiguration() {
+ private static Lwjgl3ApplicationConfiguration createWindowConfiguration(String title, int width, int height) {
Lwjgl3ApplicationConfiguration configuration = new Lwjgl3ApplicationConfiguration();
- configuration.setTitle("Friday Night Flixel': Java Edition");
+ configuration.setTitle(title);
// Vsync limits the frames per second to what your hardware can display, and helps eliminate
// screen tearing. This setting doesn't always work on Linux, so the line after is a safeguard.
configuration.useVsync(true);
@@ -41,7 +42,7 @@ private static Lwjgl3ApplicationConfiguration createWindowConfiguration() {
// If you remove the above line and set Vsync to false, you can get unlimited FPS, which can be
// useful for testing performance, but can also be very stressful to some hardware.
// You may also need to configure GPU drivers to fully disable Vsync; this can cause screen tearing.
- configuration.setWindowedMode(FunkinConstants.WINDOW_WIDTH, FunkinConstants.WINDOW_HEIGHT);
+ configuration.setWindowedMode(width, height);
// You can change these files; they are in lwjgl3/src/main/resources/ .
// They can also be loaded from the root of assets/ .
configuration.setWindowIcon("icon128.png", "icon64.png", "icon32.png", "icon16.png");
diff --git a/polyverse/build.gradle b/polyverse/build.gradle
new file mode 100644
index 0000000..cc873dd
--- /dev/null
+++ b/polyverse/build.gradle
@@ -0,0 +1,9 @@
+[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
+eclipse.project.name = appName + '-polyverse'
+
+dependencies {
+ api project(":flixelgdx")
+
+ implementation "org.apache.groovy:groovy:5.0.3"
+ implementation "org.jetbrains:annotations:26.0.2-1"
+}
diff --git a/core/src/main/java/me/stringdotjar/polyverse/Polyverse.java b/polyverse/src/main/java/me/stringdotjar/polyverse/Polyverse.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/polyverse/Polyverse.java
rename to polyverse/src/main/java/me/stringdotjar/polyverse/Polyverse.java
diff --git a/core/src/main/java/me/stringdotjar/polyverse/script/type/Script.java b/polyverse/src/main/java/me/stringdotjar/polyverse/script/type/Script.java
similarity index 100%
rename from core/src/main/java/me/stringdotjar/polyverse/script/type/Script.java
rename to polyverse/src/main/java/me/stringdotjar/polyverse/script/type/Script.java
diff --git a/polyverse/src/main/java/me/stringdotjar/polyverse/script/type/SystemScript.java b/polyverse/src/main/java/me/stringdotjar/polyverse/script/type/SystemScript.java
new file mode 100644
index 0000000..1024e0e
--- /dev/null
+++ b/polyverse/src/main/java/me/stringdotjar/polyverse/script/type/SystemScript.java
@@ -0,0 +1,17 @@
+package me.stringdotjar.polyverse.script.type;
+
+public abstract class SystemScript extends Script {
+
+ public SystemScript(String id) {
+ super(id);
+ }
+
+ /** Called when the game window gains focus. */
+ public void onWindowFocused() {}
+
+ /** Called when the game window loses focus. */
+ public void onWindowUnfocused() {}
+
+ /** Called when the game window is minimized. */
+ public void onWindowMinimized(boolean iconified) {}
+}
diff --git a/settings.gradle b/settings.gradle
index bf5968d..b3a82d8 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -5,4 +5,4 @@ plugins {
// A list of which subprojects to load as part of the same larger project.
// You can remove Strings from the list and reload the Gradle project
// if you want to temporarily disable a subproject.
-include 'lwjgl3', 'android', 'core'
+include 'funkin', 'flixelgdx', 'polyverse', 'lwjgl3', 'android'