From 42f227195c772a5623021c5e3e88bdcd3b8fdf60 Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 16 Apr 2020 09:44:31 -0400 Subject: [PATCH] Better payload framework --- core/src/mindustry/entities/def/TileComp.java | 35 ++++++++++ core/src/mindustry/world/Block.java | 1 + .../blocks/distribution/MassConveyor.java | 67 ++++++------------- .../world/blocks/payloads/Payload.java | 12 ++++ .../world/blocks/payloads/UnitPayload.java | 31 +++++++++ 5 files changed, 98 insertions(+), 48 deletions(-) create mode 100644 core/src/mindustry/world/blocks/payloads/Payload.java create mode 100644 core/src/mindustry/world/blocks/payloads/UnitPayload.java diff --git a/core/src/mindustry/entities/def/TileComp.java b/core/src/mindustry/entities/def/TileComp.java index 59279f32c9..afa7df44cd 100644 --- a/core/src/mindustry/entities/def/TileComp.java +++ b/core/src/mindustry/entities/def/TileComp.java @@ -26,6 +26,7 @@ import mindustry.type.*; import mindustry.ui.*; import mindustry.world.*; import mindustry.world.blocks.environment.*; +import mindustry.world.blocks.payloads.*; import mindustry.world.blocks.power.*; import mindustry.world.consumers.*; import mindustry.world.meta.*; @@ -315,6 +316,40 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc, QuadTree noSleep(); } + public boolean acceptPayload(Tilec source, Payload payload){ + return false; + } + + public void handlePayload(Tilec source, Payload payload){ + + } + + /** + * Tries dumping a payload. + * @param todump payload to dump. + * @return whether the payload was moved successfully + */ + public boolean dumpPayload(@NonNull Payload todump){ + Array proximity = proximity(); + int dump = rotation(); + + if(proximity.size == 0) return false; + + for(int i = 0; i < proximity.size; i++){ + Tilec other = proximity.get((i + dump) % proximity.size); + + if(other.team() == team() && other.acceptPayload(this, todump)){ + other.handlePayload(this, todump); + incrementDump(proximity.size); + return true; + } + + incrementDump(proximity.size); + } + + return false; + } + public void handleItem(Tilec source, Item item){ items.add(item, 1); } diff --git a/core/src/mindustry/world/Block.java b/core/src/mindustry/world/Block.java index cad764ccbe..09e9698983 100644 --- a/core/src/mindustry/world/Block.java +++ b/core/src/mindustry/world/Block.java @@ -43,6 +43,7 @@ public class Block extends UnlockableContent{ public boolean outputsLiquid = false; public boolean consumesPower = true; public boolean outputsPower = false; + public boolean outputsPayload = false; public int itemCapacity = 10; public float liquidCapacity = 10f; diff --git a/core/src/mindustry/world/blocks/distribution/MassConveyor.java b/core/src/mindustry/world/blocks/distribution/MassConveyor.java index 95708cecf3..11e295cac8 100644 --- a/core/src/mindustry/world/blocks/distribution/MassConveyor.java +++ b/core/src/mindustry/world/blocks/distribution/MassConveyor.java @@ -11,8 +11,8 @@ import mindustry.content.*; import mindustry.game.*; import mindustry.gen.*; import mindustry.graphics.*; -import mindustry.ui.*; import mindustry.world.*; +import mindustry.world.blocks.payloads.*; import static mindustry.Vars.*; @@ -29,6 +29,7 @@ public class MassConveyor extends Block{ size = 3; rotate = true; update = true; + outputsPayload = true; } @Override @@ -39,10 +40,10 @@ public class MassConveyor extends Block{ edgeRegion = Core.atlas.find(name + "-edge"); } - public class MassConveyorEntity extends TileEntity implements MassAcceptor{ + public class MassConveyorEntity extends TileEntity{ public @Nullable Payload item; public float progress, itemRotation, animation; - public @Nullable MassAcceptor next; + public @Nullable Tilec next; public boolean blocked; public int step = -1, stepAccepted = -1; @@ -52,9 +53,9 @@ public class MassConveyor extends Block{ Tilec accept = nearby(Geometry.d4[rotation()].x * size, Geometry.d4[rotation()].y * size); //next block must be aligned and of the same size - if(accept instanceof MassAcceptor && accept.block().size == size && + if(accept.block().size == size && tileX() + Geometry.d4[rotation()].x * size == accept.tileX() && tileY() + Geometry.d4[rotation()].y * size == accept.tileY()){ - next = (MassAcceptor)accept; + next = accept; } int ntrns = 1 + size/2; @@ -73,6 +74,7 @@ public class MassConveyor extends Block{ animation = 0f; } + //TODO nondeterministic input priority int curStep = curStep(); if(curStep > step){ boolean valid = step != -1; @@ -83,21 +85,21 @@ public class MassConveyor extends Block{ //trigger update forward next.updateTile(); - if(next.acceptMass(item, this)){ + if(next.acceptPayload(this, item)){ //move forward. - next.handleMass(item, this); + next.handlePayload(this, item); item = null; } }else if(!blocked){ //dump item forward float trnext = size * tilesize / 2f, cx = Geometry.d4[rotation()].x, cy = Geometry.d4[rotation()].y, rot = rotation() * 90; - item.dump(x + cx * trnext, y + cy * trnext, rotation() * 90); - item = null; + if(item.dump(x + cx * trnext, y + cy * trnext, rotation() * 90)){ + item = null; + } } } } - } @Override @@ -172,13 +174,13 @@ public class MassConveyor extends Block{ } @Override - public boolean acceptMass(Payload item, Tilec source){ + public boolean acceptPayload(Tilec source, Payload payload){ return this.item == null; } @Override - public void handleMass(Payload item, Tilec source){ - this.item = item; + public void handlePayload(Tilec source, Payload payload){ + this.item = payload; this.stepAccepted = curStep(); this.itemRotation = source.rotation() * 90; this.animation = 0; @@ -189,8 +191,9 @@ public class MassConveyor extends Block{ return !blocked || next != null; }else{ Tilec accept = nearby(Geometry.d4[direction].x * size, Geometry.d4[direction].y * size); - return accept instanceof MassAcceptor && accept.block().size == size && - accept.tileX() + Geometry.d4[accept.rotation()].x * size == tileX() && accept.tileY() + Geometry.d4[accept.rotation()].y * size == tileY(); + return accept.block().size == size && accept.block().outputsPayload && + //block must either be facing this one, or not be rotating + ((accept.tileX() + Geometry.d4[accept.rotation()].x * size == tileX() && accept.tileY() + Geometry.d4[accept.rotation()].y * size == tileY()) || !accept.block().rotate); } } @@ -219,7 +222,7 @@ public class MassConveyor extends Block{ } int curStep(){ - return (int)(Time.time() / moveTime); + return (int)((Time.time()) / moveTime); } float fract(){ @@ -227,36 +230,4 @@ public class MassConveyor extends Block{ } } - public interface MassAcceptor extends Tilec{ - boolean acceptMass(Payload item, Tilec source); - void handleMass(Payload item, Tilec source); - } - - public interface Payload{ - void draw(float x, float y, float rotation); - void dump(float x, float y, float rotation); - } - - public static class UnitPayload implements Payload{ - public Unitc unit; - - public UnitPayload(Unitc unit){ - this.unit = unit; - } - - @Override - public void dump(float x, float y, float rotation){ - unit.set(x, y); - unit.rotation(rotation); - unit.add(); - } - - @Override - public void draw(float x, float y, float rotation){ - Drawf.shadow(x, y, 24); - Draw.rect("pneumatic-drill", x, y, rotation); - Drawf.shadow(x, y, 20); - Draw.rect(unit.type().icon(Cicon.full), x, y, rotation - 90); - } - } } diff --git a/core/src/mindustry/world/blocks/payloads/Payload.java b/core/src/mindustry/world/blocks/payloads/Payload.java new file mode 100644 index 0000000000..4857c7fb56 --- /dev/null +++ b/core/src/mindustry/world/blocks/payloads/Payload.java @@ -0,0 +1,12 @@ +package mindustry.world.blocks.payloads; + +public interface Payload{ + + /** draws this payload at a position. */ + void draw(float x, float y, float rotation); + + /** @return whether this payload was dumped. */ + default boolean dump(float x, float y, float rotation){ + return false; + } +} diff --git a/core/src/mindustry/world/blocks/payloads/UnitPayload.java b/core/src/mindustry/world/blocks/payloads/UnitPayload.java new file mode 100644 index 0000000000..918edf85cc --- /dev/null +++ b/core/src/mindustry/world/blocks/payloads/UnitPayload.java @@ -0,0 +1,31 @@ +package mindustry.world.blocks.payloads; + +import arc.graphics.g2d.*; +import mindustry.gen.*; +import mindustry.graphics.*; +import mindustry.ui.*; + +public class UnitPayload implements Payload{ + public Unitc unit; + + public UnitPayload(Unitc unit){ + this.unit = unit; + } + + @Override + public boolean dump(float x, float y, float rotation){ + unit.set(x, y); + unit.rotation(rotation); + unit.add(); + + return true; + } + + @Override + public void draw(float x, float y, float rotation){ + Drawf.shadow(x, y, 24); + Draw.rect("pneumatic-drill", x, y, rotation); + Drawf.shadow(x, y, 20); + Draw.rect(unit.type().icon(Cicon.full), x, y, rotation - 90); + } +}