Initial commit

This commit is contained in:
Daniel
2024-10-15 22:44:48 +03:00
commit ec2b5cf98c
30 changed files with 1346 additions and 0 deletions

13
core/build.gradle Normal file
View File

@@ -0,0 +1,13 @@
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
eclipse.project.name = appName + '-core'
dependencies {
api "com.badlogicgames.box2dlights:box2dlights:$box2dlightsVersion"
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.github.czyzby:noise4j:$noise4jVersion"
if(enableGraalNative == 'true') {
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"
}
}

View File

@@ -0,0 +1,71 @@
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();
}
}

View File

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

View File

@@ -0,0 +1,96 @@
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 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 float enemySpawnTimer = 0f; // Timer to manage enemy spawning
private static final float ENEMY_SPAWN_INTERVAL = 2f;
private final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
public GameScreen() {
world = new World(new Vector2(0, 0), true);
player = Player.getInstance();
player.setPlayer(world, 100, 100);
enemies = new ArrayList<>();
}
@Override
public void show() {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(1 / 2f, 6, 2);
player.render();
enemySpawnTimer += delta;
if (enemySpawnTimer >= ENEMY_SPAWN_INTERVAL) {
enemies.add(new Enemy(world, Player.getInstance().getPosition()));
enemySpawnTimer = 0; // Reset the timer
}
// Move enemies towards the player
for (Enemy enemy : enemies) {
enemy.moveTowards(player.getPosition());
}
// Render player and enemies
player.render(); // Call the player's render method
for (Enemy enemy : enemies) {
enemy.render(); // Render each enemy
}
debugRenderer.render(world, spriteBatch.getProjectionMatrix());
}
@Override
public void resize(int i, int i1) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
spriteBatch.dispose();
world.dispose();
}
}

View File

@@ -0,0 +1,11 @@
package org.lumijiez.bugger;
import com.badlogic.gdx.Game;
/** {@link com.badlogic.gdx.ApplicationListener} implementation shared by all platforms. */
public class Main extends Game {
@Override
public void create() {
setScreen(new GameScreen());
}
}

View File

@@ -0,0 +1,98 @@
package org.lumijiez.bugger;
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;
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 {
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() { }
public static Player getInstance() {
if (instance == null) {
instance = new Player();
}
return instance;
}
public void setPlayer(World world, float x, float y) {
this.world = world;
this.body = createBody(x, y);
}
public void move(float deltaX, float deltaY) {
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();
}
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();
}
}