Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
ToolEffect.java
Go to the documentation of this file.
1 // This file is part of MicropolisJ.
2 // Copyright (C) 2013 Jason Long
3 // Portions Copyright (C) 1989-2007 Electronic Arts Inc.
4 //
5 // MicropolisJ is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GPLv3, with additional terms.
7 // See the README file, included in this distribution, for details.
8 
9 package micropolisj.engine;
10 
11 import static micropolisj.engine.TileConstants.CLEAR;
12 import micropolisj.util.Utilities;
13 
14 class ToolEffect implements ToolEffectIfc {
15  final Micropolis city;
16  final ToolPreview preview;
17  public final int originX;
18  public final int originY;
19  int playerID;
20 
21  public ToolEffect(Micropolis city) {
22  this(city, 0);
23  }
24 
25  public ToolEffect(Micropolis city, int playerID) {
26  this(city, 0, 0, playerID);
27  }
28 
29  public ToolEffect(Micropolis city, int xpos, int ypos, int playerID) {
30  this.city = city;
31  this.preview = new ToolPreview(playerID);
32  this.originX = xpos;
33  this.originY = ypos;
34  this.playerID = playerID;
35  }
36 
37  // implements ToolEffectIfc
38  public int getTile(int dx, int dy) {
39  int c = preview.getTile(dx, dy);
40  if(c != CLEAR) {
41  return c;
42  }
43 
44  if(city.testBounds(originX + dx, originY + dy)) {
45  return city.getTile(originX + dx, originY + dy);
46  }
47  else {
48  // tiles outside city's boundary assumed to be
49  // tile #0 (dirt).
50  return 0;
51  }
52  }
53 
54  // implements ToolEffectIfc
55  public void makeSound(int dx, int dy, Sound sound) {
56  preview.makeSound(dx, dy, sound);
57  }
58 
59  // implements ToolEffectIfc
60  public void setTile(int dx, int dy, int tileValue) {
61  preview.setTile(dx, dy, tileValue);
62  }
63 
64  // implements ToolEffectIfc
65  public void spend(int amount) {
66  preview.spend(amount);
67  }
68 
69  // implements ToolEffectIfc
70  public void toolResult(ToolResult tr) {
71  preview.toolResult(tr);
72  }
73 
74 
75  //TODO: dont bulldoze enemy buildings
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;
80  }
81 
82  if(city.getPlayerInfo(playerID).budget.totalFunds < preview.cost) {
83  return ToolResult.INSUFFICIENT_FUNDS;
84  }
85 
86 
87 
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];
92  if(c != CLEAR) {
93  city.setTile(originX + x - preview.offsetX, originY + y - preview.offsetY, (char) c);
94  anyFound = true;
95  }
96  }
97  }
98 
99  for(ToolPreview.SoundInfo si : preview.sounds) {
100  city.makeSound(si.x, si.y, si.sound);
101  }
102 
103  if(anyFound && preview.cost != 0) {
104  city.spend(preview.cost, city.getPlayerInfo(playerID));
105  return ToolResult.SUCCESS;
106  }
107  else {
108  return preview.toolResult;
109  }
110  }
111  public int getPlayerID() {
112  return playerID;
113  }
114 
115 }