first successful tween

This commit is contained in:
DaniTheSkunk 2022-10-12 00:57:26 +00:00
parent 91169a40b0
commit 2ac964feeb
6 changed files with 92 additions and 10 deletions

View File

@ -31,6 +31,7 @@ public abstract class BaseGame {
lastTime = currentTime;
window.tick();
//todo: frame rate control
rootNode.tick();
update(1000.0 / 60.0);
var rc = window.renderStart();
renderPre(rc);

View File

@ -11,9 +11,11 @@ public class TestNode extends BaseGame {
sprite.setTexture(window.loadTexture("demoassets/test.png"));
sprite.setPos(new Vec2f(100, 100));
rootNode.add(sprite);
sprite.tweenPos(new Vec2f(800, 400), 120).delay(600);
}
public static void main(String[] args) {
new TestNode().run();
}
}

View File

@ -2,6 +2,8 @@ package com.danitheskunk.skunkworks.nodes;
public abstract class BaseTween {
protected double progress;
protected int startFrame;
protected int endFrame;
public BaseTween() {
progress = 0;
@ -11,5 +13,10 @@ public abstract class BaseTween {
this.progress = progress;
}
public void delay(int frames) {
startFrame += frames;
endFrame += frames;
}
protected abstract void apply();
}

View File

@ -56,12 +56,13 @@ public class Node implements Iterable<Node> {
return Vec2f.add(parent.getAbsolutePos(), pos);
}
public Node getRoot() {
public NodeRoot getRoot() {
var node = this;
while(node.parent != null) {
node = node.parent;
}
return node;
//todo: check if right class
return (NodeRoot) node;
}
@Override
@ -69,14 +70,29 @@ public class Node implements Iterable<Node> {
return children.iterator();
}
public class TweenPos extends BaseTween {
private final Vec2f start;
private final Vec2f end;
public int getCurrentFrame() {
return getRoot().getCurrentFrame();
}
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
protected void apply() {

View File

@ -1,5 +1,25 @@
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;
}
}

View File

@ -2,10 +2,46 @@ package com.danitheskunk.skunkworks.nodes;
import com.danitheskunk.skunkworks.Timestep;
import java.util.ArrayList;
import java.util.List;
public class Tweener {
private Timestep timestepMode;
private List<BaseTween> tweens;
private int currentFrame;
public Tweener(Timestep 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);
}
}