From 1aa2e3d30fae2b80a99462f21eb46c31193f717b Mon Sep 17 00:00:00 2001 From: "jason@long.name" Date: Thu, 16 May 2013 23:41:57 +0000 Subject: [PATCH] ToolPreview: a class for holding the effect of a tool git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@629 d9718cc8-9f43-0410-858b-315f434eb58c --- src/micropolisj/engine/TileConstants.java | 1 + src/micropolisj/engine/ToolEffectIfc.java | 19 ++++ src/micropolisj/engine/ToolPreview.java | 103 ++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/micropolisj/engine/ToolEffectIfc.java create mode 100644 src/micropolisj/engine/ToolPreview.java diff --git a/src/micropolisj/engine/TileConstants.java b/src/micropolisj/engine/TileConstants.java index c72c74a..0a5db74 100644 --- a/src/micropolisj/engine/TileConstants.java +++ b/src/micropolisj/engine/TileConstants.java @@ -39,6 +39,7 @@ public class TileConstants // // terrain mapping // + public static final short CLEAR = -1; public static final char DIRT = 0; public static final char RIVER = 2; public static final char REDGE = 3; diff --git a/src/micropolisj/engine/ToolEffectIfc.java b/src/micropolisj/engine/ToolEffectIfc.java new file mode 100644 index 0000000..81ddc07 --- /dev/null +++ b/src/micropolisj/engine/ToolEffectIfc.java @@ -0,0 +1,19 @@ +package micropolisj.engine; + +public interface ToolEffectIfc +{ + /** + * Gets the tile at a relative location. + */ + int getTile(int dx, int dy); + + /** + * Sets the tile value at a relative location. + */ + void setTile(int dx, int dy, int tileValue); + + /** + * Deduct an amount from the controller's cash funds. + */ + void spend(int amount); +} diff --git a/src/micropolisj/engine/ToolPreview.java b/src/micropolisj/engine/ToolPreview.java new file mode 100644 index 0000000..794f3f5 --- /dev/null +++ b/src/micropolisj/engine/ToolPreview.java @@ -0,0 +1,103 @@ +package micropolisj.engine; + +import java.util.Arrays; +import static micropolisj.engine.TileConstants.*; + +public class ToolPreview implements ToolEffectIfc +{ + public int originX; + public int originY; + public int offsetX; + public int offsetY; + public short [][] tiles; + public int cost; + + ToolPreview(int x, int y) + { + this.originX = x; + this.originY = y; + this.tiles = new short[1][1]; + this.tiles[0][0] = CLEAR; + } + + //implements ToolEffectIfc + public int getTile(int dx, int dy) + { + if (inRange(dx, dy)) { + return tiles[offsetY+dy][offsetX+dx]; + } + else { + return CLEAR; + } + } + + boolean inRange(int dx, int dy) + { + return offsetY+dy >= 0 && + offsetY+dy < tiles.length && + offsetX+dx >= 0 && + offsetX+dx < tiles[0].length; + } + + void expandTo(int dx, int dy) + { + for (int i = 0; i < tiles.length; i++) { + short[] A = tiles[i]; + if (offsetX+dx >= A.length) { + int newLen = offsetX+dx+1; + short[] AA = new short[newLen]; + System.arraycopy(A, 0, AA, 0, A.length); + Arrays.fill(AA, A.length, newLen, CLEAR); + tiles[i] = AA; + } + else if (offsetX+dx < 0) { + int addl = -(offsetX+dx); + int newLen = A.length + addl; + short[] AA = new short[newLen]; + System.arraycopy(A, 0, AA, addl, A.length); + Arrays.fill(AA, 0, addl, CLEAR); + tiles[i] = AA; + } + } + + if (offsetX+dx < 0) { + int addl = -(offsetX+dx); + offsetX += addl; + } + + if (offsetY+dy >= tiles.length) { + int newLen = offsetY+dy+1; + short[][] newTiles = new short[newLen][tiles[0].length]; + System.arraycopy(tiles, 0, newTiles, 0, tiles.length); + for (int i = tiles.length; i < newLen; i++) { + Arrays.fill(newTiles[i], CLEAR); + } + tiles = newTiles; + } + else if (offsetY+dy < 0) { + int addl = -(offsetY+dy); + int newLen = tiles.length + addl; + short[][] newTiles = new short[newLen][tiles[0].length]; + System.arraycopy(tiles, addl, newTiles, 0, tiles.length); + for (int i = 0; i < addl; i++) { + Arrays.fill(newTiles[i], CLEAR); + } + tiles = newTiles; + + offsetY += addl; + } + } + + //implements ToolEffectIfc + public void setTile(int dx, int dy, int tileValue) + { + expandTo(dx, dy); + tiles[offsetY+dy][offsetX+dx] = (short)tileValue; + } + + //implements ToolEffectIfc + public void spend(int amount) + { + cost += amount; + } +}