large refactor, code management, cleanup
This commit is contained in:
184
core/src/main/java/org/lumijiez/bugger/Bugger.java
Normal file
184
core/src/main/java/org/lumijiez/bugger/Bugger.java
Normal file
@@ -0,0 +1,184 @@
|
||||
package org.lumijiez.bugger;
|
||||
|
||||
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.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
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.Wasp;
|
||||
import org.lumijiez.bugger.entities.weapons.Arrow;
|
||||
import org.lumijiez.bugger.handlers.GameContactListener;
|
||||
import org.lumijiez.bugger.vfx.ParticleManager;
|
||||
import org.lumijiez.bugger.vfx.SpaceBackground;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Bugger {
|
||||
private static Bugger instance;
|
||||
private final World world;
|
||||
private final SpaceBackground spaceBackground;
|
||||
private final Array<Arrow> 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;
|
||||
public static OrthographicCamera cam;
|
||||
public static SpriteBatch spriteBatch;
|
||||
|
||||
private Bugger() {
|
||||
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();
|
||||
cam = new OrthographicCamera();
|
||||
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
||||
cam.position.set(Player.getInstance().getPosition().x / 2f, Player.getInstance().getPosition().y / 2f, 0);
|
||||
cam.update();
|
||||
}
|
||||
|
||||
public static Bugger getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new Bugger();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void cycle(float delta) {
|
||||
updateCamera(delta);
|
||||
|
||||
renderClear();
|
||||
renderBackground();
|
||||
|
||||
step();
|
||||
|
||||
handleInput();
|
||||
|
||||
cycleProjectiles(delta);
|
||||
cycleEnemies();
|
||||
cycleParticles(delta);
|
||||
|
||||
clearEntities();
|
||||
|
||||
renderPlayer();
|
||||
renderEnemies(delta);
|
||||
renderDebug();
|
||||
}
|
||||
|
||||
public void renderBackground() {
|
||||
spaceBackground.render();
|
||||
}
|
||||
|
||||
public void renderEnemies(float delta) {
|
||||
enemySpawnTimer += delta;
|
||||
if (enemySpawnTimer >= ENEMY_SPAWN_INTERVAL) {
|
||||
enemies.add(new Wasp(world, Player.getInstance().getPosition()));
|
||||
enemySpawnTimer = 0;
|
||||
}
|
||||
|
||||
for (EnemyEntity enemy : enemies) {
|
||||
enemy.moveTowards(player.getPosition());
|
||||
enemy.render();
|
||||
}
|
||||
}
|
||||
|
||||
public void renderPlayer() {
|
||||
player.render();
|
||||
}
|
||||
|
||||
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 updateCamera(float delta) {
|
||||
cam.update();
|
||||
spriteBatch.setProjectionMatrix(cam.combined);
|
||||
cam.position.set(Player.getInstance().getPosition().x, Player.getInstance().getPosition().y, 0);
|
||||
}
|
||||
|
||||
public void cycleProjectiles(float delta) {
|
||||
for (Arrow arrow : projectiles) {
|
||||
if (!arrow.isMarkedToDestroy()) {
|
||||
arrow.update(delta);
|
||||
arrow.render();
|
||||
} else {
|
||||
entitiesToDestroy.add(arrow);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cycleEnemies() {
|
||||
for (EnemyEntity enemy : enemies) {
|
||||
if (enemy.isMarkedToDestroy()) {
|
||||
entitiesToDestroy.add(enemy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cycleParticles(float delta) {
|
||||
ParticleManager.getInstance().update(delta);
|
||||
ParticleManager.getInstance().render(spriteBatch);
|
||||
}
|
||||
|
||||
public void clearEntities() {
|
||||
for (Entity entity : entitiesToDestroy) {
|
||||
world.destroyBody(entity.getBody());
|
||||
if (entity instanceof Arrow) projectiles.removeValue((Arrow) 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(1f, 6, 2);
|
||||
}
|
||||
|
||||
public void playParticle(float x, float y) {
|
||||
ParticleManager.getInstance().playEffect(x, y);
|
||||
}
|
||||
|
||||
public void shoot() {
|
||||
Arrow arrow = player.shootArrow();
|
||||
projectiles.add(arrow);
|
||||
}
|
||||
|
||||
public void handleInput() {
|
||||
if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
|
||||
shoot();
|
||||
}
|
||||
}
|
||||
|
||||
public SpriteBatch batch() {
|
||||
return spriteBatch;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
spriteBatch.dispose();
|
||||
world.dispose();
|
||||
spaceBackground.dispose();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +1,15 @@
|
||||
package org.lumijiez.bugger;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.*;
|
||||
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.Wasp;
|
||||
import org.lumijiez.bugger.entities.weapons.Arrow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GameScreen implements Screen {
|
||||
public static OrthographicCamera cam;
|
||||
private SpaceBackground spaceBackground = new SpaceBackground();
|
||||
private final World world;
|
||||
private final Player player;
|
||||
public static final SpriteBatch spriteBatch = new SpriteBatch();
|
||||
private final List<EnemyEntity> enemies;
|
||||
private Array<Arrow> arrows;
|
||||
private Array<Entity> entitiesToDestroy;
|
||||
private float enemySpawnTimer = 0f;
|
||||
private static final float ENEMY_SPAWN_INTERVAL = 0.5f;
|
||||
private final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
|
||||
public final Bugger bugger = Bugger.getInstance();
|
||||
|
||||
|
||||
|
||||
public GameScreen() {
|
||||
world = new World(new Vector2(0, 0), true);
|
||||
player = Player.getInstance();
|
||||
player.setPlayer(world, 100, 100);
|
||||
enemies = new ArrayList<>();
|
||||
arrows = new Array<>();
|
||||
entitiesToDestroy = new Array<>();
|
||||
world.setContactListener(new GameContactListener());
|
||||
|
||||
cam = new OrthographicCamera();
|
||||
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
||||
cam.position.set(Player.getInstance().getPosition().x / 2f, Player.getInstance().getPosition().y / 2f, 0);
|
||||
cam.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -53,66 +19,7 @@ public class GameScreen implements Screen {
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
cam.update();
|
||||
spriteBatch.setProjectionMatrix(cam.combined);
|
||||
|
||||
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
spaceBackground.render();
|
||||
|
||||
world.step(1f, 6, 2);
|
||||
|
||||
if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
|
||||
Arrow arrow = player.shootArrow();
|
||||
arrows.add(arrow);
|
||||
}
|
||||
|
||||
for (Arrow arrow : arrows) {
|
||||
if (!arrow.isMarkedToDestroy()) {
|
||||
arrow.update(delta);
|
||||
arrow.render();
|
||||
} else {
|
||||
entitiesToDestroy.add(arrow);
|
||||
}
|
||||
}
|
||||
|
||||
for (EnemyEntity enemy : enemies) {
|
||||
if (enemy.isMarkedToDestroy()) {
|
||||
entitiesToDestroy.add(enemy);
|
||||
}
|
||||
}
|
||||
|
||||
for (Entity entity : entitiesToDestroy) {
|
||||
world.destroyBody(entity.getBody());
|
||||
if (entity instanceof Arrow) arrows.removeValue((Arrow) entity, true);
|
||||
if (entity instanceof EnemyEntity) {
|
||||
Gdx.app.log("EFFECT", "PLAYED");
|
||||
ParticleManager.getInstance().playEffect(entity.getBody().getPosition().x, entity.getBody().getPosition().y);
|
||||
enemies.remove(entity);
|
||||
}
|
||||
}
|
||||
entitiesToDestroy.clear();
|
||||
|
||||
ParticleManager.getInstance().update(delta);
|
||||
player.render();
|
||||
|
||||
ParticleManager.getInstance().render(spriteBatch);
|
||||
|
||||
enemySpawnTimer += delta;
|
||||
if (enemySpawnTimer >= ENEMY_SPAWN_INTERVAL) {
|
||||
enemies.add(new Wasp(world, Player.getInstance().getPosition()));
|
||||
enemySpawnTimer = 0;
|
||||
}
|
||||
|
||||
for (EnemyEntity enemy : enemies) {
|
||||
enemy.moveTowards(player.getPosition());
|
||||
enemy.render();
|
||||
}
|
||||
|
||||
debugRenderer.render(world, spriteBatch.getProjectionMatrix());
|
||||
|
||||
cam.position.set(Player.getInstance().getPosition().x, Player.getInstance().getPosition().y, 0);
|
||||
bugger.cycle(delta);
|
||||
}
|
||||
|
||||
|
||||
@@ -138,8 +45,6 @@ public class GameScreen implements Screen {
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
spriteBatch.dispose();
|
||||
world.dispose();
|
||||
spaceBackground.dispose();
|
||||
bugger.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
package org.lumijiez.bugger;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
|
||||
public class SpaceBackground {
|
||||
private Texture starTexture;
|
||||
private Texture galaxyTexture;
|
||||
private Texture nebulaTexture;
|
||||
|
||||
private float[] starPositionsX;
|
||||
private float[] starPositionsY;
|
||||
|
||||
private int numStars = 10000; // Number of stars to generate
|
||||
|
||||
// Speeds for different layers
|
||||
private float galaxyScrollSpeedX = 0.2f; // Galaxy horizontal scroll speed
|
||||
private float galaxyScrollSpeedY = 0.2f; // Galaxy vertical scroll speed
|
||||
private float nebulaScrollSpeedX = 0.025f; // Nebula horizontal scroll speed
|
||||
private float nebulaScrollSpeedY = 0.025f; // Nebula vertical scroll speed
|
||||
private float starScrollSpeedX = 0.5f; // Star horizontal scroll speed
|
||||
private float starScrollSpeedY = 0.5f; // Star vertical scroll speed
|
||||
|
||||
public SpaceBackground() {
|
||||
starTexture = new Texture(Gdx.files.internal("images/star.png"));
|
||||
galaxyTexture = new Texture(Gdx.files.internal("images/galaxy.png"));
|
||||
nebulaTexture = new Texture(Gdx.files.internal("images/nebula.png"));
|
||||
|
||||
starPositionsX = new float[numStars];
|
||||
starPositionsY = new float[numStars];
|
||||
|
||||
// Randomly place stars within a larger range to create dispersion
|
||||
for (int i = 0; i < numStars; i++) {
|
||||
// Increase the range to make stars more dispersed
|
||||
starPositionsX[i] = MathUtils.random(-Gdx.graphics.getWidth() * 10, Gdx.graphics.getWidth() * 10);
|
||||
starPositionsY[i] = MathUtils.random(-Gdx.graphics.getHeight() * 10, Gdx.graphics.getHeight() * 10);
|
||||
}
|
||||
}
|
||||
|
||||
public void render() {
|
||||
GameScreen.spriteBatch.begin();
|
||||
|
||||
// Draw galaxies with parallax effect
|
||||
drawGalaxies();
|
||||
|
||||
// Draw nebulae with parallax effect
|
||||
drawNebulae();
|
||||
|
||||
// Draw star particles with parallax effect
|
||||
drawStars();
|
||||
|
||||
GameScreen.spriteBatch.end();
|
||||
}
|
||||
|
||||
private void drawStars() {
|
||||
float cameraX = GameScreen.cam.position.x;
|
||||
float cameraY = GameScreen.cam.position.y;
|
||||
|
||||
// Adjust the star positions based on the camera's position
|
||||
for (int i = 0; i < numStars; i++) {
|
||||
// Calculate new positions based on camera movement
|
||||
float starX = starPositionsX[i] - cameraX * starScrollSpeedX; // Horizontal parallax
|
||||
float starY = starPositionsY[i] - cameraY * starScrollSpeedY; // Vertical parallax
|
||||
|
||||
// Add a slight random offset to the star's position
|
||||
float offsetX = MathUtils.random(-2f, 2f); // Change the offset range as needed
|
||||
float offsetY = MathUtils.random(-2f, 2f); // Change the offset range as needed
|
||||
|
||||
// Define a scale factor for the stars
|
||||
float starScale = 0.1f; // Adjust this value to make stars smaller or larger
|
||||
|
||||
// Calculate scaled dimensions
|
||||
float scaledWidth = starTexture.getWidth() * starScale;
|
||||
float scaledHeight = starTexture.getHeight() * starScale;
|
||||
|
||||
// Draw the star with the correct parameters
|
||||
GameScreen.spriteBatch.draw(starTexture,
|
||||
starX + offsetX, // X position
|
||||
starY + offsetY, // Y position
|
||||
scaledWidth, // Origin X
|
||||
scaledHeight, // Origin Y
|
||||
0, // Scaled Width
|
||||
0, // Scaled Height
|
||||
1, // Scale X (since we're already scaling)
|
||||
1 // Scale Y (since we're already scaling)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawGalaxies() {
|
||||
float cameraX = GameScreen.cam.position.x;
|
||||
float cameraY = GameScreen.cam.position.y;
|
||||
|
||||
// Determine Y positions for galaxies based on camera position and scroll speed
|
||||
float galaxyY1 = (cameraY * galaxyScrollSpeedY) % galaxyTexture.getHeight() - galaxyTexture.getHeight();
|
||||
float galaxyY2 = galaxyY1 + galaxyTexture.getHeight(); // Draw a second galaxy texture for seamless scrolling
|
||||
|
||||
// Draw galaxies with horizontal offset
|
||||
float galaxyX1 = -cameraX * galaxyScrollSpeedX; // Horizontal position based on camera
|
||||
float galaxyX2 = galaxyX1 + galaxyTexture.getWidth();
|
||||
|
||||
// Draw the galaxy texture
|
||||
GameScreen.spriteBatch.draw(galaxyTexture, galaxyX1, galaxyY1);
|
||||
GameScreen.spriteBatch.draw(galaxyTexture, galaxyX2, galaxyY1);
|
||||
}
|
||||
|
||||
private void drawNebulae() {
|
||||
float cameraX = GameScreen.cam.position.x;
|
||||
float cameraY = GameScreen.cam.position.y;
|
||||
|
||||
// Determine Y positions for nebulae based on camera position and scroll speed
|
||||
float nebulaY1 = (cameraY * nebulaScrollSpeedY) % nebulaTexture.getHeight() - nebulaTexture.getHeight();
|
||||
float nebulaY2 = nebulaY1 + nebulaTexture.getHeight(); // Draw a second nebula texture for seamless scrolling
|
||||
|
||||
// Draw nebulae with horizontal offset
|
||||
float nebulaX1 = -cameraX * nebulaScrollSpeedX; // Horizontal position based on camera
|
||||
float nebulaX2 = nebulaX1 + nebulaTexture.getWidth();
|
||||
|
||||
// Draw the nebula texture
|
||||
GameScreen.spriteBatch.draw(nebulaTexture, nebulaX1, nebulaY1);
|
||||
GameScreen.spriteBatch.draw(nebulaTexture, nebulaX2, nebulaY1);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
starTexture.dispose();
|
||||
galaxyTexture.dispose();
|
||||
nebulaTexture.dispose();
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ 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.GameScreen;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
|
||||
public abstract class Entity {
|
||||
protected Body body;
|
||||
@@ -42,9 +42,9 @@ public abstract class Entity {
|
||||
sprite.setSize(size, size);
|
||||
sprite.setPosition(body.getPosition().x - size / 2, body.getPosition().y - size / 2);
|
||||
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
||||
GameScreen.spriteBatch.begin();
|
||||
sprite.draw(GameScreen.spriteBatch);
|
||||
GameScreen.spriteBatch.end();
|
||||
Bugger.spriteBatch.begin();
|
||||
sprite.draw(Bugger.spriteBatch);
|
||||
Bugger.spriteBatch.end();
|
||||
}
|
||||
|
||||
public Vector2 getPosition() {
|
||||
|
||||
@@ -5,13 +5,11 @@ import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.weapons.Arrow;
|
||||
|
||||
import static org.lumijiez.bugger.GameScreen.cam;
|
||||
|
||||
public class Player extends Entity {
|
||||
private static Player instance;
|
||||
private final float speed = 5f;
|
||||
|
||||
private Player() {
|
||||
super(null, "images/wasp.png", 50f);
|
||||
@@ -30,6 +28,7 @@ public class Player extends Entity {
|
||||
}
|
||||
|
||||
public void move(float deltaX, float deltaY) {
|
||||
float speed = 5f;
|
||||
body.setLinearVelocity(deltaX * speed, deltaY * speed);
|
||||
}
|
||||
|
||||
@@ -55,7 +54,7 @@ public class Player extends Entity {
|
||||
float mouseX = Gdx.input.getX();
|
||||
float mouseY = Gdx.input.getY();
|
||||
|
||||
Vector3 mousePosition = cam.unproject(new Vector3(mouseX, mouseY, 0));
|
||||
Vector3 mousePosition = Bugger.cam.unproject(new Vector3(mouseX, mouseY, 0));
|
||||
|
||||
Vector2 direction = new Vector2(mousePosition.x, mousePosition.y).sub(body.getPosition()).nor();
|
||||
|
||||
@@ -70,7 +69,7 @@ public class Player extends Entity {
|
||||
float mouseX = Gdx.input.getX();
|
||||
float mouseY = Gdx.input.getY();
|
||||
|
||||
Vector3 mousePosition = cam.unproject(new Vector3(mouseX, mouseY, 0));
|
||||
Vector3 mousePosition = Bugger.cam.unproject(new Vector3(mouseX, mouseY, 0));
|
||||
|
||||
direction.set(mousePosition.x, mousePosition.y).sub(getPosition()).nor();
|
||||
|
||||
|
||||
@@ -5,14 +5,13 @@ import com.badlogic.gdx.physics.box2d.World;
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
|
||||
public class EnemyEntity extends Entity {
|
||||
private final float speed = 50f;
|
||||
|
||||
public EnemyEntity(World world, String texturePath, float size) {
|
||||
super(world, texturePath, size);
|
||||
}
|
||||
|
||||
public void moveTowards(Vector2 target) {
|
||||
Vector2 direction = target.cpy().sub(body.getPosition()).nor();
|
||||
float speed = 50f;
|
||||
body.setLinearVelocity(direction.scl(speed / 100f));
|
||||
|
||||
float angle = direction.angleDeg() + 270f;
|
||||
|
||||
@@ -2,8 +2,6 @@ package org.lumijiez.bugger.entities.enemies;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.*;
|
||||
import org.lumijiez.bugger.ParticleManager;
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
@@ -5,8 +5,6 @@ import com.badlogic.gdx.physics.box2d.*;
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
|
||||
public class Arrow extends Entity {
|
||||
private final float speed = 4000f;
|
||||
private final float lifetime = 3f;
|
||||
private float timeAlive = 0f;
|
||||
|
||||
public Arrow(World world, Vector2 position, Vector2 direction) {
|
||||
@@ -14,6 +12,7 @@ public class Arrow extends Entity {
|
||||
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 15f));
|
||||
|
||||
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
||||
float speed = 4000f;
|
||||
this.body.setLinearVelocity(direction.nor().scl(speed));
|
||||
}
|
||||
|
||||
@@ -38,6 +37,7 @@ public class Arrow extends Entity {
|
||||
|
||||
public void update(float delta) {
|
||||
timeAlive += delta;
|
||||
float lifetime = 3f;
|
||||
if (timeAlive >= lifetime || isMarkedToDestroy()) {
|
||||
destroy();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.lumijiez.bugger;
|
||||
package org.lumijiez.bugger.handlers;
|
||||
|
||||
import com.badlogic.gdx.physics.box2d.*;
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
import org.lumijiez.bugger.entities.enemies.EnemyEntity;
|
||||
import org.lumijiez.bugger.entities.weapons.Arrow;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package org.lumijiez.bugger;
|
||||
package org.lumijiez.bugger.vfx;
|
||||
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
|
||||
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.GameScreen;
|
||||
|
||||
public class ParticleManager {
|
||||
private static ParticleManager instance;
|
||||
@@ -47,9 +48,9 @@ public class ParticleManager {
|
||||
|
||||
public void render(SpriteBatch spriteBatch) {
|
||||
for (ParticleEffectPool.PooledEffect effect : activeEffects) {
|
||||
GameScreen.spriteBatch.begin();
|
||||
Bugger.spriteBatch.begin();
|
||||
effect.draw(spriteBatch);
|
||||
GameScreen.spriteBatch.end();
|
||||
Bugger.spriteBatch.end();
|
||||
}
|
||||
}
|
||||
|
||||
103
core/src/main/java/org/lumijiez/bugger/vfx/SpaceBackground.java
Normal file
103
core/src/main/java/org/lumijiez/bugger/vfx/SpaceBackground.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package org.lumijiez.bugger.vfx;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
|
||||
public class SpaceBackground {
|
||||
private final Texture starTexture;
|
||||
private final Texture galaxyTexture;
|
||||
private final Texture nebulaTexture;
|
||||
private final float[] starPositionsX;
|
||||
private final float[] starPositionsY;
|
||||
private final int numStars = 10000;
|
||||
|
||||
public SpaceBackground() {
|
||||
starTexture = new Texture(Gdx.files.internal("images/star.png"));
|
||||
galaxyTexture = new Texture(Gdx.files.internal("images/galaxy.png"));
|
||||
nebulaTexture = new Texture(Gdx.files.internal("images/nebula.png"));
|
||||
|
||||
starPositionsX = new float[numStars];
|
||||
starPositionsY = new float[numStars];
|
||||
|
||||
for (int i = 0; i < numStars; i++) {
|
||||
starPositionsX[i] = MathUtils.random(-Gdx.graphics.getWidth() * 10, Gdx.graphics.getWidth() * 10);
|
||||
starPositionsY[i] = MathUtils.random(-Gdx.graphics.getHeight() * 10, Gdx.graphics.getHeight() * 10);
|
||||
}
|
||||
}
|
||||
|
||||
public void render() {
|
||||
Bugger.getInstance().batch().begin();
|
||||
|
||||
float cameraX = Bugger.cam.position.x;
|
||||
float cameraY = Bugger.cam.position.y;
|
||||
|
||||
drawGalaxies(cameraX, cameraY);
|
||||
drawNebulae(cameraX, cameraY);
|
||||
drawStars(cameraX, cameraY);
|
||||
|
||||
Bugger.getInstance().batch().end();
|
||||
}
|
||||
|
||||
private void drawStars(float cameraX, float cameraY) {
|
||||
|
||||
for (int i = 0; i < numStars; i++) {
|
||||
float starScrollSpeedX = 0.5f;
|
||||
float starX = starPositionsX[i] - cameraX * starScrollSpeedX;
|
||||
float starScrollSpeedY = 0.5f;
|
||||
float starY = starPositionsY[i] - cameraY * starScrollSpeedY;
|
||||
|
||||
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;
|
||||
|
||||
Bugger.spriteBatch.draw(starTexture,
|
||||
starX + offsetX,
|
||||
starY + offsetY,
|
||||
scaledWidth,
|
||||
scaledHeight,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
starTexture.dispose();
|
||||
galaxyTexture.dispose();
|
||||
nebulaTexture.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user