-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel1.java
More file actions
179 lines (142 loc) · 5.1 KB
/
Copy pathLevel1.java
File metadata and controls
179 lines (142 loc) · 5.1 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
import greenfoot.*;
import java.util.ArrayList;
import java.util.List;
public class Level1 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 long startTime;
private static final int GAME_TIME_LIMIT = 30 * 1000;
private int remainingTime;
private boolean gameWon = false;
private int zombie4Count = 0, maxZombie4s = 5;
private int zombie2Count = 0, maxZombie2s = 5;
private int greatswordZombie4Count = 0, maxGreatswordZombie4s = 3;
private int zombie3Count = 0, maxZombie3s = 2;
private int zombie4Delay = 120;
private int zombie2Delay = 180;
private int greatswordZombie4Delay = 250;
private int zombie3Delay = 300;
private int zombie4Timer = 0;
private int zombie2Timer = 0;
private int greatswordZombie4Timer = 0;
private int zombie3Timer = 0;
private ArrayList<int[]> portalPositions;
public Level1() {
super(700, 700, 1, false);
bg = new GreenfootImage("background.png");
setBackground(bg);
backgroundMusic = new GreenfootSound("backsound.mp3");
backgroundMusic.playLoop();
startTime = System.currentTimeMillis();
player = new Player();
addObject(player, roomWidth / 2, roomHeight / 2);
addWalls();
healthBar = new HealthBar(player.getMaxHealth());
addObject(healthBar, 130, 50);
portalPositions = new ArrayList<>();
addPortal(350, 30);
addPortal(50, 300);
addPortal(650, 300);
timeDisplay = new Actor() { public void act() {} };
addObject(timeDisplay, 150, 30);
}
private void addPortal(int x, int y) {
Portal portal = new Portal();
addObject(portal, x, y);
if (x == 350 && y == 30) portal.setRotation(90);
portalPositions.add(new int[]{x, y});
}
private void addWalls() {
addObject(new Wall06(), 350, 20);
addObject(new Wall07(), 350, 680);
addObject(new Wall08(), 30, 350);
addObject(new Wall09(), 670, 350);
}
public void act() {
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();
handleSpawning();
}
private void handleSpawning() {
zombie4Timer++;
zombie2Timer++;
greatswordZombie4Timer++;
zombie3Timer++;
if (zombie4Timer >= zombie4Delay && zombie4Count < maxZombie4s) {
summonZombie4();
zombie4Timer = 0;
}
if (zombie2Timer >= zombie2Delay && zombie2Count < maxZombie2s) {
summonZombie2();
zombie2Timer = 0;
}
if (greatswordZombie4Timer >= greatswordZombie4Delay && greatswordZombie4Count < maxGreatswordZombie4s) {
summonGreatswordZombie4();
greatswordZombie4Timer = 0;
}
if (zombie3Timer >= zombie3Delay && zombie3Count < maxZombie3s) {
summonZombie3();
zombie3Timer = 0;
}
}
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 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 Level2());
}
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);
}
}