began work on collision detection and added first enemy

This commit is contained in:
Harrison Deng 2017-05-29 20:04:02 -05:00
parent 279658daa0
commit 1bc44e0b87
7 changed files with 224 additions and 57 deletions

View File

@ -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<VoidCircle> voidCirclePool = Pools.get(VoidCircle.class);
private final Array<Entity> ActiveEnemies;
public BoxOfEnemies() {
ActiveEnemies = new Array<Entity>();
}
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

@ -0,0 +1,26 @@
package zero1hd.polyjet.entity;
import com.badlogic.gdx.utils.Array;
public class CollisionDetector {
Array<Entity> firstGroup;
Array<Entity> secondGroup;
public CollisionDetector(Array<Entity> firstGroup, Array<Entity> 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);
}
}
}
}
}

View File

@ -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;

61
core/src/zero1hd/polyjet/entity/Entity.java Normal file → Executable file
View File

@ -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();
}

View File

@ -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) {
}
}

View File

@ -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) {
}
}

View File

@ -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();
}