Merge pull request #4 from devMatan/master

new features
This commit is contained in:
Sagi Dayan 2016-03-19 22:01:44 +02:00
commit 60d30f2ed2
8 changed files with 170 additions and 81 deletions

View file

@ -17,8 +17,6 @@ public abstract class AnimatedSprite extends Sprite {
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) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight);
animations = new Vector<>();
@ -48,6 +46,10 @@ public abstract class AnimatedSprite extends Sprite {
bImage = animations.get(currentAnimation).getCurrentFrame();
super.drawScreenLoopFix(g,p);
}
public int getFrameNum(){
return animations.get(currentAnimation).getFrameIndex();
}
public void setCurrentAnimation(int animation){
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 Missile(int x, int y, int acc, String imgName, int numOfFrames) {
super(x, y, 0, 0, acc, imgName, 0, 15, 15, numOfFrames);
public Missile(int x, int y, int w, int h, int acc, String imgName, int 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() {
if (this.getLocX() + this.sWidth < 0 || this.getLocX() - this.sWidth > pHeight || this.getLocY() + this.sHeight < 0 || this.getLocY() - sHeight > pHeight) {
return true;
if (this.getLocX() + sWidth < 0
|| this.getLocX() > pWidth
|| this.getLocY() +sHeight < 0
|| this.getLocY() >pHeight){
return true;
} else {
return false;
}

View file

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

View file

@ -16,6 +16,7 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
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.Player;
import com.sagi.dayan.Games.Elements.Wave;
@ -39,6 +40,7 @@ public abstract class Level extends Scene {
protected int startingAnimationIndex;
protected boolean isStarted;
protected Vector<Wave> waves;
protected Vector<Blast> blasts;
protected int numOfPlayers;
protected Map<Integer, Boolean> keys;
protected String title;
@ -53,6 +55,7 @@ public abstract class Level extends Scene {
players = new Vector<>();
p1Missiles = new Vector<>();
p2Missiles = new Vector<>();
blasts = new Vector<Blast>();
enemyMissiles = new Vector<>();
this.waveDelay = waveDelay;
this.lastWaveTime = System.currentTimeMillis();
@ -68,10 +71,13 @@ public abstract class Level extends Scene {
this.stageTitle = new JLabel(this.title);
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{
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*3, yAxisStartingAnimation[startingAnimationIndex], width, height, p1Speed, "emptyImage.png", 0, GameEngine.PLAYER_WIDTH, GameEngine.PLAYER_HEIGHT, "P2", 6));
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*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();
movePlayers();
Vector <Wave> wavesToRemove = new Vector<>();
Vector<Blast> blastTRM = new Vector<>();
long now = System.currentTimeMillis();
// if(currentWave < waveDelay.length && now - lastWaveTime >= waveDelay[currentWave] * 1000){
@ -163,7 +170,17 @@ public abstract class Level extends Scene {
System.out.println("Done");
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:
*/
if(keys.get(engine.getP1Controlles()[GameEngine.UP]) ){ //UP
players.get(0).sethDirection(1);
}
if(keys.get(engine.getP1Controlles()[GameEngine.DOWN])){ // DOWN
players.get(0).sethDirection(-1);
}
if(!keys.get(engine.getP1Controlles()[GameEngine.UP]) && !keys.get(engine.getP1Controlles()[GameEngine.DOWN])){ // Not up Or Down
players.get(0).sethDirection(0);
}
if(keys.get(engine.getP1Controlles()[GameEngine.LEFT])) { // Left
players.get(0).setvDirection(-1);
}
if(keys.get(engine.getP1Controlles()[GameEngine.RIGHT])) { // Right
players.get(0).setvDirection(1);
}
if(!keys.get(engine.getP1Controlles()[GameEngine.LEFT]) && !keys.get(engine.getP1Controlles()[GameEngine.RIGHT])){ // Not right or left
players.get(0).setvDirection(0);
}
if(keys.get(engine.getP1Controlles()[GameEngine.FIRE]) ){
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));
players.get(0).updateFireTime();
}
}
if(keys.get(engine.getP1Controlles()[GameEngine.USE_CREDIT]) && engine.getP1Health() <= 0 ){
if(engine.getCredits() > 0) {
engine.revivePlayer(0);
players.get(0).resetPlayer();
if(keys.get(engine.getP1Controlles()[GameEngine.UP]) ){ //UP
players.get(0).sethDirection(1);
}
if(keys.get(engine.getP1Controlles()[GameEngine.DOWN])){ // DOWN
players.get(0).sethDirection(-1);
}
if(!keys.get(engine.getP1Controlles()[GameEngine.UP]) && !keys.get(engine.getP1Controlles()[GameEngine.DOWN])){ // Not up Or Down
players.get(0).sethDirection(0);
}
if(keys.get(engine.getP1Controlles()[GameEngine.LEFT])) { // Left
players.get(0).setvDirection(-1);
}
if(keys.get(engine.getP1Controlles()[GameEngine.RIGHT])) { // Right
players.get(0).setvDirection(1);
}
if(!keys.get(engine.getP1Controlles()[GameEngine.LEFT]) && !keys.get(engine.getP1Controlles()[GameEngine.RIGHT])){ // Not right or left
players.get(0).setvDirection(0);
}
if(keys.get(engine.getP1Controlles()[GameEngine.FIRE]) ){
if(players.get(0).isAbleToFire() && !players.get(0).isGameOver()){
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();
}
if(engine.getP1Health() <= 0 && engine.getCredits() > 0) {
engine.revivePlayer(0);
players.get(0).resetPlayer();
}
}
}
}
/**
* Player 2 Movement
*/
if(numOfPlayers > 1){
if(keys.get(engine.getP2Controlles()[GameEngine.UP]) ){ //UP
players.get(1).sethDirection(1);
}
if(keys.get(engine.getP2Controlles()[GameEngine.DOWN])){ // DOWN
players.get(1).sethDirection(-1);
}
if(!keys.get(engine.getP2Controlles()[GameEngine.UP]) && !keys.get(engine.getP2Controlles()[GameEngine.DOWN])){ // Not up Or Down
players.get(1).sethDirection(0);
}
if(keys.get(engine.getP2Controlles()[GameEngine.LEFT])) { // Left
players.get(1).setvDirection(-1);
}
if(keys.get(engine.getP2Controlles()[GameEngine.RIGHT])) { // Right
players.get(1).setvDirection(1);
}
if(!keys.get(engine.getP2Controlles()[GameEngine.LEFT]) && !keys.get(engine.getP2Controlles()[GameEngine.RIGHT])){ // Not right or left
players.get(1).setvDirection(0);
}
if(keys.get(engine.getP2Controlles()[GameEngine.FIRE]) ){
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));
players.get(1).updateFireTime();
}
}
if(keys.get(engine.getP2Controlles()[GameEngine.USE_CREDIT]) && engine.getP2Health() <= 0){
if(engine.getCredits() > 0) {
engine.revivePlayer(1);
players.get(1).resetPlayer();
/**
* Player 2 Movement
*/
if(numOfPlayers > 1){
if(keys.get(engine.getP2Controlles()[GameEngine.UP]) ){ //UP
players.get(1).sethDirection(1);
}
if(keys.get(engine.getP2Controlles()[GameEngine.DOWN])){ // DOWN
players.get(1).sethDirection(-1);
}
if(!keys.get(engine.getP2Controlles()[GameEngine.UP]) && !keys.get(engine.getP2Controlles()[GameEngine.DOWN])){ // Not up Or Down
players.get(1).sethDirection(0);
}
if(keys.get(engine.getP2Controlles()[GameEngine.LEFT])) { // Left
players.get(1).setvDirection(-1);
}
if(keys.get(engine.getP2Controlles()[GameEngine.RIGHT])) { // Right
players.get(1).setvDirection(1);
}
if(!keys.get(engine.getP2Controlles()[GameEngine.LEFT]) && !keys.get(engine.getP2Controlles()[GameEngine.RIGHT])){ // Not right or left
players.get(1).setvDirection(0);
}
if(keys.get(engine.getP2Controlles()[GameEngine.FIRE]) ){
if(players.get(1).isAbleToFire() && !players.get(1).isGameOver()){
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();
}
if(engine.getP2Health() <= 0 && engine.getCredits() > 0) {
engine.revivePlayer(1);
players.get(0).resetPlayer();
}
}
}
}
}
}
}
@Override
@ -359,6 +372,10 @@ public abstract class Level extends Scene {
for(int i = 0 ; i < waves.size() ; i++){
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<>();
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 (int i = 0; i < players.size(); i++) {
@ -416,8 +452,10 @@ public abstract class Level extends Scene {
playerHit(i);
if(playerIsAlive(i)) {
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)) {
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) {
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

View file

@ -24,7 +24,6 @@ public class MainMenuScene extends Scene {
private MenuBoxSprite menuBox;
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 int[] axis = {370, 480, 590, 700};
@ -61,6 +60,16 @@ public class MainMenuScene extends Scene {
g.setFont(f);
g.drawString("Player1 record: " +engine.p1HighScore, 700, 50);
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));
//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
configs.add(new WaveConfig(new int[]{0,0,0,0,0,90, 90, 180} , 0.5, 8,-10 , 400));