commit
d4dff0aafa
22 changed files with 812 additions and 535 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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,69 @@ 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 currentStep;
|
||||
protected double stepDelay, fireDelay;
|
||||
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,double fireDelay, double 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 boolean isDead() {
|
||||
return hitsToDestroy == 0;
|
||||
}
|
||||
|
||||
public boolean isDone(){
|
||||
return isDone;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,28 +1,39 @@
|
|||
package com.sagi.dayan.Games.Elements;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/20/16.
|
||||
*/
|
||||
public class Player extends AnimatedSprite {
|
||||
private final int NORMAL_ANIMATION = 0, RIGHT_ANIMATION = 1, LEFT_ANIMATION = 2, PADDING_BOTTOM = 35;
|
||||
private final int NORMAL_ANIMATION = 0, RIGHT_ANIMATION = 1, LEFT_ANIMATION = 2, PADDING_BOTTOM = 35, MORTAL_DELAY = 3;
|
||||
private int hDirection = 0, vDirection = 0;
|
||||
private String imagePrefix;
|
||||
private boolean ableToFire;
|
||||
private boolean ableToFire, isMortal, toDraw;
|
||||
private int fireDelay;
|
||||
private long lastFired;
|
||||
private double imortalPulse = 0.2;
|
||||
private long lastFired, lastDrawn, created;
|
||||
|
||||
|
||||
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;
|
||||
fireDelay = 200;
|
||||
lastFired = System.currentTimeMillis();
|
||||
lastDrawn = lastFired;
|
||||
created = lastDrawn;
|
||||
setImageDimensions();
|
||||
isMortal = false;
|
||||
toDraw = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initFirstAnimation(String spriteSheet) {
|
||||
protected void initFirstAnimation(String spriteSheet, int numOfFirstFrames) {
|
||||
if(imagePrefix == null)
|
||||
return;
|
||||
System.out.println(imagePrefix);
|
||||
|
@ -31,6 +42,10 @@ public class Player extends AnimatedSprite {
|
|||
animations.add(new Animation(imagePrefix+"LeftSheet.png", 7, 200));
|
||||
}
|
||||
|
||||
public boolean isMortal() {
|
||||
return isMortal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
long now = System.currentTimeMillis();
|
||||
|
@ -69,6 +84,7 @@ public class Player extends AnimatedSprite {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isAbleToFire() {
|
||||
return ableToFire;
|
||||
}
|
||||
|
@ -88,4 +104,23 @@ public class Player extends AnimatedSprite {
|
|||
public void updateFireTime(){
|
||||
lastFired = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawSprite(Graphics g, JPanel p){
|
||||
long now = System.currentTimeMillis();
|
||||
if(isMortal){
|
||||
super.drawSprite(g, p);
|
||||
}else{
|
||||
if(now - lastDrawn >= imortalPulse * 1000){
|
||||
toDraw = !toDraw;
|
||||
lastDrawn = now;
|
||||
if(now-created >= MORTAL_DELAY * 1000){
|
||||
isMortal = true;
|
||||
}
|
||||
}
|
||||
if(toDraw) {
|
||||
super.drawSprite(g, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
88
src/com/sagi/dayan/Games/Elements/Wave.java
Normal file
88
src/com/sagi/dayan/Games/Elements/Wave.java
Normal file
|
@ -0,0 +1,88 @@
|
|||
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, acc, startX, startY;
|
||||
protected double stepDelay,fireDelay, launchDelay;
|
||||
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, double fireDelay, double stepDelay, double 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;
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -4,9 +4,9 @@ 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;
|
||||
import com.sagi.dayan.Games.Utils.WaveConfigs;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
@ -36,8 +36,14 @@ 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 p1Lives, p2Lives, p1Health, p2Health, credits, p1Score, p2Score;
|
||||
|
||||
|
||||
|
||||
private Font gameFont;
|
||||
|
||||
private WaveConfigs waveConfigs;
|
||||
|
||||
public GameEngine(int width, int height, Stage stage){
|
||||
this.currentScene = 0;
|
||||
this.isFirstGame = true;
|
||||
|
@ -46,7 +52,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));
|
||||
|
@ -60,10 +66,33 @@ public class GameEngine {
|
|||
e.printStackTrace();
|
||||
gameFont = null;
|
||||
}
|
||||
this.waveConfigs = new WaveConfigs();
|
||||
startNewGame();
|
||||
resetPlayerHealth(0);
|
||||
resetPlayerHealth(1);
|
||||
credits = 3;
|
||||
}
|
||||
|
||||
|
||||
private void resetPlayerHealth(int i){
|
||||
if (i==0){
|
||||
p1Health = 100;
|
||||
}
|
||||
else{
|
||||
p2Health = 100;
|
||||
}
|
||||
}
|
||||
private void resetPlayer(int i){
|
||||
resetPlayerHealth(i);
|
||||
|
||||
if (i==0){
|
||||
p1Lives = 3;
|
||||
}
|
||||
else{
|
||||
p2Lives = 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* initialize and reset vars and timers to "new game" configuration.
|
||||
|
@ -82,6 +111,9 @@ public class GameEngine {
|
|||
|
||||
}
|
||||
|
||||
public WaveConfigs getWaveConfigs() {
|
||||
return waveConfigs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -127,7 +159,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 FirstStage(pWidth, pHeight, numOfPlayers, this, "-= STAGE 1.0 =-", new int[]{5, 20}));
|
||||
changeScene(currentScene+1);
|
||||
}
|
||||
|
||||
|
@ -160,4 +192,51 @@ public class GameEngine {
|
|||
public Font getGameFont() {
|
||||
return gameFont;
|
||||
}
|
||||
|
||||
public int getP1Lives() {
|
||||
return p1Lives;
|
||||
}
|
||||
public int getP2Lives() {
|
||||
return p2Lives;
|
||||
}
|
||||
|
||||
public int getP1Health() {
|
||||
return p1Health;
|
||||
}
|
||||
|
||||
public int getP2Health() {
|
||||
return p2Health;
|
||||
}
|
||||
|
||||
public int getP1Score() {
|
||||
return p1Score;
|
||||
}
|
||||
|
||||
public int getP2Score() {
|
||||
return p2Score;
|
||||
}
|
||||
|
||||
public int getCredits() {return credits;}
|
||||
|
||||
|
||||
public void useCredit(){
|
||||
credits--;
|
||||
}
|
||||
|
||||
public void setScore(int i, int score)
|
||||
{
|
||||
if (i == 0) {
|
||||
p1Score += score;
|
||||
} else {
|
||||
p2Score += score;
|
||||
}
|
||||
}
|
||||
public void setPlayerHealth(int i, int strike) {
|
||||
if (i == 0) {
|
||||
p1Health += strike;
|
||||
} else {
|
||||
p2Health += strike;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
BIN
src/com/sagi/dayan/Games/Images/E1-Fire.png
Normal file
BIN
src/com/sagi/dayan/Games/Images/E1-Fire.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
|
@ -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);
|
||||
|
|
|
@ -1,274 +1,46 @@
|
|||
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.Wave;
|
||||
import com.sagi.dayan.Games.Engine.GameEngine;
|
||||
import com.sagi.dayan.Games.Utils.Utils;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.Timer;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import com.sagi.dayan.Games.Utils.WaveConfig;
|
||||
import com.sagi.dayan.Games.Utils.WaveConfigs;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/20/16.
|
||||
* Created by sagi on 3/19/16.
|
||||
*/
|
||||
public class FirstStage extends Scene {
|
||||
protected Vector<Player> players;
|
||||
protected int p1Speed = 10;
|
||||
protected Vector<Missile> missiles;
|
||||
protected Background bg;
|
||||
protected Timer enemyWaveT, enemyT;
|
||||
protected Vector<EnemyShip> enemies;
|
||||
protected int[] yAxisStartingAnimation;
|
||||
protected int startingAnimationIndex;
|
||||
protected boolean isStarted;
|
||||
protected int numOfPlayers;
|
||||
protected Map<Integer, Boolean> keys;
|
||||
protected String title;
|
||||
protected JLabel stageTitle;
|
||||
public class FirstStage extends Level{
|
||||
|
||||
|
||||
|
||||
|
||||
public FirstStage(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle){
|
||||
super(width, height, engine);
|
||||
players = new Vector<>();
|
||||
missiles = 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"));
|
||||
}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"));
|
||||
|
||||
}
|
||||
|
||||
setupKeys();
|
||||
enemies = new Vector<>();
|
||||
enemyWaveT = new Timer(10000, new enemyWaveLaunch());
|
||||
enemyT = new Timer(10000, new enemyLaunch());
|
||||
enemyWaveT.start();
|
||||
|
||||
Utils.playSound("jetSound.wav");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
public FirstStage(int width, int height, int numOfPlayers, GameEngine engine, String stageTitle, int[] waveDelay) {
|
||||
super(width, height, numOfPlayers, engine, stageTitle, waveDelay);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
|
||||
bg.update();
|
||||
movePlayers();
|
||||
|
||||
if(startingAnimationIndex < 3 && !isStarted){
|
||||
if(startingAnimationIndex == 0){
|
||||
startingAnimationIndex++;
|
||||
protected void launchWave(long now) {
|
||||
lastWaveTime = now;
|
||||
System.out.println("New Wave!! Time: " + now);
|
||||
WaveConfig wc;
|
||||
int numOfEnemies = 5, numOfHits = 1;
|
||||
double launchDelay = 0.5, fireDelay = 5;
|
||||
switch (currentWave){
|
||||
case 0:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 0.2;
|
||||
launchDelay = 0.5;
|
||||
numOfHits = 1;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);
|
||||
break;
|
||||
case 1:
|
||||
numOfEnemies = 5;
|
||||
fireDelay = 5;
|
||||
launchDelay = 1;
|
||||
numOfHits = 1;
|
||||
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);
|
||||
break;
|
||||
default:
|
||||
wc = engine.getWaveConfigs().getWaveConfig(WaveConfigs.DEMO);
|
||||
break;
|
||||
}
|
||||
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));
|
||||
// players.get(i).update();
|
||||
}
|
||||
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));
|
||||
// players.get(i).update();
|
||||
}
|
||||
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 < missiles.size() ; i++){
|
||||
missiles.get(i).update();
|
||||
}
|
||||
|
||||
for(int i = 0 ; i < enemies.size() ; i++){
|
||||
enemies.get(i).update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
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()){
|
||||
missiles.add(new Missile(players.get(0).getCenterX() - 3, (int)players.get(0).getLocY(), players.get(0).getAcceleration() + 3, "P1Laser.png"));
|
||||
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()){
|
||||
missiles.add(new Missile(players.get(1).getCenterX() - 3, (int)players.get(1).getLocY(), players.get(1).getAcceleration() + 3, "P1Laser.png"));
|
||||
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();
|
||||
if(!isStarted){
|
||||
Font f = engine.getGameFont();
|
||||
if(f == null) {
|
||||
f = g.getFont();
|
||||
}
|
||||
f = f.deriveFont(60F);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.setFont(f);
|
||||
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
for(int i = 0 ; i < missiles.size() ; i++){
|
||||
missiles.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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent keyEvent) {
|
||||
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
waves.add(new Wave(numOfEnemies, wc.getMoveVector(), fireDelay, wc.getStepDelay(), launchDelay, wc.getAcc(), "L1-ES1.png", wc.getStartX(), wc.getStartY(), this, numOfHits));
|
||||
currentWave++;
|
||||
}
|
||||
}
|
||||
|
|
384
src/com/sagi/dayan/Games/Stage/Level.java
Normal file
384
src/com/sagi/dayan/Games/Stage/Level.java
Normal file
|
@ -0,0 +1,384 @@
|
|||
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.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by sagi on 2/20/16.
|
||||
*/
|
||||
public abstract class Level extends Scene {
|
||||
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;
|
||||
|
||||
|
||||
|
||||
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));
|
||||
|
||||
}
|
||||
|
||||
setupKeys();
|
||||
Utils.playSound("jetSound.wav");
|
||||
}
|
||||
|
||||
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<Wave>();
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
if(currentWave < waveDelay.length && 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));
|
||||
// players.get(i).update();
|
||||
}
|
||||
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));
|
||||
// players.get(i).update();
|
||||
}
|
||||
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 < waves.size() ; i++){
|
||||
waves.get(i).update();
|
||||
if(waves.get(i).isWaveOver()) {
|
||||
wavesToRemove.add(waves.get(i));
|
||||
}
|
||||
}
|
||||
waves.removeAll(wavesToRemove);
|
||||
}
|
||||
checkCollision();
|
||||
}
|
||||
|
||||
protected abstract void launchWave(long time);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
}
|
||||
|
||||
//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.drawString("Player "+ (i+1) +" - Lives: " + ((i == 0) ? engine.getP1Lives() : engine.getP2Lives())+ ", Score: " + ((i == 0) ? engine.getP1Score() : engine.getP2Score()), 15, 35*(i+1));
|
||||
|
||||
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++){
|
||||
players.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 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))){
|
||||
if(players.get(i).isMortal())
|
||||
engine.setPlayerHealth(i, -10);
|
||||
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(players.get(i).isMortal())
|
||||
engine.setPlayerHealth(i, -10);
|
||||
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
|
||||
|
||||
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))) {
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
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 keyPressed(KeyEvent keyEvent) {
|
||||
if(isStarted)
|
||||
keys.put(keyEvent.getKeyCode(), true);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent keyEvent) {
|
||||
if(isStarted)
|
||||
keys.put(keyEvent.getKeyCode(), false);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -29,9 +29,9 @@ 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.wav")));
|
||||
AudioPlayer.player.start(new AudioStream(Utils.getSoundResourceAsStream("intro_LowQuality.wav")));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -91,7 +91,11 @@ public class Stage extends JPanel implements Runnable{
|
|||
|
||||
if (System.currentTimeMillis() - lastTimer > 1000) {
|
||||
lastTimer += 1000;
|
||||
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;
|
||||
}
|
||||
|
|
41
src/com/sagi/dayan/Games/Utils/WaveConfig.java
Normal file
41
src/com/sagi/dayan/Games/Utils/WaveConfig.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
package com.sagi.dayan.Games.Utils;
|
||||
|
||||
/**
|
||||
* Created by sagi on 3/18/16.
|
||||
*/
|
||||
public class WaveConfig {
|
||||
protected int[] moveVector;
|
||||
protected double stepDelay;
|
||||
protected int acc;
|
||||
protected int startX;
|
||||
protected int startY;
|
||||
|
||||
public WaveConfig(int[] moveVector, double stepDelay, int acc, int staryX, int startY){
|
||||
this.moveVector = moveVector;
|
||||
this.stepDelay = stepDelay;
|
||||
this.acc = acc;
|
||||
this.startX = staryX;
|
||||
this.startY = startY;
|
||||
}
|
||||
|
||||
|
||||
public int[] getMoveVector() {
|
||||
return moveVector;
|
||||
}
|
||||
|
||||
public double getStepDelay() {
|
||||
return stepDelay;
|
||||
}
|
||||
|
||||
public int getAcc() {
|
||||
return acc;
|
||||
}
|
||||
|
||||
public int getStartX() {
|
||||
return startX;
|
||||
}
|
||||
|
||||
public int getStartY() {
|
||||
return startY;
|
||||
}
|
||||
}
|
34
src/com/sagi/dayan/Games/Utils/WaveConfigs.java
Normal file
34
src/com/sagi/dayan/Games/Utils/WaveConfigs.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package com.sagi.dayan.Games.Utils;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* Created by sagi on 3/18/16.
|
||||
*/
|
||||
public class WaveConfigs {
|
||||
public static final int DEMO = 0;
|
||||
Vector<WaveConfig> configs;
|
||||
|
||||
public WaveConfigs(){
|
||||
configs = new Vector<>();
|
||||
// int[] moveVector, double stepDelay, int acc, int staryX, int startY
|
||||
configs.add(new WaveConfig(new int[]{90,90,120, 120, 150, 150, 270, 270, 270} , 0.5, 8,500 , 0));
|
||||
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
// configs.add(new WaveConfig( , , , , ));
|
||||
}
|
||||
|
||||
public WaveConfig getWaveConfig(int config){
|
||||
if (config < 0 || configs.size() <= config)
|
||||
throw new IllegalArgumentException("no such config...");
|
||||
return configs.get(config);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue