minor box2d addition

This commit is contained in:
Quillraven
2025-05-25 15:06:21 +02:00
parent 548edbbe0f
commit 363cd0e679
4 changed files with 51 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
package io.github.com.quillraven.component;
import com.badlogic.ashley.core.Component;
import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
public record Physic(
Body body,
Vector2 prevPosition
) implements Component {
public static final ComponentMapper<Physic> MAPPER = ComponentMapper.getFor(Physic.class);
}

View File

@@ -4,6 +4,8 @@ import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Disposable;
import io.github.com.quillraven.GdxGame;
import io.github.com.quillraven.asset.MapAsset;
@@ -18,11 +20,14 @@ public class GameScreen extends ScreenAdapter {
private final TiledService tiledService;
private final Engine engine;
private final TiledAshleySpawner tiledAshleySpawner;
private final World physicWorld;
public GameScreen(GdxGame game) {
this.tiledService = new TiledService(game.getAssetService());
this.physicWorld = new World(Vector2.Zero, true);
this.physicWorld.setAutoClearForces(false);
this.engine = new Engine();
this.tiledAshleySpawner = new TiledAshleySpawner(this.engine);
this.tiledAshleySpawner = new TiledAshleySpawner(this.engine, this.physicWorld);
// add ECS systems
this.engine.addSystem(new RenderSystem(game.getBatch(), game.getViewport(), game.getCamera()));
@@ -53,5 +58,6 @@ public class GameScreen extends ScreenAdapter {
disposable.dispose();
}
}
this.physicWorld.dispose();
}
}

View File

@@ -6,20 +6,27 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapObjects;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.GdxRuntimeException;
import io.github.com.quillraven.GdxGame;
import io.github.com.quillraven.component.Graphic;
import io.github.com.quillraven.component.Physic;
import io.github.com.quillraven.component.Transform;
public class TiledAshleySpawner {
private final Engine engine;
private final World physicWorld;
public TiledAshleySpawner(Engine engine) {
public TiledAshleySpawner(Engine engine, World physicWorld) {
this.engine = engine;
this.physicWorld = physicWorld;
}
public void loadMapObjects(TiledMap tiledMap) {
@@ -49,11 +56,28 @@ public class TiledAshleySpawner {
Entity entity = this.engine.createEntity();
addEntityTransform(tileMapObject, entity);
addEntityPhysic(tileMapObject.getTile().getObjects(), entity);
entity.add(new Graphic(tileMapObject.getTile().getTextureRegion(), Color.WHITE.cpy()));
this.engine.addEntity(entity);
}
private void addEntityPhysic(MapObjects objects, Entity entity) {
if (objects.getCount() == 0) return;
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(entity.getComponent(Transform.class).position());
bodyDef.fixedRotation = true;
Body body = this.physicWorld.createBody(bodyDef);
for (MapObject object : objects) {
}
entity.add(new Physic(body, new Vector2(body.getPosition())));
}
private static void addEntityTransform(TiledMapTileMapObject tileMapObject, Entity entity) {
Vector2 position = new Vector2(tileMapObject.getX(), tileMapObject.getY());
TextureRegion textureRegion = tileMapObject.getTile().getTextureRegion();

View File

@@ -0,0 +1,6 @@
package io.github.com.quillraven.tiled;
import com.badlogic.gdx.physics.box2d.World;
public final class TiledPhysics {
}