Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
rework KeyframeAnimations
Browse files Browse the repository at this point in the history
Signed-off-by: Malte Dostal <[email protected]>
  • Loading branch information
sanj0 committed Sep 7, 2021
1 parent 3f44459 commit 515ac64
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 883 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@

package de.edgelord.saltyengine.components;

import de.edgelord.saltyengine.core.animation.LinearKeyframeAnimation;
import de.edgelord.saltyengine.core.animation.KeyframeAnimation;
import de.edgelord.saltyengine.core.animation.TransitionFunction;
import de.edgelord.saltyengine.core.event.CollisionEvent;
import de.edgelord.saltyengine.core.graphics.SaltyGraphics;
import de.edgelord.saltyengine.core.stereotypes.ComponentContainer;
import de.edgelord.saltyengine.transform.Vector2f;

/**
* Uses a {@link LinearKeyframeAnimation} to make its parent grow and shrink
* again when the cursor touches is.
* Uses a {@link KeyframeAnimation} to make its parent grow and shrink again
* when the cursor touches is.
*/
public class DeflectionOnMouseOverComponent extends Component<ComponentContainer> {

/**
* The animation that is used.
*/
private final LinearKeyframeAnimation keyframeAnimation = new LinearKeyframeAnimation();

private final KeyframeAnimation keyframeAnimation = new KeyframeAnimation(TransitionFunction.easeInOutSine());
private final Vector2f returnPosition;

private float totalDeflection = 0f;

/**
* Whether to loop the animation or only playing it whenever the cursor
* enters the parent
*/
private boolean loop;

private boolean shouldPlay = false;

private boolean singleDeflection = false;

private boolean impact = false;

/**
Expand All @@ -64,17 +60,16 @@ public DeflectionOnMouseOverComponent(final ComponentContainer parent, final Str

this.loop = loop;

keyframeAnimation.add(interval, distance);
keyframeAnimation.add(interval * 2, 0);
keyframeAnimation.calculateAnimation();
keyframeAnimation.appendFrame(0, 0);
keyframeAnimation.appendFrame(interval, distance);
keyframeAnimation.appendFrame(interval * 2, 0);

final Vector2f currentCentre = getParent().getTransform().getCentre();
returnPosition = new Vector2f(currentCentre.getX(), currentCentre.getY());
}

@Override
public void onCursorEntersParent() {

if (!loop) {
singleDeflection = true;
} else {
Expand All @@ -88,7 +83,7 @@ public void onCursorExitsParent() {
singleDeflection = false;
shouldPlay = false;
totalDeflection = 0f;
keyframeAnimation.restart();
keyframeAnimation.reset();
}

@Override
Expand All @@ -98,15 +93,13 @@ public void draw(final SaltyGraphics saltyGraphics) {

@Override
public void onFixedTick() {

if (keyframeAnimation.animationEnded()) {

if (keyframeAnimation.finished()) {
if (impact) {
resetTransform();
impact = false;
}

keyframeAnimation.restart();
keyframeAnimation.reset();
if (!loop) {
singleDeflection = false;
}
Expand All @@ -125,7 +118,7 @@ private void resetTransform() {
}

private void nextFrame() {
final float delta = keyframeAnimation.nextDelta();
final float delta = keyframeAnimation.nextValue() - totalDeflection;
totalDeflection += delta;

getParent().setWidth(getParent().getWidth() + delta);
Expand All @@ -140,7 +133,6 @@ private void nextFrame() {
* there should be no need to call it manually.
*/
public void updatePosition() {

final Vector2f centre = getParent().getTransform().getCentre();
returnPosition.setX(centre.getX());
returnPosition.setY(centre.getY());
Expand Down Expand Up @@ -182,6 +174,6 @@ public void cancel() {
impact = false;
shouldPlay = false;
totalDeflection = 0f;
keyframeAnimation.restart();
keyframeAnimation.reset();
}
}

This file was deleted.

Loading

0 comments on commit 515ac64

Please sign in to comment.