Skip to content
Snippets Groups Projects
Commit b0234f81 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

implemented extralifepowerup

parent 2a201767
Branches
Tags
No related merge requests found
......@@ -7,13 +7,17 @@ public class ExtraLifePowerUp extends PowerUp
{
public ExtraLifePowerUp()
{
super(PowerUpType.valueOf("EXTRA_LIFE").getId(), 10);
super(PowerUpType.EXTRA_LIFE.getId(), PowerUpType.EXTRA_LIFE.getDurationInSeconds());
}
@Override
public void activate(LevelController levelController, Game game)
{
//TODO
if(game.getLivesRemaining() - 1 < levelController.MAX_LIVES)
{
game.setLivesRemaining(game.getLivesRemaining() + 1);
levelController.refreshLiveCounter();
}
}
@Override
......
......@@ -3,21 +3,28 @@ package de.bricked.game.powerups;
public enum PowerUpType
{
EXTRA_LIFE(1),
FASTER_BALL(2),
SLOWER_BALL(3),
EXPLODE_BALL(4),
NO_COLLISION_BALL(5);
EXTRA_LIFE(1, -1),
FASTER_BALL(2, 10),
SLOWER_BALL(3, 10),
EXPLODE_BALL(4, 10),
NO_COLLISION_BALL(5, 10);
private int id;
private int durationInSeconds;
PowerUpType(int id)
PowerUpType(int id, int durationInSeconds)
{
this.id = id;
this.durationInSeconds = durationInSeconds;
}
public int getId()
{
return id;
}
public int getDurationInSeconds()
{
return durationInSeconds;
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ public class ExplodeBallPowerUp extends PowerUp
{
public ExplodeBallPowerUp()
{
super(PowerUpType.valueOf("EXPLODE_BALL").getId(), -1);
super(PowerUpType.EXPLODE_BALL.getId(), PowerUpType.EXPLODE_BALL.getDurationInSeconds());
}
@Override
......@@ -23,5 +23,6 @@ public class ExplodeBallPowerUp extends PowerUp
@Override
public void deactivate(LevelController levelController, Game game)
{
levelController.changeBall(new Ball(BallType.NORMAL));
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ public class NoCollisionBallPowerUp extends PowerUp
{
public NoCollisionBallPowerUp()
{
super(PowerUpType.valueOf("NO_COLLISION_BALL").getId(), -1);
super(PowerUpType.NO_COLLISION_BALL.getId(), PowerUpType.NO_COLLISION_BALL.getDurationInSeconds());
}
@Override
......@@ -23,5 +23,6 @@ public class NoCollisionBallPowerUp extends PowerUp
@Override
public void deactivate(LevelController levelController, Game game)
{
levelController.changeBall(new Ball(BallType.NORMAL));
}
}
\ No newline at end of file
......@@ -81,7 +81,7 @@ public class LevelController
private AnimationTimer timer;
private double gamePaneWidth;
private double gamePaneHeight;
private final int MAX_LIVES = 7;
public final int MAX_LIVES = 7;
private final int TARGET_FPS = 60;
private final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
private final static double BRICK_FADE_DURATION = 300.0;
......@@ -133,8 +133,11 @@ public class LevelController
{
@Override
public void handle(MouseEvent event)
{
if(gameState.equals(GameState.WAITING))
{
startGame();
}
event.consume();
anchorPaneGame.requestFocus();
}
......@@ -499,8 +502,6 @@ public class LevelController
// move powerups
movePowerUps();
// check timed powerups
checkPowerUps();
}
};
}
......@@ -584,7 +585,7 @@ public class LevelController
}
}
private void refreshLiveCounter()
public void refreshLiveCounter()
{
vboxLives.getChildren().clear();
......@@ -789,7 +790,6 @@ public class LevelController
HitLocation hitLocation = game.collides(labelPosition, paddlePosition, paddle.getWidth(), paddle.getHeight(), true);
if(hitLocation != null && (hitLocation.equals(HitLocation.PADDLE) || hitLocation.equals(HitLocation.CORNER)))
{
// TODO activate method
Logger.log(LogLevel.DEBUG, "Collected PowerUp with ID = " + currentPowerUp.getID());
if(!currentPowerUp.isPermanent())
{
......@@ -810,12 +810,11 @@ public class LevelController
}
}
// TODO if there is already one item of this type?
private void addTimedPowerUp(PowerUp powerUp)
{
Label labelPowerUp = new Label(String.valueOf(powerUp.getDurationInSeconds()));
labelPowerUp.setStyle("-fx-background-image: url(\"de/bricked/resources/textures/powerups/" + powerUp.getID() + ".png\");" + "-fx-background-position: center center;" + "-fx-background-repeat: no-repeat;" + "-fx-background-size: contain;" + "-fx-font-size: 16;" + "-fx-font-weight: bold;"
+ "-fx-tect-fill: #cc0000;");
+ "-fx-text-fill: #000000;");
labelPowerUp.setAlignment(Pos.CENTER);
labelPowerUp.setUserData(powerUp);
......@@ -826,20 +825,7 @@ public class LevelController
timedPowerUps.add(labelPowerUp);
new CountdownTimer(powerUp.getDurationInSeconds(), labelPowerUp);
}
private void checkPowerUps()
{
for(Iterator<Label> iterator = timedPowerUps.iterator(); iterator.hasNext();)
{
Label currentLabel = iterator.next();
if(currentLabel.getText().equals("0"))
{
vboxPowerUps.getChildren().remove(currentLabel);
iterator.remove();
}
}
new CountdownTimer(powerUp.getDurationInSeconds(), labelPowerUp, this);
}
private void resetPowerUps()
......@@ -849,17 +835,26 @@ public class LevelController
vboxPowerUps.getChildren().clear();
}
public void changeBall(Ball newBall)
public void deactivatePowerUp(Label label)
{
double translateX = stackPaneBall.getTranslateX();
double translateY = stackPaneBall.getTranslateY();
Point2D direction = game.getBall().getDirection();
game.setBall(newBall);
int counter = 0;
PowerUp powerUp = (PowerUp)label.getUserData();
for(Label currentLabel : timedPowerUps)
{
PowerUp currentPowerUp = (PowerUp)currentLabel.getUserData();
if(currentPowerUp.getID() == powerUp.getID())
{
counter++;
}
}
initBall(game.getBall().getType());
stackPaneBall.setTranslateX(translateX);
stackPaneBall.setTranslateY(translateY);
game.getBall().setDirection(game.getNewSpeedDirection(direction, newBall.getType().getSpeedFactor()));
//if no other power ups of same kind are active
if(counter <= 1)
{
vboxPowerUps.getChildren().remove(label);
timedPowerUps.remove(label);
powerUp.deactivate(this, game);
}
}
public void showLabelFPS(boolean value)
......@@ -898,4 +893,20 @@ public class LevelController
anchorPaneGame.requestFocus();
}
/*
* PowerUP-Functions
*/
public void changeBall(Ball newBall)
{
double translateX = stackPaneBall.getTranslateX();
double translateY = stackPaneBall.getTranslateY();
Point2D direction = game.getBall().getDirection();
game.setBall(newBall);
initBall(game.getBall().getType());
stackPaneBall.setTranslateX(translateX);
stackPaneBall.setTranslateY(translateY);
game.getBall().setDirection(game.getNewSpeedDirection(direction, newBall.getType().getSpeedFactor()));
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package de.bricked.utils;
import java.util.Timer;
import java.util.TimerTask;
import de.bricked.ui.LevelController;
import javafx.application.Platform;
import javafx.scene.control.Label;
......@@ -10,7 +11,7 @@ public class CountdownTimer
{
private int count;
public CountdownTimer(int seconds, Label label)
public CountdownTimer(int seconds, Label label, LevelController levelController)
{
this.count = seconds;
......@@ -31,11 +32,18 @@ public class CountdownTimer
}
});
if(count > 0)
{
count--;
}
if(count == 0)
{
Platform.runLater(()->{
levelController.deactivatePowerUp(label);
});
timer.cancel();
}
}
};
timer.schedule(task, 0, 1000);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment