From 4894d6eea8d31e3f725cd3c8d2b3b79a77e83a2b Mon Sep 17 00:00:00 2001 From: Recrown Date: Tue, 30 May 2017 16:23:08 -0500 Subject: [PATCH] new entity management and hit detection concepts, began implementing in game play area --- core/src/zero1hd/polyjet/entity/Entity.java | 6 ++ .../polyjet/entity/EntityController.java | 82 +++++++++++++++++ .../zero1hd/polyjet/entity/ally/Laser.java | 12 +++ .../polyjet/entity/ally/PolyJetEntity.java | 20 +++-- .../polyjet/entity/boxes/BoxOfAllies.java | 5 -- .../polyjet/entity/boxes/BoxOfEnemies.java | 57 ------------ .../polyjet/entity/enemies/VoidCircle.java | 5 ++ .../zero1hd/polyjet/screens/GameScreen.java | 15 ++-- .../polyjet/ui/stages/GamePlayArea.java | 87 ++++++++++--------- 9 files changed, 174 insertions(+), 115 deletions(-) create mode 100755 core/src/zero1hd/polyjet/entity/EntityController.java delete mode 100755 core/src/zero1hd/polyjet/entity/boxes/BoxOfAllies.java delete mode 100755 core/src/zero1hd/polyjet/entity/boxes/BoxOfEnemies.java diff --git a/core/src/zero1hd/polyjet/entity/Entity.java b/core/src/zero1hd/polyjet/entity/Entity.java index 533c015..95d10dd 100755 --- a/core/src/zero1hd/polyjet/entity/Entity.java +++ b/core/src/zero1hd/polyjet/entity/Entity.java @@ -20,4 +20,10 @@ public interface Entity { * @return the entity type */ public Entities getEntityType(); + + /** + * If this entity's life span is over, it should be considered dead. Useful for knowing what can be freed in a pool scenario. + * @return if this entity is dead or not. + */ + public boolean isDead(); } diff --git a/core/src/zero1hd/polyjet/entity/EntityController.java b/core/src/zero1hd/polyjet/entity/EntityController.java new file mode 100755 index 0000000..b92c7a8 --- /dev/null +++ b/core/src/zero1hd/polyjet/entity/EntityController.java @@ -0,0 +1,82 @@ +package zero1hd.polyjet.entity; + +import com.badlogic.gdx.assets.AssetManager; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.Pool; + +import zero1hd.polyjet.entity.ally.Laser; +import zero1hd.polyjet.entity.enemies.VoidCircle; + +public class EntityController { + AssetManager assets; + ShapeRenderer shapes; + + public final Array ACTIVE_ALLIES; + public final Array ACTIVE_ENEMIES; + //Enemy pool declaration; + private final Pool VOID_CIRCLE_POOL; + + //Ally pool declaration; + private final Pool LASER_POOL; + public EntityController(ShapeRenderer shapeRenderer, AssetManager assetManager) { + ACTIVE_ALLIES = new Array(); + ACTIVE_ENEMIES = new Array(); + + //Enemy pool initialization; + VOID_CIRCLE_POOL = new Pool() { + @Override + protected VoidCircle newObject() { + return new VoidCircle(shapes); + } + }; + + //Ally pool initialization; + LASER_POOL = new Pool() { + @Override + protected Laser newObject() { + return new Laser(assets.get("laser.png", Texture.class)); + } + }; + + } + + public Entity retrieveEntity(Entities entity) { + switch (entity) { + case VOID_CIRCLE: + VoidCircle voidCircle = VOID_CIRCLE_POOL.obtain(); + ACTIVE_ENEMIES.add(voidCircle); + return voidCircle; + case BAR_BEAT: + return null; + case SHARDS: + return null; + case LASER: + Laser laser = LASER_POOL.obtain(); + ACTIVE_ALLIES.add(laser); + return laser; + default: + return null; + } + } + + public void free(Entity entity, Entities type) { + switch (type) { + case BAR_BEAT: + break; + case SHARDS: + break; + case VOID_CIRCLE: + ACTIVE_ENEMIES.removeValue(entity, true); + VOID_CIRCLE_POOL.free((VoidCircle) entity); + break; + case LASER: + ACTIVE_ALLIES.removeValue(entity, true); + LASER_POOL.free((Laser) entity); + break; + default: + break; + } + } +} diff --git a/core/src/zero1hd/polyjet/entity/ally/Laser.java b/core/src/zero1hd/polyjet/entity/ally/Laser.java index e3ed029..1d169e0 100755 --- a/core/src/zero1hd/polyjet/entity/ally/Laser.java +++ b/core/src/zero1hd/polyjet/entity/ally/Laser.java @@ -6,12 +6,14 @@ import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.utils.Pool.Poolable; +import zero1hd.polyjet.Polyjet; import zero1hd.polyjet.entity.Entities; import zero1hd.polyjet.entity.Entity; public class Laser extends Actor implements Entity, Poolable { private Rectangle hitBox; private float rate; + boolean dead; Texture laserTexture; public Laser(Texture laserTexture) { @@ -33,6 +35,10 @@ public class Laser extends Actor implements Entity, Poolable { hitBox.setX(getX()); hitBox.setY(getY()); super.act(delta); + + if (getY() > Polyjet.GAME_AREA_HEIGHT) { + dead = true; + } } @Override @@ -48,6 +54,7 @@ public class Laser extends Actor implements Entity, Poolable { setSize(0, 0); rate = 0; hitBox.set(0, 0, 0, 0); + dead = false; } @Override @@ -64,4 +71,9 @@ public class Laser extends Actor implements Entity, Poolable { return Entities.LASER; } + @Override + public boolean isDead() { + return dead; + } + } diff --git a/core/src/zero1hd/polyjet/entity/ally/PolyJetEntity.java b/core/src/zero1hd/polyjet/entity/ally/PolyJetEntity.java index 15037b0..09ea7c6 100755 --- a/core/src/zero1hd/polyjet/entity/ally/PolyJetEntity.java +++ b/core/src/zero1hd/polyjet/entity/ally/PolyJetEntity.java @@ -1,5 +1,6 @@ package zero1hd.polyjet.entity.ally; +import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.ParticleEffect; @@ -12,7 +13,7 @@ import zero1hd.polyjet.entity.Entities; import zero1hd.polyjet.entity.Entity; public class PolyJetEntity extends Actor implements Entity { - + public float health; private ParticleEffect thrust; private Texture polyjet; private ParticleEffect teleportCloak; @@ -20,17 +21,17 @@ public class PolyJetEntity extends Actor implements Entity { public boolean moveLeft, moveRight, moveUp, moveDown, teleporting; private float rate; - public PolyJetEntity(Polyjet core, float rate, String jet) { + public PolyJetEntity(AssetManager assets, float rate, String jet) { this.rate = rate; setSize(1.5f, 1.5f); setPosition(Polyjet.GAME_AREA_WIDTH/2 - getWidth()/2, -4f); hitbox = new Rectangle(getX(), getY(), getWidth(), getHeight()); - polyjet = core.getAssetManager().get("polyjet-" + jet + ".png", Texture.class); - thrust = core.getAssetManager().get("standard_thrust.p", ParticleEffect.class); + polyjet = assets.get("polyjet-" + jet + ".png", Texture.class); + thrust = assets.get("standard_thrust.p", ParticleEffect.class); thrust.start(); - teleportCloak = core.getAssetManager().get("teleport-cloak.p", ParticleEffect.class); + teleportCloak = assets.get("teleport-cloak.p", ParticleEffect.class); addAction(Actions.moveTo(getX(), 4f, 0.5f)); } @@ -82,4 +83,13 @@ public class PolyJetEntity extends Actor implements Entity { @Override public void collided(Entity entity) { } + + @Override + public boolean isDead() { + if (health <= 0) { + return true; + } else { + return false; + } + } } diff --git a/core/src/zero1hd/polyjet/entity/boxes/BoxOfAllies.java b/core/src/zero1hd/polyjet/entity/boxes/BoxOfAllies.java deleted file mode 100755 index 98d6f8f..0000000 --- a/core/src/zero1hd/polyjet/entity/boxes/BoxOfAllies.java +++ /dev/null @@ -1,5 +0,0 @@ -package zero1hd.polyjet.entity.boxes; - -public class BoxOfAllies { - -} diff --git a/core/src/zero1hd/polyjet/entity/boxes/BoxOfEnemies.java b/core/src/zero1hd/polyjet/entity/boxes/BoxOfEnemies.java deleted file mode 100755 index d0c4d02..0000000 --- a/core/src/zero1hd/polyjet/entity/boxes/BoxOfEnemies.java +++ /dev/null @@ -1,57 +0,0 @@ -package zero1hd.polyjet.entity.boxes; - -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.utils.Array; -import com.badlogic.gdx.utils.Pool; - -import zero1hd.polyjet.entity.Entities; -import zero1hd.polyjet.entity.Entity; -import zero1hd.polyjet.entity.enemies.VoidCircle; - -public class BoxOfEnemies { - private final Array ActiveEnemies; - - private final Pool voidCirclePool; - - public BoxOfEnemies(final ShapeRenderer shapeRenderer) { - ActiveEnemies = new Array(); - - voidCirclePool = new Pool() { - @Override - protected VoidCircle newObject() { - return new VoidCircle(shapeRenderer); - } - }; - } - - public Entity getEntity(Entities entity) { - switch (entity) { - case VOID_CIRCLE: - VoidCircle voidCircle = voidCirclePool.obtain(); - ActiveEnemies.add(voidCircle); - return voidCircle; - case BAR_BEAT: - return null; - case SHARDS: - return null; - default: - return null; - } - } - - public void free(Entity entity, Entities type) { - switch (type) { - case BAR_BEAT: - break; - case SHARDS: - break; - case VOID_CIRCLE: - ActiveEnemies.removeValue(entity, true); - voidCirclePool.free((VoidCircle) entity); - break; - default: - break; - - } - } -} diff --git a/core/src/zero1hd/polyjet/entity/enemies/VoidCircle.java b/core/src/zero1hd/polyjet/entity/enemies/VoidCircle.java index 2aa3a36..f55634e 100755 --- a/core/src/zero1hd/polyjet/entity/enemies/VoidCircle.java +++ b/core/src/zero1hd/polyjet/entity/enemies/VoidCircle.java @@ -102,4 +102,9 @@ public class VoidCircle extends Actor implements Entity, Poolable { @Override public void collided(Entity entity) { } + + @Override + public boolean isDead() { + return done; + } } diff --git a/core/src/zero1hd/polyjet/screens/GameScreen.java b/core/src/zero1hd/polyjet/screens/GameScreen.java index a33c1bb..8ee0b28 100755 --- a/core/src/zero1hd/polyjet/screens/GameScreen.java +++ b/core/src/zero1hd/polyjet/screens/GameScreen.java @@ -42,7 +42,8 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { public GameScreen(Polyjet polyJet) { core = polyJet; - + shapeRenderer = new ShapeRenderer(); + // Overlay stuff overlay = new Stage(); scoreLabel = new Label("Score: 0", core.getDefaultSkin(), "default-font", Color.WHITE); @@ -70,7 +71,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { @Override public void act(float delta) { setHeight( - ((float) gameArea.getHealth() / (float) gameArea.getMaxHealth()) * (healthBarTank.getHeight())); + ((float) gameArea.getPolyjet().health / (float) gameArea.getMaxHealth()) * (healthBarTank.getHeight())); super.act(delta); } }; @@ -90,7 +91,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { @Override public void changed(ChangeEvent event, Actor actor) { - restart(); + reBegin(); } }); } @@ -100,7 +101,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { (Gdx.graphics.getHeight() - pauseMenu.getHeight()) / 2); // Continue to add things to input multiplexer - gameArea = new GamePlayArea(core); + gameArea = new GamePlayArea(polyJet.getAssetManager(), shapeRenderer); inputs = new InputMultiplexer(); inputs.addProcessor(this); @@ -136,7 +137,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { scoreLabel.setText("Score: " + gameArea.getScore()); gameArea.act(delta); - if (gameArea.getHealth() == 0) { + if (gameArea.getPolyjet().isDead()) { end(false); } @@ -186,7 +187,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { super.resume(); } - public void restart() { + public void reBegin() { paused = false; pauseMenu.remove(); music.getPlaybackMusic().play(); @@ -214,7 +215,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { case Keys.ESCAPE: if (paused) { - restart(); + reBegin(); } else { pause(); } diff --git a/core/src/zero1hd/polyjet/ui/stages/GamePlayArea.java b/core/src/zero1hd/polyjet/ui/stages/GamePlayArea.java index ca3bb52..830303f 100755 --- a/core/src/zero1hd/polyjet/ui/stages/GamePlayArea.java +++ b/core/src/zero1hd/polyjet/ui/stages/GamePlayArea.java @@ -1,87 +1,90 @@ package zero1hd.polyjet.ui.stages; +import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.utils.viewport.FitViewport; import zero1hd.polyjet.Polyjet; import zero1hd.polyjet.controls.KeyMap; +import zero1hd.polyjet.entity.CollisionDetector; +import zero1hd.polyjet.entity.Entities; +import zero1hd.polyjet.entity.EntityController; +import zero1hd.polyjet.entity.ally.Laser; import zero1hd.polyjet.entity.ally.PolyJetEntity; public class GamePlayArea extends Stage { - public PolyJetEntity polyJet; + public PolyJetEntity polyjet; + + private EntityController entityController; + private CollisionDetector collisionDetector; - private float health = 50; private float maxHealth = 100; private float yTeleport = Polyjet.GAME_AREA_HEIGHT/2; private int score; Texture basicLaserTexture; - public GamePlayArea(Polyjet core) { + public GamePlayArea(AssetManager assetManager, ShapeRenderer shapeRenderer) { super(new FitViewport(Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT)); - polyJet = new PolyJetEntity(core, 48f, "standard"); - addActor(polyJet); + polyjet = new PolyJetEntity(assetManager, 48f, "standard"); + + entityController = new EntityController(shapeRenderer, assetManager); + collisionDetector = new CollisionDetector(entityController.ACTIVE_ALLIES, entityController.ACTIVE_ENEMIES); + addActor(polyjet); } - public void init() { - health = 50; - maxHealth = 100; - } @Override public void act(float delta) { - health -= 2*delta; + collisionDetector.collisionCheck(); - if (health <= 0) { - health = 0; + if (polyjet.getX() <= 1) { + polyjet.moveLeft = false; + polyjet.setX(1f); } - if (polyJet.getX() <= 1) { - polyJet.moveLeft = false; - polyJet.setX(1f); + if (polyjet.getX() >= Polyjet.GAME_AREA_WIDTH-1-polyjet.getWidth()) { + polyjet.moveRight = false; + polyjet.setX(Polyjet.GAME_AREA_WIDTH-1f-polyjet.getWidth()); } - if (polyJet.getX() >= Polyjet.GAME_AREA_WIDTH-1-polyJet.getWidth()) { - polyJet.moveRight = false; - polyJet.setX(Polyjet.GAME_AREA_WIDTH-1f-polyJet.getWidth()); + if (polyjet.getY() >= Polyjet.GAME_AREA_HEIGHT - 1 - polyjet.getHeight()) { + polyjet.moveUp = false; + polyjet.setY(Polyjet.GAME_AREA_HEIGHT - 1 - polyjet.getHeight()); } - if (polyJet.getY() >= Polyjet.GAME_AREA_HEIGHT - 1 - polyJet.getHeight()) { - polyJet.moveUp = false; - polyJet.setY(Polyjet.GAME_AREA_HEIGHT - 1 - polyJet.getHeight()); - } - - if (polyJet.getY() <= 1) { - polyJet.moveDown = false; - polyJet.setY(1f); + if (polyjet.getY() <= 1) { + polyjet.moveDown = false; + polyjet.setY(1f); } super.act(delta); } public void addScore (int score) { this.score += score; - health += score; - if (health > maxHealth) { - health = maxHealth; + polyjet.health += score; + if (polyjet.health > maxHealth) { + polyjet.health = maxHealth; } } @Override public boolean keyDown(int keycode) { if (keycode == KeyMap.left) { - polyJet.moveLeft = true; + polyjet.moveLeft = true; } if (keycode == KeyMap.right) { - polyJet.moveRight = true; + polyjet.moveRight = true; } if (keycode == KeyMap.up) { - polyJet.moveUp = true; + polyjet.moveUp = true; } if (keycode == KeyMap.down) { - polyJet.moveDown = true; + polyjet.moveDown = true; } return false; } @@ -89,28 +92,30 @@ public class GamePlayArea extends Stage { @Override public boolean keyUp(int keycode) { if (keycode == KeyMap.left) { - polyJet.moveLeft = false; + polyjet.moveLeft = false; } if (keycode == KeyMap.right) { - polyJet.moveRight = false; + polyjet.moveRight = false; } if (keycode == KeyMap.up) { - polyJet.moveUp = false; + polyjet.moveUp = false; } if (keycode == KeyMap.down) { - polyJet.moveDown = false; + polyjet.moveDown = false; } if (keycode == KeyMap.shoot) { + Laser laser = (Laser) entityController.retrieveEntity(Entities.LASER); + laser.init(polyjet.getX() + (polyjet.getWidth()-laser.getWidth())/2, polyjet.getY() + polyjet.getHeight()+0.25f, 30f); } return false; } - public int getScore() { - return score; + public PolyJetEntity getPolyjet() { + return polyjet; } - public float getHealth() { - return health; + public int getScore() { + return score; } public float getMaxHealth() {