Improve Animation Runners

This commit is contained in:
2021-03-23 09:25:01 +01:00
parent 23accf16df
commit ca89c302de
4 changed files with 75 additions and 4 deletions

View File

@@ -2,8 +2,11 @@ package com.bartlomiejpluta.base.lib.animation;
import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
import com.bartlomiejpluta.base.api.move.Movable;
import org.joml.Vector2fc;
public interface AnimationRunner {
void run(Context context, Layer layer, Vector2fc origin);
void run(Context context, Layer layer, Movable origin);
}

View File

@@ -6,6 +6,7 @@ import com.bartlomiejpluta.base.api.entity.Entity;
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
import com.bartlomiejpluta.base.api.move.Direction;
import com.bartlomiejpluta.base.api.move.Movable;
import com.bartlomiejpluta.base.util.path.Path;
import org.joml.Vector2fc;
import org.joml.Vector2i;
@@ -110,7 +111,25 @@ public class BulletAnimationRunner implements AnimationRunner {
@Override
public void run(Context context, Layer layer, Vector2fc origin) {
var animation = new BulletAnimation(context.createAnimation(animationUid), delay, direction, onHit);
animation.setPosition(origin.x() + offsetX, origin.y() + offsetY);
animation.setPosition(origin);
animation.setScale(scale);
animation.setAnimationSpeed(animationSpeed);
animation.setSpeed(speed);
animation.setRotation(rotation);
animation.setRepeat(repeat);
animation.setPositionOffset(offsetX, offsetY);
animation.followPath(path, range, true, true);
layer.pushAnimation(animation);
}
@Override
public void run(Context context, Layer layer, Movable origin) {
var animation = new BulletAnimation(context.createAnimation(animationUid), delay, direction, onHit);
animation.setCoordinates(origin.getCoordinates());
animation.setScale(scale);
animation.setAnimationSpeed(animationSpeed);
animation.setSpeed(speed);

View File

@@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.lib.animation;
import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
import com.bartlomiejpluta.base.api.move.Movable;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.distribution.RealDistribution;
import org.apache.commons.math3.distribution.UniformRealDistribution;
@@ -134,11 +135,12 @@ public class RandomAnimationsRunner implements AnimationRunner {
var animation = context.createAnimation(animationUids.get(random.nextInt(animationUids.size())));
if (rangeDistribution != null) {
animation.setPosition(origin.x() + (float) rangeDistribution.sample() + offsetX, origin.y() + (float) rangeDistribution.sample() + offsetY);
animation.setPosition(origin.x() + (float) rangeDistribution.sample(), origin.y() + (float) rangeDistribution.sample());
} else {
animation.setPosition(origin.x() + rangeX + offsetX, origin.y() + rangeY + offsetY);
animation.setPosition(origin.x() + rangeX, origin.y() + rangeY);
}
animation.setPositionOffset(offsetX, offsetY);
animation.setScale(scaleDistribution != null ? (float) scaleDistribution.sample() : scale);
animation.setAnimationSpeed(animationSpeedDistribution != null ? (float) animationSpeedDistribution.sample() : animationSpeed);
animation.setRotation(rotationDistribution != null ? (float) rotationDistribution.sample() : rotation);
@@ -146,4 +148,28 @@ public class RandomAnimationsRunner implements AnimationRunner {
layer.pushAnimation(new DelayedAnimation(animation, (int) (delayDistribution != null ? delayDistribution.sample() : delay)));
}
}
@Override
public void run(Context context, Layer layer, Movable origin) {
for (int i = 0; i < count; ++i) {
var animation = context.createAnimation(animationUids.get(random.nextInt(animationUids.size())));
var position = origin.getPosition();
var offset = origin.getPositionOffset();
if (rangeDistribution != null) {
animation.setPosition(position.x() - offset.x() + (float) rangeDistribution.sample(), position.y() - offset.y() + (float) rangeDistribution.sample());
} else {
animation.setPosition(position.x() - offset.x() + rangeX, position.y() - offset.y() + rangeY);
}
animation.setPositionOffset(offsetX, offsetY);
animation.setScale(scaleDistribution != null ? (float) scaleDistribution.sample() : scale);
animation.setAnimationSpeed(animationSpeedDistribution != null ? (float) animationSpeedDistribution.sample() : animationSpeed);
animation.setRotation(rotationDistribution != null ? (float) rotationDistribution.sample() : rotation);
layer.pushAnimation(new DelayedAnimation(animation, (int) (delayDistribution != null ? delayDistribution.sample() : delay)));
}
}
}

View File

@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.lib.animation;
import com.bartlomiejpluta.base.api.animation.Animation;
import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
import com.bartlomiejpluta.base.api.move.Movable;
import com.bartlomiejpluta.base.util.path.Path;
import org.joml.Vector2fc;
@@ -82,7 +83,9 @@ public class SimpleAnimationRunner implements AnimationRunner {
@Override
public void run(Context context, Layer layer, Vector2fc origin) {
var animation = new DelayedAnimation(context.createAnimation(animationUid), delay);
animation.setPosition(origin.x() + offsetX, origin.y() + offsetY);
animation.setPosition(origin);
animation.setPositionOffset(offsetX, offsetY);
animation.setScale(scale);
animation.setAnimationSpeed(animationSpeed);
animation.setSpeed(speed);
@@ -95,4 +98,24 @@ public class SimpleAnimationRunner implements AnimationRunner {
layer.pushAnimation(animation);
}
@Override
public void run(Context context, Layer layer, Movable origin) {
var animation = new DelayedAnimation(context.createAnimation(animationUid), delay);
animation.setCoordinates(origin.getCoordinates());
animation.setPositionOffset(offsetX, offsetY);
animation.setScale(scale);
animation.setAnimationSpeed(animationSpeed);
animation.setSpeed(speed);
animation.setRotation(rotation);
animation.setRepeat(repeat);
if (path != null) {
animation.followPath(path, repeatPath, finishOnPathEnd, finishOnPathFail);
}
layer.pushAnimation(animation);
}
}