cleanup physic bodies
This commit is contained in:
@@ -9,6 +9,7 @@ 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;
|
||||
import io.github.com.quillraven.system.CleanupSystem;
|
||||
import io.github.com.quillraven.system.PhysicDebugRenderSystem;
|
||||
import io.github.com.quillraven.system.RenderSystem;
|
||||
import io.github.com.quillraven.system.TiledServiceTestSystem;
|
||||
@@ -32,6 +33,7 @@ public class GameScreen extends ScreenAdapter {
|
||||
|
||||
// add ECS systems
|
||||
this.engine.addSystem(new RenderSystem(game.getBatch(), game.getViewport(), game.getCamera()));
|
||||
this.engine.addSystem(new CleanupSystem());
|
||||
this.engine.addSystem(new TiledServiceTestSystem(this.tiledService));
|
||||
this.engine.addSystem(new PhysicDebugRenderSystem(this.physicWorld, game.getCamera()));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.github.com.quillraven.system;
|
||||
|
||||
import com.badlogic.ashley.core.Engine;
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.EntityListener;
|
||||
import com.badlogic.ashley.core.EntitySystem;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.gdx.physics.box2d.Body;
|
||||
import io.github.com.quillraven.component.Physic;
|
||||
|
||||
public class CleanupSystem extends EntitySystem implements EntityListener {
|
||||
|
||||
@Override
|
||||
public void addedToEngine(Engine engine) {
|
||||
super.addedToEngine(engine);
|
||||
engine.addEntityListener(Family.all(Physic.class).get(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removedFromEngine(Engine engine) {
|
||||
super.removedFromEngine(engine);
|
||||
engine.removeEntityListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void entityAdded(Entity entity) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void entityRemoved(Entity entity) {
|
||||
// !!! Important !!!
|
||||
// This does not work if the Physic component gets removed from an entity
|
||||
// because the component is no longer accessible here.
|
||||
// This ONLY works when an entity with a Physic component gets removed entirely from the engine.
|
||||
Physic physic = Physic.MAPPER.get(entity);
|
||||
if (physic != null) {
|
||||
Body body = physic.body();
|
||||
body.getWorld().destroyBody(body);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user