9 package micropolisj.engine;
11 import static micropolisj.engine.TileConstants.CLEAR;
12 import micropolisj.util.Utilities;
14 class ToolEffect
implements ToolEffectIfc {
15 final Micropolis city;
16 final ToolPreview preview;
17 public final int originX;
18 public final int originY;
21 public ToolEffect(Micropolis city) {
25 public ToolEffect(Micropolis city,
int playerID) {
26 this(city, 0, 0, playerID);
29 public ToolEffect(Micropolis city,
int xpos,
int ypos,
int playerID) {
31 this.preview =
new ToolPreview(playerID);
34 this.playerID = playerID;
38 public int getTile(
int dx,
int dy) {
39 int c = preview.getTile(dx, dy);
44 if(city.testBounds(originX + dx, originY + dy)) {
45 return city.getTile(originX + dx, originY + dy);
55 public void makeSound(
int dx,
int dy, Sound sound) {
56 preview.makeSound(dx, dy, sound);
60 public void setTile(
int dx,
int dy,
int tileValue) {
61 preview.setTile(dx, dy, tileValue);
65 public void spend(
int amount) {
66 preview.spend(amount);
70 public void toolResult(ToolResult tr) {
71 preview.toolResult(tr);
76 public ToolResult apply(
int playerID) {
77 if(originX - preview.offsetX < 0 || originX - preview.offsetX + preview.getWidth() > city.getWidth()
78 || originY - preview.offsetY < 0 || originY - preview.offsetY + preview.getHeight() > city.getHeight()) {
79 return ToolResult.UH_OH;
82 if(city.getPlayerInfo(playerID).budget.totalFunds < preview.cost) {
83 return ToolResult.INSUFFICIENT_FUNDS;
88 boolean anyFound =
false;
89 for(
int y = 0; y < preview.tiles.length; y++) {
90 for(
int x = 0; x < preview.tiles[y].length; x++) {
91 int c = preview.tiles[y][x];
93 city.setTile(originX + x - preview.offsetX, originY + y - preview.offsetY, (
char) c);
99 for(ToolPreview.SoundInfo si : preview.sounds) {
100 city.makeSound(si.x, si.y, si.sound);
103 if(anyFound && preview.cost != 0) {
104 city.spend(preview.cost, city.getPlayerInfo(playerID));
105 return ToolResult.SUCCESS;
108 return preview.toolResult;
111 public int getPlayerID() {