Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
ToolPreview.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 java.util.*;
12 
13 import micropolisj.util.Utilities;
14 import static micropolisj.engine.TileConstants.*;
15 
16 public class ToolPreview implements ToolEffectIfc {
17  public int offsetX;
18  public int offsetY;
19  public short[][] tiles;
20  public int cost;
22  public List<SoundInfo> sounds;
23  public int playerID;
24 
25  public static class SoundInfo {
26  public int x;
27  public int y;
28  public Sound sound;
29 
30  SoundInfo(int x, int y, Sound sound) {
31  this.x = x;
32  this.y = y;
33  this.sound = sound;
34  }
35  }
36 
37  public ToolPreview(int playerID) {
38  this.tiles = new short[0][0];
39  this.sounds = new ArrayList<SoundInfo>();
40  this.toolResult = ToolResult.NONE;
41  this.playerID = playerID;
42  }
43 
44  // implements ToolEffectIfc
45  public int getTile(int dx, int dy) {
46  if(inRange(dx, dy)) {
47  return tiles[offsetY + dy][offsetX + dx];
48  }
49  else {
50  return CLEAR;
51  }
52  }
53 
54  public CityRect getBounds() {
55  return new CityRect(-offsetX, -offsetY, getWidth(), getHeight());
56  }
57 
58  int getWidth() {
59  return tiles.length != 0 ? tiles[0].length : 0;
60  }
61 
62  int getHeight() {
63  return tiles.length;
64  }
65 
66  boolean inRange(int dx, int dy) {
67  return offsetY + dy >= 0 && offsetY + dy < getHeight() && offsetX + dx >= 0 && offsetX + dx < getWidth();
68  }
69 
70  void expandTo(int dx, int dy) {
71  if(tiles == null || tiles.length == 0) {
72  tiles = new short[1][1];
73  tiles[0][0] = CLEAR;
74  offsetX = -dx;
75  offsetY = -dy;
76  return;
77  }
78 
79  // expand each existing row as needed
80  for(int i = 0; i < tiles.length; i++) {
81  short[] A = tiles[i];
82  if(offsetX + dx >= A.length) {
83  int newLen = offsetX + dx + 1;
84  short[] AA = new short[newLen];
85  System.arraycopy(A, 0, AA, 0, A.length);
86  Arrays.fill(AA, A.length, newLen, CLEAR);
87  tiles[i] = AA;
88  }
89  else if(offsetX + dx < 0) {
90  int addl = -(offsetX + dx);
91  int newLen = A.length + addl;
92  short[] AA = new short[newLen];
93  System.arraycopy(A, 0, AA, addl, A.length);
94  Arrays.fill(AA, 0, addl, CLEAR);
95  tiles[i] = AA;
96  }
97  }
98 
99  if(offsetX + dx < 0) {
100  int addl = -(offsetX + dx);
101  offsetX += addl;
102  }
103 
104  int width = tiles[0].length;
105  if(offsetY + dy >= tiles.length) {
106  int newLen = offsetY + dy + 1;
107  short[][] newTiles = new short[newLen][width];
108  System.arraycopy(tiles, 0, newTiles, 0, tiles.length);
109  for(int i = tiles.length; i < newLen; i++) {
110  Arrays.fill(newTiles[i], CLEAR);
111  }
112  tiles = newTiles;
113  }
114  else if(offsetY + dy < 0) {
115  int addl = -(offsetY + dy);
116  int newLen = tiles.length + addl;
117  short[][] newTiles = new short[newLen][width];
118  System.arraycopy(tiles, 0, newTiles, addl, tiles.length);
119  for(int i = 0; i < addl; i++) {
120  Arrays.fill(newTiles[i], CLEAR);
121  }
122  tiles = newTiles;
123 
124  offsetY += addl;
125  }
126  }
127 
128  // implements ToolEffectIfc
129  public void makeSound(int dx, int dy, Sound sound) {
130  sounds.add(new SoundInfo(dx, dy, sound));
131  }
132 
133  // implements ToolEffectIfc
134 
135  public void setTile(int dx, int dy, int tileValue) {
136  expandTo(dx, dy);
137  tiles[offsetY + dy][offsetX + dx] = Utilities.codePlayerID(tileValue, playerID);
138  }
139 
140  // implements ToolEffectIfc
141  public void spend(int amount) {
142  cost += amount;
143  }
144 
145  // implements ToolEffectIfc
146  public void toolResult(ToolResult tr) {
147  this.toolResult = tr;
148  }
149 
150  public int getPlayerID() {
151  return playerID;
152  }
153 }