Add ColliderComponent and add it to entities that can be collided with.
Only entities with ColliderComponent will be checked for collisions. Also, don't check collisions with inactive/removed entities.
This commit is contained in:
parent
3b1c075102
commit
c969b87eca
@ -6,6 +6,7 @@ import com.badlogic.gdx.math.Vector2;
|
||||
import com.me.asteroids.components.AccelerationComponent;
|
||||
import com.me.asteroids.components.AsteroidComponent;
|
||||
import com.me.asteroids.components.BulletComponent;
|
||||
import com.me.asteroids.components.ColliderComponent;
|
||||
import com.me.asteroids.components.DebrisComponent;
|
||||
import com.me.asteroids.components.ModelComponent;
|
||||
import com.me.asteroids.components.PlayerComponent;
|
||||
@ -28,6 +29,8 @@ public class EntityFactory {
|
||||
private static final Vector2 tmpA = new Vector2();
|
||||
private static final Vector2 tmpB = new Vector2();
|
||||
|
||||
private static final ColliderComponent COLLIDER = new ColliderComponent();
|
||||
|
||||
private static Entity createEntity(Engine engine) {
|
||||
Entity entity = engine.createEntity();
|
||||
if (Constants.DEBUG) {
|
||||
@ -64,6 +67,7 @@ public class EntityFactory {
|
||||
player.addComponent(velocity);
|
||||
player.addComponent(model);
|
||||
player.addComponent(accel);
|
||||
player.addComponent(COLLIDER);
|
||||
player.addComponent(new PlayerComponent());
|
||||
return player;
|
||||
}
|
||||
@ -161,6 +165,7 @@ public class EntityFactory {
|
||||
bullet.addComponent(position);
|
||||
bullet.addComponent(velocity);
|
||||
bullet.addComponent(model);
|
||||
bullet.addComponent(COLLIDER);
|
||||
bullet.addComponent(new BulletComponent());
|
||||
return bullet;
|
||||
}
|
||||
@ -185,6 +190,7 @@ public class EntityFactory {
|
||||
asteroid.addComponent(position);
|
||||
asteroid.addComponent(velocity);
|
||||
asteroid.addComponent(model);
|
||||
asteroid.addComponent(COLLIDER);
|
||||
asteroid.addComponent(new AsteroidComponent());
|
||||
return asteroid;
|
||||
}
|
||||
@ -236,6 +242,7 @@ public class EntityFactory {
|
||||
split.addComponent(position);
|
||||
split.addComponent(velocity);
|
||||
split.addComponent(asteroidComponent);
|
||||
split.addComponent(COLLIDER);
|
||||
entities[i] = split;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.me.asteroids.components;
|
||||
|
||||
import com.me.common.ecs.Component;
|
||||
|
||||
/**
|
||||
* Add this to Entities that should be checked for collisions with other entities.
|
||||
*/
|
||||
public class ColliderComponent implements Component {
|
||||
}
|
@ -11,6 +11,7 @@ import com.me.asteroids.Graphics;
|
||||
import com.me.asteroids.components.AccelerationComponent;
|
||||
import com.me.asteroids.components.AsteroidComponent;
|
||||
import com.me.asteroids.components.BulletComponent;
|
||||
import com.me.asteroids.components.ColliderComponent;
|
||||
import com.me.asteroids.components.DebrisComponent;
|
||||
import com.me.asteroids.components.ModelComponent;
|
||||
import com.me.asteroids.components.PlayerComponent;
|
||||
@ -58,6 +59,7 @@ public class GameScreen extends Screen implements Listener {
|
||||
engine.registerComponentClass(BulletComponent.class);
|
||||
engine.registerComponentClass(AsteroidComponent.class);
|
||||
engine.registerComponentClass(DebrisComponent.class);
|
||||
engine.registerComponentClass(ColliderComponent.class);
|
||||
engine.registerComponentClass(PositionComponent.class);
|
||||
engine.registerComponentClass(VelocityComponent.class);
|
||||
engine.registerComponentClass(AccelerationComponent.class);
|
||||
|
@ -2,6 +2,7 @@ package com.me.asteroids.systems;
|
||||
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.me.asteroids.EventFactory;
|
||||
import com.me.asteroids.components.ColliderComponent;
|
||||
import com.me.asteroids.components.ModelComponent;
|
||||
import com.me.common.ecs.ComponentMapper;
|
||||
import com.me.common.ecs.Engine;
|
||||
@ -13,7 +14,7 @@ public class CollisionSystem extends EntitySystem {
|
||||
private ComponentMapper<ModelComponent> modelMapper;
|
||||
|
||||
public CollisionSystem(Engine engine) {
|
||||
super(engine, ModelComponent.class);
|
||||
super(engine, ColliderComponent.class, ModelComponent.class);
|
||||
modelMapper = engine.getComponentMapper(ModelComponent.class);
|
||||
}
|
||||
|
||||
@ -22,10 +23,17 @@ public class CollisionSystem extends EntitySystem {
|
||||
Entity[] entities = getEntities().items;
|
||||
for (int i = 0, n = getEntities().size; i < n-1; i++) {
|
||||
Entity entityA = entities[i];
|
||||
if (!entityA.isActive() || entityA.isRemoved()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Rectangle aabbA = modelMapper.get(entityA).model.getBoundingBox();
|
||||
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
Entity entityB = entities[j];
|
||||
if (!entityB.isActive() || entityB.isRemoved()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Rectangle aabbB = modelMapper.get(entityB).model.getBoundingBox();
|
||||
if (aabbA.overlaps(aabbB)) {
|
||||
|
Loading…
Reference in New Issue
Block a user