-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGhost.java
More file actions
120 lines (99 loc) · 2.98 KB
/
Copy pathGhost.java
File metadata and controls
120 lines (99 loc) · 2.98 KB
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
import javafx.application.*;
import javafx.scene.* ;
import javafx.scene.shape.* ;
import javafx.scene.layout.* ;
import javafx.scene.paint.* ;
import javafx.geometry.* ;
public class Ghost
{
private Polygon triangle ;
private double x1, y1, x2, y2, x3, y3 ;
private double centerX, centerY ; // these are the coordinates used to move the triange
private double width, height ;
private Color color ;
private boolean transparent ;
public Ghost(Pane pane, double x, double y, double width, double height) // receive the layout, x, y coordinates and the width and height of the equilateral triangle
{
// store the coordinates of the triangle's center point, its height and width
this.centerX = x ;
this.centerY = y ;
this.width = width ;
this.height = height ;
// set the 3 coordinates of the equilateral triangle
this.x1 = x - (width/2) ;
this.y1 = y - (height/2) ;
this.x2 = x + (width/2) ;
this.y2 = y - (height/2) ;
this.x3 = x ;
this.y3 = y + (height/2) ;
// create the triangle using the 3 coordinates
triangle = new Polygon() ;
triangle.getPoints().addAll(new Double[]{
x1, y1,
x2, y2,
x3, y3}) ;
this.transparent = false ;
pane.getChildren().add(triangle) ;
}
public void setColor(Color c)
{
this.color = c ;
this.triangle.setFill(c) ;
}
public Color getColor()
{
return this.color ;
}
public Double getX()
{
return this.centerX ;
}
public Double getY()
{
return this.centerY ;
}
public void setX(double x_coord)
{
double moving = x_coord - this.centerX ;
this.x1 = this.x1 + moving ;
this.x2 = this.x2 + moving ;
this.x3 = this.x3 + moving ;
this.centerX = x_coord ;
updateCoords() ;
}
public void setY(double y_coord)
{
double moving = y_coord - this.centerY ;
this.y1 = this.y1 + moving ;
this.y2 = this.y2 + moving ;
this.y3 = this.y3 + moving ;
this.centerY = y_coord ;
updateCoords() ;
}
private void updateCoords()
{
triangle.getPoints().clear() ; // get rid of all the current coordinates
triangle.getPoints().addAll(new Double[]{
this.x1, this.y1,
this.x2, this.y2,
this.x3, this.y3}) ;
}
public void setTransparent(boolean b)
{
this.transparent = b ;
}
public Boolean isTransparent()
{
return this.transparent ;
}
public Boolean caughtPacman(Circle pacman, int speed) // will check if the ghost has caught pacman depending on the speed
{
if((this.centerX-speed == pacman.getCenterX() && this.centerY == pacman.getCenterY()) || (this.centerX+speed == pacman.getCenterX() && this.centerY == pacman.getCenterY()))
return true ;
if((this.centerX == pacman.getCenterX() && this.centerY-speed == pacman.getCenterY()) || (this.centerX == pacman.getCenterX() && this.centerY+speed == pacman.getCenterY()))
return true ;
if(this.centerX == pacman.getCenterX() && this.centerY == pacman.getCenterY())
return true ;
return false ;
}
}