1 | package main;
|
---|
2 |
|
---|
3 | import java.util.Iterator;
|
---|
4 | import java.awt.geom.Point2D;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.util.ArrayList;
|
---|
7 |
|
---|
8 | public class Player extends Creature
|
---|
9 | {
|
---|
10 | private NPC npcTarget;
|
---|
11 | int level;
|
---|
12 | int experience;
|
---|
13 | Weapon weapon;
|
---|
14 | Armor[] armor;
|
---|
15 | ArrayList<Item> inventory;
|
---|
16 | public Item[][] invSpace;
|
---|
17 | public int statPoints;
|
---|
18 | private int baseAttackSpeed;
|
---|
19 | private int baseMoveSpeed;
|
---|
20 | private int basePixelSpeed;
|
---|
21 | public double attackSpeedMod;
|
---|
22 | public double moveSpeedMod;
|
---|
23 | public int damageMod;
|
---|
24 | public int hpMod;
|
---|
25 | public int mpMod;
|
---|
26 |
|
---|
27 | public Player(final String name, final int xOffset, final int yOffset, final int level, final int experience) {
|
---|
28 | super(name, xOffset, yOffset);
|
---|
29 | this.invSpace = new Item[10][8];
|
---|
30 | this.npcTarget = null;
|
---|
31 | this.level = level;
|
---|
32 | this.experience = experience;
|
---|
33 | this.weapon = null;
|
---|
34 | this.armor = new Armor[4];
|
---|
35 | for (int x = 0; x < this.armor.length; ++x) {
|
---|
36 | this.armor[x] = null;
|
---|
37 | }
|
---|
38 | this.inventory = new ArrayList<Item>();
|
---|
39 | this.statPoints = 0;
|
---|
40 | final double n = 1.0;
|
---|
41 | this.moveSpeedMod = n;
|
---|
42 | this.attackSpeedMod = n;
|
---|
43 | final boolean damageMod = false;
|
---|
44 | this.mpMod = (damageMod ? 1 : 0);
|
---|
45 | this.hpMod = (damageMod ? 1 : 0);
|
---|
46 | this.damageMod = (damageMod ? 1 : 0);
|
---|
47 | this.basePixelSpeed = this.speed;
|
---|
48 | for (int i = 0; i < this.invSpace.length; ++i) {
|
---|
49 | for (int j = 0; j < this.invSpace[0].length; ++j) {
|
---|
50 | this.invSpace[i][j] = null;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public int getDamage() {
|
---|
57 | if (this.weapon == null) {
|
---|
58 | return super.getDamage() + this.stats[StatType.Strength.ordinal()] / 2 + this.damageMod;
|
---|
59 | }
|
---|
60 | return this.weapon.getDamage() + this.stats[StatType.Strength.ordinal()] / 2 + this.damageMod;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void takeDamage(final int damage) {
|
---|
65 | this.hitpoints -= (int)Math.round(damage * Math.pow(0.5, this.stats[StatType.Armor.ordinal()] / 100.0));
|
---|
66 | }
|
---|
67 |
|
---|
68 | public int getBaseAttackSpeed() {
|
---|
69 | return this.baseAttackSpeed;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public int getBaseMoveSpeed() {
|
---|
73 | return this.baseMoveSpeed;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public int getBasePixelSpeed() {
|
---|
77 | return this.basePixelSpeed;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public int getArmorRating() {
|
---|
81 | int total = 0;
|
---|
82 | for (int x = 0; x < this.armor.length; ++x) {
|
---|
83 | total += this.armor[x].getArmorRating();
|
---|
84 | }
|
---|
85 | return total;
|
---|
86 | }
|
---|
87 |
|
---|
88 | public double getAttackSpeed() {
|
---|
89 | final double dexMod = 1.0 + this.stats[StatType.Dexterity.ordinal()] / 50.0;
|
---|
90 | double weaponAttackSpeed = 1.0;
|
---|
91 | if (this.weapon != null) {
|
---|
92 | weaponAttackSpeed = this.weapon.getAttackSpeed();
|
---|
93 | }
|
---|
94 | return weaponAttackSpeed / this.attackSpeedMod / dexMod;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public double getMoveSpeed() {
|
---|
98 | final double dexMod = 1.0 + this.stats[StatType.Dexterity.ordinal()] / 50.0;
|
---|
99 | double weaponAttackSpeed = 1.0;
|
---|
100 | if (this.weapon != null) {
|
---|
101 | weaponAttackSpeed = this.weapon.getAttackSpeed();
|
---|
102 | }
|
---|
103 | return weaponAttackSpeed / this.attackSpeedMod / dexMod;
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void propagateStatChanges() {
|
---|
108 | this.maxHitpoints = this.stats[StatType.Constitution.ordinal()] * 5 + this.hpMod;
|
---|
109 | this.maxManapoints = this.stats[StatType.Intelligence.ordinal()] * 5 + this.mpMod;
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public void setModel(final Model model) {
|
---|
114 | super.setModel(model);
|
---|
115 | this.baseAttackSpeed = this.getModel().getAnimation(Direction.North, Action.Attacking).drawInterval;
|
---|
116 | this.baseMoveSpeed = this.getModel().getAnimation(Direction.North, Action.Walking).drawInterval;
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public void AI_move(final Player player, final Map map) {
|
---|
121 | if (this.enemyTarget != null && this.path != null && (this.path.getLast().distance(this.enemyTarget.loc) > this.loc.distance(this.path.getLast()) || this.loc.distance(this.enemyTarget.loc) * 2.0 < this.enemyTarget.loc.distance(this.path.getLast()))) {
|
---|
122 | this.setEnemyTarget(this.enemyTarget, map);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public void AI_react(final Map map) {
|
---|
128 | if (this.enemyTarget == null) {
|
---|
129 | return;
|
---|
130 | }
|
---|
131 | if (this.enemyTarget.getClass() == NPC.class) {
|
---|
132 | if (this.enemyTarget.intersects(this)) {
|
---|
133 | this.startDialog((NPC)this.enemyTarget);
|
---|
134 | this.enemyTarget = null;
|
---|
135 | this.path = null;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | else {
|
---|
139 | final double distance = this.loc.distance(this.enemyTarget.loc);
|
---|
140 | if (this.getModel().action != Action.Attacking && this.getModel().action != Action.BeenHit && distance <= this.startRange) {
|
---|
141 | this.startAttack(this.enemyTarget);
|
---|
142 | }
|
---|
143 | if (this.readyToAttack() && this.minRange <= distance && distance <= this.maxRange) {
|
---|
144 | this.attack(this.enemyTarget, map);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public boolean isTalking() {
|
---|
150 | return this.npcTarget != null;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public NPC getNPCTarget() {
|
---|
154 | return this.npcTarget;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void startDialog(final NPC npc) {
|
---|
158 | this.npcTarget = npc;
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void endDialog() {
|
---|
162 | this.npcTarget = null;
|
---|
163 | }
|
---|
164 |
|
---|
165 | public void attack(final Creature enemy, final Map map) {
|
---|
166 | this.enemyTarget.getModel().getCurrentAnimation().reset();
|
---|
167 | this.enemyTarget.getModel().action = Action.BeenHit;
|
---|
168 | this.enemyTarget.takeDamage(this.getDamage());
|
---|
169 | if (this.enemyTarget.hitpoints <= 0) {
|
---|
170 | this.experience += ((Enemy)enemy).xpReward;
|
---|
171 | this.checkLevelUp();
|
---|
172 | final Iterator<Item> itemDrops = ((Enemy)enemy).generateDrops().iterator();
|
---|
173 | while (itemDrops.hasNext()) {
|
---|
174 | this.pickUp(itemDrops.next());
|
---|
175 | }
|
---|
176 | this.enemyTarget.die();
|
---|
177 | this.enemyTarget = null;
|
---|
178 | this.path = null;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void pickUp(final Item item) {
|
---|
183 | for (int i = 0; i < this.invSpace.length; ++i) {
|
---|
184 | for (int j = 0; j < this.invSpace[0].length; ++j) {
|
---|
185 | if (this.itemFits(item, j, i)) {
|
---|
186 | for (int a = 0; a < item.getImgHeight(); ++a) {
|
---|
187 | for (int b = 0; b < item.getImgWidth(); ++b) {
|
---|
188 | this.invSpace[i + a][j + b] = item;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | item.invX = j;
|
---|
192 | item.invY = i;
|
---|
193 | this.inventory.add(item);
|
---|
194 | return;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | public void pickUp(final Item item, final int x, final int y) {
|
---|
201 | if (this.itemFits(item, x, y)) {
|
---|
202 | for (int a = 0; a < item.getImgHeight(); ++a) {
|
---|
203 | for (int b = 0; b < item.getImgWidth(); ++b) {
|
---|
204 | this.invSpace[y + a][x + b] = item;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | item.invX = x;
|
---|
208 | item.invY = y;
|
---|
209 | this.inventory.add(item);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | public void drop(final Item item) {
|
---|
214 | for (int a = 0; a < item.getImgHeight(); ++a) {
|
---|
215 | for (int b = 0; b < item.getImgWidth(); ++b) {
|
---|
216 | this.invSpace[item.invY + a][item.invX + b] = null;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | this.inventory.remove(item);
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void moveItem(final Item item, final int x, final int y) {
|
---|
223 | if (this.itemFits(item, x, y)) {
|
---|
224 | for (int a = 0; a < item.getImgHeight(); ++a) {
|
---|
225 | for (int b = 0; b < item.getImgWidth(); ++b) {
|
---|
226 | this.invSpace[item.invY + a][item.invX + b] = null;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | for (int a = 0; a < item.getImgHeight(); ++a) {
|
---|
230 | for (int b = 0; b < item.getImgWidth(); ++b) {
|
---|
231 | this.invSpace[y + a][x + b] = item;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | item.invX = x;
|
---|
235 | item.invY = y;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | public boolean itemFits(final Item item, final int x, final int y) {
|
---|
240 | if (x < 0 || x + item.imgWidth > this.invSpace[0].length) {
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 | if (y < 0 || y + item.imgHeight > this.invSpace.length) {
|
---|
244 | return false;
|
---|
245 | }
|
---|
246 | for (int i = 0; i < item.getImgHeight(); ++i) {
|
---|
247 | for (int j = 0; j < item.getImgWidth(); ++j) {
|
---|
248 | if (this.invSpace[i + y][j + x] != null && this.invSpace[i + y][j + x] != item) {
|
---|
249 | return false;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 | return true;
|
---|
254 | }
|
---|
255 |
|
---|
256 | public int getMaxExperience() {
|
---|
257 | return this.level * 1000;
|
---|
258 | }
|
---|
259 |
|
---|
260 | public void checkLevelUp() {
|
---|
261 | if (this.experience >= this.getMaxExperience()) {
|
---|
262 | this.experience -= this.getMaxExperience();
|
---|
263 | ++this.level;
|
---|
264 | this.statPoints += 5;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | public void equipWeapon(final Weapon i) {
|
---|
269 | this.equipItem(i);
|
---|
270 | if (this.weapon != null) {
|
---|
271 | this.unequipItem(this.weapon);
|
---|
272 | this.pickUp(this.weapon);
|
---|
273 | }
|
---|
274 | this.weapon = i;
|
---|
275 | Direction[] values;
|
---|
276 | for (int length = (values = Direction.values()).length, j = 0; j < length; ++j) {
|
---|
277 | final Direction dir = values[j];
|
---|
278 | this.getModel().getAnimation(dir, Action.Attacking).drawInterval = (int)(this.getBaseAttackSpeed() * this.getAttackSpeed());
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | public void unequipWeapon(final Weapon i) {
|
---|
283 | this.unequipItem(i);
|
---|
284 | this.weapon = null;
|
---|
285 | Direction[] values;
|
---|
286 | for (int length = (values = Direction.values()).length, j = 0; j < length; ++j) {
|
---|
287 | final Direction dir = values[j];
|
---|
288 | this.getModel().getAnimation(dir, Action.Attacking).drawInterval = (int)(this.getBaseAttackSpeed() * this.getAttackSpeed());
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | public void equipArmor(final int pos, final Armor i) {
|
---|
293 | final int[] stats = this.stats;
|
---|
294 | final int ordinal = StatType.Armor.ordinal();
|
---|
295 | stats[ordinal] += i.getArmorRating();
|
---|
296 | this.equipItem(i);
|
---|
297 | if (this.armor[pos] != null) {
|
---|
298 | final int[] stats2 = this.stats;
|
---|
299 | final int ordinal2 = StatType.Armor.ordinal();
|
---|
300 | stats2[ordinal2] -= this.armor[pos].getArmorRating();
|
---|
301 | this.unequipItem(this.armor[pos]);
|
---|
302 | this.pickUp(this.armor[pos]);
|
---|
303 | }
|
---|
304 | this.armor[pos] = i;
|
---|
305 | }
|
---|
306 |
|
---|
307 | public void unequipArmor(final int pos, final Armor i) {
|
---|
308 | final int[] stats = this.stats;
|
---|
309 | final int ordinal = StatType.Armor.ordinal();
|
---|
310 | stats[ordinal] -= i.getArmorRating();
|
---|
311 | this.unequipItem(i);
|
---|
312 | this.armor[pos] = null;
|
---|
313 | }
|
---|
314 |
|
---|
315 | private void unequipItem(final Item i) {
|
---|
316 | final Iterator<Effect> iter = i.getEffects().iterator();
|
---|
317 | while (iter.hasNext()) {
|
---|
318 | iter.next().unapply(this);
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | private void equipItem(final Item i) {
|
---|
323 | final Iterator<Effect> iter = i.getEffects().iterator();
|
---|
324 | while (iter.hasNext()) {
|
---|
325 | iter.next().apply(this);
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|