new entity management and hit detection concepts, began implementing in game play area

This commit is contained in:
Harrison Deng 2017-05-30 16:23:08 -05:00
parent 562dbb8117
commit 4894d6eea8
9 changed files with 174 additions and 115 deletions

View File

@ -20,4 +20,10 @@ public interface Entity {
* @return the entity type * @return the entity type
*/ */
public Entities getEntityType(); 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();
} }

View File

@ -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<Entity> ACTIVE_ALLIES;
public final Array<Entity> ACTIVE_ENEMIES;
//Enemy pool declaration;
private final Pool<VoidCircle> VOID_CIRCLE_POOL;
//Ally pool declaration;
private final Pool<Laser> LASER_POOL;
public EntityController(ShapeRenderer shapeRenderer, AssetManager assetManager) {
ACTIVE_ALLIES = new Array<Entity>();
ACTIVE_ENEMIES = new Array<Entity>();
//Enemy pool initialization;
VOID_CIRCLE_POOL = new Pool<VoidCircle>() {
@Override
protected VoidCircle newObject() {
return new VoidCircle(shapes);
}
};
//Ally pool initialization;
LASER_POOL = new Pool<Laser>() {
@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;
}
}
}

View File

@ -6,12 +6,14 @@ import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Pool.Poolable; import com.badlogic.gdx.utils.Pool.Poolable;
import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.entity.Entities; import zero1hd.polyjet.entity.Entities;
import zero1hd.polyjet.entity.Entity; import zero1hd.polyjet.entity.Entity;
public class Laser extends Actor implements Entity, Poolable { public class Laser extends Actor implements Entity, Poolable {
private Rectangle hitBox; private Rectangle hitBox;
private float rate; private float rate;
boolean dead;
Texture laserTexture; Texture laserTexture;
public Laser(Texture laserTexture) { public Laser(Texture laserTexture) {
@ -33,6 +35,10 @@ public class Laser extends Actor implements Entity, Poolable {
hitBox.setX(getX()); hitBox.setX(getX());
hitBox.setY(getY()); hitBox.setY(getY());
super.act(delta); super.act(delta);
if (getY() > Polyjet.GAME_AREA_HEIGHT) {
dead = true;
}
} }
@Override @Override
@ -48,6 +54,7 @@ public class Laser extends Actor implements Entity, Poolable {
setSize(0, 0); setSize(0, 0);
rate = 0; rate = 0;
hitBox.set(0, 0, 0, 0); hitBox.set(0, 0, 0, 0);
dead = false;
} }
@Override @Override
@ -64,4 +71,9 @@ public class Laser extends Actor implements Entity, Poolable {
return Entities.LASER; return Entities.LASER;
} }
@Override
public boolean isDead() {
return dead;
}
} }

View File

@ -1,5 +1,6 @@
package zero1hd.polyjet.entity.ally; package zero1hd.polyjet.entity.ally;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.ParticleEffect; import com.badlogic.gdx.graphics.g2d.ParticleEffect;
@ -12,7 +13,7 @@ import zero1hd.polyjet.entity.Entities;
import zero1hd.polyjet.entity.Entity; import zero1hd.polyjet.entity.Entity;
public class PolyJetEntity extends Actor implements Entity { public class PolyJetEntity extends Actor implements Entity {
public float health;
private ParticleEffect thrust; private ParticleEffect thrust;
private Texture polyjet; private Texture polyjet;
private ParticleEffect teleportCloak; private ParticleEffect teleportCloak;
@ -20,17 +21,17 @@ public class PolyJetEntity extends Actor implements Entity {
public boolean moveLeft, moveRight, moveUp, moveDown, teleporting; public boolean moveLeft, moveRight, moveUp, moveDown, teleporting;
private float rate; private float rate;
public PolyJetEntity(Polyjet core, float rate, String jet) { public PolyJetEntity(AssetManager assets, float rate, String jet) {
this.rate = rate; this.rate = rate;
setSize(1.5f, 1.5f); setSize(1.5f, 1.5f);
setPosition(Polyjet.GAME_AREA_WIDTH/2 - getWidth()/2, -4f); setPosition(Polyjet.GAME_AREA_WIDTH/2 - getWidth()/2, -4f);
hitbox = new Rectangle(getX(), getY(), getWidth(), getHeight()); hitbox = new Rectangle(getX(), getY(), getWidth(), getHeight());
polyjet = core.getAssetManager().get("polyjet-" + jet + ".png", Texture.class); polyjet = assets.get("polyjet-" + jet + ".png", Texture.class);
thrust = core.getAssetManager().get("standard_thrust.p", ParticleEffect.class); thrust = assets.get("standard_thrust.p", ParticleEffect.class);
thrust.start(); 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)); addAction(Actions.moveTo(getX(), 4f, 0.5f));
} }
@ -82,4 +83,13 @@ public class PolyJetEntity extends Actor implements Entity {
@Override @Override
public void collided(Entity entity) { public void collided(Entity entity) {
} }
@Override
public boolean isDead() {
if (health <= 0) {
return true;
} else {
return false;
}
}
} }

View File

@ -1,5 +0,0 @@
package zero1hd.polyjet.entity.boxes;
public class BoxOfAllies {
}

View File

@ -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<Entity> ActiveEnemies;
private final Pool<VoidCircle> voidCirclePool;
public BoxOfEnemies(final ShapeRenderer shapeRenderer) {
ActiveEnemies = new Array<Entity>();
voidCirclePool = new Pool<VoidCircle>() {
@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;
}
}
}

View File

@ -102,4 +102,9 @@ public class VoidCircle extends Actor implements Entity, Poolable {
@Override @Override
public void collided(Entity entity) { public void collided(Entity entity) {
} }
@Override
public boolean isDead() {
return done;
}
} }

View File

@ -42,7 +42,8 @@ public class GameScreen extends ScreenAdapter implements InputProcessor {
public GameScreen(Polyjet polyJet) { public GameScreen(Polyjet polyJet) {
core = polyJet; core = polyJet;
shapeRenderer = new ShapeRenderer();
// Overlay stuff // Overlay stuff
overlay = new Stage(); overlay = new Stage();
scoreLabel = new Label("Score: 0", core.getDefaultSkin(), "default-font", Color.WHITE); scoreLabel = new Label("Score: 0", core.getDefaultSkin(), "default-font", Color.WHITE);
@ -70,7 +71,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor {
@Override @Override
public void act(float delta) { public void act(float delta) {
setHeight( setHeight(
((float) gameArea.getHealth() / (float) gameArea.getMaxHealth()) * (healthBarTank.getHeight())); ((float) gameArea.getPolyjet().health / (float) gameArea.getMaxHealth()) * (healthBarTank.getHeight()));
super.act(delta); super.act(delta);
} }
}; };
@ -90,7 +91,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { 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); (Gdx.graphics.getHeight() - pauseMenu.getHeight()) / 2);
// Continue to add things to input multiplexer // Continue to add things to input multiplexer
gameArea = new GamePlayArea(core); gameArea = new GamePlayArea(polyJet.getAssetManager(), shapeRenderer);
inputs = new InputMultiplexer(); inputs = new InputMultiplexer();
inputs.addProcessor(this); inputs.addProcessor(this);
@ -136,7 +137,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor {
scoreLabel.setText("Score: " + gameArea.getScore()); scoreLabel.setText("Score: " + gameArea.getScore());
gameArea.act(delta); gameArea.act(delta);
if (gameArea.getHealth() == 0) { if (gameArea.getPolyjet().isDead()) {
end(false); end(false);
} }
@ -186,7 +187,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor {
super.resume(); super.resume();
} }
public void restart() { public void reBegin() {
paused = false; paused = false;
pauseMenu.remove(); pauseMenu.remove();
music.getPlaybackMusic().play(); music.getPlaybackMusic().play();
@ -214,7 +215,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor {
case Keys.ESCAPE: case Keys.ESCAPE:
if (paused) { if (paused) {
restart(); reBegin();
} else { } else {
pause(); pause();
} }

View File

@ -1,87 +1,90 @@
package zero1hd.polyjet.ui.stages; package zero1hd.polyjet.ui.stages;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.FitViewport;
import zero1hd.polyjet.Polyjet; import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.controls.KeyMap; 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; import zero1hd.polyjet.entity.ally.PolyJetEntity;
public class GamePlayArea extends Stage { 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 maxHealth = 100;
private float yTeleport = Polyjet.GAME_AREA_HEIGHT/2; private float yTeleport = Polyjet.GAME_AREA_HEIGHT/2;
private int score; private int score;
Texture basicLaserTexture; Texture basicLaserTexture;
public GamePlayArea(Polyjet core) { public GamePlayArea(AssetManager assetManager, ShapeRenderer shapeRenderer) {
super(new FitViewport(Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT)); super(new FitViewport(Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT));
polyJet = new PolyJetEntity(core, 48f, "standard"); polyjet = new PolyJetEntity(assetManager, 48f, "standard");
addActor(polyJet);
entityController = new EntityController(shapeRenderer, assetManager);
collisionDetector = new CollisionDetector(entityController.ACTIVE_ALLIES, entityController.ACTIVE_ENEMIES);
addActor(polyjet);
} }
public void init() {
health = 50;
maxHealth = 100;
}
@Override @Override
public void act(float delta) { public void act(float delta) {
health -= 2*delta; collisionDetector.collisionCheck();
if (health <= 0) { if (polyjet.getX() <= 1) {
health = 0; polyjet.moveLeft = false;
polyjet.setX(1f);
} }
if (polyJet.getX() <= 1) { if (polyjet.getX() >= Polyjet.GAME_AREA_WIDTH-1-polyjet.getWidth()) {
polyJet.moveLeft = false; polyjet.moveRight = false;
polyJet.setX(1f); polyjet.setX(Polyjet.GAME_AREA_WIDTH-1f-polyjet.getWidth());
} }
if (polyJet.getX() >= Polyjet.GAME_AREA_WIDTH-1-polyJet.getWidth()) { if (polyjet.getY() >= Polyjet.GAME_AREA_HEIGHT - 1 - polyjet.getHeight()) {
polyJet.moveRight = false; polyjet.moveUp = false;
polyJet.setX(Polyjet.GAME_AREA_WIDTH-1f-polyJet.getWidth()); polyjet.setY(Polyjet.GAME_AREA_HEIGHT - 1 - polyjet.getHeight());
} }
if (polyJet.getY() >= Polyjet.GAME_AREA_HEIGHT - 1 - polyJet.getHeight()) { if (polyjet.getY() <= 1) {
polyJet.moveUp = false; polyjet.moveDown = false;
polyJet.setY(Polyjet.GAME_AREA_HEIGHT - 1 - polyJet.getHeight()); polyjet.setY(1f);
}
if (polyJet.getY() <= 1) {
polyJet.moveDown = false;
polyJet.setY(1f);
} }
super.act(delta); super.act(delta);
} }
public void addScore (int score) { public void addScore (int score) {
this.score += score; this.score += score;
health += score; polyjet.health += score;
if (health > maxHealth) { if (polyjet.health > maxHealth) {
health = maxHealth; polyjet.health = maxHealth;
} }
} }
@Override @Override
public boolean keyDown(int keycode) { public boolean keyDown(int keycode) {
if (keycode == KeyMap.left) { if (keycode == KeyMap.left) {
polyJet.moveLeft = true; polyjet.moveLeft = true;
} }
if (keycode == KeyMap.right) { if (keycode == KeyMap.right) {
polyJet.moveRight = true; polyjet.moveRight = true;
} }
if (keycode == KeyMap.up) { if (keycode == KeyMap.up) {
polyJet.moveUp = true; polyjet.moveUp = true;
} }
if (keycode == KeyMap.down) { if (keycode == KeyMap.down) {
polyJet.moveDown = true; polyjet.moveDown = true;
} }
return false; return false;
} }
@ -89,28 +92,30 @@ public class GamePlayArea extends Stage {
@Override @Override
public boolean keyUp(int keycode) { public boolean keyUp(int keycode) {
if (keycode == KeyMap.left) { if (keycode == KeyMap.left) {
polyJet.moveLeft = false; polyjet.moveLeft = false;
} }
if (keycode == KeyMap.right) { if (keycode == KeyMap.right) {
polyJet.moveRight = false; polyjet.moveRight = false;
} }
if (keycode == KeyMap.up) { if (keycode == KeyMap.up) {
polyJet.moveUp = false; polyjet.moveUp = false;
} }
if (keycode == KeyMap.down) { if (keycode == KeyMap.down) {
polyJet.moveDown = false; polyjet.moveDown = false;
} }
if (keycode == KeyMap.shoot) { 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; return false;
} }
public int getScore() { public PolyJetEntity getPolyjet() {
return score; return polyjet;
} }
public float getHealth() { public int getScore() {
return health; return score;
} }
public float getMaxHealth() { public float getMaxHealth() {