smooth pawn motion rendering
parent
a1fcec4f26
commit
b773d8e92a
|
|
@ -22,4 +22,8 @@ public class Vector2f {
|
|||
return x == v.x && y == v.y;
|
||||
}
|
||||
|
||||
public Vector2f add(Vector2f v) {
|
||||
return new Vector2f(x + v.x, y + v.y);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,10 @@ public class Camera extends GameObject {
|
|||
return new Vector2f(x * tileWidth + screenWidth / 2 - focus.x * tileWidth, y * tileWidth + screenHeight / 2 - focus.y * tileWidth);
|
||||
}
|
||||
|
||||
public Vector2f world2screen(Vector2f pos) {
|
||||
return world2screen(pos.x, pos.y);
|
||||
}
|
||||
|
||||
public Vector2f screen2world(float x, float y) {
|
||||
return new Vector2f((x - screenWidth / 2 + focus.x * tileWidth) / tileWidth, (y - screenHeight / 2 + focus.y * tileWidth) / tileWidth);
|
||||
}
|
||||
|
|
@ -63,6 +67,10 @@ public class Camera extends GameObject {
|
|||
draw(layer, sprite, x, y, 1, 1);
|
||||
}
|
||||
|
||||
public void draw(float layer, Sprite sprite, Vector2f pos) {
|
||||
draw(layer, sprite, pos.x, pos.y, 1, 1);
|
||||
}
|
||||
|
||||
public void draw(float layer, Sprite sprite, float x, float y, float w, float h) {
|
||||
Vector2f screenPos = world2screen(x, y);
|
||||
Drawing.setLayer(layer + (((y + h) - minY) / (maxY - minY)));
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class Pawn extends Agent implements IWorker {
|
|||
@Override
|
||||
public void render() {
|
||||
super.render();
|
||||
camera.draw(Layers.PAWNS, Assets.pawn, x, y);
|
||||
camera.draw(Layers.PAWNS, Assets.pawn, getCalculatedPosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import xyz.valnet.hadean.pathfinding.Path;
|
|||
import xyz.valnet.hadean.util.Action;
|
||||
import xyz.valnet.hadean.util.Assets;
|
||||
import xyz.valnet.hadean.util.Layers;
|
||||
import static xyz.valnet.engine.util.Math.lerp;
|
||||
|
||||
public abstract class Agent extends WorldObject implements ISelectable {
|
||||
public abstract String getName();
|
||||
|
|
@ -35,6 +36,15 @@ public abstract class Agent extends WorldObject implements ISelectable {
|
|||
return path != null && !path.isComplete();
|
||||
}
|
||||
|
||||
protected Vector2f getCalculatedPosition() {
|
||||
if(path == null || path.isComplete()) return getWorldPosition();
|
||||
Vector2f nextPos = path.peek().getPosition().asFloat();
|
||||
return new Vector2f(
|
||||
lerp(x, nextPos.x, frameCounter / (float)speed),
|
||||
lerp(y, nextPos.y, frameCounter / (float)speed)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
super.start();
|
||||
|
|
@ -108,6 +118,7 @@ public abstract class Agent extends WorldObject implements ISelectable {
|
|||
Drawing.setLayer(Layers.GENERAL_UI);
|
||||
Assets.flat.pushColor(Vector4f.opacity(0.4f));
|
||||
if(path != null) {
|
||||
int count = 0;
|
||||
for(Node node : path) {
|
||||
glBegin(GL_LINES);
|
||||
Vector2f u, v;
|
||||
|
|
@ -116,11 +127,16 @@ public abstract class Agent extends WorldObject implements ISelectable {
|
|||
else u = camera.world2screen(node.from.x + 0.5f, node.from.y + 0.5f);
|
||||
|
||||
v = camera.world2screen(node.x + 0.5f, node.y + 0.5f);
|
||||
|
||||
if(count == path.getLength() - 1) {
|
||||
u = camera.world2screen(getCalculatedPosition().add(new Vector2f(0.5f, 0.5f)));
|
||||
}
|
||||
glVertexAttrib2f(SimpleShader.TEX_COORD, 0, 88 / 256f);
|
||||
glVertex3f(u.x, u.y, 3f);
|
||||
glVertexAttrib2f(SimpleShader.TEX_COORD, 0, 88 / 255f);
|
||||
glVertex3f(v.x, v.y, 3f);
|
||||
glEnd();
|
||||
count ++;
|
||||
}
|
||||
Assets.flat.swapColor(Vector4f.opacity(0.6f));
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,10 @@ public class Path implements Iterable<Node> {
|
|||
this.dst = dst;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return nodes.size();
|
||||
}
|
||||
|
||||
public Node peek() {
|
||||
return nodes.peek();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue