From 14f800bc0cb84c4005fdf6f87894ac740974e03d Mon Sep 17 00:00:00 2001 From: Marcus Gosselin Date: Sat, 13 Feb 2016 15:05:14 -0500 Subject: [PATCH] stuff mnanananana --- src/MAndApps/apps/spacewars/SpaceWars.java | 48 ++++++++------ .../apps/spacewars/entity/Bullet.java | 62 +------------------ src/MAndApps/apps/spacewars/entity/Enemy.java | 4 +- .../apps/spacewars/entity/Player.java | 23 ++++++- .../entity/bullet/BasicPlayerBullet.java | 34 ++++++---- .../entity/bullet/PlayerImpactBullet.java | 8 +-- .../spacewars/entity/enemy/NormalEnemy.java | 8 +-- src/MAndApps/apps/spacewars/gun/Gun.java | 51 +++------------ src/Main.java | 3 +- 9 files changed, 94 insertions(+), 147 deletions(-) diff --git a/src/MAndApps/apps/spacewars/SpaceWars.java b/src/MAndApps/apps/spacewars/SpaceWars.java index aaaf62d..f5b873b 100644 --- a/src/MAndApps/apps/spacewars/SpaceWars.java +++ b/src/MAndApps/apps/spacewars/SpaceWars.java @@ -26,10 +26,13 @@ import MAndEngine.ImageCreator; * are laying around. * * this is somewhat old architecture and some half finished new architecture can be found - * in the screensaver branch as i plan to make this both a game and a screen saver with - * a player AI. as well, some of these concepts will be ported over to mand engine + * in the screen saver branch as i plan to make this both a game and a screen saver with + * a player AI. as well, some of these concepts will be ported over to mandengine * once they are abstracted a little better. * + * screen saver branch has been remerged and deleted. + * screen saver will still be made, but i think the base engine needs more polish first + * * @author mgosselin * */ @@ -54,10 +57,17 @@ public class SpaceWars implements BasicApp { private static int HEIGHT = ORIGINAL_HEIGHT; public static int scale; + + @Override public void tick() { - // ticks enemies + //add entities + for(Entity e : toAdd) + entities.add(e); + toAdd.clear(); + + // ticks entities for (int i = 0; i < entities.size(); i++) entities.get(i).tick(); @@ -84,8 +94,8 @@ public class SpaceWars implements BasicApp { ((e2.getY() > e1.getY()) & (e2.getY() < e1.getY() + e1.getHeight()))) ) { - e1.collidedWith(e2); - e2.collidedWith(e1); + e1.collidedWith(e2); + e2.collidedWith(e1); } } } @@ -103,17 +113,19 @@ public class SpaceWars implements BasicApp { i++; } } - - public static ArrayList getEnemies() { - return entities; + + private static ArrayList toAdd; + + public static void addEntity(Entity e) { + + toAdd.add(e); + } @Override public void render(Graphics2D g) { - - - try { - g.drawImage(background, 0, 0, null); + try { + g.drawImage(background, 0, 0, null); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); @@ -123,11 +135,9 @@ public class SpaceWars implements BasicApp { for (int i = 0; i < entities.size(); i++) entities.get(i).render(g); - - // render level and xp bar. - g.setFont(levelFont); - g.setColor(Color.WHITE); g.setFont(defaultFont); + g.setColor(Color.WHITE); + g.drawString("" + entities.size(), 20, 32); if (paused) { g.setFont(pausedFont); @@ -167,9 +177,10 @@ public class SpaceWars implements BasicApp { @Override public void initialize() { try { + toAdd = new ArrayList(); player = new Player(); entities.add(player); - for(int i = 0; i < 100; i ++) + for(int i = 0; i < 10; i ++) entities.add(new NormalEnemy()); Engine.timeScale = 60d / (1000d * 1000d); @@ -182,8 +193,7 @@ public class SpaceWars implements BasicApp { @Override public void keyPressed(KeyEvent e) { - for (int i = 0; i < entities.size(); i++) - entities.get(i).keyPressed(e); + player.keyPressed(e); if (e.getKeyCode() == KeyEvent.VK_P || e.getKeyCode() == KeyEvent.VK_SPACE) { paused = !paused; } else if (e.getKeyCode() == KeyEvent.VK_Q) { diff --git a/src/MAndApps/apps/spacewars/entity/Bullet.java b/src/MAndApps/apps/spacewars/entity/Bullet.java index d9eafd4..b7a72e3 100644 --- a/src/MAndApps/apps/spacewars/entity/Bullet.java +++ b/src/MAndApps/apps/spacewars/entity/Bullet.java @@ -4,6 +4,7 @@ import MAndApps.apps.spacewars.Entity; import MAndApps.apps.spacewars.entity.bullet.BasicPlayerBullet; import MAndApps.apps.spacewars.entity.bullet.PlayerImpactBullet; import MAndApps.apps.spacewars.entity.bullet.PlayerPiercingBullet; +import MAndApps.apps.spacewars.tools.Direction; public abstract class Bullet extends Entity { @@ -36,45 +37,8 @@ public abstract class Bullet extends Entity { super.y = y; } - - /** - * bullet class now references here to get the cool down for the current bullet - * updates upon ammo change or initialization of a gun type. - * @param bulletType - * @return - */ - public static int getCooldown(int bulletType){ - switch (bulletType){ - case BASIC: - return 25; - case PLAYER_PIERCE_ONE: - return 20; - case PLAYER_PIERCE_TWO: - return 18; - case PLAYER_PIERCE_THREE: - return 16; - case PLAYER_PIERCE_FOUR: - return 14; - case PLAYER_PIERCE_FIVE: - return 12; - case PLAYER_PIERCE_SIX: - return 10; - case PLAYER_PIERCE_SEVEN: - return 8; - case PLAYER_PIERCE_EIGHT: - return 6; - case PLAYER_PIERCE_NINE: - return 4; - case PLAYER_PIERCE_TEN: - return 2; - case PLAYER_GODMODE: - return 0; - default: - return 25; - } - } - public static Bullet getNewBullet(int bulletType, int x, int y, int direction){ + public static Bullet getNewBullet(int bulletType, int x, int y, Direction direction){ switch(bulletType){ //this is the literal bit that goes through and indexes what to do //with certain IDs that you defined above.. @@ -90,27 +54,7 @@ public abstract class Bullet extends Entity { //inside the parenthesis, in the returns, are called //"arguments" or "parameters". case BASIC: - return new BasicPlayerBullet(direction, x, y); - case PLAYER_PIERCE_ONE: - return new PlayerPiercingBullet(direction, x, y, 2); - case PLAYER_PIERCE_TWO: - return new PlayerPiercingBullet(direction, x, y, 3); - case PLAYER_PIERCE_THREE: - return new PlayerPiercingBullet(direction, x, y, 4); - case PLAYER_PIERCE_FOUR: - return new PlayerPiercingBullet(direction, x, y, 5); - case PLAYER_PIERCE_FIVE: - return new PlayerPiercingBullet(direction, x, y, 6); - case PLAYER_PIERCE_SIX: - return new PlayerPiercingBullet(direction, x, y, 7); - case PLAYER_PIERCE_SEVEN: - return new PlayerPiercingBullet(direction, x, y, 8); - case PLAYER_PIERCE_EIGHT: - return new PlayerPiercingBullet(direction, x, y, 9); - case PLAYER_PIERCE_NINE: - return new PlayerPiercingBullet(direction, x, y, 10); - case PLAYER_PIERCE_TEN: - return new PlayerPiercingBullet(direction, x, y, 15); + return new BasicPlayerBullet(direction, x, y); case PLAYER_IMPACT_ONE: return new PlayerImpactBullet(direction, x, y, 1); case PLAYER_IMPACT_TWO: diff --git a/src/MAndApps/apps/spacewars/entity/Enemy.java b/src/MAndApps/apps/spacewars/entity/Enemy.java index 0d7ef0b..c771f24 100644 --- a/src/MAndApps/apps/spacewars/entity/Enemy.java +++ b/src/MAndApps/apps/spacewars/entity/Enemy.java @@ -3,6 +3,7 @@ package MAndApps.apps.spacewars.entity; import java.awt.Color; import MAndApps.apps.spacewars.Entity; +import MAndApps.apps.spacewars.SpaceWars; public abstract class Enemy extends Entity { public abstract boolean getAlive(); @@ -13,10 +14,11 @@ public abstract class Enemy extends Entity { public abstract boolean isCollidable(); @Override - public void collidedWith(Entity e) { + public final void collidedWith(Entity e) { if(e instanceof Bullet) { Bullet b = (Bullet)e; damage(b.getDamage()); + SpaceWars.BOOM(1.0, 1.0, 255, 255, 255, 1, (int)x, (int)y, 100, true, false, 4); } } } diff --git a/src/MAndApps/apps/spacewars/entity/Player.java b/src/MAndApps/apps/spacewars/entity/Player.java index ba70b8f..805912d 100644 --- a/src/MAndApps/apps/spacewars/entity/Player.java +++ b/src/MAndApps/apps/spacewars/entity/Player.java @@ -6,6 +6,7 @@ import java.awt.event.KeyEvent; import MAndApps.apps.spacewars.SpaceWars; import MAndApps.apps.spacewars.gun.Gun; +import MAndApps.apps.spacewars.tools.Direction; import MAndApps.apps.spacewars.Entity; import static MAndEngine.Utils.rand; import MAndEngine.Engine; @@ -13,8 +14,8 @@ import MAndEngine.Engine; public class Player extends Entity { private static final double ACC = 0.5, MAXSPEED = 5; private boolean alive = false; - private Gun gun = new Gun(Bullet.BASIC, 25, (int) x, (int) y); - + private Gun gun = new Gun(Bullet.BASIC); + /** * go boom is a callback when it dies because it dies on tick i assume? TODO * fix that so no die on tick. @@ -47,6 +48,15 @@ public class Player extends Entity { } gun.tick(); if (alive) { + + //hold up, before we dive into physics... + + if(Engine.keys[KeyEvent.VK_I]) gun.shoot(Direction.UP); + else if(Engine.keys[KeyEvent.VK_J]) gun.shoot(Direction.LEFT); + else if(Engine.keys[KeyEvent.VK_K]) gun.shoot(Direction.DOWN); + else if(Engine.keys[KeyEvent.VK_L]) gun.shoot(Direction.RIGHT); + + if (time != 1) time -= 0.05d * Engine.deltaTime; if (time < 1) @@ -122,7 +132,6 @@ public class Player extends Entity { if (temp == 1) g.fillRect((int) x, (int) y, (int) width, (int) height); } - gun.render(g); } private double time = 0; @@ -158,4 +167,12 @@ public class Player extends Entity { // TODO Auto-generated method stub return true; } + + public int getCenterY() { + return (int)(y + height / 2d); + } + + public int getCenterX() { + return (int)(x + width / 2d); + } } diff --git a/src/MAndApps/apps/spacewars/entity/bullet/BasicPlayerBullet.java b/src/MAndApps/apps/spacewars/entity/bullet/BasicPlayerBullet.java index 474eea4..5fec3e1 100644 --- a/src/MAndApps/apps/spacewars/entity/bullet/BasicPlayerBullet.java +++ b/src/MAndApps/apps/spacewars/entity/bullet/BasicPlayerBullet.java @@ -9,13 +9,15 @@ import MAndApps.apps.spacewars.SpaceWars; import MAndApps.apps.spacewars.entity.Bullet; import MAndApps.apps.spacewars.tools.Direction; +import MAndEngine.Engine;; + public class BasicPlayerBullet extends Bullet { - private final int direction; + private final Direction direction; private final static int SPEED = 10; private boolean alive = true; - public BasicPlayerBullet(int direction, int x, int y) { + public BasicPlayerBullet(Direction direction, int x, int y) { super(x, y, 1, 1); this.x = x; this.y = y; @@ -31,20 +33,18 @@ public class BasicPlayerBullet extends Bullet { public int tick() { if (alive) { - if (direction == Direction.UP) { - y -= SPEED; - } else if (direction == Direction.DOWN) { - y += SPEED; - } else if (direction == Direction.LEFT) { - x -= SPEED; - } else if (direction == Direction.RIGHT) { - x += SPEED; - } + y += direction.getY() * SPEED * Engine.deltaTime; + x += direction.getX() * SPEED * Engine.deltaTime; + if(y + height < 0) alive = false; + if(y > SpaceWars.getHEIGHT()) alive = false; + if(x + width < 0) alive = false; + if(x > SpaceWars.getWIDTH()) alive = false; } return 0; + } public void render(Graphics g) { @@ -77,4 +77,16 @@ public class BasicPlayerBullet extends Bullet { } + @Override + public int getWIDTH() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getHEIGHT() { + // TODO Auto-generated method stub + return 0; + } + } diff --git a/src/MAndApps/apps/spacewars/entity/bullet/PlayerImpactBullet.java b/src/MAndApps/apps/spacewars/entity/bullet/PlayerImpactBullet.java index 330fea9..86c3226 100644 --- a/src/MAndApps/apps/spacewars/entity/bullet/PlayerImpactBullet.java +++ b/src/MAndApps/apps/spacewars/entity/bullet/PlayerImpactBullet.java @@ -10,7 +10,7 @@ import MAndApps.apps.spacewars.entity.Bullet; import MAndApps.apps.spacewars.tools.Direction; public class PlayerImpactBullet extends Bullet { - private final int direction; + private final Direction direction; private final int WIDTH, HEIGHT; private final static int SPEED = 10; private double x, y; @@ -25,12 +25,12 @@ public class PlayerImpactBullet extends Bullet { return HEIGHT; } - public PlayerImpactBullet(int direction, int x, int y, int level) { + public PlayerImpactBullet(Direction direction2, int x, int y, int level) { super(x, y, 1, 1); this.x = x; this.y = y; - this.direction = direction; - if (direction == Direction.UP || direction == Direction.DOWN) { + this.direction = direction2; + if (direction2 == Direction.UP || direction2 == Direction.DOWN) { WIDTH = 3; HEIGHT = 8; } else { diff --git a/src/MAndApps/apps/spacewars/entity/enemy/NormalEnemy.java b/src/MAndApps/apps/spacewars/entity/enemy/NormalEnemy.java index 931793d..028a27d 100644 --- a/src/MAndApps/apps/spacewars/entity/enemy/NormalEnemy.java +++ b/src/MAndApps/apps/spacewars/entity/enemy/NormalEnemy.java @@ -152,7 +152,7 @@ public class NormalEnemy extends Enemy { if (SpaceWars.debug) g.drawLine((int)(x + width / 2), (int)(y + height / 2), (int)(desiredX + width / 2), (int)(desiredY + height / 2)); - healthBar += ((((double)health/(double)MAX_HEALTH)*(width + 1)) - healthBar)/6; + healthBar += (((((double)health/(double)MAX_HEALTH)*(width + 1)) - healthBar)/6) * Engine.deltaTime; //healthbar g.setColor(Color.BLACK); g.fillRect((int) x, (int) y - 5, (int)width - 1, 3); @@ -186,10 +186,4 @@ public class NormalEnemy extends Enemy { // TODO Auto-generated method stub } - - @Override - public void collidedWith(Entity e) { - // TODO Auto-generated method stub - - } } diff --git a/src/MAndApps/apps/spacewars/gun/Gun.java b/src/MAndApps/apps/spacewars/gun/Gun.java index 71d062e..08bf43b 100644 --- a/src/MAndApps/apps/spacewars/gun/Gun.java +++ b/src/MAndApps/apps/spacewars/gun/Gun.java @@ -1,58 +1,25 @@ package MAndApps.apps.spacewars.gun; -import java.awt.Graphics; -import java.util.Stack; - +import MAndApps.apps.spacewars.SpaceWars; import MAndApps.apps.spacewars.entity.Bullet; +import MAndApps.apps.spacewars.tools.Direction; public class Gun { - private Stack bullets = new Stack(); - private int bulletType; private int MAX_COOLDOWN; - private int cooldown = 0, x, y; - public Gun(int bulletType, int cooldown, int x, int y){ - MAX_COOLDOWN = cooldown; - this.bulletType = bulletType; - this.x = x; - this.y = y; - } - - public void updatePosition(int x, int y){ - this.x = x; - this.y = y; - } - - public void render(Graphics g){ - for(int i = 0; i < bullets.size(); i ++) - bullets.elementAt(i).render(g); + private double cooldown = 0; + public Gun(int bulletType){ + MAX_COOLDOWN = 5; } public void tick(){ - cooldown ++; - //tick bullets - for(int i = 0; i < bullets.size(); i++) - bullets.elementAt(i).tick(); - for(int i = 0; i < bullets.size(); i++){ - if(!bullets.elementAt(i).getAlive()) - bullets.remove(i); - else - i++; - } + cooldown += MAndEngine.Engine.deltaTime; + } - public void shoot(int direction){ + public void shoot(Direction direction){ if(cooldown >= MAX_COOLDOWN){ cooldown = 0; - bullets.push(Bullet.getNewBullet(bulletType, x-(Bullet.getNewBullet(bulletType, x, y, direction).getWIDTH()/2), y-(Bullet.getNewBullet(bulletType, x, y, direction).getHEIGHT()/2), direction)); + SpaceWars.addEntity(Bullet.getNewBullet(Bullet.BASIC, SpaceWars.getPlayer().getCenterX(), SpaceWars.getPlayer().getCenterY(), direction)); } } - - public void switchAmmo(int bulletType){ - this.bulletType = bulletType; - MAX_COOLDOWN = Bullet.getCooldown(bulletType); - } - - public void setCooldown(int i) { - MAX_COOLDOWN = i; - } } \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index d1b5c7f..60d99ba 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,7 +8,8 @@ import MAndEngine.Engine; public class Main { public static void main(String[] args) { - Engine engine = new Engine(new String[] {"MAndApps.apps.spacewars.SpaceWars"}, false, true); + Engine engine = new Engine(new String[] {"MAndApps.apps.spacewars.SpaceWars"}, false, false); + engine.debug = 2; engine.run(); }