Added Game Over

This commit is contained in:
Sagi Dayan 2016-03-19 03:40:07 +02:00
parent 224c3b824c
commit 38245db9db
5 changed files with 186 additions and 20 deletions

View file

@ -68,7 +68,7 @@ public class EnemyShip extends AnimatedSprite {
System.out.println("GOT HIT " + hitsToDestroy);
}
public boolean isDead() {
return hitsToDestroy == 0;
return hitsToDestroy <= 0;
}
public boolean isDone(){

View file

@ -14,6 +14,8 @@ public class Player extends AnimatedSprite {
private int fireDelay;
private double imortalPulse = 0.2;
private long lastFired, lastDrawn, created;
private int startX, startY;
private boolean isGameOver;
@ -30,6 +32,25 @@ public class Player extends AnimatedSprite {
setImageDimensions();
isMortal = false;
toDraw = true;
startX = locX;
startY = locY;
isGameOver = false;
}
public boolean isGameOver() {
return isGameOver;
}
public void setGameOver(boolean gameOver) {
isGameOver = gameOver;
}
public void resetPlayer(){
locX = startX;
locY = startY;
isMortal = false;
lastDrawn = System.currentTimeMillis();
created = lastDrawn;
}
@Override
@ -68,6 +89,10 @@ public class Player extends AnimatedSprite {
locY = pHeight - animations.get(currentAnimation).getCurrentFrame().getHeight() - PADDING_BOTTOM;
}
if(isGameOver){
//System.exit(1);
}
}

View file

@ -23,13 +23,14 @@ import java.util.Vector;
public class GameEngine {
private final int CREDIT_TIME = 10;
public boolean gameOn , gameOver, isFirstGame;
private JFrame frame;
private int pWidth, pHeight, numOfPlayers; //panel dimensions
private Random r;
private Stage stage;
private Vector<Scene> scenes;
private int currentScene;
private int currentScene, p1CreditTime, p2CreditTime, creditTickTime = 1;
public static final int PLAYER_WIDTH = 120, PLAYER_HEIGHT = 120;
public static final int UP=0,RIGHT=1,DOWN=2, LEFT=3, FIRE=4, SPECIAL=5;
@ -38,7 +39,7 @@ public class GameEngine {
private int p1Lives, p2Lives, p1Health, p2Health, credits, p1Score, p2Score;
private long lastP1CreditTick, lastP2CreditTick;
private Font gameFont;
@ -52,7 +53,6 @@ public class GameEngine {
this.pHeight = height;
this.scenes = new Vector<>();
this.stage = stage;
// scenes.add(new Level(width, height, 2)); // Need to be a menu Scene
scenes.add(new MainMenuScene(width, height, this));
stage.addKeyListener(scenes.get(currentScene));
stage.addMouseListener(scenes.get(currentScene));
@ -71,6 +71,8 @@ public class GameEngine {
resetPlayerHealth(0);
resetPlayerHealth(1);
credits = 3;
p1Lives = 1;
p2Lives = 1;
}
@ -94,6 +96,8 @@ public class GameEngine {
}
/**
* initialize and reset vars and timers to "new game" configuration.
*/
@ -106,9 +110,14 @@ public class GameEngine {
* Setup all actors in the game to a new game - reset timer
*/
private void initGame(){
}
public int getP1CreditTime() {
return p1CreditTime;
}
public int getP2CreditTime() {
return p2CreditTime;
}
public WaveConfigs getWaveConfigs() {
@ -135,6 +144,15 @@ public class GameEngine {
* Update all sprites, including collision handling.
*/
public void update(){
long now = System.currentTimeMillis();
if(now - lastP1CreditTick >= creditTickTime * 1000){
p1CreditTime--;
lastP1CreditTick = now;
}
if(now - lastP2CreditTick >= creditTickTime * 1000){
p2CreditTime--;
lastP2CreditTick = now;
}
scenes.get(currentScene).update();
}
@ -234,9 +252,34 @@ public class GameEngine {
public void setPlayerHealth(int i, int strike) {
if (i == 0) {
p1Health += strike;
if(p1Health <= 0){
p1Lives--;
if(p1Lives > 0)
resetPlayerHealth(i);
if(p1Lives <= 0){
p1CreditTime = 10;
lastP1CreditTick = System.currentTimeMillis();
}
}
} else {
p2Health += strike;
if(p2Health <= 0){
p2Lives--;
if(p2Lives > 0)
resetPlayerHealth(i);
if(p2Health <= 0){
p2CreditTime = 10;
lastP2CreditTick = System.currentTimeMillis();
}
}
}
}
public void setGameOver(boolean gameOver) {
if(gameOver){
changeScene(0);
this.gameOver = false;
}
}
}

View file

@ -24,14 +24,14 @@ public class FirstStage extends Level{
switch (currentWave){
case 0:
numOfEnemies = 5;
fireDelay = 0.2;
fireDelay = 0.1;
launchDelay = 0.5;
numOfHits = 1;
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);
break;
case 1:
numOfEnemies = 5;
fireDelay = 5;
fireDelay = 0.1;
launchDelay = 1;
numOfHits = 1;
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);

View file

@ -21,6 +21,8 @@ import java.util.*;
* Created by sagi on 2/20/16.
*/
public abstract class Level extends Scene {
protected final double PRESS_START_PULE = 0.3;
protected boolean toDrawStart;
protected Vector<Player> players;
protected int p1Speed = 10;
protected Vector<Missile> p1Missiles, p2Missiles, enemyMissiles;
@ -35,7 +37,7 @@ public abstract class Level extends Scene {
protected Map<Integer, Boolean> keys;
protected String title;
protected JLabel stageTitle;
protected long lastWaveTime;
protected long lastWaveTime, lastPulseTime;
@ -68,6 +70,8 @@ public abstract class Level extends Scene {
setupKeys();
Utils.playSound("jetSound.wav");
lastPulseTime = System.currentTimeMillis();
toDrawStart = true;
}
private void setupKeys() {
@ -87,7 +91,7 @@ public abstract class Level extends Scene {
public void update() {
bg.update();
movePlayers();
Vector <Wave> wavesToRemove = new Vector<Wave>();
Vector <Wave> wavesToRemove = new Vector<>();
long now = System.currentTimeMillis();
if(currentWave < waveDelay.length && now - lastWaveTime >= waveDelay[currentWave] * 1000){
@ -101,7 +105,6 @@ public abstract class Level extends Scene {
if(players.get(0).getLocY() > yAxisStartingAnimation[startingAnimationIndex] && startingAnimationIndex == 1){
for(int i = 0 ; i < players.size() ; i++){
players.get(i).setLocY((int)players.get(i).getLocY() - (p1Speed));
// players.get(i).update();
}
if(players.get(0).getLocY() <= yAxisStartingAnimation[startingAnimationIndex]){
startingAnimationIndex++;
@ -110,7 +113,6 @@ public abstract class Level extends Scene {
for(int i = 0 ; i < players.size() ; i++){
players.get(i).setLocY((int)players.get(i).getLocY() + (p1Speed - 5));
// players.get(i).update();
}
if(players.get(0).getLocY() >= yAxisStartingAnimation[startingAnimationIndex]){
startingAnimationIndex++;
@ -141,10 +143,19 @@ public abstract class Level extends Scene {
waves.removeAll(wavesToRemove);
}
checkCollision();
engine.setGameOver(isGameOver());
}
protected abstract void launchWave(long time);
private boolean isGameOver(){
if(numOfPlayers == 1) {
return players.get(0).isGameOver();
}else{
return players.get(0).isGameOver() && players.get(1).isGameOver();
}
}
private void movePlayers() {
/**
* Player 1 Movement:
@ -240,8 +251,6 @@ public abstract class Level extends Scene {
//print life bar
for(int i=0; i<players.size(); i++){
g.setColor(Color.WHITE);
g.drawString("Player "+ (i+1) +" - Lives: " + ((i == 0) ? engine.getP1Lives() : engine.getP2Lives())+ ", Score: " + ((i == 0) ? engine.getP1Score() : engine.getP2Score()), 15, 35*(i+1));
g.drawRect(15,35*(i+1)+10,100,10);
g.setColor(Color.GREEN);
@ -273,7 +282,31 @@ public abstract class Level extends Scene {
enemyMissiles.get(i).drawSprite(g,p);
}
for(int i = 0 ; i < players.size() ; i++){
players.get(i).drawSprite(g,p);
if(i == 0){
if(engine.getP1Health() > 0){
players.get(i).drawSprite(g,p);
g.setColor(Color.WHITE);
g.drawString("Player "+ (i+1) +" - Lives: " + ((i == 0) ? engine.getP1Lives() : engine.getP2Lives())+ ", Score: " + ((i == 0) ? engine.getP1Score() : engine.getP2Score()), 15, 35*(i+1));
}else{
if(players.get(i).isGameOver()){
renderGameOver(g, p, i);
}else{
renderPressStart(g, p, i);
}
}
}else{
if(engine.getP2Health() > 0){
players.get(i).drawSprite(g,p);
g.setColor(Color.WHITE);
g.drawString("Player "+ (i+1) +" - Lives: " + ((i == 0) ? engine.getP1Lives() : engine.getP2Lives())+ ", Score: " + ((i == 0) ? engine.getP1Score() : engine.getP2Score()), 15, 35*(i+1));
}else{
if(players.get(i).isGameOver()){
renderGameOver(g, p, i);
}else{
renderPressStart(g, p, i);
}
}
}
}
for(int i = 0 ; i < waves.size() ; i++){
waves.get(i).render(g,p);
@ -281,6 +314,44 @@ public abstract class Level extends Scene {
}
protected void renderPressStart(Graphics g, JPanel p, int i){
long now = System.currentTimeMillis();
if(now - lastPulseTime >= PRESS_START_PULE * 1000){
toDrawStart = !toDrawStart;
lastPulseTime = now;
}
if(i == 0){
g.drawString(engine.getP1CreditTime()+"", 15, 35 * (i + 1));
if(engine.getP1CreditTime() <= 0){
players.get(i).setGameOver(true);
}
}else{
g.drawString(engine.getP2CreditTime()+"", 15, 35 * (i + 1));
if(engine.getP2CreditTime() <= 0){
players.get(i).setGameOver(true);
}
}
if(toDrawStart) {
g.drawString("PRESS START", 45, 35 * (i + 1));
}
players.get(i).setLocY(-500);
players.get(i).setLocX(-500);
}
protected void renderGameOver(Graphics g, JPanel p, int i){
long now = System.currentTimeMillis();
players.get(i).setGameOver(true);
if(now - lastPulseTime >= PRESS_START_PULE * 1000){
toDrawStart = !toDrawStart;
lastPulseTime = now;
}
if(toDrawStart) {
g.drawString("P" + (i+1) + " GAME OVER", 15, 35 * (i + 1));
}
}
public void checkCollision() {
Vector<Missile> p1MTR, p2MTR, eMTR;
eMTR = new Vector<>();
@ -293,9 +364,10 @@ public abstract class Level extends Scene {
//player vs. enemy missile
for (int j = 0; j < enemyMissiles.size(); j++) {
if(CollisionUtil.collidesWith(players.get(i),enemyMissiles.get(j))){
if(players.get(i).isMortal())
engine.setPlayerHealth(i, -10);
eMTR.add(enemyMissiles.get(j));
playerHit(i);
if(playerIsAlive(i)) {
eMTR.add(enemyMissiles.get(j));
}
System.out.println("Hit Missile");
}
}
@ -305,9 +377,12 @@ public abstract class Level extends Scene {
// Ship hits enemy
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), players.get(i))) {
if(players.get(i).isMortal())
engine.setPlayerHealth(i, -10);
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
if(!waves.get(j).getEnemies().get(k).isDead()){
playerHit(i);
}
if(playerIsAlive(i)) {
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
}
}
}
}
@ -358,6 +433,29 @@ public abstract class Level extends Scene {
}
protected boolean playerIsAlive(int i){
if(i == 0){
return !(engine.getP1Lives() <= 0);
}else{
return !(engine.getP2Lives() <= 0);
}
}
protected void playerHit(int i){
if(players.get(i).isMortal()){
engine.setPlayerHealth(i, -10);
if(i == 0){
if(engine.getP1Health() == 100){
players.get(i).resetPlayer();
}
}else{
if(engine.getP2Health() == 100){
players.get(i).resetPlayer();
}
}
}
}
public void enemyFire(int x, int y, int acc) {
enemyMissiles.add(new Missile(x, y, acc,"E1-Fire.png", 15));
}