Added Enemy Waves - And a start for collision detection

This commit is contained in:
Sagi Dayan 2016-03-12 12:24:42 +02:00
parent d9085b8570
commit d5ca3d4dd9
19 changed files with 338 additions and 329 deletions

View file

@ -1,55 +0,0 @@
package com.sagi.dayan.Games.Elements;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created by sagi on 2/10/16.
*/
public class AnimatedDemoSprite extends AnimatedSprite {
private int timerCouner = 0;
protected Timer t;
public AnimatedDemoSprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight);
setScreenLoop(true);
animations.add(new Animation("animatedSample2.png", 16, 2000));
t = new Timer(1 * 1000, new TimerTick());
t.start();
}
@Override
public void update() {
locX += acceleration;
}
@Override
protected void initFirstAnimation(String spriteSheet) {
animations.add(new Animation(spriteSheet, 8, 1 * 1000));
}
private class TimerTick implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(timerCouner < 5){
timerCouner++;
}
else if(5 <= timerCouner && timerCouner < 7){
System.out.println("Explode");
timerCouner++;
currentAnimation = 1;
acceleration = 0;
}else{
currentAnimation = 0;
timerCouner = 0;
acceleration = 5;
}
}
}
}

View file

@ -6,6 +6,7 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.IOException;
import java.util.Vector;
@ -17,15 +18,17 @@ public abstract class AnimatedSprite extends Sprite {
protected int currentAnimation;
public AnimatedSprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight) {
public AnimatedSprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, int numOfFirstFrames) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight);
animations = new Vector<>();
initFirstAnimation(imgName);
initFirstAnimation(imgName, numOfFirstFrames);
currentAnimation = 0;
}
protected abstract void initFirstAnimation(String spriteSheet);
protected abstract void initFirstAnimation(String spriteSheet, int numOfFirstFrames);
@Override
public void drawSprite(Graphics g, JPanel p) {
@ -57,6 +60,9 @@ public abstract class AnimatedSprite extends Sprite {
}
}
public BufferedImage getImageFrame() {
return animations.get(currentAnimation).getCurrentFrame();
}
protected class Animation {
@ -82,6 +88,8 @@ public abstract class AnimatedSprite extends Sprite {
}
int frameHeight = spriteSheet.getHeight();
int frameWidth = spriteSheet.getWidth() / numOfFrames;
sWidth = frameWidth;
sHeight = frameHeight;
int currentX = 0;
for(int i = 0 ; i < numOfFrames ; i++) {
addFrame(spriteSheet.getSubimage(currentX, 0, frameWidth, frameHeight), (double)totalAnimationTime/numOfFrames);

View file

@ -1,64 +0,0 @@
package com.sagi.dayan.Games.Elements;
import java.util.Random;
/**
* Created by sagi on 2/10/16.
*/
public class DemoSprite extends Sprite {
private final int STOP=0, UP=1, DOWN=-1, TURN_SPEED=10;
private final double MAX_SPEED = 6, SLOWING_FACTOR = 0.1;
Random r;
private double selfAccel;
private int direction, turnDirection;
int counter = 0;
public DemoSprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight);
selfRotationSpeed = 5;
direction = STOP;
turnDirection = STOP;
selfAccel = acceleration;
setScreenLoop(true);
}
@Override
public void update() {
setSpeed();
this.angle+=TURN_SPEED*turnDirection;
locX += selfAccel * Math.cos(Math.toRadians(angle));
locY -= selfAccel * (-1 * Math.sin(Math.toRadians(angle)));
System.out.println("locX: " + locX + "\tlocY: "+locY);
}
private void setSpeed(){
if (direction == UP && !(selfAccel > MAX_SPEED)){
selfAccel+=SLOWING_FACTOR*2;
}
else if (direction == DOWN && (selfAccel > MAX_SPEED*(-1))){
selfAccel-=SLOWING_FACTOR*2;
}
else { //slowing down
if (selfAccel > 0) {
selfAccel -= SLOWING_FACTOR;
if (selfAccel < 0) {
selfAccel = 0;
}
}
if (selfAccel < 0) {
selfAccel += SLOWING_FACTOR;
if (selfAccel > 0) {
selfAccel = 0;
}
}
}
}
public void setDirection(int direction) {
this.direction = direction;
}
public void turnShip(int direction){
turnDirection=direction;
}
}

View file

@ -1,6 +1,7 @@
package com.sagi.dayan.Games.Elements;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -9,23 +10,68 @@ import java.awt.event.ActionListener;
* Created by sagi on 2/20/16.
*/
public class EnemyShip extends AnimatedSprite {
Timer t;
public EnemyShip(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, ActionListener timerListener) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight);
t = new Timer(1000,timerListener);
protected int fireDelay, stepDelay, currentStep;
protected long lastFireTime, lastStepTime;
protected int[] moveVector;
protected Wave wave;
protected int hitsToDestroy;
protected boolean isDone;
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) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight, numOfFirstFrames);
this.fireDelay = fireDelay;
this.stepDelay = stepDelay;
this.currentStep = 0;
this.lastFireTime = System.currentTimeMillis();
this.lastStepTime = System.currentTimeMillis();
this.moveVector = moveVector;
this.wave = wave;
this.hitsToDestroy = hitsToDestroy;
}
@Override
protected void initFirstAnimation(String spriteSheet) {
animations.add(new Animation("P1Laser.png", 4, 500));
protected void initFirstAnimation(String spriteSheet, int numOfFirstFrames) {
animations.add(new Animation(imageName, 8, 500));
}
@Override
public void update() {
locX = 200;
locY += acceleration;
long now = System.currentTimeMillis();
if(currentAnimation == 1){
if(now - startExploded >= 500){
isDone = true;
}
}
if(now - lastFireTime >= fireDelay*1000){
wave.fireFromEnemy(this);
lastFireTime = now;
}
if(now - lastStepTime >= stepDelay*1000 && currentStep < moveVector.length - 1){
currentStep++;
lastStepTime = now;
}
locX += acceleration * Math.cos(Math.toRadians(moveVector[currentStep]));
locY -= acceleration * -1* Math.sin(Math.toRadians(moveVector[currentStep]));
}
public void gotHit() {
hitsToDestroy--;
if(hitsToDestroy == 0){
startExploded = System.currentTimeMillis();
animations.add(new Animation("explosion.png", 16, 500));
currentAnimation++;
}
System.out.println("GOT HIT " + hitsToDestroy);
}
public int getHitToDestroy() {
return hitsToDestroy;
}
public boolean isDone(){
return isDone;
}

View file

@ -4,12 +4,12 @@ package com.sagi.dayan.Games.Elements;
* Created by sagi on 2/24/16.
*/
public class MenuBoxSprite extends AnimatedSprite {
public MenuBoxSprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight);
public MenuBoxSprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, int numOfFirstFrames) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight, numOfFirstFrames);
}
@Override
protected void initFirstAnimation(String spriteSheet) {
protected void initFirstAnimation(String spriteSheet, int numOfFirstFrames) {
animations.add(new Animation("menuBox.png", 15, 150));
}

View file

@ -6,13 +6,14 @@ package com.sagi.dayan.Games.Elements;
public class Missile extends AnimatedSprite {
public Missile(int x, int y, int acc, String imgName) {
super(x, y, 0, 0, acc, imgName, 0, 15, 15);
public Missile(int x, int y, int acc, String imgName, int numOfFrames) {
super(x, y, 0, 0, acc, imgName, 0, 15, 15, numOfFrames);
}
@Override
protected void initFirstAnimation(String spriteSheet) {
animations.add(new Animation("P1Laser.png", 4, 500));
protected void initFirstAnimation(String spriteSheet, int numOfFirstFrames) {
animations.add(new Animation(imageName, numOfFirstFrames, 500));
}
@Override

View file

@ -12,17 +12,18 @@ public class Player extends AnimatedSprite {
private long lastFired;
public Player(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, String imagePrefix) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight);
public Player(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight, String imagePrefix, int numOfFirstFrames) {
super(x, y, w, h, acc, imgName, angle, sWidth, sHeight, numOfFirstFrames);
this.imagePrefix = imagePrefix;
initFirstAnimation("");
initFirstAnimation("", numOfFirstFrames);
this.ableToFire = true;
fireDelay = 100;
lastFired = System.currentTimeMillis();
setImageDimensions();
}
@Override
protected void initFirstAnimation(String spriteSheet) {
protected void initFirstAnimation(String spriteSheet, int numOfFirstFrames) {
if(imagePrefix == null)
return;
System.out.println(imagePrefix);

View file

@ -14,6 +14,7 @@ public abstract class Sprite {
protected BufferedImage bImage;
protected int imageWidth, imageHeight; // image dimensions
protected URL imagePath;
protected String imageName;
protected int locX, locY;
protected int acceleration;
protected int pWidth, pHeight; // panel's dimensions
@ -27,6 +28,7 @@ public abstract class Sprite {
public Sprite(int x, int y, int w, int h, int acc, String imgName, double angle, int sWidth, int sHeight) {
this.imagePath = getClass().getResource("/com/sagi/dayan/Games/Images/" + imgName);
this.imageName = imgName;
this.sWidth = sWidth;
this.sHeight = sHeight;
locX = x;
@ -165,7 +167,7 @@ public abstract class Sprite {
* @return Rectangle
*/
public Rectangle getBounds() {
return new Rectangle((int) locX, (int) locY, sWidth, sHeight);
return new Rectangle((int) locX, (int) locY, bImage.getWidth(), bImage.getHeight());
}
public boolean isScreenLoop() {
@ -258,4 +260,12 @@ public abstract class Sprite {
}
}
public boolean isOutOfScreen() {
if (this.getLocX() + this.sWidth < 0 || this.getLocX() - this.sWidth > pHeight || this.getLocY() + this.sHeight < 0 || this.getLocY() - sHeight > pHeight) {
return true;
} else {
return false;
}
}
}

View file

@ -0,0 +1,87 @@
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.util.Vector;
/**
* Created by sagi on 3/11/16.
*/
public class Wave {
protected Level level;
protected int enemyMaxAmount, currentAmount,fireDelay, launchDelay, stepDelay, acc, startX, startY;
protected int[] moveVector;
protected Vector<EnemyShip> enemies;
protected Vector<Missile> bullets;
protected long lastLaunchTime;
protected String imageName;
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){
this.enemies = new Vector<>();
this.bullets = new Vector<>();
this.enemyMaxAmount = enemyMaxAmount;
this.currentAmount = 0;
this.imageName = imageName;
this.level = stage;
this.fireDelay = fireDelay;
this.launchDelay = launchDelay;
this.acc = acc;
this.startX = startX;
this.startY = startY;
this.stepDelay = stepDelay;
this.moveVector = moveVector;
this.lastLaunchTime = System.currentTimeMillis();
this.hitsToDestroy = hitsToDestroy;
}
public void update(){
long now = System.currentTimeMillis();
Vector <EnemyShip> enemiesToRemove = new Vector<>();
if(now - lastLaunchTime >= launchDelay * 1000 && currentAmount <= enemyMaxAmount){
// Create new enemy
enemies.add(new EnemyShip(startX, startY, level.getStageHeight(), level.getStageHeight(), acc, imageName, 0, 15, 15, fireDelay, stepDelay, this, moveVector, 7, hitsToDestroy));
lastLaunchTime = now;
currentAmount++;
}
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));
}
}
for (int i = 0; i < bullets.size() ; i++){
bullets.get(i).update();
}
enemies.removeAll(enemiesToRemove);
}
public void render(Graphics g, JPanel p){
for (int i = 0; i < bullets.size() ; i++){
bullets.get(i).drawSprite(g, p);
}
for (int i = 0; i < enemies.size() ; i++){
enemies.get(i).drawSprite(g, p);
}
}
public void fireFromEnemy(EnemyShip e){
level.enemyFire(e.getCenterX(), (int)(e.getLocY() + e.getsHeight()), -(e.getAcceleration() + 2));
}
public Vector <EnemyShip> getEnemies() {
return enemies;
}
public void enemyHit(EnemyShip es) {
es.gotHit();
}
public boolean isWaveOver() {
return enemies.size() == 0 && currentAmount == enemyMaxAmount;
}
}

View file

@ -7,6 +7,7 @@ package com.sagi.dayan.Games.Engine;
* Created by sagi on 12/19/15.
*/
import com.sagi.dayan.Games.Elements.AnimatedSprite;
import com.sagi.dayan.Games.Elements.Sprite;
import java.awt.*;
@ -78,8 +79,8 @@ public class CollisionUtil {
int[] pixels2 = new int[width*height];
//Create the pixelgrabber and fill the arrays
PixelGrabber pg1 = new PixelGrabber(sprite1.getbImage(), cornerTopX-(int)sprite1.getLocX(), cornerTopY-(int)sprite1.getLocY(), width, height, pixels1, 0, width);
PixelGrabber pg2 = new PixelGrabber(sprite2.getbImage(), cornerTopX-(int)sprite2.getLocX(), cornerTopY-(int)sprite2.getLocY(), width, height, pixels2, 0, width);
PixelGrabber pg1 = new PixelGrabber(((AnimatedSprite)sprite1).getImageFrame(), cornerTopX-(int)sprite1.getLocX(), cornerTopY-(int)sprite1.getLocY(), width, height, pixels1, 0, width);
PixelGrabber pg2 = new PixelGrabber(((AnimatedSprite)sprite2).getImageFrame(), cornerTopX-(int)sprite2.getLocX(), cornerTopY-(int)sprite2.getLocY(), width, height, pixels2, 0, width);
//Grab the pixels
try {

View file

@ -4,7 +4,6 @@ package com.sagi.dayan.Games.Engine;
* Created by sagi on 2/8/16.
*/
import com.sagi.dayan.Games.Elements.*;
import com.sagi.dayan.Games.Stage.*;
import com.sagi.dayan.Games.Utils.Utils;
@ -36,6 +35,8 @@ public class GameEngine {
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 p1Strikes, p2Strikes, p1CurrentStrikes, p2CurentStrikes;
private Font gameFont;
public GameEngine(int width, int height, Stage stage){
@ -46,7 +47,7 @@ public class GameEngine {
this.pHeight = height;
this.scenes = new Vector<>();
this.stage = stage;
// scenes.add(new FirstStage(width, height, 2)); // Need to be a menu Scene
// scenes.add(new Level(width, height, 2)); // Need to be a menu Scene
scenes.add(new MainMenuScene(width, height, this));
stage.addKeyListener(scenes.get(currentScene));
stage.addMouseListener(scenes.get(currentScene));
@ -127,7 +128,7 @@ public class GameEngine {
public void startGame(int numOfPlayers){
this.numOfPlayers = numOfPlayers;
scenes.add(new FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-"));
scenes.add(new Level(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-", new int[]{5, 20}));
changeScene(currentScene+1);
}
@ -160,4 +161,19 @@ public class GameEngine {
public Font getGameFont() {
return gameFont;
}
public int getP1Strikes() {
return p1Strikes;
}
public int getP2Strikes() {
return p2Strikes;
}
public void setPlayerStrikes(int i, int strike) {
if (i == 0) {
p1Strikes += strike;
} else {
p2Strikes += strike;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

View file

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 48 KiB

View file

@ -20,7 +20,7 @@ public class Main {
public static final int WIDTH = 1000, HEIGHT = 1000;
public static void main(String[] args) {
JFrame frame = new JFrame("Game");
JFrame frame = new JFrame("Far Out - Cold Shit IV");
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

View file

@ -1,9 +1,7 @@
package com.sagi.dayan.Games.Stage;
import com.sagi.dayan.Games.Elements.Background;
import com.sagi.dayan.Games.Elements.EnemyShip;
import com.sagi.dayan.Games.Elements.Missile;
import com.sagi.dayan.Games.Elements.Player;
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;
@ -20,28 +18,35 @@ import java.util.*;
/**
* Created by sagi on 2/20/16.
*/
public class FirstStage extends Scene {
public class Level extends Scene {
protected Vector<Player> players;
protected int p1Speed = 10;
protected Vector<Missile> missiles;
protected Vector<Missile> p1Missiles, p2Missiles, enemyMissiles;
protected Background bg;
protected Timer enemyWaveT, enemyT;
protected Vector<EnemyShip> enemies;
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;
public FirstStage(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle){
public Level(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay){
super(width, height, engine);
players = new Vector<>();
missiles = 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)};
@ -52,19 +57,14 @@ public class FirstStage extends Scene {
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"));
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"));
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"));
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();
enemies = new Vector<>();
enemyWaveT = new Timer(10000, new enemyWaveLaunch());
enemyT = new Timer(10000, new enemyLaunch());
enemyWaveT.start();
Utils.playSound("jetSound.wav");
}
@ -86,6 +86,15 @@ public class FirstStage extends Scene {
bg.update();
movePlayers();
Vector <Wave> wavesToRemove = new Vector<Wave>();
long now = System.currentTimeMillis();
if(currentWave < waveDelay.length && now - lastWaveTime >= waveDelay[currentWave] * 1000){
lastWaveTime = 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 == 0){
@ -110,22 +119,30 @@ public class FirstStage extends Scene {
}
}
}else{
isStarted = true;
for(int i = 0 ; i < players.size() ; i++){
players.get(i).update();
}
for(int i = 0 ; i < missiles.size() ; i++){
missiles.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 < enemies.size() ; i++){
enemies.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();
}
private void movePlayers() {
@ -152,7 +169,7 @@ public class FirstStage extends Scene {
}
if(keys.get(engine.getP1Controlles()[GameEngine.FIRE]) ){
if(players.get(0).isAbleToFire()){
missiles.add(new Missile(players.get(0).getCenterX() - 15, (int)players.get(0).getLocY(), players.get(0).getAcceleration() + 3, "P1Laser.png"));
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();
}
}
@ -181,7 +198,7 @@ public class FirstStage extends Scene {
}
if(keys.get(engine.getP2Controlles()[GameEngine.FIRE]) ){
if(players.get(1).isAbleToFire()){
missiles.add(new Missile(players.get(1).getCenterX() - 15, (int)players.get(1).getLocY(), players.get(1).getAcceleration() + 3, "P1Laser.png"));
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();
}
}
@ -200,9 +217,9 @@ public class FirstStage extends Scene {
if(f == null) {
f = g.getFont();
}
f = f.deriveFont(60F);
g.setColor(Color.DARK_GRAY);
g.setFont(f);
f = f.deriveFont(60F);
g.setColor(Color.DARK_GRAY);
g.setFont(f);
// Get the FontMetrics
@ -216,16 +233,85 @@ public class FirstStage extends Scene {
}
for(int i = 0 ; i < missiles.size() ; i++){
missiles.get(i).drawSprite(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++){
players.get(i).drawSprite(g,p);
}
for(int i = 0 ; i < enemies.size() ; i++){
enemies.get(i).drawSprite(g,p);
for(int i = 0 ; i < waves.size() ; i++){
waves.get(i).render(g,p);
}
}
public void checkCollision() {
Vector<Missile> p1MTR, p2MTR, eMTR;
eMTR = new Vector<>();
p1MTR = new Vector<>();
p2MTR = new Vector<>();
for (int i = 0; i < players.size(); i++) {
for (int j = 0; j < enemyMissiles.size(); j++) {
if(CollisionUtil.collidesWith(players.get(i),enemyMissiles.get(j))){
//Remove players Life
eMTR.add(enemyMissiles.get(j));
System.out.println("Hit Missile");
}
}
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))) {
engine.setPlayerStrikes(i, -1);
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
System.out.println("PIN");
}
}
}
if(i == 0){
for(int m = 0 ; m < p1Missiles.size() ; m++){
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), p1Missiles.get(m))) {
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
p1MTR.add(p1Missiles.get(m));
}
}
}
}
}else{
for(int m = 0 ; m < p2Missiles.size() ; m++){
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), p2Missiles.get(m))) {
waves.get(j).enemyHit(waves.get(j).getEnemies().get(k));
p2MTR.add(p2Missiles.get(m));
}
}
}
}
}
}
p1Missiles.removeAll(p1MTR);
p2Missiles.removeAll(p2MTR);
enemyMissiles.removeAll(eMTR);
}
public void enemyFire(int x, int y, int acc) {
enemyMissiles.add(new Missile(x, y, acc,"E1-Fire.png", 15));
}
@Override
@ -246,29 +332,5 @@ public class FirstStage extends Scene {
keys.put(keyEvent.getKeyCode(), false);
}
private class fireTimer implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
missiles.add(new Missile(((EnemyShip)actionEvent.getSource()).getCenterX(), (int)((EnemyShip)actionEvent.getSource()).getLocY(), ((EnemyShip)actionEvent.getSource()).getAcceleration() + 3, "P1Laser.png"));
}
}
private class enemyWaveLaunch implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
}
}
private class enemyLaunch implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
enemies.add(new EnemyShip(0,0,0,0,3,"L1-ES1.png",0,15,15,new fireTimer()));
}
}
}

View file

@ -29,7 +29,7 @@ public class MainMenuScene extends Scene {
public MainMenuScene(int stageWidth, int stageHeight, GameEngine engine) {
super(stageWidth, stageHeight, engine);
menuItem = 0;
menuBox = new MenuBoxSprite(X_AXIS, axis[menuItem], stageWidth, stageHeight, 0, "menu.jpg", 0, 425, 110); //bImage is the background... not trivial
menuBox = new MenuBoxSprite(X_AXIS, axis[menuItem], stageWidth, stageHeight, 0, "menu.jpg", 0, 425, 110, 15); //bImage is the background... not trivial
try {
AudioPlayer.player.start(new AudioStream(Utils.getSoundResourceAsStream("intro_LowQuality.wav")));
} catch (IOException e) {

View file

@ -1,118 +0,0 @@
package com.sagi.dayan.Games.Stage;
import com.sagi.dayan.Games.Elements.*;
import com.sagi.dayan.Games.Engine.GameEngine;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
/**
* Created by sagi on 2/10/16.
*/
public class SampleScene extends Scene {
private final int STOP=0, UP=1, DOWN=-1, TURN_SPEED=10;
private DemoSprite sprite;
private AnimatedDemoSprite animated;
private int r = 0, g = 0, b = 0;
private boolean toWhite = true;
public SampleScene(int width, int height, GameEngine engine){
super(width, height, engine);
// sprite = new DemoSprite(50,50,width, height, 50);
animated = new AnimatedDemoSprite(width/2, height/2, width, height,5,"animatedSample.png", 0, 58,87);
}
@Override
public void update() {
sprite.update();
animated.update();
}
@Override
public void render(JPanel p) {
sceneImage = new BufferedImage(this.stageWidth, this.stageHeight, Image.SCALE_FAST);
Graphics g = sceneImage.getGraphics();
g.setColor(getColor());
g.fillRect(0,0,stageWidth, stageWidth);
sprite.drawSprite(g, p);
animated.drawSprite(g, p);
}
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
System.out.println("Pressed!");
if (keyEvent.getKeyCode() == keyEvent.VK_LEFT) {
sprite.turnShip(DOWN);
}
if (keyEvent.getKeyCode() == keyEvent.VK_RIGHT) {
sprite.turnShip(UP);
}
if (keyEvent.getKeyCode() == keyEvent.VK_UP) {
sprite.setDirection(UP);
}
if (keyEvent.getKeyCode() == keyEvent.VK_DOWN) {
sprite.setDirection(DOWN);
}
}
@Override
public void keyReleased(KeyEvent keyEvent) {
switch (keyEvent.getKeyCode()){
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
sprite.setDirection(STOP);
break;
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_LEFT:
sprite.turnShip(STOP);
break;
// case KeyEvent.VK_SPACE:
// laserAudioClip.stop();
// break;
default:
break;
}
}
public void mousePressed(MouseEvent e){
System.out.println("Mouse pressed");
}
private Color getColor() {
if(toWhite){
if(r < 225) {
r++;
} else if (g < 225) {
g++;
} else if ( b < 225) {
b++;
}else if (r == 225 && g ==225 && b == 225){
toWhite = false;
}
} else {
if(b > 0) {
b--;
} else if (g > 0) {
g--;
} else if ( r > 0) {
r--;
}else if (r == 0 && g ==0 && b == 0){
toWhite = true;
}
}
return new Color(r, g, b);
}
}

View file

@ -15,6 +15,14 @@ import java.awt.image.BufferedImage;
public abstract class Scene extends MouseAdapter implements KeyListener{
public int getStageWidth() {
return stageWidth;
}
public int getStageHeight() {
return stageHeight;
}
protected int stageWidth, stageHeight;
protected BufferedImage sceneImage;
protected GameEngine engine;
@ -33,4 +41,6 @@ public abstract class Scene extends MouseAdapter implements KeyListener{
return sceneImage;
}
}

View file

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