small cleanup, add report
This commit is contained in:
0
README2.md
Normal file
0
README2.md
Normal file
@@ -1,7 +1,5 @@
|
|||||||
package org.lumijiez.bugger;
|
package org.lumijiez.bugger;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
|
||||||
import com.badlogic.gdx.graphics.GL20;
|
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
@@ -9,12 +7,13 @@ import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
|
|||||||
import com.badlogic.gdx.physics.box2d.World;
|
import com.badlogic.gdx.physics.box2d.World;
|
||||||
import org.lumijiez.bugger.entities.Player;
|
import org.lumijiez.bugger.entities.Player;
|
||||||
import org.lumijiez.bugger.handlers.*;
|
import org.lumijiez.bugger.handlers.*;
|
||||||
|
import org.lumijiez.bugger.util.GameSystemsFacade;
|
||||||
|
|
||||||
public class Bugger {
|
public class Bugger {
|
||||||
public static float deltaTime = 0f;
|
public static float deltaTime = 0f;
|
||||||
private static Bugger instance;
|
private static Bugger instance;
|
||||||
private final World world = new World(new Vector2(0, 0), true);
|
private final World world = new World(new Vector2(0, 0), true);
|
||||||
private final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
|
public final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
|
||||||
public static ShapeRenderer shapeRenderer = new ShapeRenderer();
|
public static ShapeRenderer shapeRenderer = new ShapeRenderer();
|
||||||
public static SpriteBatch spriteBatch = new SpriteBatch();
|
public static SpriteBatch spriteBatch = new SpriteBatch();
|
||||||
public static SpriteBatch uiBatch = new SpriteBatch();
|
public static SpriteBatch uiBatch = new SpriteBatch();
|
||||||
@@ -34,41 +33,7 @@ public class Bugger {
|
|||||||
|
|
||||||
public void cycle(float delta) {
|
public void cycle(float delta) {
|
||||||
deltaTime = delta;
|
deltaTime = delta;
|
||||||
renderClear();
|
GameSystemsFacade.getInstance().update(delta);
|
||||||
SpaceVFXHandler.getInstance().render();
|
|
||||||
|
|
||||||
step();
|
|
||||||
|
|
||||||
ProjectileHandler.getInstance().cycle(delta);
|
|
||||||
EnemyProjectileHandler.getInstance().cycle(delta);
|
|
||||||
EnemyHandler.getInstance().cycle();
|
|
||||||
ParticleHandler.getInstance().cycle(delta);
|
|
||||||
|
|
||||||
CleanupHandler.getInstance().tryClean();
|
|
||||||
|
|
||||||
CameraHandler.getInstance().updateCamera();
|
|
||||||
|
|
||||||
Player.getInstance().render();
|
|
||||||
|
|
||||||
EnemyHandler.getInstance().render(delta);
|
|
||||||
|
|
||||||
if (InterfaceHandler.getInstance().isDebug()) renderDebug();
|
|
||||||
|
|
||||||
InterfaceHandler.getInstance().renderUI();
|
|
||||||
InputHandler.getInstance().handleInput();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void renderClear() {
|
|
||||||
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
|
|
||||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void renderDebug() {
|
|
||||||
debugRenderer.render(world, spriteBatch.getProjectionMatrix());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void step() {
|
|
||||||
world.step((float) 1 / 30f, 6, 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
|
|||||||
@@ -37,9 +37,9 @@ public abstract class Entity {
|
|||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render() {
|
public void render(int sizeOffsetX, int sizeOffsetY) {
|
||||||
sprite.setOrigin(size / 2, size / 2);
|
sprite.setOrigin(size / 2, size / 2);
|
||||||
sprite.setSize(size, size);
|
sprite.setSize(size + sizeOffsetX, size + sizeOffsetY);
|
||||||
sprite.setPosition(body.getPosition().x - size / 2, body.getPosition().y - size / 2);
|
sprite.setPosition(body.getPosition().x - size / 2, body.getPosition().y - size / 2);
|
||||||
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
||||||
Bugger.spriteBatch.begin();
|
Bugger.spriteBatch.begin();
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class Player extends Entity {
|
|||||||
public void render() {
|
public void render() {
|
||||||
handleInput();
|
handleInput();
|
||||||
updateSpriteRotation();
|
updateSpriteRotation();
|
||||||
super.render();
|
super.render(0, 0);
|
||||||
renderHealthBar();
|
renderHealthBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,6 @@ public class EnemyEntity extends Entity {
|
|||||||
super(world, texturePath, size);
|
super(world, texturePath, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void update(Vector2 target) {
|
|
||||||
// follow(target);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
Vector2 playerPos = Player.getInstance().getPosition();
|
Vector2 playerPos = Player.getInstance().getPosition();
|
||||||
follow(playerPos);
|
follow(playerPos);
|
||||||
@@ -40,17 +36,12 @@ public class EnemyEntity extends Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void cycle(Vector2 target) {
|
|
||||||
// update(target);
|
|
||||||
// render();
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void cycle() {
|
public void cycle() {
|
||||||
update();
|
update();
|
||||||
render();
|
render();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render() {
|
public void render() {
|
||||||
super.render();
|
super.render(0,0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
package org.lumijiez.bugger.entities.weapons;
|
|
||||||
|
|
||||||
import com.badlogic.gdx.math.Vector2;
|
|
||||||
import com.badlogic.gdx.physics.box2d.*;
|
|
||||||
import org.lumijiez.bugger.entities.Entity;
|
|
||||||
|
|
||||||
public abstract class EnemyProjectile extends Entity {
|
|
||||||
|
|
||||||
protected float timeAlive = 0f;
|
|
||||||
protected boolean isEnemy = false;
|
|
||||||
|
|
||||||
public EnemyProjectile(World world, String texturePath, float size, boolean isEnemy) {
|
|
||||||
super(world, texturePath, size);
|
|
||||||
this.isEnemy = isEnemy;
|
|
||||||
this.body = createBody(0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public EnemyProjectile(World world, Vector2 position, Vector2 direction, String texturePath, float size) {
|
|
||||||
super(world, texturePath, size);
|
|
||||||
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 1f));
|
|
||||||
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
|
||||||
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
|
||||||
this.body.setLinearVelocity(direction.nor().scl(5000f));
|
|
||||||
}
|
|
||||||
|
|
||||||
public EnemyProjectile(World world, Vector2 position, Vector2 direction, String texturePath, float size, float speed) {
|
|
||||||
super(world, texturePath, size);
|
|
||||||
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 1f));
|
|
||||||
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
|
||||||
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
|
||||||
this.body.setLinearVelocity(direction.nor().scl(speed));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Body createBody(float x, float y) {
|
|
||||||
BodyDef bodyDef = new BodyDef();
|
|
||||||
bodyDef.position.set(x, y);
|
|
||||||
bodyDef.type = BodyDef.BodyType.DynamicBody;
|
|
||||||
bodyDef.gravityScale = 0;
|
|
||||||
|
|
||||||
Body body = world.createBody(bodyDef);
|
|
||||||
PolygonShape shape = new PolygonShape();
|
|
||||||
shape.setAsBox(size / 3, size + 2);
|
|
||||||
|
|
||||||
FixtureDef fixtureDef = new FixtureDef();
|
|
||||||
fixtureDef.shape = shape;
|
|
||||||
fixtureDef.isSensor = true;
|
|
||||||
body.createFixture(fixtureDef);
|
|
||||||
|
|
||||||
shape.dispose();
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update(float delta) {
|
|
||||||
timeAlive += delta;
|
|
||||||
float lifetime = 3f;
|
|
||||||
if (timeAlive >= lifetime) {
|
|
||||||
markedToDestroy = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEnemy() {
|
|
||||||
return isEnemy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void render() {
|
|
||||||
super.render();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
markedToDestroy = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
package org.lumijiez.bugger.entities.weapons;
|
|
||||||
|
|
||||||
import com.badlogic.gdx.math.Vector2;
|
|
||||||
import com.badlogic.gdx.physics.box2d.World;
|
|
||||||
import org.lumijiez.bugger.Bugger;
|
|
||||||
|
|
||||||
public class EnemyRay extends EnemyProjectile {
|
|
||||||
float speed = 5000f;
|
|
||||||
|
|
||||||
public EnemyRay(World world, boolean isEnemy) {
|
|
||||||
super(world,"images/enemyblaze.png", 5f, isEnemy);
|
|
||||||
}
|
|
||||||
|
|
||||||
public EnemyRay(World world, Vector2 position, Vector2 direction) {
|
|
||||||
super(world, position, direction, "images/enemyblaze.png", 5f);
|
|
||||||
this.body.setUserData(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public EnemyRay(World world, Vector2 position, Vector2 direction, float speed) {
|
|
||||||
super(world, position, direction, "images/enemyblaze.png", 5f, speed);
|
|
||||||
this.speed = speed;
|
|
||||||
this.body.setUserData(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init(Vector2 position, Vector2 direction, boolean isEnemy) {
|
|
||||||
this.isEnemy = isEnemy;
|
|
||||||
this.body.setTransform(position, (float) (direction.angleRad() + Math.toRadians(270f)));
|
|
||||||
this.body.setLinearVelocity(direction.nor().scl(speed));
|
|
||||||
this.body.setUserData(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reset() {
|
|
||||||
timeAlive = 0f;
|
|
||||||
markedToDestroy = false;
|
|
||||||
this.isEnemy = false;
|
|
||||||
this.body.setLinearVelocity(Vector2.Zero);
|
|
||||||
this.body.setTransform(Vector2.Zero, 0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void render() {
|
|
||||||
sprite.setOrigin(size / 2, size / 2);
|
|
||||||
sprite.setSize(size, size + 5);
|
|
||||||
sprite.setPosition(body.getPosition().x - size / 2, body.getPosition().y - size / 2);
|
|
||||||
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
|
||||||
Bugger.spriteBatch.begin();
|
|
||||||
sprite.draw(Bugger.spriteBatch);
|
|
||||||
Bugger.spriteBatch.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,33 +4,40 @@ import com.badlogic.gdx.math.Vector2;
|
|||||||
import com.badlogic.gdx.physics.box2d.*;
|
import com.badlogic.gdx.physics.box2d.*;
|
||||||
import org.lumijiez.bugger.entities.Entity;
|
import org.lumijiez.bugger.entities.Entity;
|
||||||
|
|
||||||
public abstract class Projectile extends Entity {
|
public class Projectile extends Entity {
|
||||||
|
private static final String ENEMY_TEXTURE = "images/enemyblaze.png";
|
||||||
|
private static final String PLAYER_TEXTURE = "images/blaze.png";
|
||||||
|
|
||||||
protected float timeAlive = 0f;
|
protected float timeAlive = 0f;
|
||||||
protected boolean isEnemy = false;
|
protected boolean isEnemy;
|
||||||
|
protected float speed = 5000f;
|
||||||
|
|
||||||
public Projectile(World world, String texturePath, float size, boolean isEnemy) {
|
public Projectile(World world, boolean isEnemy) {
|
||||||
super(world, texturePath, size);
|
super(world, isEnemy ? ENEMY_TEXTURE : PLAYER_TEXTURE, 5f);
|
||||||
this.isEnemy = isEnemy;
|
this.isEnemy = isEnemy;
|
||||||
this.body = createBody(0, 0);
|
this.body = createBody(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Projectile(World world, Vector2 position, Vector2 direction, String texturePath, float size) {
|
public Projectile(World world, Vector2 position, Vector2 direction, boolean isEnemy) {
|
||||||
super(world, texturePath, size);
|
super(world, isEnemy ? ENEMY_TEXTURE : PLAYER_TEXTURE, 5f);
|
||||||
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 1f));
|
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(5f + 1f));
|
||||||
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
this.isEnemy = isEnemy;
|
||||||
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
|
||||||
this.body.setLinearVelocity(direction.nor().scl(5000f));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Projectile(World world, Vector2 position, Vector2 direction, String texturePath, float size, float speed) {
|
|
||||||
super(world, texturePath, size);
|
|
||||||
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 1f));
|
|
||||||
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
||||||
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
||||||
this.body.setLinearVelocity(direction.nor().scl(speed));
|
this.body.setLinearVelocity(direction.nor().scl(speed));
|
||||||
|
this.body.setUserData(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Projectile(World world, Vector2 position, Vector2 direction, boolean isEnemy, float speed) {
|
||||||
|
super(world, isEnemy ? ENEMY_TEXTURE : PLAYER_TEXTURE, 5f);
|
||||||
|
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(5f + 1f));
|
||||||
|
this.isEnemy = isEnemy;
|
||||||
|
this.speed = speed;
|
||||||
|
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
||||||
|
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
||||||
|
this.body.setLinearVelocity(direction.nor().scl(speed));
|
||||||
|
this.body.setUserData(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Body createBody(float x, float y) {
|
protected Body createBody(float x, float y) {
|
||||||
@@ -52,6 +59,21 @@ public abstract class Projectile extends Entity {
|
|||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void init(Vector2 position, Vector2 direction, boolean isEnemy) {
|
||||||
|
this.isEnemy = isEnemy;
|
||||||
|
this.body.setTransform(position, (float) (direction.angleRad() + Math.toRadians(270f)));
|
||||||
|
this.body.setLinearVelocity(direction.nor().scl(speed));
|
||||||
|
this.body.setUserData(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
timeAlive = 0f;
|
||||||
|
markedToDestroy = false;
|
||||||
|
this.isEnemy = false;
|
||||||
|
this.body.setLinearVelocity(Vector2.Zero);
|
||||||
|
this.body.setTransform(Vector2.Zero, 0f);
|
||||||
|
}
|
||||||
|
|
||||||
public void update(float delta) {
|
public void update(float delta) {
|
||||||
timeAlive += delta;
|
timeAlive += delta;
|
||||||
float lifetime = 3f;
|
float lifetime = 3f;
|
||||||
@@ -65,7 +87,7 @@ public abstract class Projectile extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void render() {
|
public void render() {
|
||||||
super.render();
|
super.render(0, 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
package org.lumijiez.bugger.entities.weapons;
|
|
||||||
|
|
||||||
import com.badlogic.gdx.math.Vector2;
|
|
||||||
import com.badlogic.gdx.physics.box2d.*;
|
|
||||||
import org.lumijiez.bugger.Bugger;
|
|
||||||
|
|
||||||
public class Ray extends Projectile {
|
|
||||||
float speed = 5000f;
|
|
||||||
|
|
||||||
public Ray(World world, boolean isEnemy) {
|
|
||||||
super(world,"images/blaze.png", 5f, isEnemy);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Ray(World world, Vector2 position, Vector2 direction) {
|
|
||||||
super(world, position, direction, "images/blaze.png", 5f);
|
|
||||||
this.body.setUserData(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Ray(World world, Vector2 position, Vector2 direction, float speed) {
|
|
||||||
super(world, position, direction, "images/blaze.png", 5f, speed);
|
|
||||||
this.speed = speed;
|
|
||||||
this.body.setUserData(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init(Vector2 position, Vector2 direction, boolean isEnemy) {
|
|
||||||
this.isEnemy = isEnemy;
|
|
||||||
this.body.setTransform(position, (float) (direction.angleRad() + Math.toRadians(270f)));
|
|
||||||
this.body.setLinearVelocity(direction.nor().scl(speed));
|
|
||||||
this.body.setUserData(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reset() {
|
|
||||||
timeAlive = 0f;
|
|
||||||
markedToDestroy = false;
|
|
||||||
this.isEnemy = false;
|
|
||||||
this.body.setLinearVelocity(Vector2.Zero);
|
|
||||||
this.body.setTransform(Vector2.Zero, 0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void render() {
|
|
||||||
sprite.setOrigin(size / 2, size / 2);
|
|
||||||
sprite.setSize(size, size + 5);
|
|
||||||
sprite.setPosition(body.getPosition().x - size / 2, body.getPosition().y - size / 2);
|
|
||||||
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
|
||||||
Bugger.spriteBatch.begin();
|
|
||||||
sprite.draw(Bugger.spriteBatch);
|
|
||||||
Bugger.spriteBatch.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,88 +5,81 @@ import com.badlogic.gdx.physics.box2d.*;
|
|||||||
import org.lumijiez.bugger.Bugger;
|
import org.lumijiez.bugger.Bugger;
|
||||||
import org.lumijiez.bugger.entities.Player;
|
import org.lumijiez.bugger.entities.Player;
|
||||||
import org.lumijiez.bugger.entities.enemies.EnemyEntity;
|
import org.lumijiez.bugger.entities.enemies.EnemyEntity;
|
||||||
import org.lumijiez.bugger.entities.weapons.EnemyProjectile;
|
import org.lumijiez.bugger.entities.weapons.Projectile;
|
||||||
import org.lumijiez.bugger.entities.weapons.EnemyRay;
|
import org.lumijiez.bugger.util.CollisionAction;
|
||||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
import org.lumijiez.bugger.util.CollisionPair;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class CollisionHandler implements ContactListener {
|
public class CollisionHandler implements ContactListener {
|
||||||
|
|
||||||
|
private final List<Map.Entry<CollisionPair, CollisionAction>> collisionHandlers = new ArrayList<>();
|
||||||
|
|
||||||
|
public CollisionHandler() {
|
||||||
|
initializeCollisionHandlers();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeCollisionHandlers() {
|
||||||
|
registerCollisionHandler(Projectile.class, Player.class, (rayObj, playerObj) -> {
|
||||||
|
Projectile ray = (Projectile) rayObj;
|
||||||
|
if (ray.isEnemy()) handlePlayerHit(ray);
|
||||||
|
});
|
||||||
|
|
||||||
|
registerCollisionHandler(Projectile.class, EnemyEntity.class, (rayObj, enemyObj) -> {
|
||||||
|
Projectile ray = (Projectile) rayObj;
|
||||||
|
EnemyEntity enemy = (EnemyEntity) enemyObj;
|
||||||
|
if (!ray.isEnemy()) handleEnemyHit(ray, enemy);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerCollisionHandler(Class<?> typeA, Class<?> typeB, CollisionAction action) {
|
||||||
|
CollisionPair pair = new CollisionPair(typeA, typeB);
|
||||||
|
collisionHandlers.add(Map.entry(pair, action));
|
||||||
|
|
||||||
|
CollisionPair reversePair = new CollisionPair(typeB, typeA);
|
||||||
|
collisionHandlers.add(Map.entry(reversePair, (objA, objB) -> action.handle(objB, objA)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handlePlayerHit(Projectile ray) {
|
||||||
|
ray.destroy();
|
||||||
|
Player.getInstance().damage(50);
|
||||||
|
ParticleHandler instance = ParticleHandler.getInstance();
|
||||||
|
Player player = Player.getInstance();
|
||||||
|
|
||||||
|
instance.playSmallBoom(player.getPosition().x, player.getPosition().y);
|
||||||
|
instance.playHit(Gdx.graphics.getWidth() - 100, Gdx.graphics.getHeight() - 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleEnemyHit(Projectile ray, EnemyEntity enemy) {
|
||||||
|
Bugger.kills++;
|
||||||
|
ray.destroy();
|
||||||
|
enemy.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beginContact(Contact contact) {
|
public void beginContact(Contact contact) {
|
||||||
Fixture fixtureA = contact.getFixtureA();
|
Object userDataA = contact.getFixtureA().getBody().getUserData();
|
||||||
Fixture fixtureB = contact.getFixtureB();
|
Object userDataB = contact.getFixtureB().getBody().getUserData();
|
||||||
|
|
||||||
Object userDataA = fixtureA.getBody().getUserData();
|
if (userDataA == null || userDataB == null) return;
|
||||||
Object userDataB = fixtureB.getBody().getUserData();
|
|
||||||
|
|
||||||
if (isEnemy(fixtureA) && isPlayer(fixtureB)) {
|
for (var entry : collisionHandlers) {
|
||||||
EnemyRay ray = (EnemyRay) userDataA;
|
CollisionPair pair = entry.getKey();
|
||||||
if (ray.isEnemy()) {
|
if (pair.matches(userDataA, userDataB)) {
|
||||||
ray.destroy();
|
entry.getValue().handle(userDataA, userDataB);
|
||||||
Player.getInstance().damage(50);
|
return;
|
||||||
ParticleHandler.getInstance().playSmallBoom(Player.getInstance().getPosition().x, Player.getInstance().getPosition().y);
|
|
||||||
ParticleHandler.getInstance().playHit(Gdx.graphics.getWidth() - 100, Gdx.graphics.getHeight() - 60);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isEnemy(fixtureB) && isPlayer(fixtureA)) {
|
|
||||||
EnemyRay ray = (EnemyRay) userDataB;
|
|
||||||
if (ray.isEnemy()) {
|
|
||||||
ray.destroy();
|
|
||||||
Player.getInstance().damage(50);
|
|
||||||
ParticleHandler.getInstance().playSmallBoom(Player.getInstance().getPosition().x, Player.getInstance().getPosition().y);
|
|
||||||
ParticleHandler.getInstance().playHit(Gdx.graphics.getWidth() - 100, Gdx.graphics.getHeight() - 60);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (isArrow(fixtureA) && isEntity(fixtureB)) {
|
|
||||||
Ray ray = (Ray) fixtureA.getBody().getUserData();
|
|
||||||
EnemyEntity enemy = (EnemyEntity) fixtureB.getBody().getUserData();
|
|
||||||
if (ray != null && !ray.isEnemy()) {
|
|
||||||
Bugger.kills++;
|
|
||||||
ray.destroy();
|
|
||||||
enemy.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isArrow(fixtureB) && isEntity(fixtureA)) {
|
|
||||||
Ray ray = (Ray) fixtureB.getBody().getUserData();
|
|
||||||
EnemyEntity enemy = (EnemyEntity) fixtureA.getBody().getUserData();
|
|
||||||
if (ray != null && !ray.isEnemy()) {
|
|
||||||
Bugger.kills++;
|
|
||||||
ray.destroy();
|
|
||||||
enemy.destroy();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endContact(Contact contact) {
|
public void endContact(Contact contact) {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void preSolve(Contact contact, Manifold manifold) {
|
public void preSolve(Contact contact, Manifold manifold) {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postSolve(Contact contact, ContactImpulse impulse) {
|
public void postSolve(Contact contact, ContactImpulse impulse) {}
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isArrow(Fixture fixture) {
|
|
||||||
return fixture.getBody().getUserData() instanceof Ray;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isEnemy(Fixture fixture) {
|
|
||||||
return fixture.getBody().getUserData() instanceof EnemyProjectile;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isPlayer(Fixture fixture) {
|
|
||||||
return fixture.getBody().getUserData() instanceof Player;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isEntity(Fixture fixture) {
|
|
||||||
return fixture.getBody().getUserData() instanceof EnemyEntity;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ package org.lumijiez.bugger.handlers;
|
|||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
import org.lumijiez.bugger.entities.Player;
|
import org.lumijiez.bugger.entities.Player;
|
||||||
import org.lumijiez.bugger.entities.weapons.EnemyRay;
|
import org.lumijiez.bugger.entities.weapons.Projectile;
|
||||||
import org.lumijiez.bugger.pools.EnemyProjectilePool;
|
import org.lumijiez.bugger.pools.ProjectilePool;
|
||||||
|
|
||||||
public class EnemyProjectileHandler {
|
public class EnemyProjectileHandler {
|
||||||
private static EnemyProjectileHandler instance;
|
private static EnemyProjectileHandler instance;
|
||||||
private final EnemyProjectilePool projectilePool;
|
private final ProjectilePool projectilePool;
|
||||||
|
|
||||||
private EnemyProjectileHandler() {
|
private EnemyProjectileHandler() {
|
||||||
projectilePool = new EnemyProjectilePool(true);
|
projectilePool = new ProjectilePool(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EnemyProjectileHandler getInstance() {
|
public static EnemyProjectileHandler getInstance() {
|
||||||
@@ -28,14 +28,14 @@ public class EnemyProjectileHandler {
|
|||||||
public void shootEnemyProjectile(Vector2 position, float speed) {
|
public void shootEnemyProjectile(Vector2 position, float speed) {
|
||||||
Vector2 playerPos = Player.getInstance().getPosition();
|
Vector2 playerPos = Player.getInstance().getPosition();
|
||||||
Vector2 shootDirection = playerPos.cpy().sub(position).nor();
|
Vector2 shootDirection = playerPos.cpy().sub(position).nor();
|
||||||
EnemyRay projectile = projectilePool.obtain();
|
Projectile projectile = projectilePool.obtain();
|
||||||
|
|
||||||
if (projectile != null) {
|
if (projectile != null) {
|
||||||
projectile.init(position, shootDirection.scl(speed), true);
|
projectile.init(position, shootDirection.scl(speed), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Array<EnemyRay> getDeployedEnemyProjectiles() {
|
public Array<Projectile> getDeployedEnemyProjectiles() {
|
||||||
return projectilePool.getDeployedProjectiles();
|
return projectilePool.getDeployedProjectiles();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.badlogic.gdx.math.Vector2;
|
|||||||
import com.badlogic.gdx.math.Vector3;
|
import com.badlogic.gdx.math.Vector3;
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
import org.lumijiez.bugger.entities.Player;
|
import org.lumijiez.bugger.entities.Player;
|
||||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
import org.lumijiez.bugger.entities.weapons.Projectile;
|
||||||
import org.lumijiez.bugger.pools.ProjectilePool;
|
import org.lumijiez.bugger.pools.ProjectilePool;
|
||||||
|
|
||||||
public class ProjectileHandler {
|
public class ProjectileHandler {
|
||||||
@@ -41,13 +41,13 @@ public class ProjectileHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void shootRay(Vector2 position, Vector2 direction, float speed) {
|
public void shootRay(Vector2 position, Vector2 direction, float speed) {
|
||||||
Ray projectile = projectilePool.obtain();
|
Projectile projectile = projectilePool.obtain();
|
||||||
if (projectile != null) {
|
if (projectile != null) {
|
||||||
projectile.init(position, direction.nor().scl(speed), false);
|
projectile.init(position, direction.nor().scl(speed), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Array<Ray> getDeployedProjectiles() {
|
public Array<Projectile> getDeployedProjectiles() {
|
||||||
return projectilePool.getDeployedProjectiles();
|
return projectilePool.getDeployedProjectiles();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
package org.lumijiez.bugger.pools;
|
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.Array;
|
|
||||||
import org.lumijiez.bugger.Bugger;
|
|
||||||
import org.lumijiez.bugger.entities.weapons.EnemyRay;
|
|
||||||
|
|
||||||
public class EnemyProjectilePool {
|
|
||||||
private final Array<EnemyRay> deployedEnemyProjectiles = new Array<>();
|
|
||||||
private final Array<EnemyRay> freeEnemyProjectiles = new Array<>();
|
|
||||||
private static final int INITIAL_PROJECTILES = 100;
|
|
||||||
|
|
||||||
public EnemyProjectilePool(boolean isEnemy) {
|
|
||||||
for (int i = 0; i < INITIAL_PROJECTILES; i++) {
|
|
||||||
freeEnemyProjectiles.add(new EnemyRay(Bugger.getInstance().getWorld(), isEnemy));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public EnemyRay obtain() {
|
|
||||||
EnemyRay projectile;
|
|
||||||
if (freeEnemyProjectiles.size > 0) {
|
|
||||||
projectile = freeEnemyProjectiles.pop();
|
|
||||||
} else if (deployedEnemyProjectiles.size > 0) {
|
|
||||||
projectile = deployedEnemyProjectiles.first();
|
|
||||||
deployedEnemyProjectiles.removeIndex(0);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
deployedEnemyProjectiles.add(projectile);
|
|
||||||
return projectile;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void free(EnemyRay ray) {
|
|
||||||
ray.reset();
|
|
||||||
deployedEnemyProjectiles.removeValue(ray, true);
|
|
||||||
freeEnemyProjectiles.add(ray);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateAndRender(float delta) {
|
|
||||||
for (int i = 0; i < deployedEnemyProjectiles.size; i++) {
|
|
||||||
EnemyRay ray = deployedEnemyProjectiles.get(i);
|
|
||||||
if (!ray.isMarkedToDestroy()) {
|
|
||||||
ray.update(delta);
|
|
||||||
ray.render();
|
|
||||||
} else {
|
|
||||||
free(ray);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Array<EnemyRay> getDeployedProjectiles() {
|
|
||||||
return deployedEnemyProjectiles;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,53 +2,74 @@ package org.lumijiez.bugger.pools;
|
|||||||
|
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
import org.lumijiez.bugger.Bugger;
|
import org.lumijiez.bugger.Bugger;
|
||||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
import org.lumijiez.bugger.entities.weapons.Projectile;
|
||||||
|
|
||||||
public class ProjectilePool {
|
public class ProjectilePool {
|
||||||
private final Array<Ray> deployedProjectiles = new Array<>();
|
private final Array<Projectile> deployedProjectiles = new Array<>();
|
||||||
private final Array<Ray> freeProjectiles = new Array<>();
|
private final Array<Projectile> freeProjectiles = new Array<>();
|
||||||
private static final int INITIAL_PROJECTILES = 100;
|
private static final int INITIAL_PROJECTILES = 100;
|
||||||
|
private final boolean isEnemy;
|
||||||
|
|
||||||
public ProjectilePool(boolean isEnemy) {
|
public ProjectilePool(boolean isEnemy) {
|
||||||
|
this.isEnemy = isEnemy;
|
||||||
|
initializePool();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializePool() {
|
||||||
for (int i = 0; i < INITIAL_PROJECTILES; i++) {
|
for (int i = 0; i < INITIAL_PROJECTILES; i++) {
|
||||||
freeProjectiles.add(new Ray(Bugger.getInstance().getWorld(), isEnemy));
|
freeProjectiles.add(new Projectile(Bugger.getInstance().getWorld(), isEnemy));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ray obtain() {
|
public Projectile obtain() {
|
||||||
Ray projectile;
|
Projectile projectile;
|
||||||
if (freeProjectiles.size > 0) {
|
if (freeProjectiles.size > 0) {
|
||||||
projectile = freeProjectiles.pop();
|
projectile = freeProjectiles.pop();
|
||||||
} else if (deployedProjectiles.size > 0) {
|
} else if (deployedProjectiles.size > 0) {
|
||||||
projectile = deployedProjectiles.first();
|
projectile = deployedProjectiles.first();
|
||||||
deployedProjectiles.removeIndex(0);
|
deployedProjectiles.removeIndex(0);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
projectile = new Projectile(Bugger.getInstance().getWorld(), isEnemy);
|
||||||
}
|
}
|
||||||
deployedProjectiles.add(projectile);
|
deployedProjectiles.add(projectile);
|
||||||
return projectile;
|
return projectile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void free(Ray ray) {
|
public void free(Projectile ray) {
|
||||||
|
if (ray != null && deployedProjectiles.contains(ray, true)) {
|
||||||
ray.reset();
|
ray.reset();
|
||||||
deployedProjectiles.removeValue(ray, true);
|
deployedProjectiles.removeValue(ray, true);
|
||||||
freeProjectiles.add(ray);
|
freeProjectiles.add(ray);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void updateAndRender(float delta) {
|
public void updateAndRender(float delta) {
|
||||||
for (int i = 0; i < deployedProjectiles.size; i++) {
|
for (int i = deployedProjectiles.size - 1; i >= 0; i--) {
|
||||||
Ray ray = deployedProjectiles.get(i);
|
Projectile ray = deployedProjectiles.get(i);
|
||||||
if (!ray.isMarkedToDestroy()) {
|
if (!ray.isMarkedToDestroy()) {
|
||||||
ray.update(delta);
|
ray.update(delta);
|
||||||
ray.render();
|
ray.render();
|
||||||
} else {
|
} else {
|
||||||
free(ray);
|
free(ray);
|
||||||
i--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Array<Ray> getDeployedProjectiles() {
|
public void freeAll() {
|
||||||
|
for (Projectile ray : deployedProjectiles) {
|
||||||
|
free(ray);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Array<Projectile> getDeployedProjectiles() {
|
||||||
return deployedProjectiles;
|
return deployedProjectiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getActiveProjectileCount() {
|
||||||
|
return deployedProjectiles.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFreeProjectileCount() {
|
||||||
|
return freeProjectiles.size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package org.lumijiez.bugger.util;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface CollisionAction {
|
||||||
|
void handle(Object objectA, Object objectB);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.lumijiez.bugger.util;
|
||||||
|
|
||||||
|
public record CollisionPair(Class<?> typeA, Class<?> typeB) {
|
||||||
|
public boolean matches(Object objA, Object objB) {
|
||||||
|
return (typeA.isInstance(objA) && typeB.isInstance(objB));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package org.lumijiez.bugger.util;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.graphics.GL20;
|
||||||
|
import org.lumijiez.bugger.Bugger;
|
||||||
|
import org.lumijiez.bugger.entities.Player;
|
||||||
|
import org.lumijiez.bugger.handlers.*;
|
||||||
|
|
||||||
|
import static org.lumijiez.bugger.Bugger.spriteBatch;
|
||||||
|
|
||||||
|
public class GameSystemsFacade {
|
||||||
|
private static GameSystemsFacade instance;
|
||||||
|
private final ProjectileHandler projectileHandler;
|
||||||
|
private final SpaceVFXHandler spaceVFXHandler;
|
||||||
|
private final EnemyProjectileHandler enemyProjectileHandler;
|
||||||
|
private final InputHandler inputHandler;
|
||||||
|
private final CameraHandler cameraHandler;
|
||||||
|
private final EnemyHandler enemyHandler;
|
||||||
|
private final ParticleHandler particleHandler;
|
||||||
|
private final CleanupHandler cleanupHandler;
|
||||||
|
private final InterfaceHandler interfaceHandler;
|
||||||
|
private final Player player;
|
||||||
|
|
||||||
|
public GameSystemsFacade() {
|
||||||
|
player = Player.getInstance();
|
||||||
|
interfaceHandler = InterfaceHandler.getInstance();
|
||||||
|
enemyProjectileHandler = EnemyProjectileHandler.getInstance();
|
||||||
|
inputHandler = InputHandler.getInstance();
|
||||||
|
cameraHandler = CameraHandler.getInstance();
|
||||||
|
spaceVFXHandler = SpaceVFXHandler.getInstance();
|
||||||
|
projectileHandler = ProjectileHandler.getInstance();
|
||||||
|
enemyHandler = EnemyHandler.getInstance();
|
||||||
|
particleHandler = ParticleHandler.getInstance();
|
||||||
|
cleanupHandler = CleanupHandler.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GameSystemsFacade getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new GameSystemsFacade();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(float delta) {
|
||||||
|
step();
|
||||||
|
renderClear();
|
||||||
|
spaceVFXHandler.render();
|
||||||
|
projectileHandler.cycle(delta);
|
||||||
|
enemyProjectileHandler.cycle(delta);
|
||||||
|
enemyHandler.cycle();
|
||||||
|
particleHandler.cycle(delta);
|
||||||
|
cleanupHandler.tryClean();
|
||||||
|
cameraHandler.updateCamera();
|
||||||
|
player.render();
|
||||||
|
enemyHandler.render(delta);
|
||||||
|
|
||||||
|
if (interfaceHandler.isDebug()) renderDebug();
|
||||||
|
|
||||||
|
interfaceHandler.renderUI();
|
||||||
|
inputHandler.handleInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void renderClear() {
|
||||||
|
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
|
||||||
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void renderDebug() {
|
||||||
|
Bugger.getInstance().debugRenderer.render(
|
||||||
|
Bugger.getInstance().getWorld(), spriteBatch.getProjectionMatrix());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void step() {
|
||||||
|
Bugger.getInstance().getWorld().step((float) 1 / 30f, 6, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user