|
|
@@ -33,10 +33,9 @@ public class Match implements Runnable {
|
|
|
private int timeleft;
|
|
|
private long lasttime;
|
|
|
|
|
|
-
|
|
|
- //Contains all the server game logic.
|
|
|
- //The server decides when collision occurs or a player is allowed to move.
|
|
|
- //Also manages health, time and chat
|
|
|
+ // Contains all the server game logic.
|
|
|
+ // The server decides when collision occurs or a player is allowed to move.
|
|
|
+ // Also manages health, time and chat
|
|
|
public Match(NetworkUser nwuser1, NetworkUser nwuser2) {
|
|
|
nwuser1.startMatch(nwuser1.getName(), nwuser2.getName(), 1, this);
|
|
|
nwuser2.startMatch(nwuser2.getName(), nwuser1.getName(), 2, this);
|
|
|
@@ -49,7 +48,7 @@ public class Match implements Runnable {
|
|
|
levels = LevelReader.readLevelsFromFiles();
|
|
|
Collections.sort(levels, new LevelSorter());
|
|
|
playLevel(getCurrentLevel());
|
|
|
-
|
|
|
+
|
|
|
timer = new Timer();
|
|
|
timer.schedule(new TimerTask() {
|
|
|
@Override
|
|
|
@@ -59,14 +58,14 @@ public class Match implements Runnable {
|
|
|
}, 0, 1000 / 30);
|
|
|
}
|
|
|
|
|
|
- //Play a selected level
|
|
|
+ // Play a selected level
|
|
|
public void playLevel(Level level) {
|
|
|
balls.clear();
|
|
|
- if(level == null){
|
|
|
+ if (level == null) {
|
|
|
timer.cancel();
|
|
|
setOverlayMessage("No more levels to play!");
|
|
|
- }else{
|
|
|
- setOverlayMessage("Level "+(currentlevel+1));
|
|
|
+ } else {
|
|
|
+ setOverlayMessage("Level " + (currentlevel + 1));
|
|
|
player1.reset();
|
|
|
player2.reset();
|
|
|
line1 = null;
|
|
|
@@ -80,7 +79,7 @@ public class Match implements Runnable {
|
|
|
balls.add(b.clone());
|
|
|
}
|
|
|
timeleft = level.getTime();
|
|
|
- lasttime = System.currentTimeMillis();
|
|
|
+ lasttime = System.currentTimeMillis();
|
|
|
setOverlayMessage("");
|
|
|
}
|
|
|
}
|
|
|
@@ -88,9 +87,7 @@ public class Match implements Runnable {
|
|
|
public Level getCurrentLevel() {
|
|
|
if (currentlevel >= 0 && currentlevel < levels.size()) {
|
|
|
return levels.get(currentlevel);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
stopMatch();
|
|
|
}
|
|
|
return null;
|
|
|
@@ -98,25 +95,25 @@ public class Match implements Runnable {
|
|
|
|
|
|
/* UPDATE */
|
|
|
public void update() {
|
|
|
- if(player1.getHealth() == 0 && player2.getHealth() == 0){
|
|
|
+ if (player1.getHealth() == 0 && player2.getHealth() == 0) {
|
|
|
return;
|
|
|
}
|
|
|
- if(player1.getHealth() > 0){
|
|
|
+ if (player1.getHealth() > 0) {
|
|
|
if (p1direction == 1)
|
|
|
player1.walkRight();
|
|
|
else if (p1direction == -1)
|
|
|
player1.walkLeft();
|
|
|
player1.update();
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
player1.setY(-100);
|
|
|
}
|
|
|
- if(player2.getHealth() > 0){
|
|
|
+ if (player2.getHealth() > 0) {
|
|
|
if (p2direction == 1)
|
|
|
player2.walkRight();
|
|
|
else if (p2direction == -1)
|
|
|
player2.walkLeft();
|
|
|
player2.update();
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
player2.setY(-100);
|
|
|
}
|
|
|
|
|
|
@@ -169,7 +166,8 @@ public class Match implements Runnable {
|
|
|
bal.update();
|
|
|
}
|
|
|
}
|
|
|
- } catch (ConcurrentModificationException e) {}
|
|
|
+ } catch (ConcurrentModificationException e) {
|
|
|
+ }
|
|
|
timeleft -= (System.currentTimeMillis() - lasttime);
|
|
|
if (balls.size() == 0) {
|
|
|
if (player1.getHealth() > 0)
|
|
|
@@ -213,7 +211,7 @@ public class Match implements Runnable {
|
|
|
linesinfo += "0|0|0|";
|
|
|
|
|
|
String timeleft = ",0";
|
|
|
- if(getCurrentLevel() != null){
|
|
|
+ if (getCurrentLevel() != null) {
|
|
|
timeleft = "," + (int) (this.timeleft / (double) getCurrentLevel().getTime() * 100);
|
|
|
}
|
|
|
|
|
|
@@ -223,10 +221,11 @@ public class Match implements Runnable {
|
|
|
}
|
|
|
|
|
|
/* GETTERS AND SETTERS */
|
|
|
- public void setOverlayMessage(String m){
|
|
|
+ public void setOverlayMessage(String m) {
|
|
|
nwuser1.sendOverlayText(m);
|
|
|
nwuser2.sendOverlayText(m);
|
|
|
}
|
|
|
+
|
|
|
public void setPlayerDirection(int pid, int dir) {
|
|
|
if (pid == 1)
|
|
|
p1direction = dir;
|
|
|
@@ -236,15 +235,15 @@ public class Match implements Runnable {
|
|
|
|
|
|
public void playerShoot(int pid) {
|
|
|
if (pid == 1 && player1.getHealth() > 0)
|
|
|
- if(line1 == null)
|
|
|
+ if (line1 == null)
|
|
|
line1 = new ShootingLine(player1.getX() + player1.getWidth() / 2, player1.getY() + player1.getHeigth());
|
|
|
if (pid == 2 && player2.getHealth() > 0)
|
|
|
- if(line2 == null)
|
|
|
+ if (line2 == null)
|
|
|
line2 = new ShootingLine(player2.getX() + player2.getWidth() / 2, player2.getY() + player2.getHeigth());
|
|
|
}
|
|
|
|
|
|
public void stopMatch() {
|
|
|
- if(timer != null)
|
|
|
+ if (timer != null)
|
|
|
timer.cancel();
|
|
|
try {
|
|
|
nwuser1.close();
|