new features

- blasts added ship vs enemy ship
- blasts remove, missiles remove
- audio loop
This commit is contained in:
Matan 2016-03-19 22:00:13 +02:00
parent 21ae715c69
commit 4f73503f89
8 changed files with 170 additions and 81 deletions

View file

@ -17,8 +17,6 @@ public abstract class AnimatedSprite extends Sprite {
protected int currentAnimation; protected int currentAnimation;
public AnimatedSprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, int numOfFirstFrames) { public AnimatedSprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, int numOfFirstFrames) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight); super(x, y, w, h, acc, imgName, angle, sWidth, sHeight);
animations = new Vector<>(); animations = new Vector<>();
@ -48,6 +46,10 @@ public abstract class AnimatedSprite extends Sprite {
bImage = animations.get(currentAnimation).getCurrentFrame(); bImage = animations.get(currentAnimation).getCurrentFrame();
super.drawScreenLoopFix(g,p); super.drawScreenLoopFix(g,p);
} }
public int getFrameNum(){
return animations.get(currentAnimation).getFrameIndex();
}
public void setCurrentAnimation(int animation){ public void setCurrentAnimation(int animation){
if(animation < 0){ if(animation < 0){

View file

@ -0,0 +1,33 @@
package com.sagi.dayan.Games.Elements;
import com.sagi.dayan.Games.Elements.AnimatedSprite.Animation;
public class Blast extends AnimatedSprite {
protected boolean isDone;
protected int numOfFirstFrames;
public Blast(int x, int y, String imgName, int numOfFirstFrames) {
super(x, y, 0, 0, 0, imgName, 0, 15, 15, numOfFirstFrames);
// TODO Auto-generated constructor stub
isDone = false;
this.numOfFirstFrames = numOfFirstFrames;
}
@Override
protected void initFirstAnimation(String spriteSheet, int numOfFirstFrames) {
animations.add(new Animation("explosion.png", 16, 500));
}
@Override
public void update() {
System.out.println("curr: "+currentAnimation+", total: "+numOfFirstFrames);
if (getFrameNum() == numOfFirstFrames-1)
isDone=true;
// TODO Auto-generated method stub
}
public boolean isDone(){
return isDone;
}
}

View file

@ -6,8 +6,8 @@ package com.sagi.dayan.Games.Elements;
public class Missile extends AnimatedSprite { public class Missile extends AnimatedSprite {
public Missile(int x, int y, int acc, String imgName, int numOfFrames) { public Missile(int x, int y, int w, int h, int acc, String imgName, int numOfFrames) {
super(x, y, 0, 0, acc, imgName, 0, 15, 15, numOfFrames); super(x, y, w, h, acc, imgName, 0, 15, 15, numOfFrames);
} }

View file

@ -264,8 +264,11 @@ public abstract class Sprite {
} }
public boolean isOutOfScreen() { public boolean isOutOfScreen() {
if (this.getLocX() + this.sWidth < 0 || this.getLocX() - this.sWidth > pHeight || this.getLocY() + this.sHeight < 0 || this.getLocY() - sHeight > pHeight) { if (this.getLocX() + sWidth < 0
return true; || this.getLocX() > pWidth
|| this.getLocY() +sHeight < 0
|| this.getLocY() >pHeight){
return true;
} else { } else {
return false; return false;
} }

View file

@ -34,11 +34,11 @@ public class GameEngine {
private Scene scene; private Scene scene;
private int p1CreditTime, p2CreditTime, creditTickTime = 1; private int p1CreditTime, p2CreditTime, creditTickTime = 1;
public static final int PLAYER_WIDTH = 120, PLAYER_HEIGHT = 120; public static final int PLAYER_WIDTH = 120, PLAYER_HEIGHT = 120;
public static final int UP=0,RIGHT=1,DOWN=2, LEFT=3, FIRE=4, USE_CREDIT=5; public static final int UP=0,RIGHT=1,DOWN=2, LEFT=3, FIRE=4, SPECIAL=5;
public int p1HighScore, p2HighScore; public int p1HighScore, p2HighScore;
private int[] p1Controlles = {KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_K, KeyEvent.VK_J}; 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_Q, KeyEvent.VK_Z}; private int[] p2Controlles = {KeyEvent.VK_W, KeyEvent.VK_D, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_Q};
private int p1Lives, p2Lives, p1Health, p2Health, credits, p1Score, p2Score; private int p1Lives, p2Lives, p1Health, p2Health, credits, p1Score, p2Score;
@ -102,6 +102,7 @@ public class GameEngine {
private void initGame(){ private void initGame(){
resetPlayerHealth(0); resetPlayerHealth(0);
resetPlayerHealth(1); resetPlayerHealth(1);
p1Score = p2Score = 0;
credits = 3; credits = 3;
p1Lives = 1; p1Lives = 1;
p2Lives = 1; p2Lives = 1;

View file

@ -16,6 +16,7 @@ import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import com.sagi.dayan.Games.Elements.Background; import com.sagi.dayan.Games.Elements.Background;
import com.sagi.dayan.Games.Elements.Blast;
import com.sagi.dayan.Games.Elements.Missile; import com.sagi.dayan.Games.Elements.Missile;
import com.sagi.dayan.Games.Elements.Player; import com.sagi.dayan.Games.Elements.Player;
import com.sagi.dayan.Games.Elements.Wave; import com.sagi.dayan.Games.Elements.Wave;
@ -39,6 +40,7 @@ public abstract class Level extends Scene {
protected int startingAnimationIndex; protected int startingAnimationIndex;
protected boolean isStarted; protected boolean isStarted;
protected Vector<Wave> waves; protected Vector<Wave> waves;
protected Vector<Blast> blasts;
protected int numOfPlayers; protected int numOfPlayers;
protected Map<Integer, Boolean> keys; protected Map<Integer, Boolean> keys;
protected String title; protected String title;
@ -53,6 +55,7 @@ public abstract class Level extends Scene {
players = new Vector<>(); players = new Vector<>();
p1Missiles = new Vector<>(); p1Missiles = new Vector<>();
p2Missiles = new Vector<>(); p2Missiles = new Vector<>();
blasts = new Vector<Blast>();
enemyMissiles = new Vector<>(); enemyMissiles = new Vector<>();
this.waveDelay = waveDelay; this.waveDelay = waveDelay;
this.lastWaveTime = System.currentTimeMillis(); this.lastWaveTime = System.currentTimeMillis();
@ -68,10 +71,13 @@ public abstract class Level extends Scene {
this.stageTitle = new JLabel(this.title); this.stageTitle = new JLabel(this.title);
if(numOfPlayers == 1) { if(numOfPlayers == 1) {
players.add(new Player((width / 2) + (GameEngine.PLAYER_WIDTH / 2), yAxisStartingAnimation[startingAnimationIndex], width, height, p1Speed, "emptyImage.png", 0, GameEngine.PLAYER_WIDTH, GameEngine.PLAYER_HEIGHT, "P1",6)); players.add(new Player((width / 2) + (GameEngine.PLAYER_WIDTH / 2), yAxisStartingAnimation[startingAnimationIndex],
width, height, p1Speed, "emptyImage.png", 0, GameEngine.PLAYER_WIDTH, GameEngine.PLAYER_HEIGHT, "P1",6));
}else{ }else{
players.add(new Player((width / 2) + (GameEngine.PLAYER_WIDTH / 2) + GameEngine.PLAYER_WIDTH, yAxisStartingAnimation[startingAnimationIndex], width, height, p1Speed, "emptyImage.png", 0, GameEngine.PLAYER_WIDTH, GameEngine.PLAYER_HEIGHT, "P1", 6)); players.add(new Player((width / 2) + (GameEngine.PLAYER_WIDTH / 2) + GameEngine.PLAYER_WIDTH, yAxisStartingAnimation[startingAnimationIndex],
players.add(new Player((width / 2) + (GameEngine.PLAYER_WIDTH / 2) - GameEngine.PLAYER_WIDTH*3, yAxisStartingAnimation[startingAnimationIndex], width, height, p1Speed, "emptyImage.png", 0, GameEngine.PLAYER_WIDTH, GameEngine.PLAYER_HEIGHT, "P2", 6)); width, height, p1Speed, "emptyImage.png", 0, GameEngine.PLAYER_WIDTH, GameEngine.PLAYER_HEIGHT, "P1", 6));
players.add(new Player((width / 2) + (GameEngine.PLAYER_WIDTH / 2) - GameEngine.PLAYER_WIDTH*3, yAxisStartingAnimation[startingAnimationIndex],
width, height, p1Speed, "emptyImage.png", 0, GameEngine.PLAYER_WIDTH, GameEngine.PLAYER_HEIGHT, "P2", 6));
} }
@ -99,6 +105,7 @@ public abstract class Level extends Scene {
bg.update(); bg.update();
movePlayers(); movePlayers();
Vector <Wave> wavesToRemove = new Vector<>(); Vector <Wave> wavesToRemove = new Vector<>();
Vector<Blast> blastTRM = new Vector<>();
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
// if(currentWave < waveDelay.length && now - lastWaveTime >= waveDelay[currentWave] * 1000){ // if(currentWave < waveDelay.length && now - lastWaveTime >= waveDelay[currentWave] * 1000){
@ -163,7 +170,17 @@ public abstract class Level extends Scene {
System.out.println("Done"); System.out.println("Done");
engine.changeLevel(); engine.changeLevel();
} }
for(int i =0; i<blasts.size();i++){
if (blasts.get(i).isDone()){
System.out.println("removing blast");
blastTRM.add(blasts.get(i));
}
blasts.get(i).update();
}
blasts.removeAll(blastTRM);
} }
@ -181,75 +198,71 @@ public abstract class Level extends Scene {
/** /**
* Player 1 Movement: * Player 1 Movement:
*/ */
if(keys.get(engine.getP1Controlles()[GameEngine.UP]) ){ //UP if(keys.get(engine.getP1Controlles()[GameEngine.UP]) ){ //UP
players.get(0).sethDirection(1); players.get(0).sethDirection(1);
} }
if(keys.get(engine.getP1Controlles()[GameEngine.DOWN])){ // DOWN if(keys.get(engine.getP1Controlles()[GameEngine.DOWN])){ // DOWN
players.get(0).sethDirection(-1); players.get(0).sethDirection(-1);
} }
if(!keys.get(engine.getP1Controlles()[GameEngine.UP]) && !keys.get(engine.getP1Controlles()[GameEngine.DOWN])){ // Not up Or Down if(!keys.get(engine.getP1Controlles()[GameEngine.UP]) && !keys.get(engine.getP1Controlles()[GameEngine.DOWN])){ // Not up Or Down
players.get(0).sethDirection(0); players.get(0).sethDirection(0);
} }
if(keys.get(engine.getP1Controlles()[GameEngine.LEFT])) { // Left if(keys.get(engine.getP1Controlles()[GameEngine.LEFT])) { // Left
players.get(0).setvDirection(-1); players.get(0).setvDirection(-1);
} }
if(keys.get(engine.getP1Controlles()[GameEngine.RIGHT])) { // Right if(keys.get(engine.getP1Controlles()[GameEngine.RIGHT])) { // Right
players.get(0).setvDirection(1); players.get(0).setvDirection(1);
} }
if(!keys.get(engine.getP1Controlles()[GameEngine.LEFT]) && !keys.get(engine.getP1Controlles()[GameEngine.RIGHT])){ // Not right or left if(!keys.get(engine.getP1Controlles()[GameEngine.LEFT]) && !keys.get(engine.getP1Controlles()[GameEngine.RIGHT])){ // Not right or left
players.get(0).setvDirection(0); players.get(0).setvDirection(0);
} }
if(keys.get(engine.getP1Controlles()[GameEngine.FIRE]) ){ if(keys.get(engine.getP1Controlles()[GameEngine.FIRE]) ){
if(players.get(0).isAbleToFire() && !players.get(0).isGameOver()){ if(players.get(0).isAbleToFire() && !players.get(0).isGameOver()){
p1Missiles.add(new Missile(players.get(0).getCenterX() - 15, (int)players.get(0).getLocY(), players.get(0).getAcceleration() + 3, "P1Laser.png", 4)); p1Missiles.add(new Missile(players.get(0).getCenterX() - 15, (int)players.get(0).getLocY(),getStageWidth(),getStageHeight(), players.get(0).getAcceleration() + 3, "P1Laser.png", 4));
players.get(0).updateFireTime(); players.get(0).updateFireTime();
} }
} if(engine.getP1Health() <= 0 && engine.getCredits() > 0) {
if(keys.get(engine.getP1Controlles()[GameEngine.USE_CREDIT]) && engine.getP1Health() <= 0 ){ engine.revivePlayer(0);
if(engine.getCredits() > 0) { players.get(0).resetPlayer();
engine.revivePlayer(0);
players.get(0).resetPlayer();
} }
} }
/** /**
* Player 2 Movement * Player 2 Movement
*/ */
if(numOfPlayers > 1){ if(numOfPlayers > 1){
if(keys.get(engine.getP2Controlles()[GameEngine.UP]) ){ //UP if(keys.get(engine.getP2Controlles()[GameEngine.UP]) ){ //UP
players.get(1).sethDirection(1); players.get(1).sethDirection(1);
} }
if(keys.get(engine.getP2Controlles()[GameEngine.DOWN])){ // DOWN if(keys.get(engine.getP2Controlles()[GameEngine.DOWN])){ // DOWN
players.get(1).sethDirection(-1); players.get(1).sethDirection(-1);
} }
if(!keys.get(engine.getP2Controlles()[GameEngine.UP]) && !keys.get(engine.getP2Controlles()[GameEngine.DOWN])){ // Not up Or Down if(!keys.get(engine.getP2Controlles()[GameEngine.UP]) && !keys.get(engine.getP2Controlles()[GameEngine.DOWN])){ // Not up Or Down
players.get(1).sethDirection(0); players.get(1).sethDirection(0);
} }
if(keys.get(engine.getP2Controlles()[GameEngine.LEFT])) { // Left if(keys.get(engine.getP2Controlles()[GameEngine.LEFT])) { // Left
players.get(1).setvDirection(-1); players.get(1).setvDirection(-1);
} }
if(keys.get(engine.getP2Controlles()[GameEngine.RIGHT])) { // Right if(keys.get(engine.getP2Controlles()[GameEngine.RIGHT])) { // Right
players.get(1).setvDirection(1); players.get(1).setvDirection(1);
} }
if(!keys.get(engine.getP2Controlles()[GameEngine.LEFT]) && !keys.get(engine.getP2Controlles()[GameEngine.RIGHT])){ // Not right or left if(!keys.get(engine.getP2Controlles()[GameEngine.LEFT]) && !keys.get(engine.getP2Controlles()[GameEngine.RIGHT])){ // Not right or left
players.get(1).setvDirection(0); players.get(1).setvDirection(0);
} }
if(keys.get(engine.getP2Controlles()[GameEngine.FIRE]) ){ if(keys.get(engine.getP2Controlles()[GameEngine.FIRE]) ){
if(players.get(1).isAbleToFire() && !players.get(1).isGameOver()){ if(players.get(1).isAbleToFire() && !players.get(1).isGameOver()){
p2Missiles.add(new Missile(players.get(1).getCenterX() - 15, (int)players.get(1).getLocY(), players.get(1).getAcceleration() + 3, "P1Laser.png", 4)); p2Missiles.add(new Missile(players.get(1).getCenterX() - 15, (int)players.get(1).getLocY(),getStageWidth(),getStageHeight(),players.get(1).getAcceleration() + 3, "P1Laser.png", 4));
players.get(1).updateFireTime(); players.get(1).updateFireTime();
} }
} if(engine.getP2Health() <= 0 && engine.getCredits() > 0) {
if(keys.get(engine.getP2Controlles()[GameEngine.USE_CREDIT]) && engine.getP2Health() <= 0){ engine.revivePlayer(1);
if(engine.getCredits() > 0) { players.get(0).resetPlayer();
engine.revivePlayer(1);
players.get(1).resetPlayer();
} }
} }
} }
} }
@Override @Override
@ -359,6 +372,10 @@ public abstract class Level extends Scene {
for(int i = 0 ; i < waves.size() ; i++){ for(int i = 0 ; i < waves.size() ; i++){
waves.get(i).render(g,p); waves.get(i).render(g,p);
} }
for(int i =0; i<blasts.size();i++){
blasts.get(i).drawSprite(g, p);
}
} }
@ -407,6 +424,25 @@ public abstract class Level extends Scene {
p1MTR = new Vector<>(); p1MTR = new Vector<>();
p2MTR = new Vector<>(); p2MTR = new Vector<>();
// remove missiles - out of screen
for( int i = 0; i < enemyMissiles.size(); i++) {
if (enemyMissiles.get(i).isOutOfScreen()) {
eMTR.add(enemyMissiles.get(i));
}
}
for( int i = 0; i < p1Missiles.size(); i++) {
if (p1Missiles.get(i).isOutOfScreen()) {
p1MTR.add(p1Missiles.get(i));
}
}
if (players.size() > 1) {
for( int i = 0; i < p2Missiles.size(); i++) {
if (p2Missiles.get(i).isOutOfScreen()) {
p2MTR.add(p2Missiles.get(i));
}
}
}
//for each player check collisions //for each player check collisions
for (int i = 0; i < players.size(); i++) { for (int i = 0; i < players.size(); i++) {
@ -416,8 +452,10 @@ public abstract class Level extends Scene {
playerHit(i); playerHit(i);
if(playerIsAlive(i)) { if(playerIsAlive(i)) {
eMTR.add(enemyMissiles.get(j)); eMTR.add(enemyMissiles.get(j));
}else{
blasts.add(new Blast((int)players.get(i).getLocX(),(int)players.get(i).getLocY(),"explosion.png",15));
} }
System.out.println("Hit Missile");
} }
} }
@ -431,6 +469,9 @@ public abstract class Level extends Scene {
} }
if(playerIsAlive(i)) { if(playerIsAlive(i)) {
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
}else{
blasts.add(new Blast((int)players.get(i).getLocX(),(int)players.get(i).getLocY(),"explosion.png",15));
} }
} }
} }
@ -506,7 +547,7 @@ public abstract class Level extends Scene {
} }
public void enemyFire(int x, int y, int acc) { public void enemyFire(int x, int y, int acc) {
enemyMissiles.add(new Missile(x, y, acc,"E1-Fire.png", 15)); enemyMissiles.add(new Missile(x, y,getStageWidth(),getStageHeight(), acc,"E1-Fire.png", 15));
} }
@Override @Override

View file

@ -24,7 +24,6 @@ public class MainMenuScene extends Scene {
private MenuBoxSprite menuBox; private MenuBoxSprite menuBox;
private int menuItem; private int menuItem;
private final int MAX_ITEM_INDEX = 3, X_AXIS=560, ITEM_EXIT = 3, ITEM_SETTINGS = 2, ITEM_2P = 1, ITEM_1P = 0; private final int MAX_ITEM_INDEX = 3, X_AXIS=560, ITEM_EXIT = 3, ITEM_SETTINGS = 2, ITEM_2P = 1, ITEM_1P = 0;
private int[] axis = {370, 480, 590, 700}; private int[] axis = {370, 480, 590, 700};
@ -61,6 +60,16 @@ public class MainMenuScene extends Scene {
g.setFont(f); g.setFont(f);
g.drawString("Player1 record: " +engine.p1HighScore, 700, 50); g.drawString("Player1 record: " +engine.p1HighScore, 700, 50);
g.drawString("Player2 record: " +engine.p2HighScore, 700, 100); g.drawString("Player2 record: " +engine.p2HighScore, 700, 100);
try {
if(!AudioPlayer.player.isAlive()){
System.out.println("Start again");
AudioPlayer.player.start(new AudioStream(Utils.getSoundResourceAsStream("intro_LowQuality.wav")));
}
}catch (IOException e) {
e.printStackTrace();
}

View file

@ -33,7 +33,7 @@ public class WaveConfigs {
configs.add(new WaveConfig(new int[]{270,270,240, 240, 210, 210, 90} , 0.5, 8,900 , 1000)); configs.add(new WaveConfig(new int[]{270,270,240, 240, 210, 210, 90} , 0.5, 8,900 , 1000));
//middle right to middle right //middle right to middle right
configs.add(new WaveConfig(new int[]{180,180,180,180,180,90, 90, 0} , 0.5, 8,1010 , 400)); configs.add(new WaveConfig(new int[]{180,180,180,180,180,90, 90, 0} , 0.5, 8,999 , 400));
//middle left to middle left //middle left to middle left
configs.add(new WaveConfig(new int[]{0,0,0,0,0,90, 90, 180} , 0.5, 8,-10 , 400)); configs.add(new WaveConfig(new int[]{0,0,0,0,0,90, 90, 180} , 0.5, 8,-10 , 400));