-
Notifications
You must be signed in to change notification settings - Fork 0
/
fantasma.java
190 lines (174 loc) · 5.08 KB
/
fantasma.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;
/**
* Write a description of class fantasma here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Fantasma extends Actor
{
private Actor player;
private String nameImage;
private ArrayList<ArrayList<String>> scenario;
private boolean placed;
private boolean crash;
private int xi, yi, x, y, dirX, dirY;
private int scared;
private int dir, lastDir;
/**
* Act - do whatever the fantasma wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Fantasma(Actor player, String nameImage, ArrayList<ArrayList<String>> scenario){
this.player = player;
setImage(nameImage);
resize();
this.nameImage = nameImage;
this.scenario = scenario;
placed = false;
crash = true;
dirX = 0;
dirY = -1;
dir = 0;
lastDir = dir;
scared = 0;
}
public void act()
{
if(!placed){
if(x <= 477 || x >= 483){
x += x<=477? 1 : -1;
setLocation(x, y);
}else{
y--;
setLocation(x, y);
}
if(x >= 477 && x <= 483 && y <= 255){
placed = true;
}
}else{
eatPacman(scared);
movement();
}
}
public void eatPacman(int scared){
Scenario scenario = (Scenario)getWorld();
if(intersects(player)){
switch(scared){
case 0:
scenario.gameOver();
break;
case 1:
Player player1 = (Player)player;
player1.addScore(30);
scenario.refreshScore();
scare(0);
setLocation(xi, yi);
x = xi;
y = yi;
placed = false;
break;
}
}
}
private void movement(){
int j = (x-175)/20;
int i = (y-43)/20;
if(crash){
dir = Greenfoot.getRandomNumber(4);
switch(dir){
case 0:
dirX = 0;
dirY = -1;
break;
case 1:
dirX = 1;
dirY = 0;
break;
case 2:
dirX = 0;
dirY = 1;
break;
case 3:
dirX = -1;
dirY = 0;
break;
}
if(lastDir != dir){
crash = false;
}
}else{
crash = true;
switch(dir){
case 0:
if(i>0 && scenario.get(i+dirY).get(j+dirX).equals("1")){
x += dirX;
y += dirY;
lastDir = dir;
setLocation(x, y);
crash = false;
}
break;
case 1:
if(j<30 && scenario.get(i+dirY).get(j+dirX).equals("1")){
x += dirX;
y += dirY;
lastDir = dir;
setLocation(x, y);
crash = false;
}
break;
case 2:
if(i<29 && scenario.get(i+dirY).get(j+dirX).equals("1")){
x += dirX;
y += dirY;
lastDir = dir;
setLocation(x, y);
crash = false;
}
break;
case 3:
if(j>0 && scenario.get(i+dirY).get(j+dirX).equals("1")){
x += dirX;
y += dirY;
lastDir = dir;
setLocation(x, y);
crash = false;
}
break;
}
}
}
public void scare(int scared){
this.scared = scared;
switch(scared){
case 0:
setImage(nameImage);
resize();
break;
case 1:
setImage("ScaredGhost.png");
resize();
break;
}
}
public void resize(){
GreenfootImage myImage = getImage();
int newHeight = 35;
int newWidth = 35;
myImage.scale(newHeight, newWidth);
}
public void initXY(){
xi = getX();
yi = getY();
x = xi;
y = yi;
}
}