2016-02-27 20:45:32 +00:00
|
|
|
package com.sagi.dayan.Games.Engine;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by sagi on 2/8/16.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import com.sagi.dayan.Games.Stage.*;
|
|
|
|
import com.sagi.dayan.Games.Utils.Utils;
|
2016-03-18 22:33:29 +00:00
|
|
|
import com.sagi.dayan.Games.Utils.WaveConfigs;
|
2016-02-27 20:45:32 +00:00
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.*;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by sagi on 12/18/15.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class GameEngine {
|
|
|
|
public boolean gameOn , gameOver, isFirstGame;
|
|
|
|
private JFrame frame;
|
|
|
|
private int pWidth, pHeight, numOfPlayers; //panel dimensions
|
|
|
|
private Random r;
|
|
|
|
private Stage stage;
|
|
|
|
private Vector<Scene> scenes;
|
|
|
|
private int currentScene;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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};
|
|
|
|
|
2016-03-19 00:01:49 +00:00
|
|
|
private int p1Lives, p2Lives, p1Health, p2Health, credits, p1Score, p2Score;
|
2016-03-12 10:24:42 +00:00
|
|
|
|
2016-03-18 22:33:29 +00:00
|
|
|
|
|
|
|
|
2016-02-27 20:45:32 +00:00
|
|
|
private Font gameFont;
|
|
|
|
|
2016-03-18 22:33:29 +00:00
|
|
|
private WaveConfigs waveConfigs;
|
|
|
|
|
2016-02-27 20:45:32 +00:00
|
|
|
public GameEngine(int width, int height, Stage stage){
|
|
|
|
this.currentScene = 0;
|
|
|
|
this.isFirstGame = true;
|
|
|
|
this.gameOver = true;
|
|
|
|
this.pWidth = width;
|
|
|
|
this.pHeight = height;
|
|
|
|
this.scenes = new Vector<>();
|
|
|
|
this.stage = stage;
|
2016-03-12 10:24:42 +00:00
|
|
|
// scenes.add(new Level(width, height, 2)); // Need to be a menu Scene
|
2016-02-27 20:45:32 +00:00
|
|
|
scenes.add(new MainMenuScene(width, height, this));
|
|
|
|
stage.addKeyListener(scenes.get(currentScene));
|
|
|
|
stage.addMouseListener(scenes.get(currentScene));
|
|
|
|
r = new Random();
|
|
|
|
try{
|
|
|
|
gameFont = Font.createFont(Font.TRUETYPE_FONT,Utils.getFontStream("transformers.ttf"));
|
|
|
|
} catch (FontFormatException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
gameFont = null;
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
gameFont = null;
|
|
|
|
}
|
2016-03-18 22:33:29 +00:00
|
|
|
this.waveConfigs = new WaveConfigs();
|
2016-02-27 20:45:32 +00:00
|
|
|
startNewGame();
|
2016-03-19 00:01:49 +00:00
|
|
|
resetPlayerHealth(0);
|
|
|
|
resetPlayerHealth(1);
|
|
|
|
credits = 3;
|
2016-02-27 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-19 00:01:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 20:45:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize and reset vars and timers to "new game" configuration.
|
|
|
|
*/
|
|
|
|
private void startNewGame(){
|
|
|
|
this.gameOn = true;
|
|
|
|
initGame();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup all actors in the game to a new game - reset timer
|
|
|
|
*/
|
|
|
|
private void initGame(){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-18 22:33:29 +00:00
|
|
|
public WaveConfigs getWaveConfigs() {
|
|
|
|
return waveConfigs;
|
|
|
|
}
|
2016-02-27 20:45:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns gameOver flag
|
|
|
|
* @return
|
|
|
|
* boolean
|
|
|
|
*/
|
|
|
|
public boolean isGameOver(){
|
|
|
|
return this.gameOver;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update all sprites, including collision handling.
|
|
|
|
*/
|
|
|
|
public void update(){
|
|
|
|
scenes.get(currentScene).update();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void render(JPanel p) {
|
|
|
|
scenes.get(currentScene).render(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BufferedImage getScene() {
|
|
|
|
return scenes.get(currentScene).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){
|
|
|
|
this.numOfPlayers = numOfPlayers;
|
2016-03-18 22:33:29 +00:00
|
|
|
scenes.add(new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-", new int[]{5, 20}));
|
2016-02-27 20:45:32 +00:00
|
|
|
changeScene(currentScene+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void goToSettings() {
|
|
|
|
scenes.add(new SettingsMenuScene(pWidth, pHeight, this));
|
|
|
|
changeScene(currentScene+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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(){
|
|
|
|
return p1Controlles;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int[] getP2Controlles(){
|
|
|
|
return p2Controlles;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Font getGameFont() {
|
|
|
|
return gameFont;
|
|
|
|
}
|
2016-03-12 10:24:42 +00:00
|
|
|
|
2016-03-19 00:01:49 +00:00
|
|
|
public int getP1Lives() {
|
|
|
|
return p1Lives;
|
|
|
|
}
|
|
|
|
public int getP2Lives() {
|
|
|
|
return p2Lives;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getP1Health() {
|
|
|
|
return p1Health;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getP2Health() {
|
|
|
|
return p2Health;
|
2016-03-12 10:24:42 +00:00
|
|
|
}
|
2016-03-19 00:01:49 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
p1Score += score;
|
|
|
|
} else {
|
|
|
|
p2Score += score;
|
|
|
|
}
|
2016-03-12 10:24:42 +00:00
|
|
|
}
|
2016-03-19 00:01:49 +00:00
|
|
|
public void setPlayerHealth(int i, int strike) {
|
2016-03-12 10:24:42 +00:00
|
|
|
if (i == 0) {
|
2016-03-19 00:01:49 +00:00
|
|
|
p1Health += strike;
|
2016-03-12 10:24:42 +00:00
|
|
|
} else {
|
2016-03-19 00:01:49 +00:00
|
|
|
p2Health += strike;
|
2016-03-12 10:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 20:45:32 +00:00
|
|
|
}
|