From f581e41dbad4577969ecc67d8ce045aab0b7fc04 Mon Sep 17 00:00:00 2001 From: RazorPlay01 <82621554+RazorPlay01@users.noreply.github.com> Date: Sun, 15 Dec 2024 12:52:28 -0500 Subject: [PATCH] Refactor first-person mode and configuration logic - Introduced `getFirstPersonModifierOrDefault()` method to encapsulate the logic for retrieving the highest-priority modifier, prioritizing `FirstPersonModifier` instances. - Updated `getFirstPersonMode(float tickDelta)` to delegate the retrieval of modifiers and animations more cleanly. - Updated `getFirstPersonConfiguration(float tickDelta)` with a similar streamlined logic. - Added detailed Javadoc comments to explain the behavior and logic of the methods: - `getFirstPersonMode(float tickDelta)` - `getFirstPersonConfiguration(float tickDelta)` - `getFirstPersonModifierOrDefault()` - Improved code readability, modularity, and maintainability by reducing duplication and clarifying method responsibilities. This refactoring ensures greater clarity for future developers and prepares the code base for possible extensions, as well as prioritizing the newly created FirstPersonModifier. --- .../playerAnim/api/layered/ModifierLayer.java | 80 +++++++++++++++++-- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/coreLib/src/main/java/dev/kosmx/playerAnim/api/layered/ModifierLayer.java b/coreLib/src/main/java/dev/kosmx/playerAnim/api/layered/ModifierLayer.java index 2983ad2..6aa3a36 100644 --- a/coreLib/src/main/java/dev/kosmx/playerAnim/api/layered/ModifierLayer.java +++ b/coreLib/src/main/java/dev/kosmx/playerAnim/api/layered/ModifierLayer.java @@ -5,12 +5,14 @@ import dev.kosmx.playerAnim.api.firstPerson.FirstPersonMode; import dev.kosmx.playerAnim.api.layered.modifier.AbstractFadeModifier; import dev.kosmx.playerAnim.api.layered.modifier.AbstractModifier; +import dev.kosmx.playerAnim.api.layered.modifier.FirstPersonModifier; import dev.kosmx.playerAnim.core.util.Ease; import dev.kosmx.playerAnim.core.util.Vec3f; import lombok.Getter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; + import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -80,6 +82,7 @@ public void setAnimation(@Nullable T animation) { /** * Fade out from current animation into new animation. * Does not fade if there is currently no active animation + * * @param fadeModifier Fade modifier, use {@link AbstractFadeModifier#standardFadeIn(int, Ease)} for simple fade. * @param newAnimation New animation, can be null to fade into default state. */ @@ -89,6 +92,7 @@ public void replaceAnimationWithFade(@NotNull AbstractFadeModifier fadeModifier, /** * Fade out from current to a new animation + * * @param fadeModifier Fade modifier, use {@link AbstractFadeModifier#standardFadeIn(int, Ease)} for simple fade. * @param newAnimation New animation, can be null to fade into default state. * @param fadeFromNothing Do fade even if we go from nothing. (for KeyframeAnimation, it can be false by default) @@ -142,19 +146,83 @@ public void setupAnim(float tickDelta) { } else if (animation != null) animation.setupAnim(tickDelta); } + /** + * Retrieves the {@link FirstPersonMode} for the current object, based on the provided {@code tickDelta}. + *
+ * The method determines the appropriate {@link FirstPersonMode} by following this logic: + * 1. It first attempts to retrieve the active {@link AbstractModifier} by calling {@link #getFirstPersonModifierOrDefault()}. + * If a modifier is found, it delegates the call to the modifier's {@link AbstractModifier#getFirstPersonMode(float)} method. + * 2. If no modifier is available or applicable, it checks if there is an active animation. + * If an animation exists, it delegates the call to the animation's {@link IAnimation#getFirstPersonMode(float)} method. + * 3. If neither a modifier nor an animation is present, it falls back to the default implementation + * provided by the {@link IAnimation} interface. + * + * @param tickDelta A float value representing the partial tick time (used to interpolate between frames). + * This parameter is typically used in rendering calculations. + * @return The {@link FirstPersonMode} determined by the current modifier, animation, or the default implementation. + */ @Override public @NotNull FirstPersonMode getFirstPersonMode(float tickDelta) { - if (modifiers.size() > 0) { - return modifiers.get(0).getFirstPersonMode(tickDelta); - } else if (animation != null) return animation.getFirstPersonMode(tickDelta); + AbstractModifier modifier = getFirstPersonModifierOrDefault(); + if (modifier != null) { + return modifier.getFirstPersonMode(tickDelta); + } + + if (animation != null) { + return animation.getFirstPersonMode(tickDelta); + } return IAnimation.super.getFirstPersonMode(tickDelta); } + /** + * Retrieves the {@link FirstPersonConfiguration} for the current object, based on the provided {@code tickDelta}. + *
+ * The method determines the appropriate {@link FirstPersonConfiguration} by following this logic: + * 1. It first attempts to retrieve the active {@link AbstractModifier} by calling {@link #getFirstPersonModifierOrDefault()}. + * If a modifier is found, it delegates the call to the modifier's {@link AbstractModifier#getFirstPersonConfiguration(float)} method. + * 2. If no modifier is available or applicable, it checks if there is an active animation. + * If an animation exists, it delegates the call to the animation's {@link IAnimation#getFirstPersonConfiguration(float)} method. + * 3. If neither a modifier nor an animation is present, it falls back to the default implementation + * provided by the {@link IAnimation} interface. + * + * @param tickDelta A float value representing the partial tick time (used to interpolate between frames). + * This parameter is typically used in rendering calculations. + * @return The {@link FirstPersonConfiguration} determined by the current modifier, animation, or the default implementation. + */ @Override public @NotNull FirstPersonConfiguration getFirstPersonConfiguration(float tickDelta) { - if (modifiers.size() > 0) { - return modifiers.get(0).getFirstPersonConfiguration(tickDelta); - } else if (animation != null) return animation.getFirstPersonConfiguration(tickDelta); + AbstractModifier modifier = getFirstPersonModifierOrDefault(); + if (modifier != null) { + return modifier.getFirstPersonConfiguration(tickDelta); + } + + if (animation != null) { + return animation.getFirstPersonConfiguration(tickDelta); + } return IAnimation.super.getFirstPersonConfiguration(tickDelta); } + + /** + * Searches for and retrieves the highest-priority {@link AbstractModifier} + * from the list of modifiers, prioritizing instances of {@link FirstPersonModifier}. + *
+ * The method performs the following: + * 1. Iterates through the {@code modifiers} list to find the first instance + * of {@link FirstPersonModifier}. If found, it is returned immediately. + * 2. If no {@link FirstPersonModifier} is found, the first modifier in the list + * (if it exists) is returned as a fallback. + * 3. If the list of modifiers is empty, {@code null} is returned. + * + * @return The highest-priority {@link AbstractModifier}, or {@code null} if + * the modifier list is empty. + */ + private AbstractModifier getFirstPersonModifierOrDefault() { + for (AbstractModifier modifier : modifiers) { + if (modifier instanceof FirstPersonModifier) { + return modifier; + } + } + + return modifiers.isEmpty() ? null : modifiers.get(0); + } }