diff --git a/core/src/com/me/asteroids/events/CollisionEvent.java b/core/src/com/me/asteroids/events/CollisionEvent.java new file mode 100644 index 0000000..d8118c7 --- /dev/null +++ b/core/src/com/me/asteroids/events/CollisionEvent.java @@ -0,0 +1,16 @@ +package com.me.asteroids.events; + +import com.me.common.ecs.Entity; +import com.me.common.ecs.event.Event; + +public class CollisionEvent extends Event { + + public Entity entityA; + public Entity entityB; + + public CollisionEvent(Entity entityA, Entity entityB) { + this.entityA = entityA; + this.entityB = entityB; + } + +} diff --git a/core/src/com/me/asteroids/screens/GameScreen.java b/core/src/com/me/asteroids/screens/GameScreen.java index 598b380..c37f50d 100644 --- a/core/src/com/me/asteroids/screens/GameScreen.java +++ b/core/src/com/me/asteroids/screens/GameScreen.java @@ -12,6 +12,7 @@ import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.VelocityComponent; +import com.me.asteroids.systems.CollisionSystem; import com.me.asteroids.systems.ModelRenderSystem; import com.me.asteroids.systems.MovementSystem; import com.me.asteroids.systems.PlayerInputSystem; @@ -49,6 +50,7 @@ public class GameScreen extends Screen { engine.registerSystem(new PlayerInputSystem(engine)); engine.registerSystem(new MovementSystem(engine)); + engine.registerSystem(new CollisionSystem(engine)); engine.registerSystem(new ScreenWrapSystem(engine)); engine.registerSystem(new ModelRenderSystem(engine, graphics)); diff --git a/core/src/com/me/asteroids/systems/CollisionSystem.java b/core/src/com/me/asteroids/systems/CollisionSystem.java new file mode 100644 index 0000000..50f1354 --- /dev/null +++ b/core/src/com/me/asteroids/systems/CollisionSystem.java @@ -0,0 +1,42 @@ +package com.me.asteroids.systems; + +import com.badlogic.gdx.math.Rectangle; +import com.me.asteroids.components.ModelComponent; +import com.me.asteroids.events.CollisionEvent; +import com.me.common.ecs.ComponentMapper; +import com.me.common.ecs.Engine; +import com.me.common.ecs.Entity; +import com.me.common.ecs.EntitySystem; + +public class CollisionSystem extends EntitySystem { + + private ComponentMapper modelMapper; + + public CollisionSystem(Engine engine) { + super(engine, ModelComponent.class); + modelMapper = engine.getComponentMapper(ModelComponent.class); + } + + @Override + public void processEntities(float dt) { + Entity[] entities = getEntities().items; + for (int i = 0, n = getEntities().size; i < n-1; i++) { + Entity entityA = entities[i]; + Rectangle aabbA = modelMapper.get(entityA).aabb; + + for (int j = i + 1; j < n; j++) { + Entity entityB = entities[j]; + + Rectangle aabbB = modelMapper.get(entityB).aabb; + if (aabbA.overlaps(aabbB)) { + CollisionEvent event = new CollisionEvent(entityA, entityB); + engine.callEvent(event); + } + } + } + } + + @Override + public void processEntity(Entity entity, float dt) {} + +}