-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnemy.java
executable file
·208 lines (181 loc) · 6.37 KB
/
Enemy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* A class used to model enemies.
*
* @author Tiger Zhao
* @version January 13, 2016
*/
public class Enemy extends Actor
{
int screenX= 800;
int screenY= 800;
GreenfootImage[] movingSprites;
int delay;
int maxDelay;
GreenfootImage idleSprite;
int power;//amount of damage for each hit
boolean moving =false;
String direction ="";
int speed;
boolean shooting =false;
int damage;
int damageDelay;
int maxDamageDelay;
int level=1; //highest level = better stats
boolean dead =false;
static HashMap<String, int[][]> graph = new HashMap<String,int[][]>();
boolean hasPath =false;
int[][] pathToFollow;
boolean noPath = false;
int curPath=0;
boolean needPath =false;
HealthBar healthBar;
boolean paused =false;
boolean start = false;
/**
* An empty constructor
*/
public Enemy(){
}
/**
* A constructor used to create an enemy based on its level.
*/
public Enemy(int level){
start = true;
this.level=level;
}
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(paused){
return;
}
if(start){
start = false;
getWorld().addObject(healthBar, getX(),getY()+ getImage().getHeight());
}
controlMovement();
controlWeapons();
controlDeath();
}
/**
* Used to manage the enemy's death.
*/
public void controlDeath(){
if(dead){
Player p = (Player) getWorld().getObjects(Player.class).get(0);
p.gainExp(level *60);
getWorld().removeObject(this);
}
}
/**
* Used to manage the movement.
*/
public void controlMovement(){
}
/**
* Used to manage attacks.
*/
public void controlWeapons(){
}
/**
* Used to deal damage to this enemy.
*
* @param d An integer indicating the amount of damage to do.
*/
public void damage(int d){
healthBar.damage(d);
if (healthBar.health<=0){
dead =true;
}
}
/**
* Used to move the enemy more accurately and without passing through impassable objects.
*
* @param speed The speed to move.
*/
public void move(int speed){
double squareX = (Math.cos(Math.toRadians(getRotation())));
double squareY = (Math.sin(Math.toRadians(getRotation())));
int circleX = (int) (speed*squareX * Math.sqrt(1- 0.5* Math.pow(squareY,2)));
int circleY = (int) (speed*squareY * Math.sqrt(1- 0.5* Math.pow(squareX,2)));
int origX=getX();
int origY=getY();
if (getWorld().getObjectsAt(getX() +circleX, getY()+circleY, Impassable.class).isEmpty() && getWorld().getObjectsAt(getX() +circleX, getY()+circleY, Player.class).isEmpty() ) {
super.setLocation(getX()+circleX, getY()+circleY);
}else if(getWorld().getObjectsAt(getX() +circleX, getY(), Impassable.class).isEmpty() && getWorld().getObjectsAt(getX() +circleX, getY(), Player.class).isEmpty()) { //what if we only move x?
super.setLocation(getX()+circleX, getY());
}else if(getWorld().getObjectsAt(getX() , getY()+circleY, Impassable.class).isEmpty() && getWorld().getObjectsAt(getX() , getY()+circleY, Player.class).isEmpty()){//what if we only move y?
super.setLocation(getX(), getY()+circleY);
}
}
/**
* @return A boolean indicating if the enemy can see the player without any impassable objects blocking.
*/
public boolean canSeePlayer(){
Player p = (Player) getWorld().getObjects(Player.class).get(0);
return pointsMeet(getX(),getY(),p.getX(),p.getY());
}
/**
* @return A boolean indicating if the enemy can a point without any impassable objects blocking.
*
* @param destX An integer indicating the x value of the point.
* @param destY An integer indicating the y value of the point.
*/
public boolean canSeePoint(int destX, int destY){
return pointsMeet(getX(),getY(),destX,destY);
}
/**
* Determines if a point can meet another point without colliding with something impassable.
*
* @param startX An integer indicating the first x value.
* @param startY An integer indicating the first y value.
* @param destX An integer indicating the second x value.
* @param destY An integer indicating the second y value.
*/
public boolean pointsMeet(int startX, int startY, int destX, int destY){
int origX = getX();
int origY=getY();
setLocation(startX, startY);
int originalRotation = getRotation();
this.turnTowards(destX, destY);
int rotation = getRotation();
this.setRotation(originalRotation);
setLocation(origX, origY);
int s = 10;
double cos = Math.cos(Math.toRadians(rotation));
double sin = Math.sin(Math.toRadians(rotation));
double curX = startX;
double curY = startY;
int distance =0;
while (distance<1000){
curX += cos*s;
curY += sin*s;
distance+=Math.abs(cos*s);
distance+=Math.abs(sin*s);
if (Math.abs(curX-destX)<10 && Math.abs(curY-destY)<10){
return true;
}else if (getWorld().getObjectsAt((int) curX, (int) curY, Impassable.class).size() >0){
return false;
}
}
return false;
}
/**
* Calculates the returns the distance to the player.
*
* @return A double indicating the distance from the player.
*/
public double distanceToPlayer(){
List<Player> temp = getWorld().getObjects(Player.class);
if(temp.size()>0){
Player p=(Player)temp.get(0);
return Math.sqrt(Math.pow(getX()-p.getX(),2) + Math.pow(getY()-p.getY(),2));
}
return 0;
}
}