wave & stage abstraction

also cleaned some code.
This commit is contained in:
Sagi Dayan 2016-03-19 00:33:29 +02:00
parent d5ca3d4dd9
commit f6407115e6
8 changed files with 143 additions and 11 deletions

View file

@ -10,14 +10,15 @@ import java.awt.event.ActionListener;
* Created by sagi on 2/20/16. * Created by sagi on 2/20/16.
*/ */
public class EnemyShip extends AnimatedSprite { public class EnemyShip extends AnimatedSprite {
protected int fireDelay, stepDelay, currentStep; protected int currentStep;
protected double stepDelay, fireDelay;
protected long lastFireTime, lastStepTime; protected long lastFireTime, lastStepTime;
protected int[] moveVector; protected int[] moveVector;
protected Wave wave; protected Wave wave;
protected int hitsToDestroy; protected int hitsToDestroy;
protected boolean isDone; protected boolean isDone;
protected long startExploded; protected long startExploded;
public EnemyShip(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight,int fireDelay, int stepDelay, Wave wave, int[] moveVector,int numOfFirstFrames, int hitsToDestroy) { public EnemyShip(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight,double fireDelay, double stepDelay, Wave wave, int[] moveVector,int numOfFirstFrames, int hitsToDestroy) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight, numOfFirstFrames); super(x, y, w, h, acc, imgName, angle, sWidth, sHeight, numOfFirstFrames);
this.fireDelay = fireDelay; this.fireDelay = fireDelay;
this.stepDelay = stepDelay; this.stepDelay = stepDelay;

View file

@ -13,7 +13,8 @@ import java.util.Vector;
public class Wave { public class Wave {
protected Level level; protected Level level;
protected int enemyMaxAmount, currentAmount,fireDelay, launchDelay, stepDelay, acc, startX, startY; protected int enemyMaxAmount, currentAmount, acc, startX, startY;
protected double stepDelay,fireDelay, launchDelay;
protected int[] moveVector; protected int[] moveVector;
protected Vector<EnemyShip> enemies; protected Vector<EnemyShip> enemies;
protected Vector<Missile> bullets; protected Vector<Missile> bullets;
@ -21,7 +22,7 @@ public class Wave {
protected String imageName; protected String imageName;
protected int hitsToDestroy; protected int hitsToDestroy;
public Wave(int enemyMaxAmount, int[] moveVector, int fireDelay, int stepDelay, int launchDelay, int acc, String imageName, int startX, int startY, Level stage, int hitsToDestroy){ public Wave(int enemyMaxAmount, int[] moveVector, double fireDelay, double stepDelay, double launchDelay, int acc, String imageName, int startX, int startY, Level stage, int hitsToDestroy){
this.enemies = new Vector<>(); this.enemies = new Vector<>();
this.bullets = new Vector<>(); this.bullets = new Vector<>();
this.enemyMaxAmount = enemyMaxAmount; this.enemyMaxAmount = enemyMaxAmount;

View file

@ -6,6 +6,7 @@ package com.sagi.dayan.Games.Engine;
import com.sagi.dayan.Games.Stage.*; import com.sagi.dayan.Games.Stage.*;
import com.sagi.dayan.Games.Utils.Utils; import com.sagi.dayan.Games.Utils.Utils;
import com.sagi.dayan.Games.Utils.WaveConfigs;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -37,8 +38,12 @@ public class GameEngine {
private int p1Strikes, p2Strikes, p1CurrentStrikes, p2CurentStrikes; private int p1Strikes, p2Strikes, p1CurrentStrikes, p2CurentStrikes;
private Font gameFont; private Font gameFont;
private WaveConfigs waveConfigs;
public GameEngine(int width, int height, Stage stage){ public GameEngine(int width, int height, Stage stage){
this.currentScene = 0; this.currentScene = 0;
this.isFirstGame = true; this.isFirstGame = true;
@ -61,6 +66,7 @@ public class GameEngine {
e.printStackTrace(); e.printStackTrace();
gameFont = null; gameFont = null;
} }
this.waveConfigs = new WaveConfigs();
startNewGame(); startNewGame();
} }
@ -83,6 +89,9 @@ public class GameEngine {
} }
public WaveConfigs getWaveConfigs() {
return waveConfigs;
}
@ -128,7 +137,7 @@ public class GameEngine {
public void startGame(int numOfPlayers){ public void startGame(int numOfPlayers){
this.numOfPlayers = numOfPlayers; this.numOfPlayers = numOfPlayers;
scenes.add(new Level(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-", new int[]{5, 20})); scenes.add(new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-", new int[]{5, 20}));
changeScene(currentScene+1); changeScene(currentScene+1);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

@ -0,0 +1,46 @@
package com.sagi.dayan.Games.Stage;
import com.sagi.dayan.Games.Elements.Wave;
import com.sagi.dayan.Games.Engine.GameEngine;
import com.sagi.dayan.Games.Utils.WaveConfig;
import com.sagi.dayan.Games.Utils.WaveConfigs;
/**
* Created by sagi on 3/19/16.
*/
public class FirstStage extends Level{
public FirstStage(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay) {
super(width, height, numOfPlayers, engine, stageTitle, waveDelay);
}
@Override
protected void launchWave(long now) {
lastWaveTime = now;
System.out.println("New Wave!! Time: " + now);
WaveConfig wc;
int numOfEnemies = 5, numOfHits = 1;
double launchDelay = 0.5, fireDelay = 5;
switch (currentWave){
case 0:
numOfEnemies = 5;
fireDelay = 0.2;
launchDelay = 0.5;
numOfHits = 1;
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);
break;
case 1:
numOfEnemies = 5;
fireDelay = 5;
launchDelay = 1;
numOfHits = 1;
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);
break;
default:
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);
break;
}
waves.add(new Wave(numOfEnemies, wc.getMoveVector(), fireDelay, wc.getStepDelay(), launchDelay, wc.getAcc(), "L1-ES1.png", wc.getStartX(), wc.getStartY(), this, numOfHits));
currentWave++;
}
}

View file

@ -4,6 +4,8 @@ import com.sagi.dayan.Games.Elements.*;
import com.sagi.dayan.Games.Engine.CollisionUtil; import com.sagi.dayan.Games.Engine.CollisionUtil;
import com.sagi.dayan.Games.Engine.GameEngine; import com.sagi.dayan.Games.Engine.GameEngine;
import com.sagi.dayan.Games.Utils.Utils; import com.sagi.dayan.Games.Utils.Utils;
import com.sagi.dayan.Games.Utils.WaveConfig;
import com.sagi.dayan.Games.Utils.WaveConfigs;
import javax.swing.*; import javax.swing.*;
import javax.swing.Timer; import javax.swing.Timer;
@ -18,7 +20,7 @@ import java.util.*;
/** /**
* Created by sagi on 2/20/16. * Created by sagi on 2/20/16.
*/ */
public class Level extends Scene { public abstract class Level extends Scene {
protected Vector<Player> players; protected Vector<Player> players;
protected int p1Speed = 10; protected int p1Speed = 10;
protected Vector<Missile> p1Missiles, p2Missiles, enemyMissiles; protected Vector<Missile> p1Missiles, p2Missiles, enemyMissiles;
@ -83,17 +85,13 @@ public class Level extends Scene {
@Override @Override
public void update() { public void update() {
bg.update(); bg.update();
movePlayers(); movePlayers();
Vector <Wave> wavesToRemove = new Vector<Wave>(); Vector <Wave> wavesToRemove = new Vector<Wave>();
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){
lastWaveTime = now; launchWave(now);
System.out.println("New Wave!! Time: "+ now);
currentWave++;
waves.add(new Wave(5, new int[]{90,90,120, 120, 150, 150, 270, 270, 270} , 4, 1, 2, 4, "L1-ES1.png" , 500, 0, this, 1));
} }
if(startingAnimationIndex < 3 && !isStarted){ if(startingAnimationIndex < 3 && !isStarted){
@ -145,6 +143,8 @@ public class Level extends Scene {
checkCollision(); checkCollision();
} }
protected abstract void launchWave(long time);
private void movePlayers() { private void movePlayers() {
/** /**
* Player 1 Movement: * Player 1 Movement:

View file

@ -0,0 +1,41 @@
package com.sagi.dayan.Games.Utils;
/**
* Created by sagi on 3/18/16.
*/
public class WaveConfig {
protected int[] moveVector;
protected double stepDelay;
protected int acc;
protected int startX;
protected int startY;
public WaveConfig(int[] moveVector, double stepDelay, int acc, int staryX, int startY){
this.moveVector = moveVector;
this.stepDelay = stepDelay;
this.acc = acc;
this.startX = staryX;
this.startY = startY;
}
public int[] getMoveVector() {
return moveVector;
}
public double getStepDelay() {
return stepDelay;
}
public int getAcc() {
return acc;
}
public int getStartX() {
return startX;
}
public int getStartY() {
return startY;
}
}

View file

@ -0,0 +1,34 @@
package com.sagi.dayan.Games.Utils;
import java.util.Vector;
/**
* Created by sagi on 3/18/16.
*/
public class WaveConfigs {
public static final int DEMO = 0;
Vector<WaveConfig> configs;
public WaveConfigs(){
configs = new Vector<>();
// int[] moveVector, double stepDelay, int acc, int staryX, int startY
configs.add(new WaveConfig(new int[]{90,90,120, 120, 150, 150, 270, 270, 270} , 0.5, 8,500 , 0));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
// configs.add(new WaveConfig( , , , , ));
}
public WaveConfig getWaveConfig(int config){
if (config < 0 || configs.size() <= config)
throw new IllegalArgumentException("no such config...");
return configs.get(config);
}
}