Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
BiConsumer committed Oct 15, 2023
1 parent 59dc3c8 commit e3aabf9
Show file tree
Hide file tree
Showing 19 changed files with 579 additions and 84 deletions.
19 changes: 16 additions & 3 deletions api/src/main/java/team/unnamed/hephaestus/animation/Animation.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import net.kyori.examination.Examinable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import team.unnamed.hephaestus.animation.timeline.BoneTimeline;
import team.unnamed.hephaestus.animation.timeline.bone.BoneTimeline;
import team.unnamed.hephaestus.animation.timeline.effects.EffectsTimeline;

import java.util.Map;

Expand All @@ -53,9 +54,10 @@ public interface Animation extends Examinable {
final @NotNull String name,
final int length,
final @NotNull LoopMode loopMode,
final @NotNull Map<String, BoneTimeline> timelines
final @NotNull Map<String, BoneTimeline> timelines,
final @NotNull EffectsTimeline effectsTimeline
) {
return new AnimationImpl(name, length, loopMode, timelines);
return new AnimationImpl(name, length, loopMode, timelines, effectsTimeline);
}

/**
Expand Down Expand Up @@ -110,6 +112,8 @@ public interface Animation extends Examinable {
*/
@NotNull Map<String, BoneTimeline> timelines();

@NotNull EffectsTimeline effectsTimeline();

/**
* An enum containing all the possible
* loop mode values, they specify what the
Expand Down Expand Up @@ -192,6 +196,15 @@ interface Builder {
*/
@NotNull Builder timelines(final @NotNull Map<String, BoneTimeline> timelines);

/**
* Sets the animation effects timeline
*
* @param timeline The animation effects timeline
* @return This builder
* @since 1.0.0
*/
@NotNull Builder effectsTimeline(final @NotNull EffectsTimeline timeline);

/**
* Adds a bone timeline to the animation
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import net.kyori.examination.ExaminableProperty;
import net.kyori.examination.string.StringExaminer;
import org.jetbrains.annotations.NotNull;
import team.unnamed.hephaestus.animation.timeline.BoneTimeline;
import team.unnamed.hephaestus.animation.timeline.bone.BoneTimeline;
import team.unnamed.hephaestus.animation.timeline.effects.EffectsTimeline;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -41,17 +42,20 @@ final class AnimationImpl implements Animation {
private final int length;
private final LoopMode loopMode;
private final Map<String, BoneTimeline> timelines;
private final EffectsTimeline effectsTimeline;

AnimationImpl(
final @NotNull String name,
final int length,
final @NotNull LoopMode loopMode,
final @NotNull Map<String, BoneTimeline> timelines
final @NotNull Map<String, BoneTimeline> timelines,
final @NotNull EffectsTimeline effectsTimeline
) {
this.name = requireNonNull(name, "name");
this.length = length;
this.loopMode = requireNonNull(loopMode, "loopMode");
this.timelines = requireNonNull(timelines, "timelines");
this.effectsTimeline = requireNonNull(effectsTimeline, "effectsTimeline");;
}

@Override
Expand All @@ -74,6 +78,11 @@ public int length() {
return timelines;
}

@Override
public @NotNull EffectsTimeline effectsTimeline() {
return effectsTimeline;
}

@Override
public @NotNull Stream<? extends ExaminableProperty> examinableProperties() {
return Stream.of(
Expand Down Expand Up @@ -111,6 +120,7 @@ static final class BuilderImpl implements Builder {
private int length;
private LoopMode loopMode;
private Map<String, BoneTimeline> timelines;
private EffectsTimeline effectsTimeline;

@Override
public @NotNull Builder name(final @NotNull String name) {
Expand All @@ -136,6 +146,12 @@ static final class BuilderImpl implements Builder {
return this;
}

@Override
public @NotNull Builder effectsTimeline(@NotNull EffectsTimeline timeline) {
this.effectsTimeline = requireNonNull(timeline, "effectsTimeline");
return this;
}

@Override
public @NotNull Builder timeline(@NotNull String boneName, @NotNull BoneTimeline timeline) {
requireNonNull(boneName, "boneName");
Expand All @@ -149,7 +165,7 @@ static final class BuilderImpl implements Builder {

@Override
public @NotNull Animation build() {
return new AnimationImpl(name, length, loopMode, timelines);
return new AnimationImpl(name, length, loopMode, timelines, effectsTimeline);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
*/
package team.unnamed.hephaestus.animation.controller;

import net.kyori.adventure.sound.Sound;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import team.unnamed.creative.base.Vector3Float;
import team.unnamed.hephaestus.Bone;
import team.unnamed.hephaestus.animation.Animation;
import team.unnamed.hephaestus.animation.timeline.BoneFrame;
import team.unnamed.hephaestus.animation.timeline.BoneTimeline;
import team.unnamed.hephaestus.animation.timeline.BoneTimelinePlayhead;
import team.unnamed.hephaestus.animation.timeline.bone.BoneFrame;
import team.unnamed.hephaestus.animation.timeline.bone.BoneTimeline;
import team.unnamed.hephaestus.animation.timeline.bone.BoneTimelinePlayhead;
import team.unnamed.hephaestus.animation.timeline.Timeline;
import team.unnamed.hephaestus.animation.timeline.effects.EffectsFrame;
import team.unnamed.hephaestus.animation.timeline.effects.EffectsTimeline;
import team.unnamed.hephaestus.animation.timeline.effects.EffectsTimelinePlayhead;
import team.unnamed.hephaestus.util.Quaternion;
import team.unnamed.hephaestus.util.Vectors;
import team.unnamed.hephaestus.view.BaseBoneView;
Expand All @@ -49,6 +54,7 @@ class NormalAnimationController implements AnimationController {
private final BaseModelView<?> view;

private final Map<String, BoneTimelinePlayhead> iterators = new HashMap<>();
private @NotNull EffectsTimelinePlayhead effectsIterator = new EffectsTimelinePlayhead(EffectsTimeline.empty().build());

private final Map<String, BoneFrame> lastFrames = new HashMap<>();

Expand Down Expand Up @@ -76,7 +82,8 @@ public synchronized void queue(Animation animation, int transitionTicks) {
final Animation.Builder transitionAnimationBuilder = Animation.animation()
.name("$$hephaestus_transition_animation")
.length(transitionTicks)
.loopMode(Animation.LoopMode.HOLD);
.loopMode(Animation.LoopMode.HOLD)
.effectsTimeline(EffectsTimeline.empty().build());

for (Map.Entry<String, BoneFrame> entry : lastFrames.entrySet()) {
String boneName = entry.getKey();
Expand Down Expand Up @@ -175,6 +182,21 @@ public synchronized void tick(Quaternion initialRotation, Vector3Float initialPo
Vector3Float.ONE
);
}

if (currentAnimation == null) {
return;
}

EffectsFrame effectsFrame = effectsIterator.next();
if (effectsIterator.tick() + 1 >= currentAnimation.length()) {
return;
}

Sound[] sounds = effectsFrame.sounds();

for (Sound sound : sounds) {
view.playSound(sound);
}
}

private void nextAnimation() {
Expand All @@ -186,7 +208,9 @@ private void nextAnimation() {
private void createIterators(Animation animation) {
iterators.clear();
lastFrames.clear();

animation.timelines().forEach((name, list) -> iterators.put(name, list.createPlayhead()));
effectsIterator = animation.effectsTimeline().createPlayhead();
}

private BoneFrame nextFrame(String boneName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public interface Interpolator<T> {
return CatmullRomInterpolator.INSTANCE;
}

static <T> @NotNull Interpolator<T> staticInterpolator(T empty) {
return new StaticInterpolator<>(empty);
}

/**
* Returns an interpolator that will create interpolations that always
* return the provided {@code interpolated} value and will not perform
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of hephaestus-engine, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.hephaestus.animation.interpolation;

import org.jetbrains.annotations.NotNull;

final class StaticInterpolator<T> implements Interpolator<T> {

private final T empty;

StaticInterpolator(T empty) {
this.empty = empty;
}

@Override
public @NotNull Interpolation<T> interpolation(@NotNull T from, @NotNull T to) {
return new StaticInterpolation<>(empty, from, to);
}

static final class StaticInterpolation<T> implements Interpolation<T> {

private final T empty;
private final T from;
private final T to;

StaticInterpolation(
final @NotNull T empty,
final @NotNull T from,
final @NotNull T to
) {
this.empty = empty;
this.from = from;
this.to = to;
}

@Override
public @NotNull T from() {
return from;
}

@Override
public @NotNull T to() {
return to;
}

@Override
public @NotNull T interpolate(final double progress) {
if (progress == 0) {
return to;
}

return empty;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ public Interpolator<T> interpolatorOr(Interpolator<T> fallback) {
return interpolator == null ? fallback : interpolator;
}

@Override
public String toString() {
return "KeyFrame{" +
"time=" + time +
", value=" + value +
", interpolator=" + interpolator +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @param <T> The type of values in this timeline
*/
public interface Timeline<T> extends Examinable {

static <T> Builder<T> timeline() {
return new TimelineImpl.BuilderImpl<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public Playhead<T> createPlayhead() {
return Playhead.playhead(this);
}

@Override
public String toString() {
return "TimelineImpl{" +
"initialValue=" + initialValue +
", defaultInterpolator=" + defaultInterpolator +
", keyFrames=" + keyFrames +
'}';
}

static final class BuilderImpl<T> implements Builder<T> {

private T initialValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.hephaestus.animation.timeline;
package team.unnamed.hephaestus.animation.timeline.bone;

import net.kyori.examination.Examinable;
import net.kyori.examination.ExaminableProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.hephaestus.animation.timeline;
package team.unnamed.hephaestus.animation.timeline.bone;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import team.unnamed.creative.base.Vector3Float;
import team.unnamed.hephaestus.animation.timeline.Timeline;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.hephaestus.animation.timeline;
package team.unnamed.hephaestus.animation.timeline.bone;

import org.jetbrains.annotations.NotNull;
import team.unnamed.creative.base.Vector3Float;
import team.unnamed.hephaestus.animation.timeline.Timeline;

import static java.util.Objects.requireNonNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.hephaestus.animation.timeline;
package team.unnamed.hephaestus.animation.timeline.bone;

import team.unnamed.creative.base.Vector3Float;
import team.unnamed.hephaestus.animation.timeline.playhead.Playhead;
Expand Down
Loading

0 comments on commit e3aabf9

Please sign in to comment.