Added player stats UI
This commit is contained in:
parent
f6407115e6
commit
224c3b824c
4 changed files with 159 additions and 23 deletions
|
@ -67,8 +67,8 @@ public class EnemyShip extends AnimatedSprite {
|
|||
}
|
||||
System.out.println("GOT HIT " + hitsToDestroy);
|
||||
}
|
||||
public int getHitToDestroy() {
|
||||
return hitsToDestroy;
|
||||
public boolean isDead() {
|
||||
return hitsToDestroy == 0;
|
||||
}
|
||||
|
||||
public boolean isDone(){
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
package com.sagi.dayan.Games.Elements;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/20/16.
|
||||
*/
|
||||
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 String imagePrefix;
|
||||
private boolean ableToFire;
|
||||
private boolean ableToFire, isMortal, toDraw;
|
||||
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) {
|
||||
|
@ -17,9 +23,13 @@ public class Player extends AnimatedSprite {
|
|||
this.imagePrefix = imagePrefix;
|
||||
initFirstAnimation("", numOfFirstFrames);
|
||||
this.ableToFire = true;
|
||||
fireDelay = 100;
|
||||
fireDelay = 200;
|
||||
lastFired = System.currentTimeMillis();
|
||||
lastDrawn = lastFired;
|
||||
created = lastDrawn;
|
||||
setImageDimensions();
|
||||
isMortal = false;
|
||||
toDraw = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -32,6 +42,10 @@ public class Player extends AnimatedSprite {
|
|||
animations.add(new Animation(imagePrefix+"LeftSheet.png", 7, 200));
|
||||
}
|
||||
|
||||
public boolean isMortal() {
|
||||
return isMortal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
long now = System.currentTimeMillis();
|
||||
|
@ -70,6 +84,7 @@ public class Player extends AnimatedSprite {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isAbleToFire() {
|
||||
return ableToFire;
|
||||
}
|
||||
|
@ -89,4 +104,23 @@ public class Player extends AnimatedSprite {
|
|||
public void updateFireTime(){
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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[] 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();
|
||||
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.
|
||||
|
@ -171,17 +193,49 @@ public class GameEngine {
|
|||
return gameFont;
|
||||
}
|
||||
|
||||
public int getP1Strikes() {
|
||||
return p1Strikes;
|
||||
public int getP1Lives() {
|
||||
return p1Lives;
|
||||
}
|
||||
public int getP2Strikes() {
|
||||
return p2Strikes;
|
||||
public int getP2Lives() {
|
||||
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) {
|
||||
p1Strikes += strike;
|
||||
p1Score += score;
|
||||
} else {
|
||||
p2Strikes += strike;
|
||||
p2Score += score;
|
||||
}
|
||||
}
|
||||
public void setPlayerHealth(int i, int strike) {
|
||||
if (i == 0) {
|
||||
p1Health += strike;
|
||||
} else {
|
||||
p2Health += strike;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -212,8 +212,9 @@ public abstract class Level extends Scene {
|
|||
|
||||
bg.drawSprite(g, p);
|
||||
Color c = g.getColor();
|
||||
Font f = engine.getGameFont();
|
||||
|
||||
if(!isStarted){
|
||||
Font f = engine.getGameFont();
|
||||
if(f == null) {
|
||||
f = g.getFont();
|
||||
}
|
||||
|
@ -230,8 +231,37 @@ public abstract class Level extends Scene {
|
|||
int y = ((stageHeight - metrics.getHeight()) / 2) - metrics.getAscent();
|
||||
g.drawString(this.title, x, y);
|
||||
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++){
|
||||
p1Missiles.get(i).drawSprite(g,p);
|
||||
|
@ -256,45 +286,62 @@ public abstract class Level extends Scene {
|
|||
eMTR = new Vector<>();
|
||||
p1MTR = new Vector<>();
|
||||
p2MTR = new Vector<>();
|
||||
|
||||
//for each player check collisions
|
||||
for (int i = 0; i < players.size(); i++) {
|
||||
|
||||
//player vs. enemy missile
|
||||
for (int j = 0; j < enemyMissiles.size(); 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));
|
||||
System.out.println("Hit Missile");
|
||||
}
|
||||
}
|
||||
|
||||
//player vs. enemy ship
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
// 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))) {
|
||||
engine.setPlayerStrikes(i, -1);
|
||||
if(players.get(i).isMortal())
|
||||
engine.setPlayerHealth(i, -10);
|
||||
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
|
||||
System.out.println("PIN");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//player 1 missile vs. enemy
|
||||
if(i == 0){
|
||||
for(int m = 0 ; m < p1Missiles.size() ; m++){
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
// Ship hits enemy
|
||||
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
|
||||
}
|
||||
|
||||
//player 1 missile vs. enemy
|
||||
else {
|
||||
for(int m = 0 ; m < p2Missiles.size() ; m++){
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
// Ship hits enemy
|
||||
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
|
||||
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));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -304,6 +351,7 @@ public abstract class Level extends Scene {
|
|||
|
||||
|
||||
|
||||
|
||||
p1Missiles.removeAll(p1MTR);
|
||||
p2Missiles.removeAll(p2MTR);
|
||||
enemyMissiles.removeAll(eMTR);
|
||||
|
|
Loading…
Reference in a new issue