Stage management

This commit is contained in:
Sagi Dayan 2016-03-19 04:25:38 +02:00
parent 7a43d77ef4
commit dff8de829c
5 changed files with 59 additions and 52 deletions

View file

@ -29,8 +29,8 @@ public class GameEngine {
private int pWidth, pHeight, numOfPlayers; //panel dimensions private int pWidth, pHeight, numOfPlayers; //panel dimensions
private Random r; private Random r;
private Stage stage; private Stage stage;
private Vector<Scene> scenes; private Scene scene;
private int currentScene, 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, SPECIAL=5; public static final int UP=0,RIGHT=1,DOWN=2, LEFT=3, FIRE=4, SPECIAL=5;
@ -44,18 +44,16 @@ public class GameEngine {
private Font gameFont; private Font gameFont;
private WaveConfigs waveConfigs; private WaveConfigs waveConfigs;
private int currentLevel;
public GameEngine(int width, int height, Stage stage){ public GameEngine(int width, int height, Stage stage){
this.currentScene = 0;
this.isFirstGame = true; this.isFirstGame = true;
this.gameOver = true; this.gameOver = true;
this.pWidth = width; this.pWidth = width;
this.pHeight = height; this.pHeight = height;
this.scenes = new Vector<>();
this.stage = stage; this.stage = stage;
scenes.add(new MainMenuScene(width, height, this)); currentLevel = -1;
stage.addKeyListener(scenes.get(currentScene)); goToMenu();
stage.addMouseListener(scenes.get(currentScene));
r = new Random(); r = new Random();
try{ try{
gameFont = Font.createFont(Font.TRUETYPE_FONT,Utils.getFontStream("transformers.ttf")); gameFont = Font.createFont(Font.TRUETYPE_FONT,Utils.getFontStream("transformers.ttf"));
@ -68,11 +66,7 @@ public class GameEngine {
} }
this.waveConfigs = new WaveConfigs(); this.waveConfigs = new WaveConfigs();
startNewGame(); startNewGame();
resetPlayerHealth(0);
resetPlayerHealth(1);
credits = 3;
p1Lives = 1;
p2Lives = 1;
} }
@ -103,6 +97,7 @@ public class GameEngine {
*/ */
private void startNewGame(){ private void startNewGame(){
this.gameOn = true; this.gameOn = true;
this.currentLevel = -1;
initGame(); initGame();
} }
@ -110,6 +105,11 @@ public class GameEngine {
* Setup all actors in the game to a new game - reset timer * Setup all actors in the game to a new game - reset timer
*/ */
private void initGame(){ private void initGame(){
resetPlayerHealth(0);
resetPlayerHealth(1);
credits = 3;
p1Lives = 1;
p2Lives = 1;
} }
public int getP1CreditTime() { public int getP1CreditTime() {
@ -153,51 +153,49 @@ public class GameEngine {
p2CreditTime--; p2CreditTime--;
lastP2CreditTick = now; lastP2CreditTick = now;
} }
scenes.get(currentScene).update(); scene.update();
} }
public void render(JPanel p) { public void render(JPanel p) {
scenes.get(currentScene).render(p); scene.render(p);
} }
public BufferedImage getScene() { public BufferedImage getScene() {
return scenes.get(currentScene).getSceneImage(); return scene.getSceneImage();
}
private void changeScene(int index) {
if (index >= scenes.size()){
throw new IllegalArgumentException("Invalid Index. scenes size: "+scenes.size());
}
stage.removeKeyListener(scenes.get(currentScene));
stage.removeMouseListener(scenes.get(currentScene));
currentScene = index;
stage.addKeyListener(scenes.get(currentScene));
stage.addMouseListener(scenes.get(currentScene));
} }
public void startGame(int numOfPlayers){ public void startGame(int numOfPlayers){
this.numOfPlayers = numOfPlayers; this.numOfPlayers = numOfPlayers;
scenes.add(new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-", new int[]{5, 20})); startNewGame();
changeScene(currentScene+1); changeLevel();
}
public void changeLevel(){
currentLevel++;
stage.removeMouseListener(scene);
stage.removeKeyListener(scene);
switch (currentLevel){
case 0:
scene = new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-", new int[]{5, 20});
break;
case 1:
scene = new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.1 =-", new int[]{5, 20});
break;
}
stage.addKeyListener(scene);
stage.addMouseListener(scene);
} }
public void goToSettings() { public void goToSettings() {
scenes.add(new SettingsMenuScene(pWidth, pHeight, this)); stage.removeMouseListener(scene);
changeScene(currentScene+1); stage.removeKeyListener(scene);
scene = new SettingsMenuScene(pWidth, pHeight, this);
stage.addKeyListener(scene);
stage.addMouseListener(scene);
} }
public void goToMainMenu() {
changeScene(0);
for(int i = scenes.size() -1 ; i > 0 ; i--){
scenes.remove(i);
}
}
public int getScenesSize(){
return scenes.size();
}
public int[] getP1Controlles(){ public int[] getP1Controlles(){
return p1Controlles; return p1Controlles;
@ -275,11 +273,20 @@ public class GameEngine {
} }
} }
public void goToMenu(){
stage.removeMouseListener(scene);
stage.removeKeyListener(scene);
scene = new MainMenuScene(pWidth, pHeight, this);
stage.addKeyListener(scene);
stage.addMouseListener(scene);
}
public void setGameOver(boolean gameOver) { public void setGameOver(boolean gameOver) {
if(gameOver){ if(gameOver){
changeScene(0); goToMenu();
this.gameOver = false; this.gameOver = true;
} }
} }
} }

View file

@ -27,14 +27,14 @@ public class FirstStage extends Level{
fireDelay = 0.1; fireDelay = 0.1;
launchDelay = 0.5; launchDelay = 0.5;
numOfHits = 1; numOfHits = 1;
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO); wc = engine.getWaveConfigs().getWaveConfig(6);
break; break;
case 1: case 1:
numOfEnemies = 5; numOfEnemies = 5;
fireDelay = 0.1; fireDelay = 0.1;
launchDelay = 1; launchDelay = 1;
numOfHits = 1; numOfHits = 1;
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO); wc = engine.getWaveConfigs().getWaveConfig(3);
break; break;
default: default:
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO); wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);

View file

@ -51,7 +51,7 @@ public class SettingsMenuScene extends Scene {
switch (keyEvent.getKeyCode()){ switch (keyEvent.getKeyCode()){
case KeyEvent.VK_ESCAPE: case KeyEvent.VK_ESCAPE:
Utils.playSound("menuItem.wav"); Utils.playSound("menuItem.wav");
engine.goToMainMenu(); engine.goToMenu();
break; break;
} }
} }

View file

@ -92,9 +92,9 @@ public class Stage extends JPanel implements Runnable{
if (System.currentTimeMillis() - lastTimer > 1000) { if (System.currentTimeMillis() - lastTimer > 1000) {
lastTimer += 1000; lastTimer += 1000;
if(frames <= 35){ if(frames <= 35){
System.err.println("Ticks: " + ticks + "\tFps: " + frames + "\tScenes Size: " + engine.getScenesSize()); System.err.println("Ticks: " + ticks + "\tFps: " + frames);
}else{ }else{
System.out.println("Ticks: " + ticks + "\tFps: " + frames + "\tScenes Size: " + engine.getScenesSize()); System.out.println("Ticks: " + ticks + "\tFps: " + frames);
} }
frames = 0; frames = 0;
ticks = 0; ticks = 0;

View file

@ -27,16 +27,16 @@ public class WaveConfigs {
configs.add(new WaveConfig(new int[]{90,90,120, 120, 150, 150, 270} , 0.5, 8,900 , -30)); configs.add(new WaveConfig(new int[]{90,90,120, 120, 150, 150, 270} , 0.5, 8,900 , -30));
//right buttom to middle buttom //right buttom to middle buttom
configs.add(new WaveConfig(new int[]{270,270,300, 300, 330, 330, 90} , 0.5, 8,100 , 0)); configs.add(new WaveConfig(new int[]{270,270,300, 300, 330, 330, 90} , 0.5, 8,100 , 1000));
//left buttom to middle buttom //left buttom to middle buttom
configs.add(new WaveConfig(new int[]{270,270,240, 240, 210, 210, 90} , 0.5, 8,900 , 0)); 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,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,1010 , 400));
//middle left to middle left //middle left to middle left
configs.add(new WaveConfig(new int[]{0,0,0,90, 90, 180} , 0.5, 8,-30 , 400)); configs.add(new WaveConfig(new int[]{0,0,0,0,0,90, 90, 180} , 0.5, 8,-10 , 400));
} }