1 package org.argosfields.model;
2
3 import org.apache.commons.lang.NullArgumentException;
4 import org.apache.commons.lang.builder.EqualsBuilder;
5 import org.apache.commons.lang.builder.HashCodeBuilder;
6 import org.apache.commons.lang.builder.ToStringBuilder;
7 import org.argosfields.multi.Player;
8 import org.argosfields.resource.ResourceManager;
9
10 /***
11 * Unit.java
12 *
13 * @author Xavier Cho
14 * @version $Revision: 1.10 $ $Date: 2004/04/17 18:21:01 $
15 */
16 public abstract class Unit {
17 public static final int MAX_STRENGTH = 6;
18 public static final int MAX_RANK = 6;
19
20 private String name;
21 private Direction facing;
22 private int rank;
23 private int strength;
24
25 private Player player;
26
27 public Unit() {
28 String key = getTypeId();
29
30 if (key == null) {
31 String msg = "Attribute typeId cannot be null.";
32 throw new NullPointerException(msg);
33 }
34
35 key = key.toLowerCase().replace(' ', '-');
36
37 ResourceManager resources = ResourceManager.getInstance();
38
39 this.name = resources.getString("unit.name." + key);
40 this.facing = Direction.NORTH;
41 this.strength = MAX_STRENGTH;
42 this.rank = 0;
43 }
44
45 protected abstract int getIconStartIndex();
46
47 protected abstract int getTerrainFlags();
48
49 public abstract String getTypeId();
50
51 public abstract int getBuildCost();
52
53 public abstract int getDefensePoint();
54
55 public abstract int getWeight();
56
57 /***
58 * @return
59 */
60 public String getMoveSoundName() {
61 return null;
62 }
63
64 /***
65 * @return
66 */
67 public String getFireSoundName() {
68 return null;
69 }
70
71 /***
72 * @return
73 */
74 public String getName() {
75 return name;
76 }
77
78 /***
79 * @return
80 */
81 public int getIconIndex() {
82 int offset = 0;
83
84 //FIXME
85 if (player != null) {
86 //offset = (player.getId() - 1) * 6;
87 offset = 0;
88 }
89
90 return getIconStartIndex() * 12 + facing.toInt() + offset;
91 }
92
93 /***
94 * @return
95 */
96 public Direction getFacing() {
97 return facing;
98 }
99
100 /***
101 * @param facing
102 */
103 public void setFacing(final Direction facing) {
104 if (facing == null) {
105 throw new NullArgumentException("facing");
106 }
107
108 this.facing = facing;
109 }
110
111 public boolean canTraverse(final Tile tile) {
112 if (tile == null) {
113 throw new NullArgumentException("tile");
114 }
115
116 return (tile.getUnit() == null)
117 && (getTerrainFlags() & tile.getTerrainFlag()) != 0;
118 }
119
120 /***
121 * @return Returns the player.
122 */
123 public Player getPlayer() {
124 return player;
125 }
126
127 /***
128 * @param player The player to set.
129 */
130 public void setPlayer(final Player player) {
131 this.player = player;
132 }
133
134 /***
135 * @return Returns the rank.
136 */
137 public int getRank() {
138 return rank;
139 }
140
141 /***
142 * @param rank The rank to set.
143 */
144 public void setRank(final int rank) {
145 if (rank < 0 || rank > MAX_RANK) {
146 String msg = "Argument rank should be between 0 and " + MAX_RANK;
147 throw new IllegalArgumentException(msg);
148 }
149
150 this.rank = rank;
151 }
152
153 /***
154 * @return Returns the strength.
155 */
156 public int getStrength() {
157 return strength;
158 }
159
160 /***
161 * @param strength The strength to set.
162 */
163 public void setStrength(final int strength) {
164 if (strength < 0 || strength > MAX_STRENGTH) {
165 String msg =
166 "Argument strength should be between 0 and " + MAX_STRENGTH;
167 throw new IllegalArgumentException(msg);
168 }
169
170 this.strength = strength;
171 }
172
173 public static Unit forName(final String id) throws Exception {
174 if (id == null) {
175 throw new NullArgumentException("id");
176 }
177
178 Unit unit = null;
179
180 String key = id.toLowerCase().replace(' ', '-');
181 key = "unit.class.".concat(key);
182
183 ResourceManager resources = ResourceManager.getInstance();
184 String className = resources.getString(key);
185
186 unit = (Unit) Class.forName(className).newInstance();
187
188 return unit;
189 }
190
191 /***
192 * @see java.lang.Object#equals(java.lang.Object)
193 */
194 public boolean equals(final Object obj) {
195 return EqualsBuilder.reflectionEquals(obj, this);
196 }
197
198 /***
199 * @see java.lang.Object#hashCode()
200 */
201 public int hashCode() {
202 return HashCodeBuilder.reflectionHashCode(this);
203 }
204
205 /***
206 * @see java.lang.Object#toString()
207 */
208 public String toString() {
209 ToStringBuilder builder = new ToStringBuilder(this);
210 builder.append("name", name);
211 builder.append("strength", strength);
212 builder.append("rank", rank);
213 builder.append("player", player);
214 builder.append("typeId", getTypeId());
215
216 return builder.toString();
217 }
218 }
This page was automatically generated by Maven