some QoL stuff

pull/1/head
Valerie 2022-05-22 02:08:41 -04:00
parent 7fa2648a9a
commit e869abf9fb
7 changed files with 56 additions and 20 deletions

View File

@ -27,4 +27,10 @@ public class Vector2i {
return false;
}
public float distanceTo(int x, int y) {
int a = this.x - x;
int b = this.y - y;
return (float) Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
}

View File

@ -32,7 +32,7 @@ public class Tile extends GameObject {
public void start() {
camera = get(Camera.class);
if(Math.random() > 0.98) {
if(Math.random() > 0.90) {
Tree tree = new Tree(x, y);
stuff.add(tree);
add(tree);

View File

@ -6,4 +6,5 @@ public interface IWorkable {
public boolean hasWork();
public Vector2i[] getWorablePositions();
public void doWork();
public Vector2i getLocation();
}

View File

@ -7,6 +7,7 @@ import static org.lwjgl.opengl.GL11.glVertex3f;
import static org.lwjgl.opengl.GL20.glVertexAttrib2f;
import static xyz.valnet.engine.util.Math.lerp;
import java.util.Comparator;
import java.util.List;
import xyz.valnet.engine.graphics.Drawing;
@ -31,7 +32,7 @@ public class Pawn extends GameObject implements ISelectable {
private Path path;
private final float invocationThreshold = 70f;
private final float invocationThreshold = 50 + (float)(Math.random() * 20);
private Camera camera;
private Terrain terrain;
@ -88,12 +89,7 @@ public class Pawn extends GameObject implements ISelectable {
@Override
public void tick(float dTime) {
// firstly, TRY PATHING.
if(path != null && !path.isComplete()) {
move();
return;
}
// then, try to do work!
if(currentJob != null && currentJob.hasWork()) {
if(getCurrentPos().isOneOf(currentJob.getWorablePositions())) {
@ -102,12 +98,22 @@ public class Pawn extends GameObject implements ISelectable {
}
}
// firstly, TRY PATHING.
if(path != null && !path.isComplete()) {
move();
}
// then try to get work?!
currentJob = null;
tryStartWork();
if(counter == 0) {
currentJob = null;
tryStartWork();
// then wander...
if(currentJob == null && (path == null || path.isComplete())) {
// then wander...
newPath();
}
}
}
@ -119,6 +125,18 @@ public class Pawn extends GameObject implements ISelectable {
private void tryStartWork() {
List<IWorkable> workables = getAll(IWorkable.class);
workables.sort(new Comparator<IWorkable>() {
@Override
public int compare(IWorkable a, IWorkable b) {
float distA = a.getLocation().distanceTo((int)x, (int)y);
float distB = b.getLocation().distanceTo((int)x, (int)y);
if(distA > distB) return -1;
if(distB > distA) return 1;
return 0;
}
});
if(workables.size() > 0) {
for(IWorkable job : workables) {
if(!job.hasWork()) continue;
@ -134,14 +152,15 @@ public class Pawn extends GameObject implements ISelectable {
}
}
// private void newPath() {
// // set new destination
// dx = 0.5f + (float)Math.floor(Math.random() * Terrain.WORLD_SIZE);
// dy = 0.5f + (float)Math.floor(Math.random() * Terrain.WORLD_SIZE);
private void newPath() {
// set new destination
int randomX = (int)Math.floor(Math.random() * Terrain.WORLD_SIZE);
int randomY = (int)Math.floor(Math.random() * Terrain.WORLD_SIZE);
path = pathfinder.getPath((int)x, (int)y, randomX, randomY);
// // and route there.
// route();
// }
// // and route there.
// reroute();
}
private void reroute() {
// intify all the coordinates

View File

@ -29,6 +29,9 @@ public class Tree extends GameObject implements ITileThing, ISelectable, IWorkab
Assets.flat.pushColor(new Vector4f(1 - getProgress(), 1 - getProgress(), 1 - getProgress(), 1.0f));
camera.draw(Assets.tree, x - 1, y - 2, 3, 3);
Assets.flat.popColor();
if(hasWork()) {
camera.draw(Assets.lilAxe, x, y);
}
}
@Override
@ -38,7 +41,7 @@ public class Tree extends GameObject implements ITileThing, ISelectable, IWorkab
@Override
public Vector4f getWorldBox() {
return new Vector4f(x - 1, y - 2, x + 2, y + 1);
return new Vector4f(x, y, x + 1, y + 1);
}
public static final Action ACTION_CHOP = new Action("Chop");
@ -112,4 +115,9 @@ public class Tree extends GameObject implements ITileThing, ISelectable, IWorkab
public void onRemove() {
add(new Log(x, y));
}
@Override
public Vector2i getLocation() {
return new Vector2i(x, y);
}
}

View File

@ -78,7 +78,7 @@ public class GameScene implements IScene {
@Override
public void enable() {
objects.add(new Terrain());
for(int i = 0; i < 1; i ++) {
for(int i = 0; i < 5; i ++) {
objects.add(new Pawn());
}
objects.add(new Camera());

View File

@ -28,6 +28,7 @@ public class Assets {
public static final Sprite tree;
public static final Sprite rocks;
public static final Sprite log;
public static final Sprite lilAxe;
public static final SimpleShader flat;
@ -47,6 +48,7 @@ public class Assets {
tree = new Sprite(atlas, 64, 64, 24, 24);
rocks = new Sprite(atlas, 64, 104, 8, 8);
log = new Sprite(atlas, 48, 96, 16, 16);
lilAxe = new Sprite(atlas, 64, 88, 16, 16);
Map<Character, Sprite> charset = new HashMap<Character, Sprite>();