1 | package main;
|
---|
2 |
|
---|
3 | import java.util.Iterator;
|
---|
4 | import java.util.Random;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.awt.geom.Point2D;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.awt.Point;
|
---|
9 |
|
---|
10 | public class Enemy extends Creature
|
---|
11 | {
|
---|
12 | int xpReward;
|
---|
13 | Point patrolTarget;
|
---|
14 | public int spotRadius;
|
---|
15 | public int patrolRadius;
|
---|
16 | boolean pursuing;
|
---|
17 | private HashMap<Item, Double> itemDrops;
|
---|
18 |
|
---|
19 | public Enemy(final String name, final int xOffset, final int yOffset, final int xpReward) {
|
---|
20 | super(name, xOffset, yOffset);
|
---|
21 | this.pursuing = false;
|
---|
22 | this.xpReward = xpReward;
|
---|
23 | this.pursuing = false;
|
---|
24 | this.itemDrops = new HashMap<Item, Double>();
|
---|
25 | }
|
---|
26 |
|
---|
27 | public Enemy(final Enemy cr) {
|
---|
28 | super(cr);
|
---|
29 | this.pursuing = false;
|
---|
30 | this.xpReward = cr.xpReward;
|
---|
31 | this.spotRadius = cr.spotRadius;
|
---|
32 | this.patrolRadius = cr.patrolRadius;
|
---|
33 | this.itemDrops = cr.itemDrops;
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public Enemy copy(final Point newLoc) {
|
---|
38 | final Enemy newCr = new Enemy(this);
|
---|
39 | newCr.initLoc(newLoc);
|
---|
40 | return newCr;
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | protected void initLoc(final Point newLoc) {
|
---|
45 | super.initLoc(newLoc);
|
---|
46 | this.patrolTarget = new Point(newLoc);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void AI_move(final Player player, final Map map) {
|
---|
51 | if (this.pursuing) {
|
---|
52 | if (player.dead || this.patrolTarget.distance(player.loc) > this.patrolRadius) {
|
---|
53 | this.setEnemyTarget(null, map);
|
---|
54 | this.setTarget(this.patrolTarget, map);
|
---|
55 | this.pursuing = false;
|
---|
56 | }
|
---|
57 | else if (this.path == null || this.path.getLast().distance(player.loc) > this.loc.distance(this.path.getLast()) || this.loc.distance(player.loc) * 2.0 < player.loc.distance(this.path.getLast())) {
|
---|
58 | this.setEnemyTarget(player, map);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | else if (this.patrolTarget.distance(player.loc) < this.spotRadius && !player.dead) {
|
---|
62 | this.setEnemyTarget(player, map);
|
---|
63 | this.pursuing = true;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void AI_react(final Map map) {
|
---|
69 | if (this.enemyTarget != null) {
|
---|
70 | final double distance = this.loc.distance(this.enemyTarget.loc);
|
---|
71 | if (this.getModel().action != Action.Attacking && this.getModel().action != Action.BeenHit && distance <= this.startRange) {
|
---|
72 | this.startAttack(this.enemyTarget);
|
---|
73 | }
|
---|
74 | if (this.readyToAttack() && this.minRange <= distance && distance <= this.maxRange) {
|
---|
75 | this.attack(this.enemyTarget);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void addItemDrop(final Item i, final double dropChance) {
|
---|
81 | if (0.0 <= dropChance && dropChance <= 1.0) {
|
---|
82 | this.itemDrops.put(i, dropChance);
|
---|
83 | return;
|
---|
84 | }
|
---|
85 | throw new IllegalArgumentException("dropChance must be between 0.0 and 1.0");
|
---|
86 | }
|
---|
87 |
|
---|
88 | public LinkedList<Item> generateDrops() {
|
---|
89 | final LinkedList<Item> drops = new LinkedList<Item>();
|
---|
90 | final Iterator<java.util.Map.Entry<Item, Double>> iter = this.itemDrops.entrySet().iterator();
|
---|
91 | final Random rand = new Random();
|
---|
92 | while (iter.hasNext()) {
|
---|
93 | final java.util.Map.Entry<Item, Double> cur = iter.next();
|
---|
94 | if (rand.nextDouble() < cur.getValue()) {
|
---|
95 | drops.add(cur.getKey().copy(new Point(0, 0)));
|
---|
96 | }
|
---|
97 | }
|
---|
98 | return drops;
|
---|
99 | }
|
---|
100 | }
|
---|