extensive code breakdown, utility classes
This commit is contained in:
@@ -15,8 +15,7 @@ import org.lumijiez.bugger.entities.Player;
|
||||
import org.lumijiez.bugger.entities.enemies.*;
|
||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
||||
import org.lumijiez.bugger.entities.weapons.Projectile;
|
||||
import org.lumijiez.bugger.factories.EnemyFactory;
|
||||
import org.lumijiez.bugger.handlers.GameContactListener;
|
||||
import org.lumijiez.bugger.handlers.*;
|
||||
import org.lumijiez.bugger.vfx.ParticleManager;
|
||||
import org.lumijiez.bugger.vfx.SpaceBackground;
|
||||
|
||||
@@ -25,35 +24,24 @@ import java.util.List;
|
||||
|
||||
public class Bugger {
|
||||
private static Bugger instance;
|
||||
private final World world;
|
||||
private final SpaceBackground spaceBackground;
|
||||
private final Array<Projectile> projectiles;
|
||||
private final List<EnemyEntity> enemies;
|
||||
private final Array<Entity> entitiesToDestroy;
|
||||
private final Player player;
|
||||
private float enemySpawnTimer = 0f;
|
||||
private static final float ENEMY_SPAWN_INTERVAL = 0.5f;
|
||||
private final Box2DDebugRenderer debugRenderer;
|
||||
private final World world = new World(new Vector2(0, 0), true);;
|
||||
private final SpaceBackground spaceBackground = new SpaceBackground();
|
||||
private final Array<Projectile> projectiles = new Array<>();
|
||||
private final List<EnemyEntity> enemies = new ArrayList<>();
|
||||
private final Array<Entity> entitiesToDestroy = new Array<>();
|
||||
private final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
|
||||
public static SpriteBatch spriteBatch = new SpriteBatch();
|
||||
public static SpriteBatch uiBatch = new SpriteBatch();
|
||||
public static OrthographicCamera cam;
|
||||
public static OrthographicCamera uiCam;
|
||||
public static SpriteBatch spriteBatch;
|
||||
public static SpriteBatch uiBatch;
|
||||
private final Player player;
|
||||
public static int kills = 0;
|
||||
private final BitmapFont bitmapFont;
|
||||
|
||||
private Bugger() {
|
||||
bitmapFont = new BitmapFont(Gdx.files.internal("EA.fnt"), Gdx.files.internal("EA.png"), false);
|
||||
world = new World(new Vector2(0, 0), true);
|
||||
this.projectiles = new Array<>();
|
||||
this.entitiesToDestroy = new Array<>();
|
||||
this.enemies = new ArrayList<>();
|
||||
this.spaceBackground = new SpaceBackground();
|
||||
this.player = Player.getInstance();
|
||||
this.player.setPlayer(world, 100, 100);
|
||||
this.world.setContactListener(new GameContactListener());
|
||||
this.debugRenderer = new Box2DDebugRenderer();
|
||||
spriteBatch = new SpriteBatch();
|
||||
uiBatch = new SpriteBatch();
|
||||
|
||||
cam = new OrthographicCamera(160, 90);
|
||||
cam.position.set(Player.getInstance().getPosition().x / 2f, Player.getInstance().getPosition().y / 2f, 0);
|
||||
cam.update();
|
||||
@@ -80,44 +68,22 @@ public class Bugger {
|
||||
cycleEnemies();
|
||||
cycleParticles(delta);
|
||||
|
||||
clearEntities();
|
||||
EntityCleaner.getInstance().tryClean();
|
||||
|
||||
cam.position.set(player.getBody().getPosition().x, player.getBody().getPosition().y, 0);
|
||||
cam.update();
|
||||
updateCamera();
|
||||
|
||||
renderPlayer();
|
||||
renderEnemies(delta);
|
||||
// renderDebug();
|
||||
// renderDebug();
|
||||
|
||||
UIRenderer.getInstance().renderUI();
|
||||
InputHandler.getInstance().handleInput();
|
||||
}
|
||||
|
||||
public void updateCamera() {
|
||||
cam.position.set(player.getBody().getPosition().x, player.getBody().getPosition().y, 0);
|
||||
cam.update();
|
||||
spriteBatch.setProjectionMatrix(cam.combined);
|
||||
|
||||
uiBatch.begin();
|
||||
|
||||
uiBatch.setColor(1, 1, 1, 1);
|
||||
bitmapFont.draw(uiBatch, String.format("Speed: %.2f", Player.getInstance().getBody().getLinearVelocity().len()),
|
||||
10, uiCam.viewportHeight - 10);
|
||||
bitmapFont.draw(uiBatch, "Kills: " + kills, 10, uiCam.viewportHeight - 40);
|
||||
|
||||
bitmapFont.setColor(0, 1, 0, 1);
|
||||
|
||||
|
||||
bitmapFont.draw(uiBatch, "Enemies: " + enemies.size(), 10, uiCam.viewportHeight - 70);
|
||||
bitmapFont.draw(uiBatch, "Projectiles: " + projectiles.size, 10, uiCam.viewportHeight - 100);
|
||||
bitmapFont.draw(uiBatch, "Entities to Destroy: " + entitiesToDestroy.size, 10, uiCam.viewportHeight - 130);
|
||||
bitmapFont.draw(uiBatch, String.format("Player Pos: (%.2f, %.2f)",
|
||||
Player.getInstance().getBody().getPosition().x,
|
||||
Player.getInstance().getBody().getPosition().y), 10, uiCam.viewportHeight - 160);
|
||||
bitmapFont.draw(uiBatch, "Bodies: " + world.getBodyCount(), 10, uiCam.viewportHeight - 190);
|
||||
bitmapFont.draw(uiBatch, "Proxies: " + world.getProxyCount(), 10, uiCam.viewportHeight - 220);
|
||||
|
||||
bitmapFont.setColor(1, 1, 1, 1);
|
||||
|
||||
uiBatch.end();
|
||||
|
||||
|
||||
uiBatch.setProjectionMatrix(uiCam.combined);
|
||||
uiCam.update();
|
||||
handleInput();
|
||||
}
|
||||
|
||||
public void renderBackground() {
|
||||
@@ -125,17 +91,8 @@ public class Bugger {
|
||||
}
|
||||
|
||||
public void renderEnemies(float delta) {
|
||||
|
||||
enemySpawnTimer += delta;
|
||||
if (enemySpawnTimer >= ENEMY_SPAWN_INTERVAL) {
|
||||
enemies.add(EnemyFactory.createRandomEnemy(world, Player.getInstance().getPosition()));
|
||||
enemySpawnTimer = 0;
|
||||
}
|
||||
|
||||
for (EnemyEntity enemy : enemies) {
|
||||
enemy.moveTowards(player.getPosition());
|
||||
enemy.render();
|
||||
}
|
||||
EnemySpawner.getInstance().cycle(delta);
|
||||
for (EnemyEntity enemy : enemies) enemy.cycle();
|
||||
}
|
||||
|
||||
public void renderPlayer() {
|
||||
@@ -175,49 +132,35 @@ public class Bugger {
|
||||
ParticleManager.getInstance().render(spriteBatch);
|
||||
}
|
||||
|
||||
public void clearEntities() {
|
||||
for (Entity entity : entitiesToDestroy) {
|
||||
world.destroyBody(entity.getBody());
|
||||
if (entity instanceof Projectile) {
|
||||
projectiles.removeValue((Projectile) entity, true);
|
||||
}
|
||||
if (entity instanceof EnemyEntity) {
|
||||
playParticle(entity.getBody().getPosition().x, entity.getBody().getPosition().y);
|
||||
enemies.remove(entity);
|
||||
}
|
||||
|
||||
}
|
||||
entitiesToDestroy.clear();
|
||||
}
|
||||
|
||||
public void step() {
|
||||
world.step(1 / 30f, 6, 2);
|
||||
}
|
||||
|
||||
public void playParticle(float x, float y) {
|
||||
ParticleManager.getInstance().playEffect(x, y);
|
||||
}
|
||||
|
||||
public void shoot() {
|
||||
Ray ray = player.shootArrow();
|
||||
projectiles.add(ray);
|
||||
}
|
||||
|
||||
public void handleInput() {
|
||||
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
|
||||
shoot();
|
||||
}
|
||||
}
|
||||
|
||||
public SpriteBatch batch() {
|
||||
return spriteBatch;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
spriteBatch.dispose();
|
||||
uiBatch.dispose();
|
||||
world.dispose();
|
||||
spaceBackground.dispose();
|
||||
ParticleManager.getInstance().dispose();
|
||||
EntityCleaner.getInstance().disposeAll();
|
||||
}
|
||||
|
||||
public List<EnemyEntity> getEnemies() {
|
||||
return enemies;
|
||||
}
|
||||
|
||||
public World getWorld() {
|
||||
return world;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public Array<Entity> getEntitiesToDestroy() {
|
||||
return entitiesToDestroy;
|
||||
}
|
||||
|
||||
public Array<Projectile> getProjectiles() {
|
||||
return projectiles;
|
||||
}
|
||||
|
||||
public SpaceBackground getSpaceBackground() {
|
||||
return spaceBackground;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.lumijiez.bugger.entities;
|
||||
|
||||
public enum EntityType {
|
||||
PROJECTILE,
|
||||
ENEMY
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package org.lumijiez.bugger.entities.enemies;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
|
||||
public class EnemyEntity extends Entity {
|
||||
@@ -9,8 +10,17 @@ public class EnemyEntity extends Entity {
|
||||
super(world, texturePath, size);
|
||||
}
|
||||
|
||||
public void moveTowards(Vector2 target) {
|
||||
Vector2 direction = target.cpy().sub(body.getPosition()).nor();
|
||||
public void update(Vector2 target) {
|
||||
follow(target);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
Vector2 playerPos = Bugger.getInstance().getPlayer().getPosition();
|
||||
follow(playerPos);
|
||||
}
|
||||
|
||||
private void follow(Vector2 playerPos) {
|
||||
Vector2 direction = playerPos.cpy().sub(body.getPosition()).nor();
|
||||
float speed = 10f;
|
||||
body.setLinearVelocity(direction.scl(speed));
|
||||
|
||||
@@ -18,6 +28,16 @@ public class EnemyEntity extends Entity {
|
||||
body.setTransform(body.getPosition(), angle * (float) Math.PI / 180f);
|
||||
}
|
||||
|
||||
public void cycle(Vector2 target) {
|
||||
update(target);
|
||||
render();
|
||||
}
|
||||
|
||||
public void cycle() {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
public void render() {
|
||||
super.render();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,47 @@
|
||||
package org.lumijiez.bugger.handlers;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.enemies.Enemies;
|
||||
import org.lumijiez.bugger.factories.EnemyFactory;
|
||||
|
||||
public class EnemySpawner {
|
||||
private static enem
|
||||
private static EnemySpawner instance;
|
||||
private float enemySpawnTimer = 0f;
|
||||
private static final float ENEMY_SPAWN_INTERVAL = 0.5f;
|
||||
|
||||
private EnemySpawner() {}
|
||||
|
||||
public static EnemySpawner getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new EnemySpawner();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void cycle(float delta) {
|
||||
enemySpawnTimer += delta;
|
||||
if (enemySpawnTimer >= ENEMY_SPAWN_INTERVAL) {
|
||||
World world = Bugger.getInstance().getWorld();
|
||||
Vector2 playerPos = Bugger.getInstance().getPlayer().getPosition();
|
||||
trySpawnRandom(world, playerPos);
|
||||
enemySpawnTimer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void spawn(Enemies enemy) {
|
||||
World world = Bugger.getInstance().getWorld();
|
||||
Vector2 playerPos = Bugger.getInstance().getPlayer().getPosition();
|
||||
Bugger.getInstance().getEnemies().add(EnemyFactory.createEnemy(enemy, world, playerPos));
|
||||
}
|
||||
|
||||
public void spawn(Enemies enemy, World world, Vector2 position) {
|
||||
Bugger.getInstance().getEnemies().add(EnemyFactory.createEnemy(enemy, world, position));
|
||||
}
|
||||
|
||||
private void trySpawnRandom(World world, Vector2 position) {
|
||||
Bugger.getInstance().getEnemies().add(EnemyFactory.createRandomEnemy(world, position));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.lumijiez.bugger.handlers;
|
||||
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
import org.lumijiez.bugger.entities.enemies.EnemyEntity;
|
||||
import org.lumijiez.bugger.entities.weapons.Projectile;
|
||||
import org.lumijiez.bugger.vfx.ParticleManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EntityCleaner {
|
||||
private static EntityCleaner instance;
|
||||
|
||||
private EntityCleaner() {}
|
||||
|
||||
public static EntityCleaner getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new EntityCleaner();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void tryClean() {
|
||||
Array<Entity> entities = Bugger.getInstance().getEntitiesToDestroy();
|
||||
Array<Projectile> projectiles = Bugger.getInstance().getProjectiles();
|
||||
List<EnemyEntity> enemies = Bugger.getInstance().getEnemies();
|
||||
World world = Bugger.getInstance().getWorld();
|
||||
|
||||
for (Entity entity : entities) {
|
||||
world.destroyBody(entity.getBody());
|
||||
|
||||
if (entity instanceof Projectile) {
|
||||
projectiles.removeValue((Projectile) entity, true);
|
||||
}
|
||||
|
||||
if (entity instanceof EnemyEntity) {
|
||||
ParticleManager.getInstance().playEffect(entity.getBody().getPosition().x, entity.getBody().getPosition().y);
|
||||
enemies.remove(entity);
|
||||
}
|
||||
|
||||
}
|
||||
entities.clear();
|
||||
}
|
||||
|
||||
public void disposeAll() {
|
||||
Bugger.spriteBatch.dispose();
|
||||
Bugger.uiBatch.dispose();
|
||||
Bugger.getInstance().getWorld().dispose();
|
||||
Bugger.getInstance().getSpaceBackground().dispose();
|
||||
ParticleManager.getInstance().dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.lumijiez.bugger.handlers;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
||||
|
||||
public class InputHandler {
|
||||
private static InputHandler instance;
|
||||
|
||||
private InputHandler() {}
|
||||
|
||||
public static InputHandler getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new InputHandler();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void handleInput() {
|
||||
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
|
||||
Ray ray = Bugger.getInstance().getPlayer().shootArrow();
|
||||
Bugger.getInstance().getProjectiles().add(ray);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.lumijiez.bugger.handlers;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.Sprite;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.Player;
|
||||
|
||||
public class UIRenderer {
|
||||
private final BitmapFont bitmapFont;
|
||||
private static UIRenderer instance;
|
||||
|
||||
private UIRenderer() {
|
||||
bitmapFont = new BitmapFont(Gdx.files.internal("EA.fnt"), Gdx.files.internal("EA.png"), false);
|
||||
}
|
||||
|
||||
public static UIRenderer getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new UIRenderer();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void renderUI() {
|
||||
SpriteBatch uiBatch = Bugger.uiBatch;
|
||||
OrthographicCamera uiCam = Bugger.uiCam;
|
||||
int kills = Bugger.kills;
|
||||
int enemies = Bugger.getInstance().getEnemies().size();
|
||||
int projectiles = Bugger.getInstance().getProjectiles().size;
|
||||
int bodies = Bugger.getInstance().getWorld().getBodyCount();
|
||||
uiBatch.begin();
|
||||
|
||||
uiBatch.setColor(1, 1, 1, 1);
|
||||
bitmapFont.draw(uiBatch, String.format("Speed: %.2f", Player.getInstance().getBody().getLinearVelocity().len()),
|
||||
10, uiCam.viewportHeight - 10);
|
||||
bitmapFont.draw(uiBatch, "Kills: " + kills, 10, uiCam.viewportHeight - 40);
|
||||
|
||||
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)",
|
||||
Player.getInstance().getBody().getPosition().x,
|
||||
Player.getInstance().getBody().getPosition().y), 10, uiCam.viewportHeight - 130);
|
||||
bitmapFont.draw(uiBatch, "Bodies: " + bodies, 10, uiCam.viewportHeight - 160);
|
||||
|
||||
bitmapFont.setColor(1, 1, 1, 1);
|
||||
|
||||
uiBatch.end();
|
||||
uiBatch.setProjectionMatrix(uiCam.combined);
|
||||
uiCam.update();
|
||||
}
|
||||
}
|
||||
17
core/src/main/java/org/lumijiez/bugger/util/Util.java
Normal file
17
core/src/main/java/org/lumijiez/bugger/util/Util.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package org.lumijiez.bugger.util;
|
||||
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
import org.lumijiez.bugger.entities.EntityType;
|
||||
import org.lumijiez.bugger.entities.enemies.EnemyEntity;
|
||||
import org.lumijiez.bugger.entities.weapons.Projectile;
|
||||
|
||||
public class Util {
|
||||
public static EntityType determineEntityType(Entity entity) {
|
||||
if (entity instanceof Projectile) {
|
||||
return EntityType.PROJECTILE;
|
||||
} else if (entity instanceof EnemyEntity) {
|
||||
return EntityType.ENEMY;
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown entity type");
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public class SpaceBackground {
|
||||
}
|
||||
|
||||
public void render() {
|
||||
Bugger.getInstance().batch().begin();
|
||||
Bugger.spriteBatch.begin();
|
||||
|
||||
float cameraX = Bugger.cam.position.x;
|
||||
float cameraY = Bugger.cam.position.y;
|
||||
@@ -58,7 +58,7 @@ public class SpaceBackground {
|
||||
drawGalaxies(cameraX, cameraY);
|
||||
drawNebulae(cameraX, cameraY);
|
||||
|
||||
Bugger.getInstance().batch().end();
|
||||
Bugger.spriteBatch.end();
|
||||
}
|
||||
|
||||
private void drawStars(float cameraX, float cameraY) {
|
||||
|
||||
Reference in New Issue
Block a user