pin
This commit is contained in:
parent
1c1667d37a
commit
9e8fda4eb7
17 changed files with 802 additions and 497 deletions
|
@ -1,15 +1,14 @@
|
|||
package com.sagi.dayan.Games.Elements;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.ImageProducer;
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/10/16.
|
||||
*/
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
package com.sagi.dayan.Games.Elements;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.MatteBorder;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package com.sagi.dayan.Games.Elements;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.Graphics;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/20/16.
|
||||
|
@ -52,7 +53,7 @@ public class Player extends AnimatedSprite {
|
|||
lastDrawn = System.currentTimeMillis();
|
||||
created = lastDrawn;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void initFirstAnimation(String spriteSheet, int numOfFirstFrames) {
|
||||
if(imagePrefix == null)
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
package com.sagi.dayan.Games.Elements;
|
||||
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
public abstract class Sprite {
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package com.sagi.dayan.Games.Elements;
|
||||
|
||||
import com.sagi.dayan.Games.Stage.Level;
|
||||
import com.sagi.dayan.Games.Stage.Scene;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.Graphics;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import com.sagi.dayan.Games.Stage.Level;
|
||||
|
||||
/**
|
||||
* Created by sagi on 3/11/16.
|
||||
*/
|
||||
|
@ -52,6 +52,7 @@ public class Wave {
|
|||
for (int i = 0; i < enemies.size() ; i++){
|
||||
enemies.get(i).update();
|
||||
if (enemies.get(i).isDone() || enemies.get(i).isOutOfScreen()) {
|
||||
|
||||
enemiesToRemove.add(enemies.get(i));
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +73,8 @@ public class Wave {
|
|||
}
|
||||
|
||||
public void fireFromEnemy(EnemyShip e){
|
||||
level.enemyFire(e.getCenterX(), (int)(e.getLocY() + e.getsHeight()), -(e.getAcceleration() + 2));
|
||||
if(!e.isDead())
|
||||
level.enemyFire(e.getCenterX(), (int)(e.getLocY() + e.getsHeight()), -(e.getAcceleration() + 2));
|
||||
}
|
||||
|
||||
public Vector <EnemyShip> getEnemies() {
|
||||
|
@ -83,6 +85,6 @@ public class Wave {
|
|||
es.gotHit();
|
||||
}
|
||||
public boolean isWaveOver() {
|
||||
return enemies.size() == 0 && currentAmount == enemyMaxAmount;
|
||||
return enemies.size() == 0 && currentAmount >= enemyMaxAmount;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,11 +7,11 @@ package com.sagi.dayan.Games.Engine;
|
|||
* Created by sagi on 12/19/15.
|
||||
*/
|
||||
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.PixelGrabber;
|
||||
|
||||
import com.sagi.dayan.Games.Elements.AnimatedSprite;
|
||||
import com.sagi.dayan.Games.Elements.Sprite;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.PixelGrabber;
|
||||
public class CollisionUtil {
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,21 +4,23 @@ 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;
|
||||
import com.sagi.dayan.Games.Utils.WaveConfigs;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontFormatException;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* Created by sagi on 12/18/15.
|
||||
*/
|
||||
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;
|
||||
|
||||
|
||||
|
||||
|
@ -33,9 +35,10 @@ public class GameEngine {
|
|||
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, SPECIAL=5;
|
||||
public int p1HighScore, p2HighScore;
|
||||
|
||||
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};
|
||||
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;
|
||||
|
||||
|
@ -47,6 +50,7 @@ public class GameEngine {
|
|||
private int currentLevel;
|
||||
|
||||
public GameEngine(int width, int height, Stage stage){
|
||||
p1HighScore = p2HighScore = 0;
|
||||
this.isFirstGame = true;
|
||||
this.gameOver = true;
|
||||
this.pWidth = width;
|
||||
|
@ -78,16 +82,7 @@ public class GameEngine {
|
|||
p2Health = 100;
|
||||
}
|
||||
}
|
||||
private void resetPlayer(int i){
|
||||
resetPlayerHealth(i);
|
||||
|
||||
if (i==0){
|
||||
p1Lives = 3;
|
||||
}
|
||||
else{
|
||||
p2Lives = 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -171,15 +166,22 @@ public class GameEngine {
|
|||
}
|
||||
|
||||
public void changeLevel(){
|
||||
System.out.println("current level: "+currentLevel);
|
||||
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});
|
||||
scene = new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-", new int[]{5, 1,1,1});
|
||||
break;
|
||||
case 1:
|
||||
scene = new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.1 =-", new int[]{5, 20});
|
||||
scene = new SecondStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 2.0 =-", new int[]{5, 5,5,5});
|
||||
break;
|
||||
case 2:
|
||||
scene = new ThirdStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 3.0 =-", new int[]{5, 0,0,8});
|
||||
break;
|
||||
case 3:
|
||||
scene = new FourthStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 4.0 =-", new int[]{5, 0,0,8});
|
||||
break;
|
||||
|
||||
}
|
||||
|
@ -238,6 +240,22 @@ public class GameEngine {
|
|||
public void useCredit(){
|
||||
credits--;
|
||||
}
|
||||
|
||||
public void revivePlayer(int i)
|
||||
{
|
||||
useCredit();
|
||||
|
||||
if(i==0){
|
||||
p1Health=100;
|
||||
p1Lives =3;
|
||||
}
|
||||
else{
|
||||
p2Health=100;
|
||||
p2Lives =3;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void setScore(int i, int score)
|
||||
{
|
||||
|
@ -255,7 +273,7 @@ public class GameEngine {
|
|||
if(p1Lives > 0)
|
||||
resetPlayerHealth(i);
|
||||
if(p1Lives <= 0){
|
||||
p1CreditTime = 10;
|
||||
p1CreditTime = 10;
|
||||
lastP1CreditTick = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
@ -289,4 +307,26 @@ public class GameEngine {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -9,15 +9,18 @@ import com.sagi.dayan.Games.Utils.WaveConfigs;
|
|||
* Created by sagi on 3/19/16.
|
||||
*/
|
||||
public class FirstStage extends Level{
|
||||
|
||||
protected final int NUM_OF_WAVES = 4;
|
||||
|
||||
public FirstStage(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay) {
|
||||
super(width, height, numOfPlayers, engine, stageTitle, waveDelay);
|
||||
numOfWaves = NUM_OF_WAVES;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void launchWave(long now) {
|
||||
lastWaveTime = now;
|
||||
System.out.println("New Wave!! Time: " + now);
|
||||
System.out.println("New Wave!! " + currentWave + ", Time: " + now);
|
||||
WaveConfig wc;
|
||||
int numOfEnemies = 5, numOfHits = 1;
|
||||
double launchDelay = 0.5, fireDelay = 5;
|
||||
|
@ -31,11 +34,25 @@ public class FirstStage extends Level{
|
|||
break;
|
||||
case 1:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 0.1;
|
||||
fireDelay = 1;
|
||||
launchDelay = 1;
|
||||
numOfHits = 1;
|
||||
numOfHits = 2;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(3);
|
||||
break;
|
||||
case 2:
|
||||
numOfEnemies = 10;
|
||||
fireDelay = 0.8;
|
||||
launchDelay = 1;
|
||||
numOfHits = 3;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(2);
|
||||
break;
|
||||
case 3:
|
||||
numOfEnemies = 15;
|
||||
fireDelay = 1.5;
|
||||
launchDelay = 1;
|
||||
numOfHits = 5;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(7);
|
||||
break;
|
||||
default:
|
||||
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);
|
||||
break;
|
||||
|
|
63
src/com/sagi/dayan/Games/Stage/FourthStage.java
Normal file
63
src/com/sagi/dayan/Games/Stage/FourthStage.java
Normal file
|
@ -0,0 +1,63 @@
|
|||
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 FourthStage extends Level{
|
||||
|
||||
protected final int NUM_OF_WAVES = 4;
|
||||
|
||||
public FourthStage(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay) {
|
||||
super(width, height, numOfPlayers, engine, stageTitle, waveDelay);
|
||||
numOfWaves = NUM_OF_WAVES;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void launchWave(long now) {
|
||||
lastWaveTime = now;
|
||||
System.out.println("New Wave!! " + currentWave + ", Time: " + now);
|
||||
WaveConfig wc;
|
||||
int numOfEnemies = 5, numOfHits = 1;
|
||||
double launchDelay = 0.5, fireDelay = 5;
|
||||
switch (currentWave){
|
||||
case 0:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 0.5;
|
||||
launchDelay = 0.5;
|
||||
numOfHits = 1;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(6);
|
||||
break;
|
||||
case 1:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 0.3;
|
||||
launchDelay = 1;
|
||||
numOfHits = 2;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(3);
|
||||
break;
|
||||
case 2:
|
||||
numOfEnemies = 10;
|
||||
fireDelay = 0.3;
|
||||
launchDelay = 1;
|
||||
numOfHits = 3;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(2);
|
||||
break;
|
||||
case 3:
|
||||
numOfEnemies = 15;
|
||||
fireDelay = 0.2;
|
||||
launchDelay = 1;
|
||||
numOfHits = 5;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(7);
|
||||
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++;
|
||||
}
|
||||
}
|
|
@ -1,482 +1,527 @@
|
|||
package com.sagi.dayan.Games.Stage;
|
||||
|
||||
import com.sagi.dayan.Games.Elements.*;
|
||||
import com.sagi.dayan.Games.Engine.CollisionUtil;
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
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.Timer;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import com.sagi.dayan.Games.Elements.Background;
|
||||
import com.sagi.dayan.Games.Elements.Missile;
|
||||
import com.sagi.dayan.Games.Elements.Player;
|
||||
import com.sagi.dayan.Games.Elements.Wave;
|
||||
import com.sagi.dayan.Games.Engine.CollisionUtil;
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
import com.sagi.dayan.Games.Utils.Utils;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/20/16.
|
||||
*/
|
||||
public abstract class Level extends Scene {
|
||||
protected final double PRESS_START_PULE = 0.3;
|
||||
protected boolean toDrawStart;
|
||||
protected Vector<Player> players;
|
||||
protected int p1Speed = 10;
|
||||
protected Vector<Missile> p1Missiles, p2Missiles, enemyMissiles;
|
||||
protected Background bg;
|
||||
protected int[] waveDelay;
|
||||
protected int currentWave;
|
||||
protected int[] yAxisStartingAnimation;
|
||||
protected int startingAnimationIndex;
|
||||
protected boolean isStarted;
|
||||
protected Vector<Wave> waves;
|
||||
protected int numOfPlayers;
|
||||
protected Map<Integer, Boolean> keys;
|
||||
protected String title;
|
||||
protected JLabel stageTitle;
|
||||
protected long lastWaveTime, lastPulseTime;
|
||||
protected final double PRESS_START_PULE = 0.3;
|
||||
protected boolean toDrawStart;
|
||||
protected Vector<Player> players;
|
||||
protected int p1Speed = 10;
|
||||
protected Vector<Missile> p1Missiles, p2Missiles, enemyMissiles;
|
||||
protected Background bg;
|
||||
protected int[] waveDelay;
|
||||
protected int currentWave;
|
||||
protected int[] yAxisStartingAnimation;
|
||||
protected int startingAnimationIndex;
|
||||
protected boolean isStarted;
|
||||
protected Vector<Wave> waves;
|
||||
protected int numOfPlayers;
|
||||
protected Map<Integer, Boolean> keys;
|
||||
protected String title;
|
||||
protected JLabel stageTitle;
|
||||
protected long lastWaveTime, lastPulseTime;
|
||||
protected int numOfWaves;
|
||||
|
||||
|
||||
|
||||
public Level(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay){
|
||||
super(width, height, engine);
|
||||
players = new Vector<>();
|
||||
p1Missiles = new Vector<>();
|
||||
p2Missiles = new Vector<>();
|
||||
enemyMissiles = new Vector<>();
|
||||
this.waveDelay = waveDelay;
|
||||
this.lastWaveTime = System.currentTimeMillis();
|
||||
this.currentWave = 0;
|
||||
this.waves = new Vector<>();
|
||||
isStarted = false;
|
||||
keys = new HashMap<>();
|
||||
yAxisStartingAnimation = new int[]{height + (5*GameEngine.PLAYER_HEIGHT) , height - (4*GameEngine.PLAYER_HEIGHT) , height - (GameEngine.PLAYER_HEIGHT + 15)};
|
||||
startingAnimationIndex = 0;
|
||||
bg = new Background(0,0,width,height, 1, "L1-BG.jpg", 0,1000, 4760);
|
||||
this.numOfPlayers = numOfPlayers;
|
||||
this.title = stageTitle;
|
||||
this.stageTitle = new JLabel(this.title);
|
||||
public Level(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay){
|
||||
super(width, height, engine);
|
||||
players = new Vector<>();
|
||||
p1Missiles = new Vector<>();
|
||||
p2Missiles = new Vector<>();
|
||||
enemyMissiles = new Vector<>();
|
||||
this.waveDelay = waveDelay;
|
||||
this.lastWaveTime = System.currentTimeMillis();
|
||||
this.currentWave = 0;
|
||||
this.waves = new Vector<>();
|
||||
isStarted = false;
|
||||
keys = new HashMap<>();
|
||||
yAxisStartingAnimation = new int[]{height + (5*GameEngine.PLAYER_HEIGHT) , height - (4*GameEngine.PLAYER_HEIGHT) , height - (GameEngine.PLAYER_HEIGHT + 15)};
|
||||
startingAnimationIndex = 0;
|
||||
bg = new Background(0,0,width,height, 1, "L1-BG.jpg", 0,1000, 4760);
|
||||
this.numOfPlayers = numOfPlayers;
|
||||
this.title = stageTitle;
|
||||
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));
|
||||
}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));
|
||||
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));
|
||||
}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));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
setupKeys();
|
||||
Utils.playSound("jetSound.wav");
|
||||
lastPulseTime = System.currentTimeMillis();
|
||||
toDrawStart = true;
|
||||
}
|
||||
setupKeys();
|
||||
Utils.playSound("jetSound.wav");
|
||||
lastPulseTime = System.currentTimeMillis();
|
||||
toDrawStart = true;
|
||||
}
|
||||
|
||||
private void setupKeys() {
|
||||
int[] p1 = engine.getP1Controlles();
|
||||
for(int i = 0 ; i < p1.length ; i++){
|
||||
keys.put(p1[i], false);
|
||||
}
|
||||
if(numOfPlayers > 1){
|
||||
int[] p2 = engine.getP2Controlles();
|
||||
for(int i = 0 ; i < p1.length ; i++){
|
||||
keys.put(p2[i], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void setupKeys() {
|
||||
int[] p1 = engine.getP1Controlles();
|
||||
for(int i = 0 ; i < p1.length ; i++){
|
||||
keys.put(p1[i], false);
|
||||
}
|
||||
if(numOfPlayers > 1){
|
||||
int[] p2 = engine.getP2Controlles();
|
||||
for(int i = 0 ; i < p1.length ; i++){
|
||||
keys.put(p2[i], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
bg.update();
|
||||
movePlayers();
|
||||
Vector <Wave> wavesToRemove = new Vector<>();
|
||||
@Override
|
||||
public void update() {
|
||||
bg.update();
|
||||
movePlayers();
|
||||
Vector <Wave> wavesToRemove = new Vector<>();
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
if(currentWave < waveDelay.length && now - lastWaveTime >= waveDelay[currentWave] * 1000){
|
||||
launchWave(now);
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
// if(currentWave < waveDelay.length && now - lastWaveTime >= waveDelay[currentWave] * 1000){
|
||||
if(currentWave < numOfWaves && now - lastWaveTime >= waveDelay[currentWave] * 1000){
|
||||
launchWave(now);
|
||||
}
|
||||
|
||||
if(startingAnimationIndex < 3 && !isStarted){
|
||||
if(startingAnimationIndex == 0){
|
||||
startingAnimationIndex++;
|
||||
}
|
||||
if(players.get(0).getLocY() > yAxisStartingAnimation[startingAnimationIndex] && startingAnimationIndex == 1){
|
||||
for(int i = 0 ; i < players.size() ; i++){
|
||||
players.get(i).setLocY((int)players.get(i).getLocY() - (p1Speed));
|
||||
}
|
||||
if(players.get(0).getLocY() <= yAxisStartingAnimation[startingAnimationIndex]){
|
||||
startingAnimationIndex++;
|
||||
}
|
||||
}else{
|
||||
if(startingAnimationIndex < 3 && !isStarted){
|
||||
if(startingAnimationIndex == 0){
|
||||
startingAnimationIndex++;
|
||||
}
|
||||
if(players.get(0).getLocY() > yAxisStartingAnimation[startingAnimationIndex] && startingAnimationIndex == 1){
|
||||
for(int i = 0 ; i < players.size() ; i++){
|
||||
players.get(i).setLocY((int)players.get(i).getLocY() - (p1Speed));
|
||||
}
|
||||
if(players.get(0).getLocY() <= yAxisStartingAnimation[startingAnimationIndex]){
|
||||
startingAnimationIndex++;
|
||||
}
|
||||
}else{
|
||||
|
||||
for(int i = 0 ; i < players.size() ; i++){
|
||||
players.get(i).setLocY((int)players.get(i).getLocY() + (p1Speed - 5));
|
||||
}
|
||||
if(players.get(0).getLocY() >= yAxisStartingAnimation[startingAnimationIndex]){
|
||||
startingAnimationIndex++;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
isStarted = true;
|
||||
for(int i = 0 ; i < players.size() ; i++){
|
||||
players.get(i).update();
|
||||
}
|
||||
for(int i = 0 ; i < players.size() ; i++){
|
||||
players.get(i).setLocY((int)players.get(i).getLocY() + (p1Speed - 5));
|
||||
}
|
||||
if(players.get(0).getLocY() >= yAxisStartingAnimation[startingAnimationIndex]){
|
||||
startingAnimationIndex++;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
isStarted = true;
|
||||
for(int i = 0 ; i < players.size() ; i++){
|
||||
players.get(i).update();
|
||||
}
|
||||
|
||||
for(int i = 0 ; i < p1Missiles.size() ; i++){
|
||||
p1Missiles.get(i).update();
|
||||
}
|
||||
for(int i = 0 ; i < p2Missiles.size() ; i++){
|
||||
p2Missiles.get(i).update();
|
||||
}
|
||||
for(int i = 0 ; i < enemyMissiles.size() ; i++){
|
||||
enemyMissiles.get(i).update();
|
||||
}
|
||||
for(int i = 0 ; i < p1Missiles.size() ; i++){
|
||||
p1Missiles.get(i).update();
|
||||
}
|
||||
for(int i = 0 ; i < p2Missiles.size() ; i++){
|
||||
p2Missiles.get(i).update();
|
||||
}
|
||||
for(int i = 0 ; i < enemyMissiles.size() ; i++){
|
||||
enemyMissiles.get(i).update();
|
||||
}
|
||||
|
||||
for(int i = 0 ; i < waves.size() ; i++){
|
||||
waves.get(i).update();
|
||||
if(waves.get(i).isWaveOver()) {
|
||||
wavesToRemove.add(waves.get(i));
|
||||
}
|
||||
}
|
||||
waves.removeAll(wavesToRemove);
|
||||
}
|
||||
checkCollision();
|
||||
engine.setGameOver(isGameOver());
|
||||
}
|
||||
for(int i = 0 ; i < waves.size() ; i++){
|
||||
waves.get(i).update();
|
||||
if(waves.get(i).isWaveOver()) {
|
||||
System.out.println("in remove");
|
||||
wavesToRemove.add(waves.get(i));
|
||||
}
|
||||
}
|
||||
waves.removeAll(wavesToRemove);
|
||||
|
||||
protected abstract void launchWave(long time);
|
||||
|
||||
private boolean isGameOver(){
|
||||
if(numOfPlayers == 1) {
|
||||
return players.get(0).isGameOver();
|
||||
}else{
|
||||
return players.get(0).isGameOver() && players.get(1).isGameOver();
|
||||
}
|
||||
}
|
||||
|
||||
private void movePlayers() {
|
||||
/**
|
||||
* 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()){
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()){
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(JPanel p) {
|
||||
sceneImage = new BufferedImage(this.stageWidth, this.stageHeight, Image.SCALE_FAST);
|
||||
Graphics g = sceneImage.getGraphics();
|
||||
|
||||
bg.drawSprite(g, p);
|
||||
Color c = g.getColor();
|
||||
Font f = engine.getGameFont();
|
||||
|
||||
if(!isStarted){
|
||||
if(f == null) {
|
||||
f = g.getFont();
|
||||
}
|
||||
f = f.deriveFont(60F);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.setFont(f);
|
||||
if(engine.getP1Score() > engine.getP1HighScore())
|
||||
engine.setP1HighScore(engine.getP1Score());
|
||||
if(engine.getP2Score() > engine.getP2HighScore())
|
||||
engine.setP2HighScore(engine.getP2Score());
|
||||
}
|
||||
checkCollision();
|
||||
engine.setGameOver(isGameOver());
|
||||
if(currentWave >= numOfWaves && waves.size()==0)
|
||||
{
|
||||
System.out.println("Done");
|
||||
engine.changeLevel();
|
||||
}
|
||||
|
||||
|
||||
// Get the FontMetrics
|
||||
FontMetrics metrics = g.getFontMetrics(f);
|
||||
// Determine the X coordinate for the text
|
||||
int x = (stageWidth - metrics.stringWidth(this.title)) / 2;
|
||||
// Determine the Y coordinate for the text
|
||||
int y = ((stageHeight - metrics.getHeight()) / 2) - metrics.getAscent();
|
||||
g.drawString(this.title, x, y);
|
||||
g.setColor(c);
|
||||
}
|
||||
|
||||
}
|
||||
protected abstract void launchWave(long time);
|
||||
|
||||
//print score
|
||||
f = f.deriveFont(15F);
|
||||
g.setFont(f);
|
||||
private boolean isGameOver(){
|
||||
if(numOfPlayers == 1) {
|
||||
return players.get(0).isGameOver();
|
||||
}else{
|
||||
return players.get(0).isGameOver() && players.get(1).isGameOver();
|
||||
}
|
||||
}
|
||||
|
||||
//print life bar
|
||||
for(int i=0; i<players.size(); i++){
|
||||
private void movePlayers() {
|
||||
/**
|
||||
* 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(engine.getP1Health() <= 0 && engine.getCredits() > 0) {
|
||||
engine.revivePlayer(0);
|
||||
players.get(0).resetPlayer();
|
||||
|
||||
g.drawRect(15,35*(i+1)+10,100,10);
|
||||
g.setColor(Color.GREEN);
|
||||
}
|
||||
}
|
||||
|
||||
if (i==0 && engine.getP1Health()<=30)
|
||||
g.setColor(Color.RED);
|
||||
else if( i==1 && engine.getP2Health()<=30)
|
||||
g.setColor(Color.RED);
|
||||
/**
|
||||
* 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(engine.getP2Health() <= 0 && engine.getCredits() > 0) {
|
||||
engine.revivePlayer(1);
|
||||
players.get(0).resetPlayer();
|
||||
|
||||
g.fillRect(15,35*(i+1)+10,((i == 0) ? engine.getP1Health() : engine.getP2Health()),10);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(JPanel p) {
|
||||
sceneImage = new BufferedImage(this.stageWidth, this.stageHeight, Image.SCALE_FAST);
|
||||
Graphics g = sceneImage.getGraphics();
|
||||
|
||||
bg.drawSprite(g, p);
|
||||
Color c = g.getColor();
|
||||
Font f = engine.getGameFont();
|
||||
|
||||
if(!isStarted){
|
||||
if(f == null) {
|
||||
f = g.getFont();
|
||||
}
|
||||
f = f.deriveFont(60F);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.setFont(f);
|
||||
|
||||
|
||||
//print credits
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString("Credits: "+ engine.getCredits(), stageWidth/2, 30);
|
||||
// Get the FontMetrics
|
||||
FontMetrics metrics = g.getFontMetrics(f);
|
||||
// Determine the X coordinate for the text
|
||||
int x = (stageWidth - metrics.stringWidth(this.title)) / 2;
|
||||
// Determine the Y coordinate for the text
|
||||
int y = ((stageHeight - metrics.getHeight()) / 2) - metrics.getAscent();
|
||||
g.drawString(this.title, x, y);
|
||||
g.setColor(c);
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (isGameOver())
|
||||
{
|
||||
try {
|
||||
System.in.read();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//print score
|
||||
f = f.deriveFont(15F);
|
||||
g.setFont(f);
|
||||
|
||||
//print life bar
|
||||
for(int i=0; i<players.size(); i++){
|
||||
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawRect(15,35*(i+1)+10,100,10);
|
||||
|
||||
g.setColor(Color.GREEN);
|
||||
|
||||
if (i==0 && engine.getP1Health()<=30)
|
||||
g.setColor(Color.RED);
|
||||
else if( i==1 && engine.getP2Health()<=30)
|
||||
g.setColor(Color.RED);
|
||||
|
||||
g.fillRect(15,35*(i+1)+10,((i == 0) ? engine.getP1Health() : engine.getP2Health()),10);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//print credits
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString("Credits: "+ engine.getCredits(), stageWidth/2, 30);
|
||||
|
||||
|
||||
|
||||
|
||||
for(int i = 0 ; i < p1Missiles.size() ; i++){
|
||||
p1Missiles.get(i).drawSprite(g,p);
|
||||
}
|
||||
for(int i = 0 ; i < p2Missiles.size() ; i++){
|
||||
p2Missiles.get(i).drawSprite(g,p);
|
||||
}
|
||||
for(int i = 0 ; i < enemyMissiles.size() ; i++){
|
||||
enemyMissiles.get(i).drawSprite(g,p);
|
||||
}
|
||||
for(int i = 0 ; i < players.size() ; i++){
|
||||
if(i == 0){
|
||||
if(engine.getP1Health() > 0){
|
||||
players.get(i).drawSprite(g,p);
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString("Player "+ (i+1) +" - Lives: " + ((i == 0) ? engine.getP1Lives() : engine.getP2Lives())+ ", Score: " + ((i == 0) ? engine.getP1Score() : engine.getP2Score()), 15, 35*(i+1));
|
||||
}else{
|
||||
if(players.get(i).isGameOver()){
|
||||
renderGameOver(g, p, i);
|
||||
}else{
|
||||
renderPressStart(g, p, i);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(engine.getP2Health() > 0){
|
||||
players.get(i).drawSprite(g,p);
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString("Player "+ (i+1) +" - Lives: " + ((i == 0) ? engine.getP1Lives() : engine.getP2Lives())+ ", Score: " + ((i == 0) ? engine.getP1Score() : engine.getP2Score()), 15, 35*(i+1));
|
||||
}else{
|
||||
if(players.get(i).isGameOver()){
|
||||
renderGameOver(g, p, i);
|
||||
}else{
|
||||
renderPressStart(g, p, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i = 0 ; i < waves.size() ; i++){
|
||||
waves.get(i).render(g,p);
|
||||
}
|
||||
for(int i = 0 ; i < p1Missiles.size() ; i++){
|
||||
p1Missiles.get(i).drawSprite(g,p);
|
||||
}
|
||||
for(int i = 0 ; i < p2Missiles.size() ; i++){
|
||||
p2Missiles.get(i).drawSprite(g,p);
|
||||
}
|
||||
for(int i = 0 ; i < enemyMissiles.size() ; i++){
|
||||
enemyMissiles.get(i).drawSprite(g,p);
|
||||
}
|
||||
for(int i = 0 ; i < players.size() ; i++){
|
||||
if(i == 0){
|
||||
if(engine.getP1Health() > 0){
|
||||
players.get(i).drawSprite(g,p);
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString("Player "+ (i+1) +" - Lives: " + ((i == 0) ? engine.getP1Lives() : engine.getP2Lives())+ ", Score: " + ((i == 0) ? engine.getP1Score() : engine.getP2Score()), 15, 35*(i+1));
|
||||
}else{
|
||||
if(players.get(i).isGameOver()){
|
||||
renderGameOver(g, p, i);
|
||||
}else{
|
||||
renderPressStart(g, p, i);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(engine.getP2Health() > 0){
|
||||
players.get(i).drawSprite(g,p);
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString("Player "+ (i+1) +" - Lives: " + ((i == 0) ? engine.getP1Lives() : engine.getP2Lives())+ ", Score: " + ((i == 0) ? engine.getP1Score() : engine.getP2Score()), 15, 35*(i+1));
|
||||
}else{
|
||||
if(players.get(i).isGameOver()){
|
||||
renderGameOver(g, p, i);
|
||||
}else{
|
||||
renderPressStart(g, p, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i = 0 ; i < waves.size() ; i++){
|
||||
waves.get(i).render(g,p);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected void renderPressStart(Graphics g, JPanel p, int i){
|
||||
long now = System.currentTimeMillis();
|
||||
if(now - lastPulseTime >= PRESS_START_PULE * 1000){
|
||||
toDrawStart = !toDrawStart;
|
||||
lastPulseTime = now;
|
||||
}
|
||||
if(i == 0){
|
||||
g.drawString(engine.getP1CreditTime()+"", 15, 35 * (i + 1));
|
||||
if(engine.getP1CreditTime() <= 0){
|
||||
players.get(i).setGameOver(true);
|
||||
}
|
||||
}else{
|
||||
g.drawString(engine.getP2CreditTime()+"", 15, 35 * (i + 1));
|
||||
if(engine.getP2CreditTime() <= 0){
|
||||
players.get(i).setGameOver(true);
|
||||
}
|
||||
protected void renderPressStart(Graphics g, JPanel p, int i){
|
||||
long now = System.currentTimeMillis();
|
||||
if(now - lastPulseTime >= PRESS_START_PULE * 1000){
|
||||
toDrawStart = !toDrawStart;
|
||||
lastPulseTime = now;
|
||||
}
|
||||
if(i == 0){
|
||||
g.drawString(engine.getP1CreditTime()+"", 15, 35 * (i + 1));
|
||||
if(engine.getP1CreditTime() <= 0){
|
||||
players.get(i).setGameOver(true);
|
||||
}
|
||||
}else{
|
||||
g.drawString(engine.getP2CreditTime()+"", 15, 35 * (i + 1));
|
||||
if(engine.getP2CreditTime() <= 0){
|
||||
players.get(i).setGameOver(true);
|
||||
}
|
||||
|
||||
}
|
||||
if(toDrawStart) {
|
||||
g.drawString("PRESS START", 45, 35 * (i + 1));
|
||||
}
|
||||
if(toDrawStart) {
|
||||
g.drawString("PRESS START", 45, 35 * (i + 1));
|
||||
|
||||
}
|
||||
players.get(i).setLocY(-500);
|
||||
players.get(i).setLocX(-500);
|
||||
}
|
||||
}
|
||||
players.get(i).setLocY(-500);
|
||||
players.get(i).setLocX(-500);
|
||||
}
|
||||
|
||||
protected void renderGameOver(Graphics g, JPanel p, int i){
|
||||
long now = System.currentTimeMillis();
|
||||
players.get(i).setGameOver(true);
|
||||
if(now - lastPulseTime >= PRESS_START_PULE * 1000){
|
||||
toDrawStart = !toDrawStart;
|
||||
lastPulseTime = now;
|
||||
}
|
||||
if(toDrawStart) {
|
||||
g.drawString("P" + (i+1) + " GAME OVER", 15, 35 * (i + 1));
|
||||
}
|
||||
}
|
||||
protected void renderGameOver(Graphics g, JPanel p, int i){
|
||||
System.out.println("HERE");
|
||||
long now = System.currentTimeMillis();
|
||||
players.get(i).setGameOver(true);
|
||||
if(now - lastPulseTime >= PRESS_START_PULE * 1000){
|
||||
toDrawStart = !toDrawStart;
|
||||
lastPulseTime = now;
|
||||
}
|
||||
if(toDrawStart) {
|
||||
g.drawString("P" + (i+1) + " GAME OVER", 15, 35 * (i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
public void checkCollision() {
|
||||
Vector<Missile> p1MTR, p2MTR, eMTR;
|
||||
eMTR = new Vector<>();
|
||||
p1MTR = new Vector<>();
|
||||
p2MTR = new Vector<>();
|
||||
public void checkCollision() {
|
||||
Vector<Missile> p1MTR, p2MTR, eMTR;
|
||||
eMTR = new Vector<>();
|
||||
p1MTR = new Vector<>();
|
||||
p2MTR = new Vector<>();
|
||||
|
||||
//for each player check collisions
|
||||
for (int i = 0; i < players.size(); i++) {
|
||||
//for each player check collisions
|
||||
for (int i = 0; i < players.size(); i++) {
|
||||
|
||||
//player vs. enemy missile
|
||||
for (int j = 0; j < enemyMissiles.size(); j++) {
|
||||
if(CollisionUtil.collidesWith(players.get(i),enemyMissiles.get(j))){
|
||||
playerHit(i);
|
||||
if(playerIsAlive(i)) {
|
||||
eMTR.add(enemyMissiles.get(j));
|
||||
}
|
||||
System.out.println("Hit Missile");
|
||||
}
|
||||
}
|
||||
//player vs. enemy missile
|
||||
for (int j = 0; j < enemyMissiles.size(); j++) {
|
||||
if(CollisionUtil.collidesWith(players.get(i),enemyMissiles.get(j))){
|
||||
playerHit(i);
|
||||
if(playerIsAlive(i)) {
|
||||
eMTR.add(enemyMissiles.get(j));
|
||||
}
|
||||
System.out.println("Hit Missile");
|
||||
}
|
||||
}
|
||||
|
||||
//player vs. enemy ship
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
// Ship hits enemy
|
||||
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
|
||||
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), players.get(i))) {
|
||||
if(!waves.get(j).getEnemies().get(k).isDead()){
|
||||
playerHit(i);
|
||||
}
|
||||
if(playerIsAlive(i)) {
|
||||
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//player vs. enemy ship
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
// Ship hits enemy
|
||||
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
|
||||
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), players.get(i))) {
|
||||
if(!waves.get(j).getEnemies().get(k).isDead()){
|
||||
playerHit(i);
|
||||
}
|
||||
if(playerIsAlive(i)) {
|
||||
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//player 1 missile vs. enemy
|
||||
if(i == 0){
|
||||
for(int m = 0 ; m < p1Missiles.size() ; m++){
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
|
||||
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), p1Missiles.get(m))) {
|
||||
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); //remove enemy life
|
||||
//player 1 missile vs. enemy
|
||||
if(i == 0){
|
||||
for(int m = 0 ; m < p1Missiles.size() ; m++){
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
|
||||
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), p1Missiles.get(m))) {
|
||||
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); //remove enemy life
|
||||
|
||||
if (waves.get(j).getEnemies().get(k).isDead()) //if enemy is dead
|
||||
engine.setScore(i,10);
|
||||
p1MTR.add(p1Missiles.get(m));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (waves.get(j).getEnemies().get(k).isDead()) //if enemy is dead
|
||||
engine.setScore(i,10);
|
||||
p1MTR.add(p1Missiles.get(m));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//player 1 missile vs. enemy
|
||||
else {
|
||||
for(int m = 0 ; m < p2Missiles.size() ; m++){
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
|
||||
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), p2Missiles.get(m))) {
|
||||
//player 1 missile vs. enemy
|
||||
else {
|
||||
for(int m = 0 ; m < p2Missiles.size() ; m++){
|
||||
for (int j = 0; j < waves.size(); j++) {
|
||||
for (int k = 0; k < waves.get(j).getEnemies().size(); k++) {
|
||||
if (CollisionUtil.collidesWith(waves.get(j).getEnemies().get(k), p2Missiles.get(m))) {
|
||||
|
||||
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); //remove enemy life
|
||||
if (waves.get(j).getEnemies().get(k).isDead()) //if enemy is dead
|
||||
engine.setScore(i,10);
|
||||
p2MTR.add(p2Missiles.get(m));
|
||||
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k)); //remove enemy life
|
||||
if (waves.get(j).getEnemies().get(k).isDead()) //if enemy is dead
|
||||
engine.setScore(i,10);
|
||||
p2MTR.add(p2Missiles.get(m));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
p1Missiles.removeAll(p1MTR);
|
||||
p2Missiles.removeAll(p2MTR);
|
||||
enemyMissiles.removeAll(eMTR);
|
||||
p1Missiles.removeAll(p1MTR);
|
||||
p2Missiles.removeAll(p2MTR);
|
||||
enemyMissiles.removeAll(eMTR);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean playerIsAlive(int i){
|
||||
if(i == 0){
|
||||
return !(engine.getP1Lives() <= 0);
|
||||
}else{
|
||||
return !(engine.getP2Lives() <= 0);
|
||||
}
|
||||
}
|
||||
protected boolean playerIsAlive(int i){
|
||||
if(i == 0){
|
||||
return !(engine.getP1Lives() <= 0);
|
||||
}else{
|
||||
return !(engine.getP2Lives() <= 0);
|
||||
}
|
||||
}
|
||||
|
||||
protected void playerHit(int i){
|
||||
if(players.get(i).isMortal()){
|
||||
engine.setPlayerHealth(i, -10);
|
||||
if(i == 0){
|
||||
if(engine.getP1Health() == 100){
|
||||
players.get(i).resetPlayer();
|
||||
}
|
||||
}else{
|
||||
if(engine.getP2Health() == 100){
|
||||
players.get(i).resetPlayer();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
protected void playerHit(int i){
|
||||
if(players.get(i).isMortal()){
|
||||
engine.setPlayerHealth(i, -10);
|
||||
if(i == 0){
|
||||
if(engine.getP1Health() == 100){
|
||||
players.get(i).resetPlayer();
|
||||
}
|
||||
}else{
|
||||
if(engine.getP2Health() == 100){
|
||||
players.get(i).resetPlayer();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void enemyFire(int x, int y, int acc) {
|
||||
enemyMissiles.add(new Missile(x, y, acc,"E1-Fire.png", 15));
|
||||
}
|
||||
public void enemyFire(int x, int y, int acc) {
|
||||
enemyMissiles.add(new Missile(x, y, acc,"E1-Fire.png", 15));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent keyEvent) {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent keyEvent) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent keyEvent) {
|
||||
if(isStarted)
|
||||
keys.put(keyEvent.getKeyCode(), true);
|
||||
@Override
|
||||
public void keyPressed(KeyEvent keyEvent) {
|
||||
if(isStarted)
|
||||
keys.put(keyEvent.getKeyCode(), true);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent keyEvent) {
|
||||
if(isStarted)
|
||||
keys.put(keyEvent.getKeyCode(), false);
|
||||
}
|
||||
@Override
|
||||
public void keyReleased(KeyEvent keyEvent) {
|
||||
if(isStarted)
|
||||
keys.put(keyEvent.getKeyCode(), false);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
package com.sagi.dayan.Games.Stage;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import sun.audio.AudioPlayer;
|
||||
import sun.audio.AudioStream;
|
||||
|
||||
import com.sagi.dayan.Games.Elements.MenuBoxSprite;
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
import com.sagi.dayan.Games.Utils.Utils;
|
||||
import sun.audio.AudioPlayer;
|
||||
import sun.audio.AudioStream;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/24/16.
|
||||
|
@ -51,6 +55,15 @@ public class MainMenuScene extends Scene {
|
|||
Graphics g = sceneImage.getGraphics();
|
||||
g.drawImage(menuBox.getbImage(), 0, 0, p);
|
||||
menuBox.drawSprite(g, p);
|
||||
|
||||
Font f = engine.getGameFont();
|
||||
f = f.deriveFont(25F);
|
||||
g.setFont(f);
|
||||
g.drawString("Player1 record: " +engine.p1HighScore, 700, 50);
|
||||
g.drawString("Player2 record: " +engine.p2HighScore, 700, 100);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -4,15 +4,14 @@ package com.sagi.dayan.Games.Stage;
|
|||
* Created by sagi on 2/8/16.
|
||||
*/
|
||||
|
||||
import com.sagi.dayan.Games.Elements.*;
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
|
||||
public abstract class Scene extends MouseAdapter implements KeyListener{
|
||||
|
||||
public int getStageWidth() {
|
||||
|
|
63
src/com/sagi/dayan/Games/Stage/SecondStage.java
Normal file
63
src/com/sagi/dayan/Games/Stage/SecondStage.java
Normal file
|
@ -0,0 +1,63 @@
|
|||
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 SecondStage extends Level{
|
||||
|
||||
protected final int NUM_OF_WAVES = 4;
|
||||
|
||||
public SecondStage(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay) {
|
||||
super(width, height, numOfPlayers, engine, stageTitle, waveDelay);
|
||||
numOfWaves = NUM_OF_WAVES;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void launchWave(long now) {
|
||||
lastWaveTime = now;
|
||||
System.out.println("New Wave!! " + currentWave + ", Time: " + now);
|
||||
WaveConfig wc;
|
||||
int numOfEnemies = 5, numOfHits = 1;
|
||||
double launchDelay = 0.5, fireDelay = 5;
|
||||
switch (currentWave){
|
||||
case 0:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 0.5;
|
||||
launchDelay = 0.5;
|
||||
numOfHits = 1;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(6);
|
||||
break;
|
||||
case 1:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 0.3;
|
||||
launchDelay = 1;
|
||||
numOfHits = 2;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(3);
|
||||
break;
|
||||
case 2:
|
||||
numOfEnemies = 10;
|
||||
fireDelay = 0.3;
|
||||
launchDelay = 1;
|
||||
numOfHits = 3;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(2);
|
||||
break;
|
||||
case 3:
|
||||
numOfEnemies = 15;
|
||||
fireDelay = 0.2;
|
||||
launchDelay = 1;
|
||||
numOfHits = 5;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(7);
|
||||
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++;
|
||||
}
|
||||
}
|
|
@ -1,17 +1,19 @@
|
|||
package com.sagi.dayan.Games.Stage;
|
||||
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
import com.sagi.dayan.Games.Utils.Utils;
|
||||
import sun.audio.AudioPlayer;
|
||||
import sun.audio.AudioStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
import com.sagi.dayan.Games.Utils.Utils;
|
||||
//import sun.audio.AudioPlayer;
|
||||
//import sun.audio.AudioStream;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/27/16.
|
||||
*/
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
package com.sagi.dayan.Games.Stage;
|
||||
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
import com.sagi.dayan.Games.Utils.Utils;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferStrategy;
|
||||
import java.awt.Graphics;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/8/16.
|
||||
*/
|
||||
|
|
63
src/com/sagi/dayan/Games/Stage/ThirdStage.java
Normal file
63
src/com/sagi/dayan/Games/Stage/ThirdStage.java
Normal file
|
@ -0,0 +1,63 @@
|
|||
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 ThirdStage extends Level{
|
||||
|
||||
protected final int NUM_OF_WAVES = 4;
|
||||
|
||||
public ThirdStage(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay) {
|
||||
super(width, height, numOfPlayers, engine, stageTitle, waveDelay);
|
||||
numOfWaves = NUM_OF_WAVES;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void launchWave(long now) {
|
||||
lastWaveTime = now;
|
||||
System.out.println("New Wave!! " + currentWave + ", Time: " + now);
|
||||
WaveConfig wc;
|
||||
int numOfEnemies = 5, numOfHits = 1;
|
||||
double launchDelay = 0.5, fireDelay = 5;
|
||||
switch (currentWave){
|
||||
case 0:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 0.5;
|
||||
launchDelay = 0.5;
|
||||
numOfHits = 1;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(6);
|
||||
break;
|
||||
case 1:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 0.3;
|
||||
launchDelay = 1;
|
||||
numOfHits = 2;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(3);
|
||||
break;
|
||||
case 2:
|
||||
numOfEnemies = 10;
|
||||
fireDelay = 0.3;
|
||||
launchDelay = 1;
|
||||
numOfHits = 3;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(2);
|
||||
break;
|
||||
case 3:
|
||||
numOfEnemies = 15;
|
||||
fireDelay = 0.2;
|
||||
launchDelay = 1;
|
||||
numOfHits = 5;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(7);
|
||||
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++;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
package com.sagi.dayan.Games.Utils;
|
||||
|
||||
import sun.audio.AudioPlayer;
|
||||
import sun.audio.AudioStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
import sun.audio.AudioPlayer;
|
||||
import sun.audio.AudioStream;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/24/16.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue