Added player stats UI

This commit is contained in:
Sagi Dayan 2016-03-19 02:01:49 +02:00
parent f6407115e6
commit 224c3b824c
4 changed files with 159 additions and 23 deletions

View file

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

View file

@ -1,15 +1,21 @@
package com.sagi.dayan.Games.Elements; package com.sagi.dayan.Games.Elements;
import javax.swing.*;
import java.awt.*;
/** /**
* Created by sagi on 2/20/16. * Created by sagi on 2/20/16.
*/ */
public class Player extends AnimatedSprite { public class Player extends AnimatedSprite {
private final int NORMAL_ANIMATION = 0, RIGHT_ANIMATION = 1, LEFT_ANIMATION = 2, PADDING_BOTTOM = 35; private final int NORMAL_ANIMATION = 0, RIGHT_ANIMATION = 1, LEFT_ANIMATION = 2, PADDING_BOTTOM = 35, MORTAL_DELAY = 3;
private int hDirection = 0, vDirection = 0; private int hDirection = 0, vDirection = 0;
private String imagePrefix; private String imagePrefix;
private boolean ableToFire; private boolean ableToFire, isMortal, toDraw;
private int fireDelay; private int fireDelay;
private long lastFired; private double imortalPulse = 0.2;
private long lastFired, lastDrawn, created;
public Player(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, String imagePrefix, int numOfFirstFrames) { public Player(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, String imagePrefix, int numOfFirstFrames) {
@ -17,9 +23,13 @@ public class Player extends AnimatedSprite {
this.imagePrefix = imagePrefix; this.imagePrefix = imagePrefix;
initFirstAnimation("", numOfFirstFrames); initFirstAnimation("", numOfFirstFrames);
this.ableToFire = true; this.ableToFire = true;
fireDelay = 100; fireDelay = 200;
lastFired = System.currentTimeMillis(); lastFired = System.currentTimeMillis();
lastDrawn = lastFired;
created = lastDrawn;
setImageDimensions(); setImageDimensions();
isMortal = false;
toDraw = true;
} }
@Override @Override
@ -32,6 +42,10 @@ public class Player extends AnimatedSprite {
animations.add(new Animation(imagePrefix+"LeftSheet.png", 7, 200)); animations.add(new Animation(imagePrefix+"LeftSheet.png", 7, 200));
} }
public boolean isMortal() {
return isMortal;
}
@Override @Override
public void update() { public void update() {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
@ -70,6 +84,7 @@ public class Player extends AnimatedSprite {
} }
} }
public boolean isAbleToFire() { public boolean isAbleToFire() {
return ableToFire; return ableToFire;
} }
@ -89,4 +104,23 @@ public class Player extends AnimatedSprite {
public void updateFireTime(){ public void updateFireTime(){
lastFired = System.currentTimeMillis(); lastFired = System.currentTimeMillis();
} }
@Override
public void drawSprite(Graphics g, JPanel p){
long now = System.currentTimeMillis();
if(isMortal){
super.drawSprite(g, p);
}else{
if(now - lastDrawn >= imortalPulse * 1000){
toDraw = !toDraw;
lastDrawn = now;
if(now-created >= MORTAL_DELAY * 1000){
isMortal = true;
}
}
if(toDraw) {
super.drawSprite(g, p);
}
}
}
} }

View file

@ -36,7 +36,7 @@ public class GameEngine {
private int[] p1Controlles = {KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_K}; private int[] p1Controlles = {KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_K};
private int[] p2Controlles = {KeyEvent.VK_W, KeyEvent.VK_D, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_SHIFT}; private int[] p2Controlles = {KeyEvent.VK_W, KeyEvent.VK_D, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_SHIFT};
private int p1Strikes, p2Strikes, p1CurrentStrikes, p2CurentStrikes; private int p1Lives, p2Lives, p1Health, p2Health, credits, p1Score, p2Score;
@ -68,9 +68,31 @@ public class GameEngine {
} }
this.waveConfigs = new WaveConfigs(); this.waveConfigs = new WaveConfigs();
startNewGame(); startNewGame();
resetPlayerHealth(0);
resetPlayerHealth(1);
credits = 3;
} }
private void resetPlayerHealth(int i){
if (i==0){
p1Health = 100;
}
else{
p2Health = 100;
}
}
private void resetPlayer(int i){
resetPlayerHealth(i);
if (i==0){
p1Lives = 3;
}
else{
p2Lives = 3;
}
}
/** /**
* initialize and reset vars and timers to "new game" configuration. * initialize and reset vars and timers to "new game" configuration.
@ -171,17 +193,49 @@ public class GameEngine {
return gameFont; return gameFont;
} }
public int getP1Strikes() { public int getP1Lives() {
return p1Strikes; return p1Lives;
} }
public int getP2Strikes() { public int getP2Lives() {
return p2Strikes; return p2Lives;
} }
public void setPlayerStrikes(int i, int strike) {
public int getP1Health() {
return p1Health;
}
public int getP2Health() {
return p2Health;
}
public int getP1Score() {
return p1Score;
}
public int getP2Score() {
return p2Score;
}
public int getCredits() {return credits;}
public void useCredit(){
credits--;
}
public void setScore(int i, int score)
{
if (i == 0) { if (i == 0) {
p1Strikes += strike; p1Score += score;
} else { } else {
p2Strikes += strike; p2Score += score;
}
}
public void setPlayerHealth(int i, int strike) {
if (i == 0) {
p1Health += strike;
} else {
p2Health += strike;
} }
} }

View file

@ -212,8 +212,9 @@ public abstract class Level extends Scene {
bg.drawSprite(g, p); bg.drawSprite(g, p);
Color c = g.getColor(); Color c = g.getColor();
if(!isStarted){
Font f = engine.getGameFont(); Font f = engine.getGameFont();
if(!isStarted){
if(f == null) { if(f == null) {
f = g.getFont(); f = g.getFont();
} }
@ -230,8 +231,37 @@ public abstract class Level extends Scene {
int y = ((stageHeight - metrics.getHeight()) / 2) - metrics.getAscent(); int y = ((stageHeight - metrics.getHeight()) / 2) - metrics.getAscent();
g.drawString(this.title, x, y); g.drawString(this.title, x, y);
g.setColor(c); g.setColor(c);
} }
//print score
f = f.deriveFont(15F);
g.setFont(f);
//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);
if (i==0 && engine.getP1Health()<=30)
g.setColor(Color.RED);
else if( i==1 && engine.getP2Health()<=30)
g.setColor(Color.RED);
g.fillRect(15,35*(i+1)+10,((i == 0) ? engine.getP1Health() : engine.getP2Health()),10);
}
//print credits
g.setColor(Color.WHITE);
g.drawString("Credits: "+ engine.getCredits(), stageWidth/2, 30);
for(int i = 0 ; i < p1Missiles.size() ; i++){ for(int i = 0 ; i < p1Missiles.size() ; i++){
p1Missiles.get(i).drawSprite(g,p); p1Missiles.get(i).drawSprite(g,p);
@ -256,45 +286,62 @@ public abstract class Level extends Scene {
eMTR = new Vector<>(); eMTR = new Vector<>();
p1MTR = new Vector<>(); p1MTR = new Vector<>();
p2MTR = new Vector<>(); p2MTR = new Vector<>();
//for each player check collisions
for (int i = 0; i < players.size(); i++) { for (int i = 0; i < players.size(); i++) {
//player vs. enemy missile
for (int j = 0; j < enemyMissiles.size(); j++) { for (int j = 0; j < enemyMissiles.size(); j++) {
if(CollisionUtil.collidesWith(players.get(i),enemyMissiles.get(j))){ if(CollisionUtil.collidesWith(players.get(i),enemyMissiles.get(j))){
//Remove players Life if(players.get(i).isMortal())
engine.setPlayerHealth(i, -10);
eMTR.add(enemyMissiles.get(j)); eMTR.add(enemyMissiles.get(j));
System.out.println("Hit Missile"); System.out.println("Hit Missile");
} }
} }
//player vs. enemy ship
for (int j = 0; j < waves.size(); j++) { for (int j = 0; j < waves.size(); j++) {
// Ship hits enemy // Ship hits enemy
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) { for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), players.get(i))) { if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), players.get(i))) {
engine.setPlayerStrikes(i, -1); if(players.get(i).isMortal())
engine.setPlayerHealth(i, -10);
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
System.out.println("PIN");
} }
} }
} }
//player 1 missile vs. enemy
if(i == 0){ if(i == 0){
for(int m = 0 ; m < p1Missiles.size() ; m++){ for(int m = 0 ; m < p1Missiles.size() ; m++){
for (int j = 0; j < waves.size(); j++) { for (int j = 0; j < waves.size(); j++) {
// Ship hits enemy
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) { for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), p1Missiles.get(m))) { if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), p1Missiles.get(m))) {
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); //remove enemy life
if (waves.get(j).getEnemies().get(k).isDead()) //if enemy is dead
engine.setScore(i,10);
p1MTR.add(p1Missiles.get(m)); p1MTR.add(p1Missiles.get(m));
} }
} }
} }
} }
}else{
}
//player 1 missile vs. enemy
else {
for(int m = 0 ; m < p2Missiles.size() ; m++){ for(int m = 0 ; m < p2Missiles.size() ; m++){
for (int j = 0; j < waves.size(); j++) { for (int j = 0; j < waves.size(); j++) {
// Ship hits enemy
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) { for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), p2Missiles.get(m))) { if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), p2Missiles.get(m))) {
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); //remove enemy life
if (waves.get(j).getEnemies().get(k).isDead()) //if enemy is dead
engine.setScore(i,10);
p2MTR.add(p2Missiles.get(m)); p2MTR.add(p2Missiles.get(m));
} }
} }
} }
@ -304,6 +351,7 @@ public abstract class Level extends Scene {
p1Missiles.removeAll(p1MTR); p1Missiles.removeAll(p1MTR);
p2Missiles.removeAll(p2MTR); p2Missiles.removeAll(p2MTR);
enemyMissiles.removeAll(eMTR); enemyMissiles.removeAll(eMTR);