-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMonster.java
92 lines (92 loc) · 3.56 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
import java.util.*;
import processing.core.PApplet;
public class Monster extends Character{
private ArrayList<Cell> path;
private int x,y;
private boolean chase;
private PApplet sketch;
private Maze maze;
private float matrixX, matrixY;
private Character player;
public Monster(int x, int y, Maze maze, Character player){
super(maze);
this.x=x;
this.y=y;
this.maze=maze;
sketch=Main.getInstance();
chase = false;
path = generateRandomPath();
matrixX = x*Maze.WALL_SCALE+Maze.WALL_SCALE/2;
matrixY = y*Maze.WALL_SCALE+Maze.WALL_SCALE/2;
this.player = player;
}
public void move(){
int velocity = chase? 25:100;
if(path.size()!=0){
Cell next = path.get(path.size()-1);
float nextMatrixX = next.getY()*Maze.WALL_SCALE+Maze.WALL_SCALE/2;
float nextMatrixY = next.getX()*Maze.WALL_SCALE+Maze.WALL_SCALE/2;
if(matrixX==nextMatrixX && matrixY==nextMatrixY){
path.remove(path.size()-1);
} else {
matrixX += (nextMatrixX-matrixX>0? 1:(nextMatrixX-matrixX==0? 0:-1))*Maze.WALL_SCALE/velocity;
matrixY += (nextMatrixY-matrixY>0? 1:(nextMatrixY-matrixY==0? 0:-1))*Maze.WALL_SCALE/velocity;
int[] matrixPoint = Maze.getMatrixPoint(new Point(matrixX,matrixY));
x = matrixPoint[0]/2;
y = matrixPoint[1]/2;
}
} else if(chase){
Point playerPos = player.getPos();
float tempX = matrixX + (playerPos.getX()-matrixX>0? 1:(playerPos.getX()-matrixX==0? 0:-1))*Maze.WALL_SCALE/velocity;
float tempY = matrixY + (playerPos.getY()-matrixY>0? 1:(playerPos.getY()-matrixY==0? 0:-1))*Maze.WALL_SCALE/velocity;
if(matrixX<playerPos.getX()){
matrixX = sketch.constrain(tempX,tempX,playerPos.getX());
} else {
matrixX = sketch.constrain(tempX,playerPos.getX(),tempX);
}
if(matrixY<playerPos.getY()){
matrixY = sketch.constrain(tempY,tempY,playerPos.getY());
} else {
matrixY = sketch.constrain(tempY,playerPos.getY(),tempY);
}
} else {
path = generateRandomPath();
}
}
public void render(){
if(player.canSeeCharacter(player,this)){
sketch.noStroke();
sketch.fill(160,160,160);
sketch.ellipse(matrixX+maze.getOffsetX(),matrixY+maze.getOffsetY(),20,20);
chase=true;
int[] playerPos = Maze.getMatrixPoint(player.getPos());
ArrayList<Cell> tempPath = maze.solve(y,x,playerPos[1]/2,playerPos[0]/2);
if(tempPath.size()>0){
tempPath.remove(tempPath.size()-1);
}
path = tempPath;
} else if(chase){
chase=false;
path = generateRandomPath();
}
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public boolean onTopOfPlayer(){
return matrixX==player.getPos().getX() && matrixY==player.getPos().getY();
}
public ArrayList<Cell> generateRandomPath(){
ArrayList<Cell> returnPath = maze.solve(y,x,(int)(Math.random()*maze.getLength()),(int)(Math.random()*maze.getWidth()));
if(returnPath.size()>0){
returnPath.remove(returnPath.size()-1);
}
return returnPath;
}
public Point location(){
return new Point(matrixX+maze.getOffsetX(), matrixY+maze.getOffsetY());
}
}