From e5856cf73b58e6c5c3e885b8c39dfc17bbfad15a Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 15 Jan 2020 11:28:44 -0500 Subject: [PATCH] More objects --- .../entities/traits/BuilderTrait.java | 9 ++++--- core/src/mindustry/input/InputHandler.java | 8 +++--- core/src/mindustry/world/Block.java | 27 ++++++++++--------- core/src/mindustry/world/Tile.java | 4 +-- .../world/blocks/distribution/Sorter.java | 5 ++-- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/core/src/mindustry/entities/traits/BuilderTrait.java b/core/src/mindustry/entities/traits/BuilderTrait.java index 570f5eabf9..f141e30d8a 100644 --- a/core/src/mindustry/entities/traits/BuilderTrait.java +++ b/core/src/mindustry/entities/traits/BuilderTrait.java @@ -1,10 +1,11 @@ package mindustry.entities.traits; import arc.*; -import arc.struct.Queue; +import arc.struct.*; import arc.graphics.g2d.*; import arc.math.*; import arc.math.geom.*; +import arc.struct.Queue; import arc.util.ArcAnnotate.*; import arc.util.*; import mindustry.*; @@ -289,10 +290,10 @@ public interface BuilderTrait extends Entity, TeamTrait{ public @Nullable Block block; /** Whether this is a break request.*/ public boolean breaking; - /** Whether this request comes with a config int. If yes, any blocks placed with this request will not call playerPlaced.*/ + /** Whether this request comes with a config. If yes, any blocks placed with this request will not call playerPlaced.*/ public boolean hasConfig; /** Config int. Not used unless hasConfig is true.*/ - public int config; + public Object config; /** Original position, only used in schematics.*/ public int originalX, originalY, originalWidth, originalHeight; @@ -376,7 +377,7 @@ public interface BuilderTrait extends Entity, TeamTrait{ return y*tilesize + block.offset(); } - public BuildRequest configure(int config){ + public BuildRequest configure(Object config){ this.config = config; this.hasConfig = true; return this; diff --git a/core/src/mindustry/input/InputHandler.java b/core/src/mindustry/input/InputHandler.java index d82358cfe1..f0e80ebbf7 100644 --- a/core/src/mindustry/input/InputHandler.java +++ b/core/src/mindustry/input/InputHandler.java @@ -266,8 +266,8 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ requests.each(req -> { //rotate config position - if(req.block.posConfig){ - int cx = Pos.x(req.config) - req.originalX, cy = Pos.y(req.config) - req.originalY; + if(req.config instanceof Point2){ + int cx = ((Point2)req.config).x - req.originalX, cy = ((Point2)req.config).y - req.originalY; int lx = cx; if(direction >= 0){ @@ -277,7 +277,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ cx = cy; cy = -lx; } - req.config = Pos.get(cx + req.originalX, cy + req.originalY); + req.config = new Point2(cx + req.originalX, cy + req.originalY); } //rotate actual request, centered on its multiblock position @@ -308,7 +308,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ req.y = (int)((value - req.block.offset()) / tilesize); } - if(req.block.posConfig){ + if(req.config instanceof Point2){ int corigin = x ? req.originalWidth/2 : req.originalHeight/2; int nvalue = -((x ? Pos.x(req.config) : Pos.y(req.config)) - corigin) + corigin; if(x){ diff --git a/core/src/mindustry/world/Block.java b/core/src/mindustry/world/Block.java index df6a02be69..2f8064f63a 100644 --- a/core/src/mindustry/world/Block.java +++ b/core/src/mindustry/world/Block.java @@ -144,7 +144,7 @@ public class Block extends BlockStorage{ protected TextureRegion[] cacheRegions = {}; protected Array cacheRegionStrings = new Array<>(); protected Prov entityType = TileEntity::new; - protected ObjectMap, Cons2> configurations = new ObjectMap<>(); + protected ObjectMap, ConfigHandler> configurations = new ObjectMap<>(); protected Array tempTiles = new Array<>(); protected TextureRegion[] generatedIcons; @@ -471,20 +471,17 @@ public class Block extends BlockStorage{ } - /** Called when arbitrary int configuration is applied to a tile. */ - protected void configuredPos(Tile tile, @Nullable Player player, Point2 point){ - + /** Called when arbitrary configuration is applied to a tile. */ + public void configured(Tile tile, @Nullable Player player, @Nullable Object value){ + if(value == null){ + tapped(tile, player); + }else if(configurations.containsKey(value.getClass())){ + configurations.get(value.getClass()).configured(tile, player, value); + } } - /** Called when arbitrary configuration is applied to a tile. - * The default behavior is to treat this as integer configuration. */ - @CallSuper - public void configured(Tile tile, @Nullable Player player, @Nullable Object value){ - if(value instanceof Integer){ - configured_(tile, player, (int)value); - }else if(value == null){ - tapped(tile, player); - } + public void config(Class type, ConfigHandler config){ + configurations.put(type, config); } /** Returns whether or not a hand cursor should be shown over this block. */ @@ -928,4 +925,8 @@ public class Block extends BlockStorage{ Arrays.sort(requirements, Structs.comparingInt(i -> i.item.id)); } + public interface ConfigHandler{ + void configured(Tile tile, Player player, T value); + } + } diff --git a/core/src/mindustry/world/Tile.java b/core/src/mindustry/world/Tile.java index 606a3f1ccd..34784d0590 100644 --- a/core/src/mindustry/world/Tile.java +++ b/core/src/mindustry/world/Tile.java @@ -91,11 +91,11 @@ public class Tile implements Position, TargetTrait{ } /** Configure a tile with the current, local player. */ - public void configure(int value){ + public void configure(Object value){ Call.onTileConfig(player, this, value); } - public void configureAny(int value){ + public void configureAny(Object value){ Call.onTileConfig(null, this, value); } diff --git a/core/src/mindustry/world/blocks/distribution/Sorter.java b/core/src/mindustry/world/blocks/distribution/Sorter.java index 1df5fe75e5..1e23486548 100644 --- a/core/src/mindustry/world/blocks/distribution/Sorter.java +++ b/core/src/mindustry/world/blocks/distribution/Sorter.java @@ -29,6 +29,7 @@ public class Sorter extends Block{ configurable = true; unloadable = false; entityType = SorterEntity::new; + config(Item.class, (tile, player, item) -> tile.ent().sortItem = item); } @Override @@ -142,8 +143,8 @@ public class Sorter extends Block{ @Nullable Item sortItem; @Override - public int config(){ - return sortItem == null ? -1 : sortItem.id; + public Object config(){ + return sortItem; } @Override