started writing node system

This commit is contained in:
DaniTheSkunk 2022-10-11 02:15:14 +00:00
parent e81af5e193
commit 661fc6d275
6 changed files with 116 additions and 1 deletions

View File

@ -3,17 +3,20 @@ package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.Color;
import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.nodes.Node;
public abstract class BaseGame { public abstract class BaseGame {
protected Engine engine; protected Engine engine;
protected IWindow window; protected IWindow window;
protected IFont debugFont; protected IFont debugFont;
protected Node rootNode;
public BaseGame(Vec2i windowSize, String windowTitle) { public BaseGame(Vec2i windowSize, String windowTitle) {
this.engine = new Engine(); this.engine = new Engine();
this.window = engine.openWindow(windowSize, windowTitle); this.window = engine.openWindow(windowSize, windowTitle);
//todo: load from .jar //todo: load from .jar
this.debugFont = window.loadFontTileset("fonts/ega-8x14.png"); this.debugFont = window.loadFontTileset("fonts/ega-8x14.png");
this.rootNode = new Node();
init(); init();
} }
@ -23,6 +26,8 @@ public abstract class BaseGame {
//todo: frame rate control //todo: frame rate control
update(1000.0 / 60.0); update(1000.0 / 60.0);
var rc = window.renderStart(); var rc = window.renderStart();
renderPre(rc);
rootNode.render(rc);
render(rc); render(rc);
window.renderFinish(rc); window.renderFinish(rc);
} }
@ -32,8 +37,12 @@ public abstract class BaseGame {
} }
protected void renderPre(IRenderContext rc) {
}
protected void render(IRenderContext rc) { protected void render(IRenderContext rc) {
rc.drawString(new Vec2i(8, 8), "Welcome to Skunkworks, please overide the render method to get started", debugFont, Color.LIGHT_GRAY); //rc.drawString(new Vec2i(8, 8), "Welcome to Skunkworks, please overide the render method to get started", debugFont, Color.LIGHT_GRAY);
} }
protected void update(double delta) { protected void update(double delta) {

View File

@ -0,0 +1,19 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.nodes.NodeSprite;
public class TestNode extends BaseGame {
NodeSprite sprite;
public TestNode() {
super(new Vec2i(1280, 720), "Skunkworks");
sprite = new NodeSprite();
sprite.setTexture(window.loadTexture("demoassets/test.png"));
sprite.setPos(new Vec2f(100, 100));
rootNode.add(sprite);
}
public static void main(String[] args) {
new TestNode().run();
}
}

View File

@ -1,6 +1,7 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
public final class Vec2f { public final class Vec2f {
public final static Vec2f ZERO = new Vec2f(0, 0);
private final double x, y; private final double x, y;
//constructors //constructors

View File

@ -0,0 +1,59 @@
package com.danitheskunk.skunkworks.nodes;
import com.danitheskunk.skunkworks.Vec2f;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import java.sql.Array;
import java.util.*;
//todo: figure out the whole translation position thingie
public class Node implements Iterable<Node> {
protected List<Node> children;
protected Node parent;
protected Vec2f pos;
public Node() {
children = new ArrayList<>();
parent = null;
pos = Vec2f.ZERO;
}
public Vec2f getPos() {
return pos;
}
public void setPos(Vec2f pos) {
this.pos = pos;
}
public void add(Node node) {
if(node.parent != null) {
throw new RuntimeException("Can't add node to two nodes");
}
node.parent = this;
children.add(node);
}
public void remove(Node node) {
node.parent = null;
children.remove(node);
}
public void render(IRenderContext rc) {
for(var child : this) {
child.render(rc);
}
}
public Vec2f getAbsolutePos() {
if(parent == null)
return pos;
return Vec2f.add(parent.getAbsolutePos(), pos);
}
@Override
public Iterator<Node> iterator() {
return children.iterator();
}
}

View File

@ -0,0 +1,27 @@
package com.danitheskunk.skunkworks.nodes;
import com.danitheskunk.skunkworks.Vec2f;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture;
public class NodeSprite extends Node {
//todo: support animations
private ITexture texture;
public NodeSprite() {
texture = null;
}
public ITexture getTexture() {
return texture;
}
public void setTexture(ITexture texture) {
this.texture = texture;
}
@Override
public void render(IRenderContext rc) {
rc.drawTexture(getAbsolutePos().toVec2i(), texture);
}
}

BIN
demoassets/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB