Repository navigation
Expand file tree
/
Copy pathLevel2.java
More file actions
187 lines (163 loc) · 7.37 KB
/
Copy pathLevel2.java
File metadata and controls
187 lines (163 loc) · 7.37 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
import city.cs.engine.*;
import city.cs.engine.Shape;
import org.jbox2d.common.Vec2;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;
/**
* - Lava platforms added (ball will set on fire and become more bouncy when collided with)
* - Circles randomly spawn, making game harder
* - Freeze ability added
* */
public class Level2 extends Game {
private final World world;
private final JFrame frame;
private final int player1Level1Score;
private final int player2Level1Score;
private Timer circleTimer;
private int timeRemaining = 60;
private static SoundClip barca;
public Level2(String stadiumBackground, int l1p1Score, int l1p2Score) {
super(stadiumBackground);
this.player1Level1Score = l1p1Score;
this.player2Level1Score = l1p2Score;
world = getWorld();
frame = getFrame();
System.out.println("LEVEL 1: " + player1Level1Score + "-" + player2Level1Score);
// NEW BODIES/SHAPES
// Lava platforms (ball turns on fire and more bouncy)
Shape leftLavaShape = new PolygonShape(6.15f,-0.217f, -6.052f,-0.242f, -6.027f,0.25f, 6.101f,0.25f);
StaticBody leftLava = new StaticBody(world, leftLavaShape);
leftLava.addImage(new BodyImage("data/stadiums/lava.gif", 0.5f));
leftLava.setPosition(new Vec2(-11, 4.5f));
leftLava.setAngleDegrees(-40);
FireBall fire1 = new FireBall(getBall());
leftLava.addCollisionListener(fire1);
Shape rightLavaShape = new PolygonShape(6.15f,-0.217f, -6.052f,-0.242f, -6.027f,0.25f, 6.101f,0.25f);
StaticBody rightLava = new StaticBody(world, rightLavaShape);
rightLava.addImage(new BodyImage("data/stadiums/lava.gif", 0.5f));
rightLava.setPosition(new Vec2(11, 4.5f));
rightLava.setAngleDegrees(40);
FireBall fire2 = new FireBall(getBall());
rightLava.addCollisionListener(fire2);
circleMethod();
circleTimer.start();
}
static {
try {
barca = new SoundClip("data/sounds/barca.wav");
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
System.out.println(e);
}
}
@Override
public void nextLevel() {
frame.dispose();
new Level3("data/stadiums/SantiagoBernabeu.png", player1Level1Score, player2Level1Score,
getPlayer1().getPlayer1Score(), getPlayer2().getPlayer2Score());
barca.stop();
}
public int randomXcircle (){
int min = -19;
int max = 19;
return (int) (Math.random() * (max - min)) + min;
}
public int randomYcircle (){
int min = -4;
int max = 11;
return (int) (Math.random() * (max - min)) + min;
}
// spawns circle obstacles every 9 seconds at random positions
public void circleMethod(){
circleTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timeRemaining --;
if (timeRemaining % 9 == 0) {
Shape circle = new CircleShape(1.8f);
StaticBody circleBody = new StaticBody(world, circle);
circleBody.setLineColor(null);
circleBody.setFillColor(Color.ORANGE);
circleBody.setPosition(new Vec2(randomXcircle(),randomYcircle()));
} else if (timeRemaining <= 0){
circleTimer.stop();
}
}
});
}
@Override
public void playBackgroundSound(){
barca.play();
barca.setVolume(0.6f);
barca.loop();
}
@Override
public void buttonListeners() {
ActionListener settingsButton = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Settings(getView(),barca, world, frame, getPlayer1(), getPlayer2());
}
};
getSettings().addActionListener(settingsButton);
}
/**
* power up is overridden to add level specific power ups (in this case "FREEZE")
* - also removes certain power ups from previous levels (level 2 doesn't have speed, instead has been replaced by freeze*/
// override powerUp
@Override
public void powerUp(){
String[] powerUps = {"FREEZE", "BIG NET", "JUMP BOOST", "BASKETBALL"};
String chosenPowerUp = powerUps[new Random().nextInt(powerUps.length)]; // selects random ability within powerUps list
// switch case runs appropriate code depending on what ability is chosen
switch (chosenPowerUp) {
case "BIG NET" -> {
// makes net bigger
Shape enlargeShape = new BoxShape(0f, 0);
StaticBody enlarge = new StaticBody(world, enlargeShape);
enlarge.setPosition(new Vec2(randomX(), randomY()));
enlarge.addImage(new BodyImage("data/powerups/goalEnlargement.png", 4));
RightGoalEnlargement rightEnlarge = new RightGoalEnlargement(getRightCol(), getRightNet(), world, getPlayer1(), getBall());
enlarge.addCollisionListener(rightEnlarge);
LeftGoalEnlargement leftEnlarge = new LeftGoalEnlargement(getLeftCol(), getLeftNet(), world, getPlayer2(), getBall());
enlarge.addCollisionListener(leftEnlarge);
}
case "JUMP BOOST" -> {
// gives a jump boost
Shape jumpShape = new BoxShape(0f, 0f);
StaticBody jump = new StaticBody(world, jumpShape);
jump.setPosition(new Vec2(randomX(), randomY()));
jump.addImage(new BodyImage("data/powerups/jump.gif", 3));
PlayerOneJumpBoost p1Jump = new PlayerOneJumpBoost(getPlayer1(), getView());
jump.addCollisionListener(p1Jump);
PlayerTwoJumpBoost p2Jump = new PlayerTwoJumpBoost(getPlayer2(), getView());
jump.addCollisionListener(p2Jump);
}
case "BASKETBALL" -> {
// makes ball bouncier
Shape basketBallShape = new BoxShape(0f, 0f);
StaticBody basketBall = new StaticBody(world, basketBallShape);
basketBall.setPosition(new Vec2(randomX(), randomY()));
basketBall.addImage(new BodyImage("data/balls/basketBall.png", 2));
BasketBall b= new BasketBall(getBall(),world);
basketBall.addCollisionListener(b);
}
case "FREEZE" -> {
// opposition can't move
Shape freezeShape = new BoxShape(0f, 0f);
StaticBody freeze = new StaticBody(world, freezeShape);
freeze.setPosition(new Vec2(randomX(), randomY()));
freeze.addImage(new BodyImage("data/powerups/ice.png", 4));
Freeze f = new Freeze(getPlayer1(),getPlayer2(),getView());
freeze.addCollisionListener(f);
}
default -> {
}
}
}
}