Repository navigation
Expand file tree
/
Copy pathLevel3.java
More file actions
162 lines (138 loc) · 6.56 KB
/
Copy pathLevel3.java
File metadata and controls
162 lines (138 loc) · 6.56 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
import city.cs.engine.*;
import org.jbox2d.common.Vec2;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;
/**
* - Horizontal Moving platforms added which patrol an area
* - Portals added
* */
public class Level3 extends Game {
private final World world;
private final JFrame frame;
private final int player1Level1Score;
private final int player2Level1Score;
private final int player1Level2Score;
private final int player2Level2Score;
private static SoundClip madrid;
public Level3(String stadiumBackground, int l1p1Score, int l1p2Score, int l2p1Score, int l2p2Score) {
super(stadiumBackground);
this.player1Level1Score = l1p1Score;
this.player2Level1Score = l1p2Score;
this.player1Level2Score = l2p1Score;
this.player2Level2Score = l2p2Score;
world = getWorld();
frame = getFrame();
// moving platforms
MovingPlatform p2 = new MovingPlatform(world, 7, new Vec2(-15,7));
p2.setPosition(new Vec2(-15,7));
MovingPlatform p3 = new MovingPlatform(world, 3, new Vec2(-5,3));
p3.setPosition(new Vec2(-15,3));
MovingPlatform p4 = new MovingPlatform(world, 0, new Vec2(-3,0));
p4.setPosition(new Vec2(0,0));
MovingPlatform p5 = new MovingPlatform(world, 2, new Vec2(-20,3));
p5.setPosition(new Vec2(-20,3));
MovingPlatform p6 = new MovingPlatform(world, 2, new Vec2(13,9));
p6.setPosition(new Vec2(10,9));
MovingPlatform p7 = new MovingPlatform(world, 9, new Vec2(0,9));
p7.setPosition(new Vec2(0,9));
System.out.println("LEVEL 2: " + player1Level2Score + "-" + player2Level2Score);
}
@Override
public void nextLevel() {
frame.dispose();
new Level4("data/stadiums/final.png", player1Level1Score, player2Level1Score,
player1Level2Score, player2Level2Score, getPlayer1().getPlayer1Score(),
getPlayer2().getPlayer2Score());
madrid.stop();
}
@Override
public void buttonListeners() {
ActionListener settingsButton = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Settings(getView(),madrid, world, frame, getPlayer1(), getPlayer2());
}
};
getSettings().addActionListener(settingsButton);
}
static {
try {
madrid = new SoundClip("data/sounds/madrid.wav");
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
System.out.println(e);
}
}
@Override
public void playBackgroundSound(){
madrid.play();
madrid.setVolume(0.6f);
madrid.loop();
}
// override powerUp by adding portal ability
@Override
public void powerUp(){
String[] powerUps = {"BIG NET", "JUMP BOOST", "BASKETBALL", "SPEED", "PORTAL", "PORTAL"}; // added portal twice, so it's the most common power up
String chosenPowerUp = powerUps[new Random().nextInt(powerUps.length)];
// switch case runs appropriate code depending on what ability is chosen
switch (chosenPowerUp) {
case "SPEED" -> {
// makes player go faster
Shape speedShape = new BoxShape(0f, 0f);
StaticBody speed = new StaticBody(world, speedShape);
speed.setPosition(new Vec2(randomX(), randomY())); // 0,-10
speed.addImage(new BodyImage("data/powerups/lightningBolt.png", 3));
PlayerOneSpeedListener p1Speed = new PlayerOneSpeedListener(getPlayer1(), getView());
speed.addCollisionListener(p1Speed);
PlayerTwoSpeedListener p2Speed = new PlayerTwoSpeedListener(getPlayer2(), getView());
speed.addCollisionListener(p2Speed);
}
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 "PORTAL" -> {
// teleports to high position in the world
Shape portalShape = new BoxShape(0f, 0f);
StaticBody portal = new StaticBody(world, portalShape);
portal.setPosition(new Vec2(randomX(), randomY()));
portal.addImage(new BodyImage("data/powerups/portal.png", 4));
Portal p= new Portal(getPlayer1(),getPlayer2());
portal.addCollisionListener(p);
}
default -> {
}
}
}
}