ToolEffect: stores effect of tool and then applies it as transaction
git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@631 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
parent
76c96640c1
commit
b3522c514c
2 changed files with 77 additions and 5 deletions
76
src/micropolisj/engine/ToolEffect.java
Normal file
76
src/micropolisj/engine/ToolEffect.java
Normal file
|
@ -0,0 +1,76 @@
|
|||
package micropolisj.engine;
|
||||
|
||||
import static micropolisj.engine.TileConstants.CLEAR;
|
||||
|
||||
class ToolEffect implements ToolEffectIfc
|
||||
{
|
||||
final Micropolis city;
|
||||
final ToolPreview preview;
|
||||
final int originX;
|
||||
final int originY;
|
||||
|
||||
ToolEffect(Micropolis city, int xpos, int ypos)
|
||||
{
|
||||
this.city = city;
|
||||
this.preview = new ToolPreview();
|
||||
this.originX = xpos;
|
||||
this.originY = ypos;
|
||||
}
|
||||
|
||||
//implements ToolEffectIfc
|
||||
public int getTile(int dx, int dy)
|
||||
{
|
||||
int c = preview.getTile(dx, dy);
|
||||
if (c != CLEAR) {
|
||||
return c;
|
||||
}
|
||||
|
||||
if (city.testBounds(originX + dx, originY + dy)) {
|
||||
return city.getTile(originX + dx, originY + dy);
|
||||
}
|
||||
else {
|
||||
return CLEAR;
|
||||
}
|
||||
}
|
||||
|
||||
//implements ToolEffectIfc
|
||||
public void setTile(int dx, int dy, int tileValue)
|
||||
{
|
||||
preview.setTile(dx, dy, tileValue);
|
||||
}
|
||||
|
||||
//implements ToolEffectIfc
|
||||
public void spend(int amount)
|
||||
{
|
||||
preview.spend(amount);
|
||||
}
|
||||
|
||||
ToolResult apply()
|
||||
{
|
||||
if (originX - preview.offsetX < 0 ||
|
||||
originX - preview.offsetX >= city.getWidth() ||
|
||||
originY - preview.offsetY < 0 ||
|
||||
originY - preview.offsetY >= city.getHeight())
|
||||
{
|
||||
return ToolResult.UH_OH;
|
||||
}
|
||||
|
||||
if (city.budget.totalFunds < preview.cost) {
|
||||
return ToolResult.INSUFFICIENT_FUNDS;
|
||||
}
|
||||
|
||||
boolean anyFound = false;
|
||||
for (int y = 0; y < preview.tiles.length; y++) {
|
||||
for (int x = 0; x < preview.tiles[y].length; x++) {
|
||||
int c = preview.tiles[y][x];
|
||||
if (c != CLEAR) {
|
||||
city.setTile(originX + x - preview.offsetX, originY + y - preview.offsetY, (char) c);
|
||||
anyFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return anyFound && preview.cost != 0 ?
|
||||
ToolResult.SUCCESS :
|
||||
ToolResult.NONE;
|
||||
}
|
||||
}
|
|
@ -5,17 +5,13 @@ 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)
|
||||
ToolPreview()
|
||||
{
|
||||
this.originX = x;
|
||||
this.originY = y;
|
||||
this.tiles = new short[1][1];
|
||||
this.tiles[0][0] = CLEAR;
|
||||
}
|
||||
|
|
Reference in a new issue