-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedGhost.java
More file actions
166 lines (145 loc) · 4.22 KB
/
RedGhost.java
File metadata and controls
166 lines (145 loc) · 4.22 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
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class BlueGhost here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class RedGhost extends Ghosts {
/**
* Images for every rotation
*/
private GreenfootImage imageUp = new GreenfootImage ("redUp.png");
private GreenfootImage imageDown = new GreenfootImage ("redDown.png");
private GreenfootImage imageLeft = new GreenfootImage ("redLeft.png");
private GreenfootImage imageRight = new GreenfootImage ("redRight.png");
public void act() {
//turnRandom();
CheckWall();
checkRotationAndImage();
setMovementSpeed();
}
/**
* Adds some random movement by changing direction with about
* 10% probability every frame
*/
private void turnRandom() {
if (Greenfoot.getRandomNumber(100) > 90) {
checkRotation();
}
}
/**
* Checks which images should be set according to the 'GhostBusters'
* state
*/
private void checkRotationAndImage() {
if (Timer.timer == 0) {
rotationNormal();
} else {
rotationScared();
}
}
/**
* Sets random rotation and sets image for normal Ghost
*/
private void turnRotationNormal() {
int turnDirection = Greenfoot.getRandomNumber(4);
if (turnDirection == 0 && Timer.timer == 0) {
setRotation(270);
setImage(imageUp);
} else if (turnDirection == 1) {
setRotation(90);
setImage(imageDown);
} else if (turnDirection == 2) {
setRotation(180);
setImage(imageLeft);
} else if (turnDirection == 3) {
setRotation(0);
setImage(imageRight);
}
}
/**
* Sets random rotation and sets image for scared Ghost
*/
private void turnRotationScared() {
int turnDirection = Greenfoot.getRandomNumber(4);
if (turnDirection == 0) {
setRotation(270);
setImage(imageScaredUp);
} else if (turnDirection == 1) {
setRotation(90);
setImage(imageScaredDown);
} else if (turnDirection == 2) {
setRotation(180);
setImage(imageScaredLeft);
} else if (turnDirection == 3) {
setRotation(0);
setImage(imageScaredRight);
}
}
/**
* Checks normal Ghost rotation and sets image
* (for accurate image switch)
*/
private void rotationNormal() {
int rotation = getRotation();
if (rotation == 270) {
setImage(imageUp);
} else if (rotation == 90) {
setImage(imageDown);
} else if (rotation == 180) {
setImage(imageLeft);
} else if (rotation == 0) {
setImage(imageRight);
}
}
/**
* Checks scared Ghost rotation and sets image
* (for accurate image switch)
*/
private void rotationScared() {
int rotation = getRotation();
if (rotation == 270) {
setImage(imageScaredUp);
} else if (rotation == 90) {
setImage(imageScaredDown);
} else if (rotation == 180) {
setImage(imageScaredLeft);
} else if (rotation == 0) {
setImage(imageScaredRight);
}
}
/**
* Combines both random rotation switch methods
*/
private void checkRotation() {
if (Timer.timer == 0) {
turnRotationNormal();
} else {
turnRotationScared();
}
}
/**
* Sets Ghost's movement speed according to 'GhostBusters'
*/
private void setMovementSpeed() {
if (Timer.timer == 0) {
move(4);
} else {
move(2);
}
}
/**
* Checks for walls. Acts as collision
*/
private void CheckWall() {
if (isTouching(Wall.class) || isTouching(CodeDoor.class)) {
if (Timer.timer == 0) {
move(-4);
} else {
move(-2);
}
checkRotation();
}
}
}