-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel2.java
More file actions
197 lines (166 loc) · 6.17 KB
/
Copy pathLevel2.java
File metadata and controls
197 lines (166 loc) · 6.17 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
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
import greenfoot.*;
import java.util.ArrayList;
public class Level2 extends World {
private GreenfootImage bg;
private GreenfootSound backgroundMusic;
private int roomWidth = 700;
private int roomHeight = 700;
private Player player;
private HealthBar healthBar;
private Actor timeDisplay;
private Actor countdownDisplay;
private long startTime;
private static final int GAME_TIME_LIMIT = 50 * 1000;
private int remainingTime;
private int Zombie4Count = 0, maxZombie4s = 7;
private int zombie2Count = 0, maxZombie2s = 7;
private int greatswordZombie4Count = 0, maxGreatswordZombie4s = 5;
private int zombie3Count = 0, maxZombie3s = 4;
private long lastSpawnTime = 0;
private int spawnInterval = 2000;
private ArrayList<int[]> portalPositions;
private boolean gameStarted = false;
private int delayCounter = 3 * 60;
public Level2() {
super(700, 700, 1, false);
bg = new GreenfootImage("bg2.png");
setBackground(bg);
backgroundMusic = new GreenfootSound("backsound.mp3");
backgroundMusic.playLoop();
player = new Player();
addObject(player, roomWidth / 2, roomHeight / 2);
healthBar = new HealthBar(player.getMaxHealth());
addObject(healthBar, 130, 50);
portalPositions = new ArrayList<>();
addPortal(350, 30);
addPortal(50, 350);
addPortal(650, 350);
addPortal(350, 670);
timeDisplay = new Actor() {
public void act() {
if (gameStarted) updateTimeDisplay();
}
};
addObject(timeDisplay, 150, 30);
countdownDisplay = new Actor() { public void act() {} };
addObject(countdownDisplay, roomWidth / 2, 150);
updateCountdownDisplay();
}
private void addPortal(int x, int y) {
Portal portal = new Portal();
addObject(portal, x, y);
if (y < 100 || y > 600) portal.setRotation(90);
portalPositions.add(new int[]{x, y});
}
public void act() {
if (!gameStarted) {
if (delayCounter > 0) {
delayCounter--;
updateCountdownDisplay();
} else {
removeObject(countdownDisplay);
startGame();
}
return;
}
if (player == null) return;
if (player.getHealth() <= 0 || player.isDead()) {
gameOver();
return;
}
if (System.currentTimeMillis() - startTime >= GAME_TIME_LIMIT) {
victory();
return;
}
healthBar.updateHealth(player.getHealth());
remainingTime = (int) ((GAME_TIME_LIMIT - (System.currentTimeMillis() - startTime)) / 1000);
updateTimeDisplay();
spawnEnemiesGradually();
}
private void startGame() {
gameStarted = true;
startTime = System.currentTimeMillis();
}
private void updateCountdownDisplay() {
int seconds = (int) Math.ceil((double) delayCounter / 60.0);
String text = seconds > 0 ? "Level 2 dimulai dalam: " + seconds : "";
GreenfootImage img = new GreenfootImage(roomWidth, roomHeight);
Font font = new Font("Monospaced", true, false, 40);
img.setFont(font);
GreenfootImage textImg = new GreenfootImage(text, 40, Color.RED, new Color(0, 0, 0, 0));
int x = (img.getWidth() - textImg.getWidth()) / 2 - 10;
int y = (img.getHeight() - textImg.getHeight()) / 2;
img.drawImage(textImg, x, y);
countdownDisplay.setImage(img);
}
private void spawnEnemiesGradually() {
long now = System.currentTimeMillis();
if (now - lastSpawnTime >= spawnInterval) {
int type = Greenfoot.getRandomNumber(4);
switch (type) {
case 0:
if (Zombie4Count < maxZombie4s) summonZombie4();
break;
case 1:
if (zombie2Count < maxZombie2s) summonZombie2();
break;
case 2:
if (greatswordZombie4Count < maxGreatswordZombie4s) summonGreatswordZombie4();
break;
case 3:
if (zombie3Count < maxZombie3s) summonZombie3();
break;
}
lastSpawnTime = now;
}
}
public void stopped() {
if (backgroundMusic != null) backgroundMusic.stop();
}
public void started() {
if (backgroundMusic != null && !backgroundMusic.isPlaying()) backgroundMusic.playLoop();
}
private void gameOver() {
backgroundMusic.stop();
Greenfoot.setWorld(new GameOver());
}
private void victory() {
backgroundMusic.stop();
Greenfoot.setWorld(new Level3());
}
private void summonZombie4() {
Zombie4 s = new Zombie4();
int[] pos = portalPositions.get(Greenfoot.getRandomNumber(portalPositions.size()));
addObject(s, pos[0], pos[1]);
Zombie4Count++;
}
private void summonZombie2() {
Zombie2 z = new Zombie2();
int[] pos = portalPositions.get(Greenfoot.getRandomNumber(portalPositions.size()));
addObject(z, pos[0], pos[1]);
zombie2Count++;
}
private void summonGreatswordZombie4() {
Zombie1 g = new Zombie1();
int[] pos = portalPositions.get(Greenfoot.getRandomNumber(portalPositions.size()));
addObject(g, pos[0], pos[1]);
greatswordZombie4Count++;
}
private void summonZombie3() {
Zombie3 r = new Zombie3();
int[] pos = portalPositions.get(Greenfoot.getRandomNumber(portalPositions.size()));
addObject(r, pos[0], pos[1]);
zombie3Count++;
}
public void decreaseZombie4Count() { Zombie4Count--; }
public void decreaseZombie2Count() { zombie2Count--; }
public void decreaseGreatswordZombie4Count() { greatswordZombie4Count--; }
public void decreaseZombie3Count() { zombie3Count--; }
private void updateTimeDisplay() {
GreenfootImage img = new GreenfootImage(220, 50);
img.setColor(Color.WHITE);
img.setFont(new Font(true, false, 20));
img.drawString("Time: " + remainingTime + "s", 10, 30);
timeDisplay.setImage(img);
}
}