1
2
3
4 package spellcast.enchantment;
5
6 import spellcast.beings.IBeing;
7 import spellcast.damage.DamageType;
8 import spellcast.damage.NullDamageType;
9 import spellcast.model.Id;
10
11 /***
12 * The default implementation of <code>Enchantment</code>.
13 *
14 * @author Barrie Treloar
15 * @version $Revision: 1.1 $
16 *
17 * @see Enchantment
18 */
19 public class EnchantmentImpl implements Enchantment {
20 /*** Use serialVersionUID for interoperability. */
21 private static final long serialVersionUID = 1L;
22
23 /*** The <code>Id</code> of this <code>Enchantment</code>. */
24 private Id id = Id.createId();
25
26 /*** The name of this <code>Enchantment</code>. */
27 private String name = "";
28
29 /*** The description of this <code>Enchantment</code>. */
30 private String description = "";
31
32 /*** Indicates whether this <code>Enchantment</code> is permanent. */
33 private boolean permanent;
34
35 /***
36 * The <code>DamageTypes</code> that this <code>Enchantment</code>
37 * provides resistance for.
38 */
39 private DamageType resistance = new NullDamageType();
40
41 /*** The number of turns an Enchantment lasts for. */
42 private int duration;
43
44 /*** The caster <code>IBeing</code> of this <code>Enchantment</code>. */
45 private IBeing caster;
46
47 /*** The target <code>IBeing</code> of this <code>Enchantment</code>. */
48 private IBeing target;
49
50 /***
51 * Provided for serialization, do not use as a constructor.
52 */
53 protected EnchantmentImpl() {
54 }
55
56 /***
57 * Create the <code>Enchantment</code> cast by theCaster at theTarget.
58 *
59 * @param theName the name of this <code>Enchantment</code>.
60 * @param theCaster the <code>IBeing</code> casting the
61 * <code>Enchantment</code>.
62 * @param theTarget the <code>IBeing</code> that is the target of the
63 * <code>Enchantment</code>.
64 */
65 public EnchantmentImpl(
66 final String theName, final IBeing theCaster, final IBeing theTarget
67 ) {
68 setName(theName);
69 setCaster(theCaster);
70 setTarget(theTarget);
71 }
72
73 /***
74 * {@inheritDoc}
75 */
76 public final Id getId() {
77 return id;
78 }
79
80 /***
81 * Set the <code>Id</code> of this <code>Enchantment</code>.
82 *
83 * @param theId the <code>Id</code> of this <code>Enchantment</code>.
84 */
85 public final void setId(final Id theId) {
86 id = theId;
87 }
88
89 /***
90 * Determine is this object equals the specified object.
91 *
92 * @param o the object to test equality against.
93 *
94 * @return true if this <code>Enchantment</code> equals the specified
95 * object.
96 */
97 public final boolean equals(final Object o) {
98 boolean result = false;
99
100 if (o instanceof EnchantmentImpl) {
101 EnchantmentImpl other = (EnchantmentImpl) o;
102 result = getId().equals(other.getId());
103 }
104
105 return result;
106 }
107
108 /***
109 * The hashCode of this object as per <code>Object.hashCode</code>.
110 *
111 * @return the hashCode of this object.
112 */
113 public final int hashCode() {
114 int hashCode = 0;
115 hashCode ^= getId().hashCode();
116
117 return hashCode;
118 }
119
120 /***
121 * {@inheritDoc}
122 */
123 public final String getName() {
124 return name;
125 }
126
127 /***
128 * Set the name of this <code>Enchantment</code>.
129 *
130 * @param theName the name of this <code>Enchantment</code>.
131 */
132 public final void setName(final String theName) {
133 name = theName;
134 }
135
136 /***
137 * {@inheritDoc}
138 */
139 public final String getDescription() {
140 return description;
141 }
142
143 /***
144 * Set the description of this <code>Enchantment</code>.
145 *
146 * @param theDescription the description of this <code>Enchantment</code>.
147 */
148 public final void setDescription(final String theDescription) {
149 description = theDescription;
150 }
151
152 /***
153 * {@inheritDoc}
154 */
155 public final boolean isPermanent() {
156 return permanent;
157 }
158
159 /***
160 * Sets whether this <code>Enchantment</code> is permanent.
161 *
162 * @param isPermanent whether this <code>Enchantment</code> is permanent.
163 */
164 public final void setPermanent(final boolean isPermanent) {
165 permanent = isPermanent;
166 duration = 0;
167 }
168
169 /***
170 * {@inheritDoc}
171 */
172 public final boolean isResistant(final DamageType damageType) {
173 return damageType.equals(resistance);
174 }
175
176 /***
177 * Gets the resistance of this <code>Enchantment</code>. If no resistances
178 * then <code>NullDamageType</code> is returned.
179 *
180 * @return the <code>DamageType</code> that this <code>Enchantment</code>
181 * is resistant to. If no resistances then
182 * <code>NullDamageType</code> is returned.
183 */
184 public final DamageType getResistance() {
185 return resistance;
186 }
187
188 /***
189 * Sets the resistance of this <code>Enchantment</code> to the specified
190 * <code>DamageType</code>.
191 *
192 * @param damageType the <code>DamageType</code> this
193 * <code>Enchantment</code> will be resistant to.
194 */
195 public final void setResistance(final DamageType damageType) {
196 resistance = damageType;
197 }
198
199 /***
200 * Return the duration left on this <code>Enchantment</code> before it
201 * expires. If the <code>Enchantment</code> is permanent then this value
202 * has no meaning.
203 *
204 * @return the duration left on this <code>Enchantment</code>.
205 */
206 public final int getDuration() {
207 return duration;
208 }
209
210 /***
211 * Sets the duration of this <code>Enchantment</code>.
212 *
213 * @param theDuration the duration of the <code>Enchantment</code>.
214 */
215 public final void setDuration(final int theDuration) {
216 duration = theDuration;
217 }
218
219 /***
220 * {@inheritDoc}
221 */
222 public final boolean turnComplete() {
223 boolean wornOff = false;
224
225 if (!isPermanent()) {
226 duration--;
227
228 if (duration <= 0) {
229 wornOff = true;
230 }
231 }
232
233 return wornOff;
234 }
235
236 /***
237 * {@inheritDoc}
238 */
239 public final IBeing getCaster() {
240 return caster;
241 }
242
243 /***
244 * Sets the casting <code>Wizard</code> of this <code>Enchantment</code>.
245 *
246 * @param theCaster the casting <code>IBeing</code>.
247 */
248 public final void setCaster(final IBeing theCaster) {
249 caster = theCaster;
250 }
251
252 /***
253 * {@inheritDoc}
254 */
255 public final IBeing getTarget() {
256 return target;
257 }
258
259 /***
260 * Sets the target <code>IBeing</code> of this <code>Enchantment</code>.
261 *
262 * @param theTarget the target <code>IBeing</code> of this
263 * <code>Enchantment</code>.
264 */
265 public final void setTarget(final IBeing theTarget) {
266 target = theTarget;
267 }
268 }