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;
|
||||
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.*;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.ScreenUtils;
|
||||
import org.lumijiez.bugger.Main;
|
||||
import org.lumijiez.bugger.entities.Player;
|
||||
import org.lumijiez.bugger.entities.enemies.Wasp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class GameScreen implements Screen {
|
||||
private final World world;
|
||||
private final Player player;
|
||||
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 static final float ENEMY_SPAWN_INTERVAL = 2f;
|
||||
private final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
|
||||
@@ -49,18 +43,18 @@ public class GameScreen implements Screen {
|
||||
|
||||
enemySpawnTimer += delta;
|
||||
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
|
||||
}
|
||||
|
||||
// Move enemies towards the player
|
||||
for (Enemy enemy : enemies) {
|
||||
for (Wasp enemy : enemies) {
|
||||
enemy.moveTowards(player.getPosition());
|
||||
}
|
||||
|
||||
// Render player and enemies
|
||||
player.render(); // Call the player's render method
|
||||
for (Enemy enemy : enemies) {
|
||||
for (Wasp enemy : enemies) {
|
||||
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.Input;
|
||||
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.physics.box2d.Body;
|
||||
import com.badlogic.gdx.physics.box2d.BodyDef;
|
||||
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.math.Vector2;
|
||||
|
||||
import static org.lumijiez.bugger.GameScreen.spriteBatch;
|
||||
|
||||
public class Player {
|
||||
public class Player extends Entity {
|
||||
private static Player instance;
|
||||
private Body body;
|
||||
private final float size = 50f;
|
||||
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() {
|
||||
if (instance == null) {
|
||||
@@ -41,58 +35,31 @@ public class Player {
|
||||
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() {
|
||||
handleInput();
|
||||
sprite.setSize(size, size);
|
||||
sprite.setPosition(body.getPosition().x - size / 2, body.getPosition().y - size / 2); // Center the sprite
|
||||
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();
|
||||
updateSpriteRotation();
|
||||
super.render();
|
||||
}
|
||||
|
||||
public void handleInput() {
|
||||
Vector2 direction = new Vector2(0, 0);
|
||||
|
||||
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.W)) direction.y = 1;
|
||||
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.S)) direction.y = -1;
|
||||
|
||||
if (direction.len() > 1) direction.nor();
|
||||
|
||||
move(direction.x, direction.y);
|
||||
}
|
||||
|
||||
public Body getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public float getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public Vector2 getPosition() {
|
||||
return body.getPosition();
|
||||
private void updateSpriteRotation() {
|
||||
float mouseX = Gdx.input.getX();
|
||||
float mouseY = Gdx.input.getY();
|
||||
Vector2 mousePosition = new Vector2(mouseX, Gdx.graphics.getHeight() - mouseY);
|
||||
Vector2 direction = mousePosition.cpy().sub(body.getPosition()).nor();
|
||||
float angle = direction.angleDeg() + 270f;
|
||||
body.setTransform(body.getPosition(), angle * (float) Math.PI / 180f);
|
||||
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
||||
}
|
||||
}
|
||||
@@ -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