refactor, entity base class
This commit is contained in:
@@ -1,71 +0,0 @@
|
|||||||
package org.lumijiez.bugger;
|
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
|
||||||
import com.badlogic.gdx.graphics.g2d.Sprite;
|
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
|
||||||
import com.badlogic.gdx.math.Vector2;
|
|
||||||
import com.badlogic.gdx.physics.box2d.*;
|
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import static org.lumijiez.bugger.GameScreen.spriteBatch;
|
|
||||||
|
|
||||||
public class Enemy {
|
|
||||||
private Body body;
|
|
||||||
private final float size = 20f;
|
|
||||||
private final float speed = 50f;
|
|
||||||
private final Vector2 position;
|
|
||||||
private static final Random random = new Random();
|
|
||||||
private Sprite sprite = new Sprite(new Texture(Gdx.files.internal("images/wasp.png")));
|
|
||||||
|
|
||||||
public Enemy(World world, Vector2 playerPosition) {
|
|
||||||
float spawnRadius = 100;
|
|
||||||
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.position = new Vector2(spawnX, spawnY);
|
|
||||||
this.body = createBody(world, position.x, position.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Body createBody(World world, 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;
|
|
||||||
body.createFixture(fixtureDef);
|
|
||||||
|
|
||||||
shape.dispose();
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void moveTowards(Vector2 target) {
|
|
||||||
Vector2 direction = target.cpy().sub(body.getPosition()).nor();
|
|
||||||
body.setLinearVelocity(direction.scl(speed / 100f));
|
|
||||||
|
|
||||||
float angle = direction.angleDeg() + 270f;
|
|
||||||
body.setTransform(body.getPosition(), angle * (float) Math.PI / 180f);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void render() {
|
|
||||||
sprite.setOrigin(size / 2, size / 2);
|
|
||||||
sprite.setSize(size, size);
|
|
||||||
sprite.setPosition(body.getPosition().x - size / 2, body.getPosition().y - size / 2);
|
|
||||||
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
|
||||||
spriteBatch.begin();
|
|
||||||
sprite.draw(spriteBatch);
|
|
||||||
spriteBatch.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Vector2 getPosition() {
|
|
||||||
return body.getPosition();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package org.lumijiez.bugger;
|
|
||||||
|
|
||||||
public class Entity {
|
|
||||||
}
|
|
||||||
@@ -1,28 +1,22 @@
|
|||||||
package org.lumijiez.bugger;
|
package org.lumijiez.bugger;
|
||||||
|
|
||||||
import com.badlogic.gdx.Game;
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.Input;
|
|
||||||
import com.badlogic.gdx.Screen;
|
import com.badlogic.gdx.Screen;
|
||||||
import com.badlogic.gdx.graphics.Color;
|
|
||||||
import com.badlogic.gdx.graphics.GL20;
|
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.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.badlogic.gdx.physics.box2d.*;
|
import com.badlogic.gdx.physics.box2d.*;
|
||||||
import com.badlogic.gdx.utils.Array;
|
import org.lumijiez.bugger.entities.Player;
|
||||||
import com.badlogic.gdx.utils.ScreenUtils;
|
import org.lumijiez.bugger.entities.enemies.Wasp;
|
||||||
import org.lumijiez.bugger.Main;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class GameScreen implements Screen {
|
public class GameScreen implements Screen {
|
||||||
private final World world;
|
private final World world;
|
||||||
private final Player player;
|
private final Player player;
|
||||||
public static final SpriteBatch spriteBatch = new SpriteBatch();;
|
public static final SpriteBatch spriteBatch = new SpriteBatch();;
|
||||||
private List<Enemy> enemies;
|
private List<Wasp> enemies;
|
||||||
private float enemySpawnTimer = 0f; // Timer to manage enemy spawning
|
private float enemySpawnTimer = 0f; // Timer to manage enemy spawning
|
||||||
private static final float ENEMY_SPAWN_INTERVAL = 2f;
|
private static final float ENEMY_SPAWN_INTERVAL = 2f;
|
||||||
private final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
|
private final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
|
||||||
@@ -49,18 +43,18 @@ public class GameScreen implements Screen {
|
|||||||
|
|
||||||
enemySpawnTimer += delta;
|
enemySpawnTimer += delta;
|
||||||
if (enemySpawnTimer >= ENEMY_SPAWN_INTERVAL) {
|
if (enemySpawnTimer >= ENEMY_SPAWN_INTERVAL) {
|
||||||
enemies.add(new Enemy(world, Player.getInstance().getPosition()));
|
enemies.add(new Wasp(world, Player.getInstance().getPosition()));
|
||||||
enemySpawnTimer = 0; // Reset the timer
|
enemySpawnTimer = 0; // Reset the timer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move enemies towards the player
|
// Move enemies towards the player
|
||||||
for (Enemy enemy : enemies) {
|
for (Wasp enemy : enemies) {
|
||||||
enemy.moveTowards(player.getPosition());
|
enemy.moveTowards(player.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render player and enemies
|
// Render player and enemies
|
||||||
player.render(); // Call the player's render method
|
player.render(); // Call the player's render method
|
||||||
for (Enemy enemy : enemies) {
|
for (Wasp enemy : enemies) {
|
||||||
enemy.render(); // Render each enemy
|
enemy.render(); // Render each enemy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
52
core/src/main/java/org/lumijiez/bugger/entities/Entity.java
Normal file
52
core/src/main/java/org/lumijiez/bugger/entities/Entity.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package org.lumijiez.bugger.entities;
|
||||||
|
|
||||||
|
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.GameScreen;
|
||||||
|
|
||||||
|
public abstract class Entity {
|
||||||
|
protected Body body;
|
||||||
|
protected Sprite sprite;
|
||||||
|
protected final float size;
|
||||||
|
protected World world;
|
||||||
|
|
||||||
|
public Entity(World world, String texturePath, float size) {
|
||||||
|
this.world = world;
|
||||||
|
this.size = size;
|
||||||
|
this.sprite = new Sprite(new Texture(Gdx.files.internal(texturePath)));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
body.createFixture(fixtureDef);
|
||||||
|
|
||||||
|
shape.dispose();
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void render() {
|
||||||
|
sprite.setOrigin(size / 2, size / 2);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector2 getPosition() {
|
||||||
|
return body.getPosition();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
package org.lumijiez.bugger;
|
package org.lumijiez.bugger.entities;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.Input;
|
import com.badlogic.gdx.Input;
|
||||||
import com.badlogic.gdx.graphics.Color;
|
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
import com.badlogic.gdx.graphics.g2d.Sprite;
|
import com.badlogic.gdx.graphics.g2d.Sprite;
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
|
||||||
import com.badlogic.gdx.physics.box2d.Body;
|
import com.badlogic.gdx.physics.box2d.Body;
|
||||||
import com.badlogic.gdx.physics.box2d.BodyDef;
|
import com.badlogic.gdx.physics.box2d.BodyDef;
|
||||||
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
||||||
@@ -13,17 +11,13 @@ import com.badlogic.gdx.physics.box2d.PolygonShape;
|
|||||||
import com.badlogic.gdx.physics.box2d.World;
|
import com.badlogic.gdx.physics.box2d.World;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
|
||||||
import static org.lumijiez.bugger.GameScreen.spriteBatch;
|
public class Player extends Entity {
|
||||||
|
|
||||||
public class Player {
|
|
||||||
private static Player instance;
|
private static Player instance;
|
||||||
private Body body;
|
|
||||||
private final float size = 50f;
|
|
||||||
private final float speed = 5f;
|
private final float speed = 5f;
|
||||||
public World world;
|
|
||||||
private Sprite sprite = new Sprite(new Texture(Gdx.files.internal("images/wasp.png")));
|
|
||||||
|
|
||||||
private Player() { }
|
private Player() {
|
||||||
|
super(null, "images/wasp.png", 50f); // World will be set later
|
||||||
|
}
|
||||||
|
|
||||||
public static Player getInstance() {
|
public static Player getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
@@ -41,58 +35,31 @@ public class Player {
|
|||||||
body.setLinearVelocity(deltaX * speed, deltaY * speed);
|
body.setLinearVelocity(deltaX * speed, deltaY * speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
private 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;
|
|
||||||
body.createFixture(fixtureDef);
|
|
||||||
|
|
||||||
shape.dispose();
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void render() {
|
public void render() {
|
||||||
handleInput();
|
handleInput();
|
||||||
sprite.setSize(size, size);
|
updateSpriteRotation();
|
||||||
sprite.setPosition(body.getPosition().x - size / 2, body.getPosition().y - size / 2); // Center the sprite
|
super.render();
|
||||||
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI)); // Convert radians to degrees
|
|
||||||
spriteBatch.begin(); // Start batch
|
|
||||||
sprite.draw(spriteBatch); // Draw the enemy sprite
|
|
||||||
spriteBatch.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleInput() {
|
public void handleInput() {
|
||||||
Vector2 direction = new Vector2(0, 0);
|
Vector2 direction = new Vector2(0, 0);
|
||||||
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.A)) direction.x = -1;
|
if (Gdx.input.isKeyPressed(Input.Keys.A)) direction.x = -1;
|
||||||
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.D)) direction.x = 1;
|
if (Gdx.input.isKeyPressed(Input.Keys.D)) direction.x = 1;
|
||||||
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.W)) direction.y = 1;
|
if (Gdx.input.isKeyPressed(Input.Keys.W)) direction.y = 1;
|
||||||
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.S)) direction.y = -1;
|
if (Gdx.input.isKeyPressed(Input.Keys.S)) direction.y = -1;
|
||||||
|
|
||||||
if (direction.len() > 1) direction.nor();
|
if (direction.len() > 1) direction.nor();
|
||||||
|
|
||||||
move(direction.x, direction.y);
|
move(direction.x, direction.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Body getBody() {
|
private void updateSpriteRotation() {
|
||||||
return body;
|
float mouseX = Gdx.input.getX();
|
||||||
}
|
float mouseY = Gdx.input.getY();
|
||||||
|
Vector2 mousePosition = new Vector2(mouseX, Gdx.graphics.getHeight() - mouseY);
|
||||||
public float getSize() {
|
Vector2 direction = mousePosition.cpy().sub(body.getPosition()).nor();
|
||||||
return size;
|
float angle = direction.angleDeg() + 270f;
|
||||||
}
|
body.setTransform(body.getPosition(), angle * (float) Math.PI / 180f);
|
||||||
|
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
||||||
public Vector2 getPosition() {
|
|
||||||
return body.getPosition();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package org.lumijiez.bugger.entities.enemies;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.badlogic.gdx.physics.box2d.*;
|
||||||
|
import org.lumijiez.bugger.entities.Entity;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Wasp extends Entity {
|
||||||
|
private final float speed = 50f;
|
||||||
|
private static final Random random = new Random();
|
||||||
|
|
||||||
|
public Wasp(World world, Vector2 playerPosition) {
|
||||||
|
super(world, "images/wasp.png", 20f);
|
||||||
|
float spawnRadius = 100;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void moveTowards(Vector2 target) {
|
||||||
|
Vector2 direction = target.cpy().sub(body.getPosition()).nor();
|
||||||
|
body.setLinearVelocity(direction.scl(speed / 100f));
|
||||||
|
|
||||||
|
float angle = direction.angleDeg() + 270f;
|
||||||
|
body.setTransform(body.getPosition(), angle * (float) Math.PI / 180f);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void render() {
|
||||||
|
super.render();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user