fixed ui camera, display stats

This commit is contained in:
Daniel
2024-10-16 20:46:25 +03:00
parent 2b5bcc6ace
commit 28cf6c363d
13 changed files with 567 additions and 62 deletions

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
@@ -13,8 +14,10 @@ import com.badlogic.gdx.utils.Array;
import org.lumijiez.bugger.entities.Entity;
import org.lumijiez.bugger.entities.Player;
import org.lumijiez.bugger.entities.enemies.EnemyEntity;
import org.lumijiez.bugger.entities.enemies.Stalker;
import org.lumijiez.bugger.entities.enemies.Wasp;
import org.lumijiez.bugger.entities.weapons.Arrow;
import org.lumijiez.bugger.entities.weapons.Projectile;
import org.lumijiez.bugger.handlers.GameContactListener;
import org.lumijiez.bugger.vfx.ParticleManager;
import org.lumijiez.bugger.vfx.SpaceBackground;
@@ -26,7 +29,7 @@ public class Bugger {
private static Bugger instance;
private final World world;
private final SpaceBackground spaceBackground;
private final Array<Arrow> projectiles;
private final Array<Projectile> projectiles;
private final List<EnemyEntity> enemies;
private final Array<Entity> entitiesToDestroy;
private final Player player;
@@ -34,9 +37,13 @@ public class Bugger {
private static final float ENEMY_SPAWN_INTERVAL = 0.5f;
private final Box2DDebugRenderer debugRenderer;
public static OrthographicCamera cam;
public static OrthographicCamera uiCam;
public static SpriteBatch spriteBatch;
public static SpriteBatch uiBatch;
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<>();
@@ -47,9 +54,14 @@ public class Bugger {
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();
uiCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
uiCam.position.set(uiCam.viewportWidth / 2, uiCam.viewportHeight / 2, 0); // Center the camera
uiCam.update();
}
public static Bugger getInstance() {
@@ -60,19 +72,10 @@ public class Bugger {
}
public void cycle(float delta) {
cam.update();
renderClear();
renderBackground();
renderPlayer();
step();
spriteBatch.setProjectionMatrix(cam.combined);
cam.position.set(player.getPosition().x, player.getPosition().y, 0);
handleInput();
cycleProjectiles(delta);
cycleEnemies();
@@ -80,9 +83,22 @@ public class Bugger {
clearEntities();
cam.position.set(player.getBody().getPosition().x, player.getBody().getPosition().y, 0);
cam.update();
renderPlayer();
renderEnemies(delta);
renderDebug();
spriteBatch.setProjectionMatrix(cam.combined);
uiBatch.begin();
bitmapFont.draw(uiBatch, "Speed: " + Player.getInstance().getBody().getLinearVelocity().len(), 100, 150);
uiBatch.end();
uiBatch.setProjectionMatrix(uiCam.combined);
uiCam.update();
handleInput();
}
public void renderBackground() {
@@ -92,6 +108,7 @@ public class Bugger {
public void renderEnemies(float delta) {
enemySpawnTimer += delta;
if (enemySpawnTimer >= ENEMY_SPAWN_INTERVAL) {
enemies.add(new Stalker(world, Player.getInstance().getPosition()));
enemies.add(new Wasp(world, Player.getInstance().getPosition()));
enemySpawnTimer = 0;
}
@@ -116,7 +133,7 @@ public class Bugger {
}
public void cycleProjectiles(float delta) {
for (Arrow arrow : projectiles) {
for (Projectile arrow : projectiles) {
if (!arrow.isMarkedToDestroy()) {
arrow.update(delta);
arrow.render();
@@ -152,7 +169,7 @@ public class Bugger {
}
public void step() {
world.step(1 / 20f, 6, 2);
world.step(1 / 60f, 6, 2);
}
public void playParticle(float x, float y) {

View File

@@ -3,10 +3,11 @@ package org.lumijiez.bugger.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.math.Vector2;
import org.lumijiez.bugger.Bugger;
import org.lumijiez.bugger.entities.weapons.Arrow;
import org.lumijiez.bugger.vfx.ParticleManager;
public class Player extends Entity {
private static Player instance;
@@ -22,6 +23,29 @@ public class Player extends Entity {
return instance;
}
@Override
protected Body createBody(float x, float y) {
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(x, y);
bodyDef.type = BodyDef.BodyType.DynamicBody;
Body body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(size / 2, size / 2);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
fixtureDef.friction = 0.5f;
fixtureDef.restitution = 0.3f;
body.createFixture(fixtureDef);
shape.dispose();
return body;
}
public void setPlayer(World world, float x, float y) {
this.world = world;
this.body = createBody(x, y);
@@ -29,9 +53,10 @@ public class Player extends Entity {
public void move(float deltaX, float deltaY) {
float speed = 500f;
body.setLinearVelocity(deltaX * speed, deltaY * speed);
}
Vector2 force = new Vector2(deltaX * speed, deltaY * speed);
body.applyForceToCenter(force, true);
}
public void render() {
handleInput();
updateSpriteRotation();

View File

@@ -0,0 +1,20 @@
package org.lumijiez.bugger.entities.enemies;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import java.util.Random;
public class Stalker extends EnemyEntity {
private static final Random random = new Random();
public Stalker(World world, Vector2 playerPosition) {
super(world, "images/stalker.png", 10f);
float spawnRadius = 50;
float angle = random.nextFloat() * 2 * (float) Math.PI;
float spawnX = playerPosition.x + (float) Math.cos(angle) * (spawnRadius + size);
float spawnY = playerPosition.y + (float) Math.sin(angle) * (spawnRadius + size);
this.body = createBody(spawnX, spawnY);
this.body.setUserData(this);
}
}

View File

@@ -2,49 +2,33 @@ package org.lumijiez.bugger.entities.weapons;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import org.lumijiez.bugger.entities.Entity;
import org.lumijiez.bugger.Bugger;
public class Arrow extends Entity {
private float timeAlive = 0f;
public class Arrow extends Projectile {
public Arrow(World world, Vector2 position, Vector2 direction) {
super(world, "images/wasp.png", 1f);
super(world, "images/arrow.png", 5f);
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)));
float speed = 5000f;
this.body.setLinearVelocity(direction.nor().scl(speed));
this.body.setAngularVelocity(speed);
}
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 / 2, 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 || isMarkedToDestroy()) {
destroy();
}
}
@Override
public void render() {
super.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();
}
}

View File

@@ -0,0 +1,50 @@
package org.lumijiez.bugger.entities.weapons;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import org.lumijiez.bugger.Bugger;
import org.lumijiez.bugger.entities.Entity;
public abstract class Projectile extends Entity {
private float timeAlive = 0f;
public Projectile(World world, String texturePath, float size) {
super(world, texturePath, size);
}
@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 / 2, size + 5);
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 || isMarkedToDestroy()) {
destroy();
}
}
public void render() {
super.render();
}
}

View File

@@ -0,0 +1,4 @@
package org.lumijiez.bugger.handlers;
public class FontRenderer {
}

View File

@@ -15,7 +15,7 @@ public class ParticleManager {
private ParticleManager() {
ParticleEffect effect = new ParticleEffect();
effect.load(Gdx.files.internal("particles/boom.p"), Gdx.files.internal("particles"));
effect.scaleEffect(0.6f);
effect.scaleEffect(0.5f);
particleEffectPool = new ParticleEffectPool(effect, 1, 20);
activeEffects = new Array<>();
}

View File

@@ -13,6 +13,9 @@ public class SpaceBackground {
private final float[] starPositionsY;
private final int numStars = 10000;
// Scale factor for all textures
private final float scaleFactor = 0.1f; // Scaling down by 0.1
public SpaceBackground() {
starTexture = new Texture(Gdx.files.internal("images/star.png"));
galaxyTexture = new Texture(Gdx.files.internal("images/galaxy.png"));
@@ -41,7 +44,6 @@ public class SpaceBackground {
}
private void drawStars(float cameraX, float cameraY) {
for (int i = 0; i < numStars; i++) {
float starScrollSpeedX = 0.5f;
float starX = starPositionsX[i] - cameraX * starScrollSpeedX;
@@ -51,10 +53,9 @@ public class SpaceBackground {
float offsetX = MathUtils.random(-2f, 2f);
float offsetY = MathUtils.random(-2f, 2f);
float starScale = 0.1f;
float scaledWidth = starTexture.getWidth() * starScale;
float scaledHeight = starTexture.getHeight() * starScale;
// Scale the star size
float scaledWidth = starTexture.getWidth() * scaleFactor;
float scaledHeight = starTexture.getHeight() * scaleFactor;
Bugger.spriteBatch.draw(starTexture,
starX + offsetX,
@@ -72,27 +73,31 @@ public class SpaceBackground {
private void drawGalaxies(float cameraX, float cameraY) {
float galaxyScrollSpeedY = 0.2f;
float galaxyY1 = (cameraY * galaxyScrollSpeedY) % galaxyTexture.getHeight() - galaxyTexture.getHeight();
// float galaxyY2 = galaxyY1 + galaxyTexture.getHeight();
float galaxyScrollSpeedX = 0.2f;
float galaxyX1 = -cameraX * galaxyScrollSpeedX;
float galaxyX2 = galaxyX1 + galaxyTexture.getWidth();
Bugger.spriteBatch.draw(galaxyTexture, galaxyX1, galaxyY1);
Bugger.spriteBatch.draw(galaxyTexture, galaxyX2, galaxyY1);
// Scale the galaxy size
float scaledGalaxyWidth = galaxyTexture.getWidth() * scaleFactor;
float scaledGalaxyHeight = galaxyTexture.getHeight() * scaleFactor;
Bugger.spriteBatch.draw(galaxyTexture, galaxyX1, galaxyY1, scaledGalaxyWidth, scaledGalaxyHeight);
Bugger.spriteBatch.draw(galaxyTexture, galaxyX2, galaxyY1, scaledGalaxyWidth, scaledGalaxyHeight);
}
private void drawNebulae(float cameraX, float cameraY) {
float nebulaScrollSpeedY = 0.025f;
float nebulaY1 = (cameraY * nebulaScrollSpeedY) % nebulaTexture.getHeight() - nebulaTexture.getHeight();
// float nebulaY2 = nebulaY1 + nebulaTexture.getHeight();
float nebulaScrollSpeedX = 0.025f;
float nebulaX1 = -cameraX * nebulaScrollSpeedX;
float nebulaX2 = nebulaX1 + nebulaTexture.getWidth();
Bugger.spriteBatch.draw(nebulaTexture, nebulaX1, nebulaY1);
Bugger.spriteBatch.draw(nebulaTexture, nebulaX2, nebulaY1);
// Scale the nebula size
float scaledNebulaWidth = nebulaTexture.getWidth() * scaleFactor;
float scaledNebulaHeight = nebulaTexture.getHeight() * scaleFactor;
Bugger.spriteBatch.draw(nebulaTexture, nebulaX1, nebulaY1, scaledNebulaWidth, scaledNebulaHeight);
Bugger.spriteBatch.draw(nebulaTexture, nebulaX2, nebulaY1, scaledNebulaWidth, scaledNebulaHeight);
}
public void dispose() {