Far-Out/src/com/sagi/dayan/Games/Engine/GameEngine.java

347 lines
8.8 KiB
Java
Raw Normal View History

2016-02-27 20:45:32 +00:00
package com.sagi.dayan.Games.Engine;
/**
* Created by sagi on 2/8/16.
*/
2016-03-19 17:13:03 +00:00
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.event.KeyEvent;
2016-02-27 20:45:32 +00:00
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
2016-03-19 17:13:03 +00:00
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sagi.dayan.Games.Stage.*;
import com.sagi.dayan.Games.Stage.MainMenuScene;
import com.sagi.dayan.Games.Stage.Scene;
import com.sagi.dayan.Games.Stage.SettingsMenuScene;
import com.sagi.dayan.Games.Stage.Stage;
import com.sagi.dayan.Games.Utils.Utils;
import com.sagi.dayan.Games.Utils.WaveConfigs;
2016-02-27 20:45:32 +00:00
2016-03-20 21:21:31 +00:00
/**
* this class is the game engine
* it updates and renders the current scene (i.e. start menu, stages etc.)
*/
2016-02-27 20:45:32 +00:00
public class GameEngine {
2016-03-20 21:21:31 +00:00
private final int CREDIT_TIME = 10, FULL_HEALTH = 100; //timeout for credits
public boolean gameOn , gameOver, isFirstGame; //flags to determine game state
2016-02-27 20:45:32 +00:00
private JFrame frame;
private int pWidth, pHeight, numOfPlayers; //panel dimensions
private Random r;
private Stage stage;
2016-03-19 02:25:38 +00:00
private Scene scene;
private int p1CreditTime, p2CreditTime, creditTickTime = 1;
2016-02-27 20:45:32 +00:00
public static final int PLAYER_WIDTH = 120, PLAYER_HEIGHT = 120;
2016-03-20 21:21:31 +00:00
//player 1 & 2 controls
public static final int UP=0,RIGHT=1,DOWN=2, LEFT=3, FIRE=4, USE_CREDIT=5;
2016-03-20 12:57:34 +00:00
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};
2016-02-27 20:45:32 +00:00
2016-03-19 00:01:49 +00:00
private int p1Lives, p2Lives, p1Health, p2Health, credits, p1Score, p2Score;
2016-03-20 21:21:31 +00:00
public int p1HighScore, p2HighScore;
2016-03-19 01:40:07 +00:00
private long lastP1CreditTick, lastP2CreditTick;
2016-02-27 20:45:32 +00:00
private Font gameFont;
private WaveConfigs waveConfigs;
2016-03-19 02:25:38 +00:00
private int currentLevel;
2016-03-20 21:21:31 +00:00
2016-02-27 20:45:32 +00:00
public GameEngine(int width, int height, Stage stage){
2016-03-19 17:13:03 +00:00
p1HighScore = p2HighScore = 0;
2016-02-27 20:45:32 +00:00
this.isFirstGame = true;
this.gameOver = true;
this.pWidth = width;
this.pHeight = height;
this.stage = stage;
2016-03-19 02:25:38 +00:00
currentLevel = -1;
goToMenu();
2016-02-27 20:45:32 +00:00
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;
}
this.waveConfigs = new WaveConfigs();
2016-03-19 02:25:38 +00:00
2016-03-20 21:21:31 +00:00
startNewGame(); //initialize a new game
2016-02-27 20:45:32 +00:00
}
2016-03-20 21:21:31 +00:00
//reset player health to full health
2016-03-19 00:01:49 +00:00
private void resetPlayerHealth(int i){
if (i==0){
2016-03-20 21:21:31 +00:00
p1Health = FULL_HEALTH;
2016-03-19 00:01:49 +00:00
}
else{
2016-03-20 21:21:31 +00:00
p2Health = FULL_HEALTH;
2016-03-19 00:01:49 +00:00
}
}
2016-03-19 01:40:07 +00:00
2016-02-27 20:45:32 +00:00
/**
* initialize and reset vars and timers to "new game" configuration.
*/
private void startNewGame(){
this.gameOn = true;
2016-03-19 02:25:38 +00:00
this.currentLevel = -1;
2016-02-27 20:45:32 +00:00
initGame();
}
/**
* Setup all actors in the game to a new game - reset timer
*/
private void initGame(){
2016-03-19 02:25:38 +00:00
resetPlayerHealth(0);
resetPlayerHealth(1);
p1Score = p2Score = 0;
2016-03-19 02:25:38 +00:00
credits = 3;
p1Lives = 3;
p2Lives = 3;
2016-03-19 01:40:07 +00:00
}
2016-02-27 20:45:32 +00:00
2016-03-19 01:40:07 +00:00
public int getP1CreditTime() {
return p1CreditTime;
}
2016-02-27 20:45:32 +00:00
2016-03-19 01:40:07 +00:00
public int getP2CreditTime() {
return p2CreditTime;
2016-02-27 20:45:32 +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;
}
/**
2016-03-20 21:21:31 +00:00
* Update all scenes
2016-02-27 20:45:32 +00:00
*/
public void update(){
2016-03-19 01:40:07 +00:00
long now = System.currentTimeMillis();
if(now - lastP1CreditTick >= creditTickTime * 1000){
p1CreditTime--;
lastP1CreditTick = now;
}
if(now - lastP2CreditTick >= creditTickTime * 1000){
p2CreditTime--;
lastP2CreditTick = now;
}
2016-03-19 02:25:38 +00:00
scene.update();
2016-02-27 20:45:32 +00:00
}
public void render(JPanel p) {
2016-03-19 02:25:38 +00:00
scene.render(p);
2016-02-27 20:45:32 +00:00
}
public BufferedImage getScene() {
2016-03-19 02:25:38 +00:00
return scene.getSceneImage();
2016-02-27 20:45:32 +00:00
}
2016-03-20 21:21:31 +00:00
//initializes a new game
2016-02-27 20:45:32 +00:00
public void startGame(int numOfPlayers){
this.numOfPlayers = numOfPlayers;
2016-03-19 02:25:38 +00:00
startNewGame();
changeLevel();
2016-02-27 20:45:32 +00:00
}
2016-03-20 21:21:31 +00:00
//move to next level (scene) and attach listeners
2016-03-19 02:25:38 +00:00
public void changeLevel(){
currentLevel++;
stage.removeMouseListener(scene);
stage.removeKeyListener(scene);
2016-03-20 21:42:50 +00:00
switch (currentLevel%7){
2016-03-19 02:25:38 +00:00
case 0:
2016-03-20 21:42:50 +00:00
scene = new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE "+(currentLevel+1)+".0 =-", new int[]{5, 5});
2016-03-19 02:25:38 +00:00
break;
case 1:
2016-03-20 21:42:50 +00:00
scene = new SecondStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE "+(currentLevel+1)+".0 =-", new int[]{5, 5,5});
2016-03-19 17:13:03 +00:00
break;
case 2:
2016-03-20 21:42:50 +00:00
scene = new ThirdStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE "+(currentLevel+1)+".0 =-", new int[]{5, 0,2,8});
2016-03-19 17:13:03 +00:00
break;
case 3:
2016-03-20 21:42:50 +00:00
scene = new FourthStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE "+(currentLevel+1)+".0 =-", new int[]{5, 0,0,7});
break;
case 4:
2016-03-20 21:42:50 +00:00
scene = new FifthStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE "+(currentLevel+1)+".0 =-", new int[]{5, 1,3,8, 10, 10});
break;
case 5:
2016-03-20 21:42:50 +00:00
scene = new SixthStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE "+(currentLevel+1)+".0 =-", new int[]{5, 0,0,0, 0, 0, 10, 20});
break;
case 6:
scene = new BlitzStage(pWidth, pHeight, numOfPlayers, this, "-= BLITZ STAGE =-", new int[]{5, 0,0,0, 0, 0, 0, 0, 0, 0, 0});
2016-03-19 02:25:38 +00:00
break;
2016-02-27 20:45:32 +00:00
2016-03-19 02:25:38 +00:00
}
stage.addKeyListener(scene);
stage.addMouseListener(scene);
2016-02-27 20:45:32 +00:00
}
2016-03-20 21:21:31 +00:00
//load controls menu
2016-03-19 02:25:38 +00:00
public void goToSettings() {
stage.removeMouseListener(scene);
stage.removeKeyListener(scene);
scene = new SettingsMenuScene(pWidth, pHeight, this);
stage.addKeyListener(scene);
stage.addMouseListener(scene);
2016-02-27 20:45:32 +00:00
}
public int[] getP1Controlles(){
return p1Controlles;
}
public int[] getP2Controlles(){
return p2Controlles;
}
public Font getGameFont() {
return gameFont;
}
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-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--;
}
2016-03-19 17:13:03 +00:00
2016-03-20 21:21:31 +00:00
//when a player uses a credit - reset health, lives and consume credit
2016-03-19 17:13:03 +00:00
public void revivePlayer(int i)
{
useCredit();
if(i==0){
p1Health=100;
p1Lives =3;
}
else{
p2Health=100;
p2Lives =3;
}
}
2016-03-20 21:21:31 +00:00
//add score to the correct player
2016-03-19 00:01:49 +00:00
public void setScore(int i, int score)
{
if (i == 0) {
p1Score += score;
} else {
p2Score += score;
}
}
2016-03-20 21:21:31 +00:00
// if a player gets hit - consume health
//if health is 0 - consume life
2016-03-19 00:01:49 +00:00
public void setPlayerHealth(int i, int strike) {
if (i == 0) {
2016-03-19 00:01:49 +00:00
p1Health += strike;
2016-03-19 01:40:07 +00:00
if(p1Health <= 0){
p1Lives--;
if(p1Lives > 0)
resetPlayerHealth(i);
if(p1Lives <= 0){
2016-03-19 17:13:03 +00:00
p1CreditTime = 10;
2016-03-19 01:40:07 +00:00
lastP1CreditTick = System.currentTimeMillis();
}
}
} else {
2016-03-19 00:01:49 +00:00
p2Health += strike;
2016-03-19 01:40:07 +00:00
if(p2Health <= 0){
p2Lives--;
if(p2Lives > 0)
resetPlayerHealth(i);
if(p2Health <= 0){
p2CreditTime = 10;
lastP2CreditTick = System.currentTimeMillis();
}
}
}
}
2016-03-20 21:21:31 +00:00
//go to main menu
2016-03-19 02:25:38 +00:00
public void goToMenu(){
stage.removeMouseListener(scene);
stage.removeKeyListener(scene);
scene = new MainMenuScene(pWidth, pHeight, this);
stage.addKeyListener(scene);
stage.addMouseListener(scene);
}
2016-03-19 01:40:07 +00:00
public void setGameOver(boolean gameOver) {
if(gameOver){
2016-03-19 02:25:38 +00:00
goToMenu();
this.gameOver = true;
2016-03-19 01:40:07 +00:00
}
2016-03-19 02:25:38 +00:00
2016-03-19 01:40:07 +00:00
}
2016-03-19 17:13:03 +00:00
public int getP1HighScore() {
return p1HighScore;
}
public void setP1HighScore(int p1HighScore) {
this.p1HighScore = p1HighScore;
}
public int getP2HighScore() {
return p2HighScore;
}
public void setP2HighScore(int p2HighScore) {
this.p2HighScore = p2HighScore;
}
2016-02-27 20:45:32 +00:00
}