Better bridge linking

This commit is contained in:
Anuken
2020-12-17 18:35:50 -05:00
parent 0cc3340bd6
commit 6815f3305f
8 changed files with 64 additions and 41 deletions

View File

@@ -393,6 +393,11 @@ public class Block extends UnlockableContent{
return null;
}
/** Called when a new build plan is created in the player's queue. Blocks can maintain a reference to this plan and add configs to it later. */
public void onNewPlan(BuildPlan plan){
}
public void drawPlan(BuildPlan req, Eachable<BuildPlan> list, boolean valid){
drawPlan(req, list, valid, 1f);
}

View File

@@ -28,7 +28,12 @@ public class ItemBridge extends Block{
public @Load("@-end") TextureRegion endRegion;
public @Load("@-bridge") TextureRegion bridgeRegion;
public @Load("@-arrow") TextureRegion arrowRegion;
public int lastPlaced = -1;
//for autolink
@Nullable
public ItemBridgeBuild lastBuild;
@Nullable
public BuildPlan lastPlan;
public ItemBridge(String name){
super(name);
@@ -53,27 +58,31 @@ public class ItemBridge extends Block{
public void drawRequestConfigTop(BuildPlan req, Eachable<BuildPlan> list){
otherReq = null;
list.each(other -> {
if(other.block == this && req != other && req.config instanceof Point2 && ((Point2)req.config).equals(other.x - req.x, other.y - req.y)){
if(other.block == this && req != other && req.config instanceof Point2 p && p.equals(other.x - req.x, other.y - req.y)){
otherReq = other;
}
});
if(otherReq == null) return;
if(otherReq != null){
drawBridge(req, otherReq.drawx(), otherReq.drawy(), 0);
}
}
public void drawBridge(BuildPlan req, float ox, float oy, float flip){
Lines.stroke(8f);
Tmp.v1.set(otherReq.drawx(), otherReq.drawy()).sub(req.drawx(), req.drawy()).setLength(tilesize/2f);
Tmp.v1.set(ox, oy).sub(req.drawx(), req.drawy()).setLength(tilesize/2f);
Lines.line(
bridgeRegion,
req.drawx() + Tmp.v1.x,
req.drawy() + Tmp.v1.y,
otherReq.drawx() - Tmp.v1.x,
otherReq.drawy() - Tmp.v1.y, false
ox - Tmp.v1.x,
oy - Tmp.v1.y, false
);
Draw.rect(arrowRegion, (req.drawx() + otherReq.drawx()) / 2f, (req.drawy() + otherReq.drawy()) / 2f,
Angles.angle(req.drawx(), req.drawy(), otherReq.drawx(), otherReq.drawy()));
Draw.rect(arrowRegion, (req.drawx() + ox) / 2f, (req.drawy() + oy) / 2f,
Angles.angle(req.drawx(), req.drawy(), ox, oy) + flip);
}
@Override
@@ -109,27 +118,40 @@ public class ItemBridge extends Block{
}
public boolean linkValid(Tile tile, Tile other, boolean checkDouble){
if(other == null || tile == null) return false;
if(tile.x == other.x){
if(Math.abs(tile.y - other.y) > range) return false;
}else if(tile.y == other.y){
if(Math.abs(tile.x - other.x) > range) return false;
}else{
return false;
}
if(other == null || tile == null || !positionsValid(tile.x, tile.y, other.x, other.y)) return false;
return ((other.block() == tile.block() && tile.block() == this) || (!(tile.block() instanceof ItemBridge) && other.block() == this))
&& (other.team() == tile.team() || tile.block() != this)
&& (!checkDouble || ((ItemBridgeBuild)other.build).link != tile.pos());
}
public boolean positionsValid(int x1, int y1, int x2, int y2){
if(x1 == x2){
return Math.abs(y1 - y2) <= range;
}else if(y1 == y2){
return Math.abs(x1 - x2) <= range;
}else{
return false;
}
}
public Tile findLink(int x, int y){
if(world.tiles.in(x, y) && linkValid(world.tile(x, y), world.tile(lastPlaced)) && lastPlaced != Point2.pack(x, y)){
return world.tile(lastPlaced);
Tile tile = world.tile(x, y);
if(tile != null && lastBuild != null && linkValid(tile, lastBuild.tile) && lastBuild.tile != tile){
return lastBuild.tile;
}
return null;
}
@Override
public void onNewPlan(BuildPlan plan){
if(lastPlan != null && lastPlan.config == null && positionsValid(lastPlan.x, lastPlan.y, plan.x, plan.y)){
lastPlan.config = new Point2(plan.x - lastPlan.x, plan.y - lastPlan.y);
}
lastPlan = plan;
}
public class ItemBridgeBuild extends Building{
public int link = -1;
public IntSet incoming = new IntSet();
@@ -149,7 +171,7 @@ public class ItemBridge extends Block{
link.build.configure(tile.pos());
}
lastPlaced = tile.pos();
lastBuild = this;
}
@Override