more refactoring and reorganizing.
parent
793b6d6762
commit
24a2056c34
|
|
@ -30,4 +30,10 @@ public abstract class Entity extends BasicTickAndRender{
|
|||
}
|
||||
|
||||
public abstract boolean isCollidable();
|
||||
|
||||
public abstract void die();
|
||||
|
||||
public abstract boolean getAlive();
|
||||
|
||||
public abstract void collidedWith(Entity e);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,4 +101,16 @@ public class Particle extends Entity{
|
|||
public boolean isCollidable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ import javax.imageio.ImageIO;
|
|||
|
||||
import MAndApps.apps.spacewars.entity.Enemy;
|
||||
import MAndApps.apps.spacewars.entity.Player;
|
||||
import MAndApps.apps.spacewars.entity.enemy.NormalEnemy;
|
||||
import MAndApps.apps.spacewars.entity.enemy.RedEnemy;
|
||||
import MAndApps.apps.spacewars.tools.Explosion;
|
||||
import MAndEngine.BasicApp;
|
||||
|
||||
|
|
@ -27,10 +29,8 @@ public class SpaceWars implements BasicApp {
|
|||
private static boolean debug = false;
|
||||
private static final int WIDTH = 1024, HEIGHT = 600;
|
||||
private static Image background;
|
||||
private static Random r = new Random();
|
||||
private static ArrayList<Enemy> entities = new ArrayList<Enemy>();
|
||||
private static ArrayList<Entity> entities = new ArrayList<Entity>();
|
||||
private static Player player = new Player();
|
||||
private static ArrayList<Explosion> explosions = new ArrayList<Explosion>();
|
||||
|
||||
//
|
||||
public static final Font defaultFont = new Font("Ubuntu", Font.BOLD, 10);
|
||||
|
|
@ -40,49 +40,41 @@ public class SpaceWars implements BasicApp {
|
|||
|
||||
@Override
|
||||
public void tick() {
|
||||
|
||||
|
||||
// ticks enemies
|
||||
for (int i = 0; i < entities.size(); i++)
|
||||
entities.get(i).tick();
|
||||
|
||||
// tick explosions
|
||||
for (int i = 0; i < explosions.size(); i++)
|
||||
explosions.get(i).tick();
|
||||
|
||||
// tick player object
|
||||
Rectangle playerRect = player.getBoundingBox();
|
||||
if (player.getAlive())
|
||||
for (int i = 0; i < entities.size(); i++)
|
||||
if (entities.get(i).isCollidable() && entities.get(i).getBoundingBox().intersects(playerRect))
|
||||
player.collideWithEnemy(entities.get(i).getX(), entities.get(i).getY());
|
||||
|
||||
int i = 0;
|
||||
while (i < entities.size()) {
|
||||
if (!entities.get(i).getAlive()) {
|
||||
|
||||
BOOM( 75, 1.2, entities.get(i).getColor().getRed()-50,
|
||||
entities.get(i).getColor().getGreen()-50,
|
||||
entities.get(i).getColor().getBlue()-50, 50,
|
||||
(int)entities.get(i).getX(),
|
||||
(int)entities.get(i).getY(), 550, true, true, 10 );
|
||||
|
||||
entities.remove(i);
|
||||
|
||||
// check dem collisions yo
|
||||
for (int i = 0; i < entities.size(); i++) {
|
||||
Entity e1 = entities.get(i);
|
||||
if (e1.isCollidable()) {
|
||||
for (int j = i + 1; j < entities.size(); j++) {
|
||||
Entity e2 = entities.get(j);
|
||||
if(e2.isCollidable()) {
|
||||
|
||||
//because efficiency.
|
||||
e1.collidedWith(e2);
|
||||
e2.collidedWith(e1);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// cleanup method.
|
||||
for (int i = 0; i < entities.size();) {
|
||||
Entity entity = entities.get(i);
|
||||
if (!entity.getAlive()) {
|
||||
entity.die();
|
||||
entities.remove(i);
|
||||
} else
|
||||
i++;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
while (i < explosions.size()) {
|
||||
if (!explosions.get(i).getAlive()) {
|
||||
explosions.remove(i);
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ArrayList<Enemy> getEnemies() {
|
||||
public static ArrayList<Entity> getEnemies() {
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
|
@ -96,11 +88,7 @@ public class SpaceWars implements BasicApp {
|
|||
|
||||
g.setFont(defaultFont);
|
||||
g.drawImage(background, 0, 0, WIDTH, HEIGHT, null);
|
||||
|
||||
for (int i = 0; i < explosions.size(); i++)
|
||||
explosions.get(i).render(g, i);
|
||||
|
||||
player.render(g);
|
||||
|
||||
for (int i = 0; i < entities.size(); i++)
|
||||
entities.get(i).render(g);
|
||||
|
||||
|
|
@ -136,6 +124,10 @@ public class SpaceWars implements BasicApp {
|
|||
public void initialize() {
|
||||
try {
|
||||
background = ImageIO.read(new URL("http://wallpaperswiki.org/wallpapers/2012/11/Wallpaper-Abstract-Wallpaper-Background-Texture-Texture-Yellow-Pictures-600x1024.jpg"));
|
||||
entities.add(player);
|
||||
entities.add(new NormalEnemy(0, 0));
|
||||
entities.add(new NormalEnemy(0, 0));
|
||||
entities.add(new NormalEnemy(0, 0));
|
||||
} catch (Exception e) {
|
||||
background = (Image) new BufferedImage(1024, 600, BufferedImage.TRANSLUCENT);
|
||||
Graphics g = background.getGraphics();
|
||||
|
|
@ -146,7 +138,7 @@ public class SpaceWars implements BasicApp {
|
|||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -177,14 +169,14 @@ public class SpaceWars implements BasicApp {
|
|||
}
|
||||
|
||||
public static void BOOM(double speed, double decay, int r, int g, int b, int variant, int x, int y, int size, boolean singleVariant, boolean bubble, int sizeOfParticles) {
|
||||
|
||||
|
||||
Explosion explosion = new Explosion(speed, decay, r, g, b, variant, singleVariant);
|
||||
|
||||
explosions.add(explosion);
|
||||
|
||||
entities.add((Entity) explosion);
|
||||
explosion.goBoom(x, y, size, bubble, sizeOfParticles);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visibleInMenu() {
|
||||
return true;
|
||||
|
|
@ -200,11 +192,11 @@ public class SpaceWars implements BasicApp {
|
|||
|
||||
@Override
|
||||
public void resized(int width, int height) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import MAndApps.apps.spacewars.entity.enemy.GreenEnemy;
|
|||
import MAndApps.apps.spacewars.entity.enemy.NormalEnemy;
|
||||
import MAndApps.apps.spacewars.entity.enemy.RedEnemy;
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.SpaceWars;
|
||||
|
||||
public abstract class Enemy extends Entity {
|
||||
public static final int NORMAL = 0;
|
||||
|
|
@ -57,4 +58,12 @@ public abstract class Enemy extends Entity {
|
|||
public abstract void damage(int damage);
|
||||
|
||||
public abstract boolean isCollidable();
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
if(e instanceof Bullet) {
|
||||
Bullet b = (Bullet)e;
|
||||
damage(b.getDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,11 @@ public class Player extends Entity {
|
|||
private boolean A = false, S = false, D = false, W = false, alive = true;
|
||||
private Random r = new Random();
|
||||
private Gun gun = new Gun(Bullet.BASIC, 25, (int)x, (int)y);
|
||||
|
||||
/**
|
||||
* go boom is a callback when it dies because it dies on tick i assume?
|
||||
* TODO fix that so no die on tick.
|
||||
*/
|
||||
private boolean goBoom = false;
|
||||
|
||||
public Player() {
|
||||
|
|
@ -130,67 +135,6 @@ public class Player extends Entity {
|
|||
|
||||
private double time = 0;
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W:
|
||||
W = true;
|
||||
break;
|
||||
case KeyEvent.VK_D:
|
||||
D = true;
|
||||
break;
|
||||
case KeyEvent.VK_S:
|
||||
S = true;
|
||||
break;
|
||||
case KeyEvent.VK_A:
|
||||
A = true;
|
||||
break;
|
||||
case KeyEvent.VK_I:
|
||||
shoot(Direction.UP);
|
||||
break;
|
||||
case KeyEvent.VK_J:
|
||||
shoot(Direction.LEFT);
|
||||
break;
|
||||
case KeyEvent.VK_K:
|
||||
shoot(Direction.DOWN);
|
||||
break;
|
||||
case KeyEvent.VK_L:
|
||||
shoot(Direction.RIGHT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void shoot(int direction) {
|
||||
gun.updatePosition((int)x + WIDTH/2, (int)y + HEIGHT/2);
|
||||
gun.shoot(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W:
|
||||
W = false;
|
||||
break;
|
||||
case KeyEvent.VK_D:
|
||||
D = false;
|
||||
break;
|
||||
case KeyEvent.VK_S:
|
||||
S = false;
|
||||
break;
|
||||
case KeyEvent.VK_A:
|
||||
A = false;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void collideWithEnemy(double x, double y) {
|
||||
goBoom = true;
|
||||
timer = 0;
|
||||
alive = false;
|
||||
time = 5;
|
||||
}
|
||||
|
||||
public boolean getAlive() {
|
||||
return alive;
|
||||
}
|
||||
|
|
@ -199,4 +143,20 @@ public class Player extends Entity {
|
|||
public boolean isCollidable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
if(e instanceof Enemy) {
|
||||
goBoom = true;
|
||||
timer = 0;
|
||||
alive = false;
|
||||
time = 5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
|||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.SpaceWars;
|
||||
import MAndApps.apps.spacewars.entity.Bullet;
|
||||
import MAndApps.apps.spacewars.tools.Direction;
|
||||
|
|
@ -14,7 +15,8 @@ public class BasicPlayerBullet extends Bullet {
|
|||
private final static int SPEED = 10;
|
||||
private double x, y;
|
||||
private boolean alive = true;
|
||||
|
||||
|
||||
|
||||
public BasicPlayerBullet(int direction, int x, int y) {
|
||||
super(x, y, 1, 1);
|
||||
this.x = x;
|
||||
|
|
@ -41,16 +43,12 @@ public class BasicPlayerBullet extends Bullet {
|
|||
} else if (direction == Direction.RIGHT) {
|
||||
x += SPEED;
|
||||
}
|
||||
|
||||
|
||||
|
||||
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
|
||||
Rectangle r = getBoundingBox();
|
||||
for (int i = 0; i < SpaceWars.getEnemies().size(); i++) {
|
||||
if (r.intersects(SpaceWars.getEnemies().get(i).getBoundingBox())) {
|
||||
SpaceWars.getEnemies().get(i).damage(getDamage());
|
||||
alive = false;
|
||||
i = SpaceWars.getEnemies().size();
|
||||
}
|
||||
}
|
||||
|
||||
//if out of bound
|
||||
if(x > SpaceWars.getWIDTH() || x < 0 - WIDTH || y > SpaceWars.getHEIGHT() || y < 0 - HEIGHT){
|
||||
alive = false;
|
||||
}
|
||||
|
|
@ -86,4 +84,16 @@ public class BasicPlayerBullet extends Bullet {
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
|||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.SpaceWars;
|
||||
import MAndApps.apps.spacewars.entity.Bullet;
|
||||
import MAndApps.apps.spacewars.tools.Direction;
|
||||
|
|
@ -54,13 +55,7 @@ public class PlayerImpactBullet extends Bullet {
|
|||
|
||||
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
|
||||
Rectangle r = getBoundingBox();
|
||||
for (int i = 0; i < SpaceWars.getEnemies().size(); i++) {
|
||||
if (r.intersects(SpaceWars.getEnemies().get(i).getBoundingBox())) {
|
||||
SpaceWars.getEnemies().get(i).damage(getDamage());
|
||||
alive = false;
|
||||
i = SpaceWars.getEnemies().size();
|
||||
}
|
||||
}
|
||||
|
||||
if(x > SpaceWars.getWIDTH() || x < 0 - WIDTH || y > SpaceWars.getHEIGHT() || y < 0 - HEIGHT){
|
||||
alive = false;
|
||||
}
|
||||
|
|
@ -87,4 +82,16 @@ public class PlayerImpactBullet extends Bullet {
|
|||
public boolean isCollidable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package MAndApps.apps.spacewars.entity.bullet;
|
|||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.SpaceWars;
|
||||
import MAndApps.apps.spacewars.entity.Bullet;
|
||||
import MAndApps.apps.spacewars.tools.Direction;
|
||||
|
|
@ -61,16 +62,7 @@ public class PlayerPiercingBullet extends Bullet {
|
|||
break;
|
||||
}
|
||||
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
|
||||
for (int i = 0; i < SpaceWars.getEnemies().size() && hits < MAX_HITS; i++) {
|
||||
if (SpaceWars.getEnemies().get(i).getBoundingBox().intersectsLine((int)x, (int)y, (int)oldX, (int)oldY)) {
|
||||
hits++;
|
||||
SpaceWars.getEnemies().get(i).damage(getDamage());
|
||||
if(hits >= MAX_HITS){
|
||||
alive = false;
|
||||
i = SpaceWars.getEnemies().size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(x > SpaceWars.getWIDTH() || x < 0 - WIDTH || y > SpaceWars.getHEIGHT() || y < 0 - HEIGHT){
|
||||
alive = false;
|
||||
}
|
||||
|
|
@ -98,4 +90,16 @@ public class PlayerPiercingBullet extends Bullet {
|
|||
public boolean isCollidable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
|||
import java.awt.Graphics;
|
||||
import java.util.Random;
|
||||
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.SpaceWars;
|
||||
import MAndApps.apps.spacewars.entity.Enemy;
|
||||
|
||||
|
|
@ -178,4 +179,16 @@ public class BlueEnemy extends Enemy {
|
|||
public boolean isCollidable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
|||
import java.awt.Graphics;
|
||||
import java.util.Random;
|
||||
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.SpaceWars;
|
||||
import MAndApps.apps.spacewars.entity.Enemy;
|
||||
|
||||
|
|
@ -185,4 +186,16 @@ public class GreenEnemy extends Enemy {
|
|||
public boolean isCollidable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
|||
import java.awt.Graphics;
|
||||
import java.util.Random;
|
||||
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.SpaceWars;
|
||||
import MAndApps.apps.spacewars.entity.Enemy;
|
||||
|
||||
|
|
@ -179,4 +180,16 @@ public class NormalEnemy extends Enemy {
|
|||
public boolean isCollidable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
|||
import java.awt.Graphics;
|
||||
import java.util.Random;
|
||||
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.SpaceWars;
|
||||
import MAndApps.apps.spacewars.entity.Enemy;
|
||||
|
||||
|
|
@ -176,4 +177,16 @@ public class RedEnemy extends Enemy {
|
|||
public boolean isCollidable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,16 +5,18 @@ import java.awt.Graphics;
|
|||
import java.util.Random;
|
||||
import java.util.Stack;
|
||||
|
||||
import MAndApps.apps.spacewars.Entity;
|
||||
import MAndApps.apps.spacewars.Particle;
|
||||
|
||||
|
||||
public class Explosion {
|
||||
public class Explosion extends Entity{
|
||||
private Stack<Particle> bits = new Stack<Particle>();
|
||||
private Random rand = new Random();
|
||||
private final double SPEED, DECAY;
|
||||
private final int r, g, b, variant;
|
||||
private final boolean singleVariant;
|
||||
public Explosion(double speed, double decay, int r, int g, int b, int variant, boolean singleVariant){
|
||||
super(0, 0, 1, 1);
|
||||
SPEED = speed;
|
||||
DECAY = decay;
|
||||
this.r = r;
|
||||
|
|
@ -23,7 +25,8 @@ public class Explosion {
|
|||
this.variant = variant;
|
||||
this.singleVariant = singleVariant;
|
||||
}
|
||||
public void tick() {
|
||||
|
||||
public int tick() {
|
||||
for(int i = 0; i < bits.size(); i++)
|
||||
bits.elementAt(i).tick();
|
||||
int i = 0;
|
||||
|
|
@ -34,6 +37,7 @@ public class Explosion {
|
|||
i++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void render(Graphics g, int line) {
|
||||
|
|
@ -77,5 +81,27 @@ public class Explosion {
|
|||
alive = true;
|
||||
return alive;
|
||||
}
|
||||
@Override
|
||||
public boolean isCollidable() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public void render(Graphics g) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collidedWith(Entity e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue