-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scenario.java
248 lines (203 loc) · 7.84 KB
/
Scenario.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
import java.util.List;
import java.io.File;
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 escenario here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Scenario extends World
{
ArrayList<ArrayList<String>> scenario = new ArrayList<ArrayList<String>>();
Player player;
Fantasma fantasma;
Fantasma fantasma1;
Fantasma fantasma2;
Fantasma fantasma3;
Clock clock;
RecordsManager recordsManager;
public static GreenfootSound sound = new GreenfootSound("Music.mp3");
private int playSound;
private Character score1;
private Character score2;
private Character score3;
private Character score4;
private String[] targetsMap={
"1,1,1,1,1,1,1,1,1,1,1,1,1,1,,,,,,,,,,, 1,1,1,1,1,1,1,1,1,1,1,1",
"1,0,0,0,0,0,1,0,0,0,0,0,0,1,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,1,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0",
"0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0",
"0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0",
"1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1",
"1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1",
"1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1",
"1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1",
"0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0",
"0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0",
"1,1,1,1,1,1,1,0,0,0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1",
"1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1",
"1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1",
"1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1",
};
/**
* Constructor for objects of class escenario.
*
*/
public Scenario()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(900, 720, 1);
prepare();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
readScenario();
player = new Player(scenario);
addObject(player,180,48);
fantasma = new Fantasma(player,"Pink.png", scenario);
addObject(fantasma, 420, 320);
fantasma.initXY();
fantasma1 = new Fantasma(player,"Blue.png", scenario);
addObject(fantasma1, 460, 320);
fantasma1.initXY();
fantasma2 = new Fantasma(player,"Red.png", scenario);
addObject(fantasma2, 500, 320);
fantasma2.initXY();
fantasma3 = new Fantasma(player,"Yellow.png", scenario);
addObject(fantasma3, 540, 320);
fantasma3.initXY();
clock = new Clock(this);
Cherry cherry = new Cherry(player, this, clock);
addObject(cherry, 780, 48);
Snail snail = new Snail(player);
addObject(snail, 780, 625);
Chilli chilli = new Chilli(player);
addObject(chilli, 185, 625);
ReturnButton returnButton = new ReturnButton(1);
addObject(returnButton, 70, 600);
PantallaPrincipal.stopMusic();
recordsManager = new RecordsManager();
score1 = new Character();
addObject(score1 , 35, 470);
score2 = new Character();
addObject(score2 , 62, 470);
score3 = new Character();
addObject(score3 , 89, 470);
score4 = new Character();
addObject(score4 , 116, 470);
dibujaEscenario();
}
public void act(){
if(playSound == 0)
playSound = playMusic();
/*MouseInfo mouse = Greenfoot.getMouseInfo();
if (Greenfoot.mouseClicked(null)) {
int x = mouse.getX();
int y = mouse.getY();
System.out.println(x + "," + y);
}*/
clock.time();
}
public void readScenario(){
try {
List<String> lines = Files.readAllLines(Paths.get("scenario.csv"));
Iterator<String> linesIterator = lines.iterator();
while (linesIterator.hasNext()) {
String csvLine = linesIterator.next();
ArrayList<String> xArray = new ArrayList<String>();
String[] line = csvLine.split(",", 31);
for(int j = 0; j < 31; j++){
xArray.add(line[j]);
}
scenario.add(xArray);
}
} catch (IOException e) {
e.printStackTrace();
}
//imprimir();
}
public void imprimir(){
for(int i = 0; i < 30; i++){
for(int j = 0; j < 31; j++){
System.out.print(scenario.get(i).get(j));
}
System.out.print("\n");
}
}
public void eatGhost(int scared){
fantasma.scare(scared);
fantasma1.scare(scared);
fantasma2.scare(scared);
fantasma3.scare(scared);
}
public void gameOver(){
sound.stop();
Greenfoot.playSound("GameOver.mp3");
Greenfoot.setWorld(new SaveScreen(player.getScore()));
}
public int playMusic(){
int wasPlayed = 0;
if(playSound == 0){
sound.play();
wasPlayed = 1;
}
return wasPlayed;
}
public static void stopMusic(){
sound.stop();
}
public void refreshScore(){
int score = player.getScore();
score4.changeImage(String.valueOf(score % 10));
score /= 10;
score3.changeImage(String.valueOf(score % 10));
score /= 10;
score2.changeImage(String.valueOf(score % 10));
score /= 10;
score1.changeImage(String.valueOf(score));
}
public void dibujaEscenario(){
for(int i = 0; i<targetsMap.length; i++){
String mapline = targetsMap[i];
for(int j = 0; j < mapline.length(); j++){
char mapchar = mapline.charAt(j);
int y = (i*20)+48;
int x = (j*10)+180;
switch(mapchar){
case '1' :
addObject(new Punto(player),x,y);
break;
default:
break;
}
}
}
}
}