nicer health render, pooling
This commit is contained in:
@@ -7,8 +7,6 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
import com.badlogic.gdx.scenes.scene2d.Action;
|
||||
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
|
||||
import org.lumijiez.bugger.entities.Player;
|
||||
import org.lumijiez.bugger.handlers.*;
|
||||
|
||||
@@ -21,7 +19,6 @@ public class Bugger {
|
||||
public static SpriteBatch spriteBatch = new SpriteBatch();
|
||||
public static SpriteBatch uiBatch = new SpriteBatch();
|
||||
public static int kills = 0;
|
||||
private boolean logoPlayed = false;
|
||||
|
||||
private Bugger() {
|
||||
Player.getInstance().setPlayer(world, 100, 100);
|
||||
@@ -54,7 +51,8 @@ public class Bugger {
|
||||
Player.getInstance().render();
|
||||
|
||||
EnemyHandler.getInstance().render(delta);
|
||||
// renderDebug();
|
||||
|
||||
if (InterfaceHandler.getInstance().isDebug()) renderDebug();
|
||||
|
||||
InterfaceHandler.getInstance().renderUI();
|
||||
InputHandler.getInstance().handleInput();
|
||||
@@ -70,7 +68,7 @@ public class Bugger {
|
||||
}
|
||||
|
||||
public void step() {
|
||||
world.step(1 / 30f, 6, 2);
|
||||
world.step((float) 1 / 30f, 6, 2);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package org.lumijiez.bugger;
|
||||
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
|
||||
/** {@link com.badlogic.gdx.ApplicationListener} implementation shared by all platforms. */
|
||||
public class Main extends Game {
|
||||
@Override
|
||||
public void create() {
|
||||
setScreen(new GameScreen());
|
||||
Gdx.graphics.setResizable(false);
|
||||
Gdx.graphics.setTitle("Bugger");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.physics.box2d.*;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
||||
import org.lumijiez.bugger.handlers.CameraHandler;
|
||||
|
||||
import static org.lumijiez.bugger.Bugger.shapeRenderer;
|
||||
@@ -99,10 +97,6 @@ public class Player extends Entity {
|
||||
health -= damage;
|
||||
}
|
||||
|
||||
public int getHealth() {
|
||||
return health;
|
||||
}
|
||||
|
||||
private void renderHealthBar() {
|
||||
float maxHealth = 1000f;
|
||||
float healthPercentage = Math.max(health / maxHealth, 0);
|
||||
@@ -114,6 +108,9 @@ public class Player extends Entity {
|
||||
shapeRenderer.setProjectionMatrix(CameraHandler.getInstance().getCamera().combined);
|
||||
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
|
||||
|
||||
shapeRenderer.setColor(0.3f, 0.3f, 0.3f, 1);
|
||||
shapeRenderer.rect(healthBarX, healthBarY, healthBarWidth , healthBarHeight);
|
||||
|
||||
shapeRenderer.setColor(1, 0, 0, 1);
|
||||
shapeRenderer.rect(healthBarX, healthBarY, healthBarWidth * healthPercentage, healthBarHeight);
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
import org.lumijiez.bugger.entities.Player;
|
||||
import org.lumijiez.bugger.handlers.EnemyProjectileHandler;
|
||||
import org.lumijiez.bugger.handlers.ProjectileHandler;
|
||||
|
||||
public class EnemyEntity extends Entity {
|
||||
private float shootTimer = 0.0f;
|
||||
@@ -15,9 +14,9 @@ public class EnemyEntity extends Entity {
|
||||
super(world, texturePath, size);
|
||||
}
|
||||
|
||||
public void update(Vector2 target) {
|
||||
follow(target);
|
||||
}
|
||||
// public void update(Vector2 target) {
|
||||
// follow(target);
|
||||
// }
|
||||
|
||||
public void update() {
|
||||
Vector2 playerPos = Player.getInstance().getPosition();
|
||||
@@ -36,15 +35,15 @@ public class EnemyEntity extends Entity {
|
||||
|
||||
float shootCooldown = 2.0f;
|
||||
if (shootTimer >= shootCooldown) {
|
||||
EnemyProjectileHandler.getInstance().shootEnemyProjectile(this.body.getPosition(), playerPos, 50f);
|
||||
EnemyProjectileHandler.getInstance().shootEnemyProjectile(this.body.getPosition(), 50f);
|
||||
shootTimer = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
public void cycle(Vector2 target) {
|
||||
update(target);
|
||||
render();
|
||||
}
|
||||
// public void cycle(Vector2 target) {
|
||||
// update(target);
|
||||
// render();
|
||||
// }
|
||||
|
||||
public void cycle() {
|
||||
update();
|
||||
|
||||
@@ -2,20 +2,16 @@ package org.lumijiez.bugger.handlers;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.Player;
|
||||
import org.lumijiez.bugger.entities.weapons.EnemyRay;
|
||||
import org.lumijiez.bugger.pools.EnemyProjectilePool;
|
||||
|
||||
public class EnemyProjectileHandler {
|
||||
private final Array<EnemyRay> deployedEnemyProjectiles = new Array<>();
|
||||
private final Array<EnemyRay> freeEnemyProjectiles = new Array<>();
|
||||
private static EnemyProjectileHandler instance;
|
||||
private static final int INITIAL_PROJECTILES = 50;
|
||||
private final EnemyProjectilePool projectilePool;
|
||||
|
||||
private EnemyProjectileHandler() {
|
||||
for (int i = 0; i < INITIAL_PROJECTILES; i++) {
|
||||
freeEnemyProjectiles.add(new EnemyRay(Bugger.getInstance().getWorld(), true));
|
||||
}
|
||||
projectilePool = new EnemyProjectilePool(true);
|
||||
}
|
||||
|
||||
public static EnemyProjectileHandler getInstance() {
|
||||
@@ -26,38 +22,20 @@ public class EnemyProjectileHandler {
|
||||
}
|
||||
|
||||
public void cycle(float delta) {
|
||||
for (int i = 0; i < deployedEnemyProjectiles.size; i++) {
|
||||
EnemyRay ray = deployedEnemyProjectiles.get(i);
|
||||
if (!ray.isMarkedToDestroy()) {
|
||||
ray.update(delta);
|
||||
ray.render();
|
||||
} else {
|
||||
ray.reset();
|
||||
freeEnemyProjectiles.add(ray);
|
||||
deployedEnemyProjectiles.removeIndex(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
projectilePool.updateAndRender(delta);
|
||||
}
|
||||
|
||||
public void shootEnemyProjectile(Vector2 position, Vector2 direction, float speed) {
|
||||
EnemyRay projectile;
|
||||
|
||||
public void shootEnemyProjectile(Vector2 position, float speed) {
|
||||
Vector2 playerPos = Player.getInstance().getPosition();
|
||||
|
||||
Vector2 shootDirection = playerPos.cpy().sub(position).nor();
|
||||
EnemyRay projectile = projectilePool.obtain();
|
||||
|
||||
if (freeEnemyProjectiles.size > 0) {
|
||||
projectile = freeEnemyProjectiles.pop();
|
||||
} else if (deployedEnemyProjectiles.size > 0) {
|
||||
projectile = deployedEnemyProjectiles.first();
|
||||
deployedEnemyProjectiles.removeIndex(0);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (projectile != null) {
|
||||
projectile.init(position, shootDirection.scl(speed), true);
|
||||
deployedEnemyProjectiles.add(projectile);
|
||||
}
|
||||
}
|
||||
|
||||
public Array<EnemyRay> getDeployedEnemyProjectiles() {
|
||||
return projectilePool.getDeployedProjectiles();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,10 @@ public class InputHandler {
|
||||
ProjectileHandler.getInstance().shootRay();
|
||||
}
|
||||
|
||||
if (Gdx.input.isKeyJustPressed(Input.Keys.X)) {
|
||||
InterfaceHandler.getInstance().toggleDebug();
|
||||
}
|
||||
|
||||
if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT)) {
|
||||
float numRays = 8;
|
||||
float radius = 0.5f;
|
||||
@@ -44,15 +48,13 @@ public class InputHandler {
|
||||
Vector2 direction = new Vector2(x, y).add(Player.getInstance().getPosition());
|
||||
Vector2 shootDirection = direction.cpy().sub(Player.getInstance().getPosition()).nor();
|
||||
|
||||
Gdx.app.postRunnable(() -> {
|
||||
ProjectileHandler.getInstance().shootRay(Player.getInstance().getPosition(), shootDirection, 20f);
|
||||
});
|
||||
Gdx.app.postRunnable(() -> ProjectileHandler.getInstance().shootRay(Player.getInstance().getPosition(), shootDirection, 20f));
|
||||
|
||||
try {
|
||||
float delay = (2f / raysPerCircle) * 500;
|
||||
Thread.sleep((long) delay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.lumijiez.bugger.entities.Player;
|
||||
public class InterfaceHandler {
|
||||
private final BitmapFont bitmapFont;
|
||||
private static InterfaceHandler instance;
|
||||
private boolean debug;
|
||||
|
||||
private InterfaceHandler() {
|
||||
bitmapFont = new BitmapFont(Gdx.files.internal("EA.fnt"), Gdx.files.internal("EA.png"), false);
|
||||
@@ -22,13 +23,21 @@ public class InterfaceHandler {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void toggleDebug() {
|
||||
debug = !debug;
|
||||
}
|
||||
|
||||
public void renderUI() {
|
||||
SpriteBatch uiBatch = Bugger.uiBatch;
|
||||
OrthographicCamera uiCam = CameraHandler.getInstance().getUICamera();
|
||||
int kills = Bugger.kills;
|
||||
int enemies = EnemyHandler.getInstance().getEnemies().size();
|
||||
int projectiles = ProjectileHandler.getInstance().getDeployedProjectiles().size;
|
||||
int enemyProjectiles = EnemyProjectileHandler.getInstance().getDeployedEnemyProjectiles().size;
|
||||
int fixtures = Bugger.getInstance().getWorld().getFixtureCount();
|
||||
int bodies = Bugger.getInstance().getWorld().getBodyCount();
|
||||
int collisions = Bugger.getInstance().getWorld().getContactCount();
|
||||
int fps = Gdx.graphics.getFramesPerSecond();
|
||||
uiBatch.begin();
|
||||
|
||||
uiBatch.setColor(1, 1, 1, 1);
|
||||
@@ -36,19 +45,36 @@ public class InterfaceHandler {
|
||||
10, uiCam.viewportHeight - 10);
|
||||
bitmapFont.draw(uiBatch, "Kills: " + kills, 10, uiCam.viewportHeight - 40);
|
||||
|
||||
if (debug) {
|
||||
bitmapFont.setColor(0, 1, 0, 1);
|
||||
|
||||
bitmapFont.draw(uiBatch, "Enemies: " + enemies, 10, uiCam.viewportHeight - 70);
|
||||
bitmapFont.draw(uiBatch, "Projectiles: " + projectiles, 10, uiCam.viewportHeight - 100);
|
||||
bitmapFont.draw(uiBatch, String.format("Player Pos: (%.2f, %.2f)",
|
||||
bitmapFont.draw(uiBatch, "FPS: " + fps, 10, uiCam.viewportHeight - 70);
|
||||
bitmapFont.draw(uiBatch, String.format("Player Pos: %.2f, %.2f",
|
||||
Player.getInstance().getBody().getPosition().x,
|
||||
Player.getInstance().getBody().getPosition().y), 10, uiCam.viewportHeight - 130);
|
||||
bitmapFont.draw(uiBatch, "Bodies: " + bodies, 10, uiCam.viewportHeight - 160);
|
||||
Player.getInstance().getBody().getPosition().y), 10, uiCam.viewportHeight - 100);
|
||||
bitmapFont.draw(uiBatch, "Enemies: " + enemies, 10, uiCam.viewportHeight - 130);
|
||||
bitmapFont.draw(uiBatch, "Projectiles: " + projectiles, 10, uiCam.viewportHeight - 160);
|
||||
bitmapFont.draw(uiBatch, "Enemy Projectiles: " + enemyProjectiles, 10, uiCam.viewportHeight - 190);
|
||||
bitmapFont.draw(uiBatch, "Physics Bodies: " + bodies, 10, uiCam.viewportHeight - 220);
|
||||
bitmapFont.draw(uiBatch, "Collisions: " + collisions, 10, uiCam.viewportHeight - 250);
|
||||
bitmapFont.draw(uiBatch, "Fixtures: " + fixtures, 10, uiCam.viewportHeight - 280);
|
||||
|
||||
bitmapFont.setColor(1, 1, 1, 1);
|
||||
} else {
|
||||
bitmapFont.setColor(0, 1, 0, 1);
|
||||
|
||||
bitmapFont.draw(uiBatch, "Open Debug: X", 10, uiCam.viewportHeight - 70);
|
||||
|
||||
bitmapFont.setColor(1, 1, 1, 1);
|
||||
}
|
||||
|
||||
|
||||
uiBatch.end();
|
||||
uiBatch.setProjectionMatrix(uiCam.combined);
|
||||
uiCam.update();
|
||||
}
|
||||
|
||||
public boolean isDebug() {
|
||||
return debug;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,22 +4,16 @@ import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.Player;
|
||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
||||
import org.lumijiez.bugger.pools.ProjectilePool;
|
||||
|
||||
public class ProjectileHandler {
|
||||
private final Array<Ray> deployedProjectiles = new Array<>();
|
||||
private final Array<Ray> freeProjectiles = new Array<>();
|
||||
private final Array<Ray> deployedEnemyProjectiles = new Array<>();
|
||||
private final Array<Ray> freeEnemyProjectiles = new Array<>();
|
||||
private static ProjectileHandler instance;
|
||||
private static final int INITIAL_PROJECTILES = 50;
|
||||
private final ProjectilePool projectilePool;
|
||||
|
||||
private ProjectileHandler() {
|
||||
for (int i = 0; i < INITIAL_PROJECTILES; i++) {
|
||||
freeProjectiles.add(new Ray(Bugger.getInstance().getWorld(), false));
|
||||
}
|
||||
projectilePool = new ProjectilePool(false);
|
||||
}
|
||||
|
||||
public static ProjectileHandler getInstance() {
|
||||
@@ -30,18 +24,7 @@ public class ProjectileHandler {
|
||||
}
|
||||
|
||||
public void cycle(float delta) {
|
||||
for (int i = 0; i < deployedProjectiles.size; i++) {
|
||||
Ray ray = deployedProjectiles.get(i);
|
||||
if (!ray.isMarkedToDestroy()) {
|
||||
ray.update(delta);
|
||||
ray.render();
|
||||
} else {
|
||||
ray.reset();
|
||||
freeProjectiles.add(ray);
|
||||
deployedProjectiles.removeIndex(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
projectilePool.updateAndRender(delta);
|
||||
}
|
||||
|
||||
public void shootRay() {
|
||||
@@ -58,26 +41,13 @@ public class ProjectileHandler {
|
||||
}
|
||||
|
||||
public void shootRay(Vector2 position, Vector2 direction, float speed) {
|
||||
Ray projectile;
|
||||
|
||||
if (freeProjectiles.size > 0) {
|
||||
projectile = freeProjectiles.pop();
|
||||
} else if (deployedProjectiles.size > 0) {
|
||||
projectile = deployedProjectiles.first();
|
||||
deployedProjectiles.removeIndex(0);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
Ray projectile = projectilePool.obtain();
|
||||
if (projectile != null) {
|
||||
projectile.init(position, direction.nor().scl(speed), false);
|
||||
deployedProjectiles.add(projectile);
|
||||
}
|
||||
}
|
||||
|
||||
public Array<Ray> getDeployedProjectiles() {
|
||||
return deployedProjectiles;
|
||||
}
|
||||
|
||||
public Array<Ray> getDeployedEnemyProjectiles() {
|
||||
return deployedEnemyProjectiles;
|
||||
return projectilePool.getDeployedProjectiles();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.lumijiez.bugger.pools;
|
||||
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
||||
|
||||
public class ProjectilePool {
|
||||
private final Array<Ray> deployedProjectiles = new Array<>();
|
||||
private final Array<Ray> freeProjectiles = new Array<>();
|
||||
private static final int INITIAL_PROJECTILES = 100;
|
||||
|
||||
public ProjectilePool(boolean isEnemy) {
|
||||
for (int i = 0; i < INITIAL_PROJECTILES; i++) {
|
||||
freeProjectiles.add(new Ray(Bugger.getInstance().getWorld(), isEnemy));
|
||||
}
|
||||
}
|
||||
|
||||
public Ray obtain() {
|
||||
Ray projectile;
|
||||
if (freeProjectiles.size > 0) {
|
||||
projectile = freeProjectiles.pop();
|
||||
} else if (deployedProjectiles.size > 0) {
|
||||
projectile = deployedProjectiles.first();
|
||||
deployedProjectiles.removeIndex(0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
deployedProjectiles.add(projectile);
|
||||
return projectile;
|
||||
}
|
||||
|
||||
public void free(Ray ray) {
|
||||
ray.reset();
|
||||
deployedProjectiles.removeValue(ray, true);
|
||||
freeProjectiles.add(ray);
|
||||
}
|
||||
|
||||
public void updateAndRender(float delta) {
|
||||
for (int i = 0; i < deployedProjectiles.size; i++) {
|
||||
Ray ray = deployedProjectiles.get(i);
|
||||
if (!ray.isMarkedToDestroy()) {
|
||||
ray.update(delta);
|
||||
ray.render();
|
||||
} else {
|
||||
free(ray);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Array<Ray> getDeployedProjectiles() {
|
||||
return deployedProjectiles;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user