-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonster.java
111 lines (96 loc) · 3.68 KB
/
Monster.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
package Game;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
public class Monster extends Entity{
Point destination = new Point(0,0);
double angleInRadians;
double angleInDegrees;
double movementSpeed = 3;
Rectangle collisionBox;
public static BufferedImage monsterImage;
public Monster(int xpos, int ypos, String monsterType) {
this.xpos = xpos;
this.ypos = ypos;
if (monsterType.equals("monster1")){
monsterImage = FileIO.loadImage("/Textures/Player.png");
}
}
public void moveTowardsDestination(){
if(!atDestination()){
//find angle between current position and destination
angleInDegrees = getAngleToDestination();
angleInRadians = Math.toRadians(angleInDegrees);
//move towards destination
setPosition(xpos+(movementSpeed*Math.cos(angleInRadians)),ypos+(movementSpeed*Math.sin(angleInRadians)));
}
}
public void setPosition(double x, double y){
Rectangle collisionBoxAtNewXPosition = new Rectangle((int)x,(int)ypos,32,32);
Rectangle collisionBoxAtNewYPosition = new Rectangle((int)xpos,(int)y,32,32);
boolean collidedWithSomethingX = false;
boolean collidedWithSomethingY = false;
for(int x1 = (int)(xpos/32)-((int)movementSpeed+1);x1<(int)(xpos/32)+(int)movementSpeed+1;x1++){
for(int y1 = (int)(ypos/32)-((int)movementSpeed+1);y1<(int)(ypos/32)+(int)movementSpeed+1;y1++){
if(x1>=0&&x1<GamePanel.levels.get(GamePanel.currentLevel).width&&y1>=0&&y1<GamePanel.levels.get(GamePanel.currentLevel).height){
Tile temp = GamePanel.levels.get(GamePanel.currentLevel).tileMap[x1][y1];
if(collisionBoxAtNewXPosition.intersects(temp.collisionBox)&&temp.collisionType>=1){
//GamePanel.levels.get(GamePanel.currentLevel).tileMap[x1][y1].flagged=true;
collidedWithSomethingX = true;
}
}
}
}
for(int x1 = (int)(xpos/32)-((int)movementSpeed+1);x1<(int)(xpos/32)+(int)movementSpeed+1;x1++){
for(int y1 = (int)(ypos/32)-((int)movementSpeed+1);y1<(int)(ypos/32)+(int)movementSpeed+1;y1++){
if(x1>=0&&x1<GamePanel.levels.get(GamePanel.currentLevel).width&&y1>=0&&y1<GamePanel.levels.get(GamePanel.currentLevel).height){
Tile temp = GamePanel.levels.get(GamePanel.currentLevel).tileMap[x1][y1];
if(collisionBoxAtNewYPosition.intersects(temp.collisionBox)&&temp.collisionType>=1){
//GamePanel.levels.get(GamePanel.currentLevel).tileMap[x1][y1].flagged=true;
collidedWithSomethingY = true;
}
}
}
}
//if player is going to collide with the tile at their future position
if(collidedWithSomethingX==false||GamePanel.godmode){
xpos = x;
collisionBox.x=(int)xpos;
}
if(collidedWithSomethingY==false||GamePanel.godmode){
ypos = y;
collisionBox.y=(int)ypos;
}
}
/*
* check if the player is already at their destination
*
* @return true if player is at their destination, false if not
*/
public boolean atDestination(){
if(Math.abs(xpos-destination.x)<=movementSpeed&&Math.abs(ypos-destination.y)<=movementSpeed){
setPosition(destination.x,destination.y);
return true;
}
return false;
}
/*
* get angle between the player and the destination
*
* @return the angle to move in
*/
public double getAngleToDestination() {
double angle = (float) Math.toDegrees(Math.atan2((double)destination.y - ypos, (double)destination.x - xpos));
if(angle < 0){
angle += 360;
}
return angle;
}
public void update(){
moveTowardsDestination();
}
public void Draw(Graphics2D g){
g.drawImage(GamePanel.playerImage,(ApplicationUI.windowWidth/2)-16,(ApplicationUI.windowHeight/2)-16,32,32,null);
}
}