first successful tween
This commit is contained in:
parent
91169a40b0
commit
2ac964feeb
|
@ -31,6 +31,7 @@ public abstract class BaseGame {
|
||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
window.tick();
|
window.tick();
|
||||||
//todo: frame rate control
|
//todo: frame rate control
|
||||||
|
rootNode.tick();
|
||||||
update(1000.0 / 60.0);
|
update(1000.0 / 60.0);
|
||||||
var rc = window.renderStart();
|
var rc = window.renderStart();
|
||||||
renderPre(rc);
|
renderPre(rc);
|
||||||
|
|
|
@ -11,9 +11,11 @@ public class TestNode extends BaseGame {
|
||||||
sprite.setTexture(window.loadTexture("demoassets/test.png"));
|
sprite.setTexture(window.loadTexture("demoassets/test.png"));
|
||||||
sprite.setPos(new Vec2f(100, 100));
|
sprite.setPos(new Vec2f(100, 100));
|
||||||
rootNode.add(sprite);
|
rootNode.add(sprite);
|
||||||
|
sprite.tweenPos(new Vec2f(800, 400), 120).delay(600);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new TestNode().run();
|
new TestNode().run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ package com.danitheskunk.skunkworks.nodes;
|
||||||
|
|
||||||
public abstract class BaseTween {
|
public abstract class BaseTween {
|
||||||
protected double progress;
|
protected double progress;
|
||||||
|
protected int startFrame;
|
||||||
|
protected int endFrame;
|
||||||
|
|
||||||
public BaseTween() {
|
public BaseTween() {
|
||||||
progress = 0;
|
progress = 0;
|
||||||
|
@ -11,5 +13,10 @@ public abstract class BaseTween {
|
||||||
this.progress = progress;
|
this.progress = progress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void delay(int frames) {
|
||||||
|
startFrame += frames;
|
||||||
|
endFrame += frames;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract void apply();
|
protected abstract void apply();
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,12 +56,13 @@ public class Node implements Iterable<Node> {
|
||||||
return Vec2f.add(parent.getAbsolutePos(), pos);
|
return Vec2f.add(parent.getAbsolutePos(), pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getRoot() {
|
public NodeRoot getRoot() {
|
||||||
var node = this;
|
var node = this;
|
||||||
while(node.parent != null) {
|
while(node.parent != null) {
|
||||||
node = node.parent;
|
node = node.parent;
|
||||||
}
|
}
|
||||||
return node;
|
//todo: check if right class
|
||||||
|
return (NodeRoot) node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -69,15 +70,30 @@ public class Node implements Iterable<Node> {
|
||||||
return children.iterator();
|
return children.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TweenPos extends BaseTween {
|
public int getCurrentFrame() {
|
||||||
private final Vec2f start;
|
return getRoot().getCurrentFrame();
|
||||||
private final Vec2f end;
|
|
||||||
|
|
||||||
public TweenPos(Vec2f start, Vec2f end) {
|
|
||||||
this.start = start;
|
|
||||||
this.end = end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void prepareTween(BaseTween tween, int frames) {
|
||||||
|
var root = getRoot();
|
||||||
|
tween.startFrame = root.getCurrentFrame();
|
||||||
|
tween.endFrame = tween.startFrame + frames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TweenPos tweenPos(Vec2f to, int frames) {
|
||||||
|
var tween = new TweenPos();
|
||||||
|
tween.start = pos;
|
||||||
|
tween.end = to;
|
||||||
|
prepareTween(tween, frames);
|
||||||
|
getRoot().getTweener().add(tween);
|
||||||
|
return tween;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class TweenPos extends BaseTween {
|
||||||
|
private Vec2f start;
|
||||||
|
private Vec2f end;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void apply() {
|
protected void apply() {
|
||||||
pos = Vec2f.tween(start, end, progress);
|
pos = Vec2f.tween(start, end, progress);
|
||||||
|
|
|
@ -1,5 +1,25 @@
|
||||||
package com.danitheskunk.skunkworks.nodes;
|
package com.danitheskunk.skunkworks.nodes;
|
||||||
|
|
||||||
public class NodeRoot extends Node {
|
import com.danitheskunk.skunkworks.Timestep;
|
||||||
|
import com.danitheskunk.skunkworks.Vec2f;
|
||||||
|
|
||||||
|
public class NodeRoot extends Node {
|
||||||
|
private Tweener tweener;
|
||||||
|
|
||||||
|
public NodeRoot() {
|
||||||
|
tweener = new Tweener(Timestep.FIXED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCurrentFrame() {
|
||||||
|
return tweener.getCurrentFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tick() {
|
||||||
|
tweener.tick();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tweener getTweener() {
|
||||||
|
return tweener;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,46 @@ package com.danitheskunk.skunkworks.nodes;
|
||||||
|
|
||||||
import com.danitheskunk.skunkworks.Timestep;
|
import com.danitheskunk.skunkworks.Timestep;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Tweener {
|
public class Tweener {
|
||||||
private Timestep timestepMode;
|
private Timestep timestepMode;
|
||||||
|
private List<BaseTween> tweens;
|
||||||
|
private int currentFrame;
|
||||||
|
|
||||||
public Tweener(Timestep timestepMode) {
|
public Tweener(Timestep timestepMode) {
|
||||||
this.timestepMode = timestepMode;
|
this.timestepMode = timestepMode;
|
||||||
|
this.tweens = new ArrayList<>();
|
||||||
|
this.currentFrame = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCurrentFrame() {
|
||||||
|
return currentFrame;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tick() {
|
||||||
|
var toRemove = new ArrayList<BaseTween>();
|
||||||
|
for(var tween : tweens) {
|
||||||
|
if(currentFrame >= tween.startFrame &&
|
||||||
|
currentFrame <= tween.endFrame) {
|
||||||
|
var len = tween.endFrame - tween.startFrame;
|
||||||
|
var off = currentFrame - tween.startFrame;
|
||||||
|
var progress = (double) off / (double) len;
|
||||||
|
tween.setProgress(progress);
|
||||||
|
tween.apply();
|
||||||
|
}
|
||||||
|
if(currentFrame == tween.endFrame) {
|
||||||
|
toRemove.add(tween);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(var tween : toRemove) {
|
||||||
|
tweens.remove(tween);
|
||||||
|
}
|
||||||
|
++currentFrame;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(BaseTween tween) {
|
||||||
|
tweens.add(tween);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue