From 1bc44e0b874a883d901eca8c75faba792f20a6db Mon Sep 17 00:00:00 2001 From: Recrown Date: Mon, 29 May 2017 20:04:02 -0500 Subject: [PATCH] began work on collision detection and added first enemy --- .../zero1hd/polyjet/entity/BoxOfEnemies.java | 45 ++++++++ .../polyjet/entity/CollisionDetector.java | 26 +++++ core/src/zero1hd/polyjet/entity/Entities.java | 2 +- core/src/zero1hd/polyjet/entity/Entity.java | 61 +++-------- .../zero1hd/polyjet/entity/PolyJetEntity.java | 26 ++++- .../zero1hd/polyjet/entity/VoidCircle.java | 102 ++++++++++++++++++ .../zero1hd/polyjet/screens/GameScreen.java | 19 ++-- 7 files changed, 224 insertions(+), 57 deletions(-) create mode 100755 core/src/zero1hd/polyjet/entity/BoxOfEnemies.java create mode 100755 core/src/zero1hd/polyjet/entity/CollisionDetector.java mode change 100644 => 100755 core/src/zero1hd/polyjet/entity/Entity.java create mode 100755 core/src/zero1hd/polyjet/entity/VoidCircle.java diff --git a/core/src/zero1hd/polyjet/entity/BoxOfEnemies.java b/core/src/zero1hd/polyjet/entity/BoxOfEnemies.java new file mode 100755 index 0000000..ce19237 --- /dev/null +++ b/core/src/zero1hd/polyjet/entity/BoxOfEnemies.java @@ -0,0 +1,45 @@ +package zero1hd.polyjet.entity; + +import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.Pool; +import com.badlogic.gdx.utils.Pools; + +public class BoxOfEnemies { + private final Pool voidCirclePool = Pools.get(VoidCircle.class); + private final Array ActiveEnemies; + + public BoxOfEnemies() { + ActiveEnemies = new Array(); + } + + 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/CollisionDetector.java b/core/src/zero1hd/polyjet/entity/CollisionDetector.java new file mode 100755 index 0000000..4d4bff9 --- /dev/null +++ b/core/src/zero1hd/polyjet/entity/CollisionDetector.java @@ -0,0 +1,26 @@ +package zero1hd.polyjet.entity; + +import com.badlogic.gdx.utils.Array; + +public class CollisionDetector { + Array firstGroup; + Array secondGroup; + + public CollisionDetector(Array firstGroup, Array secondGroup) { + this.firstGroup = firstGroup; + this.secondGroup = secondGroup; + } + + public void collisionCheck() { + while (firstGroup.iterator().hasNext()) { + Entity firstEntity = firstGroup.iterator().next(); + while (secondGroup.iterator().hasNext()) { + Entity secondEntity = secondGroup.iterator().next(); + if (firstEntity.getHitZone().overlaps(secondEntity.getHitZone())) { + firstEntity.collided(secondEntity); + secondEntity.collided(firstEntity); + } + } + } + } +} diff --git a/core/src/zero1hd/polyjet/entity/Entities.java b/core/src/zero1hd/polyjet/entity/Entities.java index 6ae0576..ef923b2 100755 --- a/core/src/zero1hd/polyjet/entity/Entities.java +++ b/core/src/zero1hd/polyjet/entity/Entities.java @@ -1,7 +1,7 @@ package zero1hd.polyjet.entity; public enum Entities { - BAR_BEAT, VOID_CIRCLE, SHARDS; + POLYJET, BAR_BEAT, VOID_CIRCLE, SHARDS; public float x; public float y; diff --git a/core/src/zero1hd/polyjet/entity/Entity.java b/core/src/zero1hd/polyjet/entity/Entity.java old mode 100644 new mode 100755 index 1660400..533c015 --- a/core/src/zero1hd/polyjet/entity/Entity.java +++ b/core/src/zero1hd/polyjet/entity/Entity.java @@ -1,52 +1,23 @@ package zero1hd.polyjet.entity; import com.badlogic.gdx.math.Rectangle; -import com.badlogic.gdx.scenes.scene2d.Actor; -public class Entity extends Actor { - private Rectangle box; +public interface Entity { + /** + * Called whenever a collision is detected + * @param entity is the entity that hit this one. + */ + public void collided(Entity entity); - public Entity() { - box = new Rectangle(); - } + /** + * gets the box that represents the hit box to calculate whether there is a collision or not + * @return the object that represents the hit box + */ + public Rectangle getHitZone(); - @Override - public void setHeight(float height) { - box.height = height; - super.setHeight(height); - } - - @Override - public void setWidth(float width) { - box.width = width; - super.setWidth(width); - } - - @Override - public void setSize(float width, float height) { - box.setSize(width, height); - super.setSize(width, height); - } - - @Override - public void setX(float x) { - box.setX(x); - super.setX(x); - } - - @Override - public void setY(float y) { - box.setY(y); - super.setY(y); - } - - @Override - public void setPosition(float x, float y) { - box.setPosition(x, y); - super.setPosition(x, y); - } - - public Rectangle getBox() { - return box; - } + /** + * gets the type of entity this entity is + * @return the entity type + */ + public Entities getEntityType(); } diff --git a/core/src/zero1hd/polyjet/entity/PolyJetEntity.java b/core/src/zero1hd/polyjet/entity/PolyJetEntity.java index 4387a50..9a8e490 100755 --- a/core/src/zero1hd/polyjet/entity/PolyJetEntity.java +++ b/core/src/zero1hd/polyjet/entity/PolyJetEntity.java @@ -3,27 +3,27 @@ package zero1hd.polyjet.entity; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.ParticleEffect; +import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.actions.Actions; import zero1hd.polyjet.Polyjet; -public class PolyJetEntity extends Entity { +public class PolyJetEntity extends Actor implements Entity { private ParticleEffect thrust; private Texture polyjet; private ParticleEffect teleportCloak; - + private Rectangle hitbox; public boolean moveLeft, moveRight, moveUp, moveDown, teleporting; private float rate; public PolyJetEntity(Polyjet core, 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); thrust.start(); @@ -35,11 +35,12 @@ public class PolyJetEntity extends Entity { @Override public void act(float delta) { + hitbox.setPosition(getX(), getY()); + thrust.setPosition(getX()+(getWidth())/2 - 1f/16f, getY()-0.15f); thrust.update(delta); teleportCloak.setPosition(getX() +(getWidth()-1)/2, getY() + (getHeight()-1)/2); - super.act(delta); //Movement! if (moveLeft && !moveRight) { @@ -56,6 +57,7 @@ public class PolyJetEntity extends Entity { moveBy(0, -rate*delta); } + super.act(delta); } @Override @@ -64,4 +66,18 @@ public class PolyJetEntity extends Entity { batch.draw(polyjet, getX(), getY(), getWidth(), getHeight()); super.draw(batch, parentAlpha); } + + @Override + public Rectangle getHitZone() { + return hitbox; + } + + @Override + public Entities getEntityType() { + return Entities.POLYJET; + } + + @Override + public void collided(Entity entity) { + } } diff --git a/core/src/zero1hd/polyjet/entity/VoidCircle.java b/core/src/zero1hd/polyjet/entity/VoidCircle.java new file mode 100755 index 0000000..159b778 --- /dev/null +++ b/core/src/zero1hd/polyjet/entity/VoidCircle.java @@ -0,0 +1,102 @@ +package zero1hd.polyjet.entity; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.scenes.scene2d.Actor; +import com.badlogic.gdx.utils.Pool.Poolable; + +public class VoidCircle extends Actor implements Entity, Poolable { + float timer; + float endRadius; + private float currentRadius; + Rectangle hitBox; + ShapeRenderer shapeRenderer; + float growthRate; + boolean done; + + public VoidCircle(ShapeRenderer shapeRenderer) { + hitBox = new Rectangle(); + this.shapeRenderer = shapeRenderer; + } + + public void init(float endRadius, float x, float y, float growthRate, float warningTime) { + timer = warningTime; + this.endRadius = endRadius; + setSize(2*endRadius, 2*endRadius); + setX(x); + setY(y); + this.growthRate = growthRate; + } + + @Override + public void act(float delta) { + hitBox.setPosition(getX(), getY()); + if (timer > 0) { + timer -= delta; + } else { + if (currentRadius < endRadius) { + growCurrentRadius(delta*growthRate); + } else { + endRadius = -1f; + if (currentRadius > 0) { + growCurrentRadius(delta*-3*growthRate); + } else { + done = true; + } + } + } + super.act(delta); + } + + @Override + public void draw(Batch batch, float parentAlpha) { + if (timer <= 0) { + shapeRenderer.set(ShapeType.Filled); + shapeRenderer.setColor(Color.BLACK); + shapeRenderer.circle(getX(), getY(), currentRadius); + } + shapeRenderer.set(ShapeType.Line); + shapeRenderer.setColor(Color.RED); + shapeRenderer.circle(getX(), getY(), endRadius); + super.draw(batch, parentAlpha); + } + + @Override + public void reset() { + hitBox.set(0, 0, 0, 0); + currentRadius = 0; + growthRate = 0; + timer = 0; + endRadius = 0; + done = false; + } + + public void setCurrentRadius(float currentRadius) { + this.currentRadius = currentRadius; + hitBox.setSize(2*(currentRadius*currentRadius)); + hitBox.setCenter(getX(), getY()); + } + + public void growCurrentRadius(float currentRadius) { + this.currentRadius += currentRadius; + hitBox.setSize(2*(currentRadius*currentRadius)); + hitBox.setCenter(getX(), getY()); + } + + @Override + public Rectangle getHitZone() { + return hitBox; + } + + @Override + public Entities getEntityType() { + return Entities.VOID_CIRCLE; + } + + @Override + public void collided(Entity entity) { + } +} diff --git a/core/src/zero1hd/polyjet/screens/GameScreen.java b/core/src/zero1hd/polyjet/screens/GameScreen.java index 782a325..a33c1bb 100755 --- a/core/src/zero1hd/polyjet/screens/GameScreen.java +++ b/core/src/zero1hd/polyjet/screens/GameScreen.java @@ -7,6 +7,7 @@ import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.ScreenAdapter; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; @@ -37,6 +38,8 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { private AudioData music; + private ShapeRenderer shapeRenderer; + public GameScreen(Polyjet polyJet) { core = polyJet; @@ -87,7 +90,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { @Override public void changed(ChangeEvent event, Actor actor) { - recommence(); + restart(); } }); } @@ -120,13 +123,16 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { Gdx.gl.glClearColor(1f, 1f, 1f, 1f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); gameArea.getViewport().apply(); + + shapeRenderer.begin(); gameArea.draw(); - + shapeRenderer.end(); + + overlay.getViewport().apply(); overlay.draw(); if (!paused) { music.readIndexUpdate(); - gameArea.setAudioIndex(music.getReadIndex()); scoreLabel.setText("Score: " + gameArea.getScore()); gameArea.act(delta); @@ -135,12 +141,13 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { } overlay.act(delta); - } else { } if (!music.getPlaybackMusic().isPlaying()) { end(true); } + + super.render(delta); } private void end(boolean trueEnd) { @@ -179,7 +186,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { super.resume(); } - public void recommence() { + public void restart() { paused = false; pauseMenu.remove(); music.getPlaybackMusic().play(); @@ -207,7 +214,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor { case Keys.ESCAPE: if (paused) { - recommence(); + restart(); } else { pause(); }