Add CollisionSystem
This commit is contained in:
parent
74ba61ee53
commit
b9148c028d
16
core/src/com/me/asteroids/events/CollisionEvent.java
Normal file
16
core/src/com/me/asteroids/events/CollisionEvent.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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));
|
||||
|
||||
|
42
core/src/com/me/asteroids/systems/CollisionSystem.java
Normal file
42
core/src/com/me/asteroids/systems/CollisionSystem.java
Normal file
@ -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<ModelComponent> 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) {}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user