source: lost-perception/main/Enemy.java@ a10d422

Last change on this file since a10d422 was a10d422, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 4 years ago

Create a working makefile target for MapEditor

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