use of de.gurkenlabs.litiengine.entities.CollisionBox in project litiengine by gurkenlabs.
the class EnvironmentTests method testLoading.
@Test
public void testLoading() {
CollisionBox testCollider = new CollisionBox(true);
testCollider.setMapId(1);
testCollider.setName("test");
this.testEnvironment.add(testCollider);
Prop testProp = new Prop(0, 0, null);
testProp.setMapId(1);
testProp.setName("test");
this.testEnvironment.add(testProp);
Emitter testEmitter = new Emitter(1, 1) {
@Override
protected Particle createNewParticle() {
return null;
}
};
testEmitter.setMapId(1);
testEmitter.setName("test");
this.testEnvironment.add(testEmitter);
this.testEnvironment.load();
// load a second time to ensure nothing brakes
this.testEnvironment.load();
assertTrue(this.testEnvironment.isLoaded());
CollisionBox testCollider2 = new CollisionBox(true);
testCollider.setMapId(2);
testCollider.setName("test2");
this.testEnvironment.add(testCollider2);
this.testEnvironment.unload();
assertFalse(this.testEnvironment.isLoaded());
}
use of de.gurkenlabs.litiengine.entities.CollisionBox in project litiengine by gurkenlabs.
the class EnvironmentTests method testCollisionBox.
@Test
public void testCollisionBox() {
CollisionBox testCollider = new CollisionBox(true);
testCollider.setMapId(1);
testCollider.setName("test");
this.testEnvironment.add(testCollider);
assertNotNull(this.testEnvironment.getCollisionBox(1));
assertNotNull(this.testEnvironment.getCollisionBox("test"));
assertEquals(1, this.testEnvironment.getByType(CollisionBox.class).size());
assertEquals(1, this.testEnvironment.getEntities().size());
this.testEnvironment.remove(testCollider);
assertNull(this.testEnvironment.getCollisionBox(1));
assertNull(this.testEnvironment.getCollisionBox("test"));
assertEquals(0, this.testEnvironment.getByType(CollisionBox.class).size());
assertEquals(0, this.testEnvironment.getEntities().size());
}
use of de.gurkenlabs.litiengine.entities.CollisionBox in project litiengine by gurkenlabs.
the class CollisionBoxMapObjectLoader method load.
@Override
public Collection<IEntity> load(IEnvironment environment, IMapObject mapObject) {
if (MapObjectType.get(mapObject.getType()) != MapObjectType.COLLISIONBOX) {
throw new IllegalArgumentException("Cannot load a mapobject of the type " + mapObject.getType() + " with a loader of the type " + CollisionBoxMapObjectLoader.class);
}
boolean isObstacle = mapObject.getCustomPropertyBool(MapObjectProperty.PROP_OBSTACLE, true);
boolean isObstructingLight = mapObject.getCustomPropertyBool(MapObjectProperty.COLLISIONBOX_OBSTRUCTINGLIGHTS);
final CollisionBox col = this.createCollisionBox(mapObject, isObstacle, isObstructingLight);
loadDefaultProperties(col, mapObject);
col.setCollisionBoxWidth(col.getWidth());
col.setCollisionBoxHeight(col.getHeight());
Collection<IEntity> entities = super.load(environment, mapObject);
entities.add(col);
if (isObstructingLight) {
entities.add(new StaticShadow(col));
}
return entities;
}
use of de.gurkenlabs.litiengine.entities.CollisionBox in project litiengine by gurkenlabs.
the class Environment method add.
@Override
public void add(final IEntity entity) {
if (entity == null) {
return;
}
// set local map id if none is set for the entity
if (entity.getMapId() == 0) {
entity.setMapId(this.getLocalMapId());
}
if (entity instanceof Emitter) {
Emitter emitter = (Emitter) entity;
this.getGroundRenderables().add(emitter.getGroundRenderable());
this.getOverlayRenderables().add(emitter.getOverlayRenderable());
this.emitters.add(emitter);
}
if (entity instanceof ICombatEntity) {
this.combatEntities.put(entity.getMapId(), (ICombatEntity) entity);
}
if (entity instanceof IMobileEntity) {
this.mobileEntities.put(entity.getMapId(), (IMobileEntity) entity);
}
if (entity instanceof Prop) {
this.props.add((Prop) entity);
}
if (entity instanceof Creature) {
this.creatures.add((Creature) entity);
}
if (entity instanceof CollisionBox) {
this.colliders.add((CollisionBox) entity);
}
if (entity instanceof LightSource) {
this.lightSources.add((LightSource) entity);
}
if (entity instanceof Trigger) {
this.triggers.add((Trigger) entity);
}
if (entity instanceof Spawnpoint) {
this.spawnPoints.add((Spawnpoint) entity);
}
if (entity instanceof StaticShadow) {
this.staticShadows.add((StaticShadow) entity);
} else if (entity instanceof MapArea) {
this.mapAreas.add((MapArea) entity);
}
for (String rawTag : entity.getTags()) {
if (rawTag == null) {
continue;
}
final String tag = rawTag.trim().toLowerCase();
if (tag.isEmpty()) {
continue;
}
if (this.entitiesByTag.containsKey(tag)) {
this.entitiesByTag.get(tag).add(entity);
continue;
}
this.entitiesByTag.put(tag, new CopyOnWriteArrayList<>());
this.entitiesByTag.get(tag).add(entity);
}
// we need to load the new entity manually
if (this.loaded) {
this.load(entity);
}
this.entities.get(entity.getRenderType()).put(entity.getMapId(), entity);
for (Consumer<IEntity> cons : this.entityAddedConsumers) {
cons.accept(entity);
}
}
use of de.gurkenlabs.litiengine.entities.CollisionBox in project litiengine by gurkenlabs.
the class Environment method unload.
/**
* Unload the specified entity by performing the following steps:
* <ol>
* <li>remove entities from physics engine</li>
* <li>unregister units from update</li>
* <li>unregister ai controller from update</li>
* <li>unregister animation controller from update</li>
* <li>unregister movement controller from update</li>
* </ol>
*
* @param entity
*/
private void unload(final IEntity entity) {
// 1. remove from physics engine
if (entity instanceof CollisionBox) {
final CollisionBox coll = (CollisionBox) entity;
if (coll.isObstacle()) {
Game.getPhysicsEngine().remove(coll.getBoundingBox());
} else {
Game.getPhysicsEngine().remove(coll);
}
} else if (entity instanceof ICollisionEntity) {
final ICollisionEntity coll = (ICollisionEntity) entity;
Game.getPhysicsEngine().remove(coll);
}
// 2. unregister from update
if (entity instanceof IUpdateable) {
Game.getLoop().detach((IUpdateable) entity);
}
// 3. unregister ai controller from update
final IEntityController<? extends IEntity> controller = Game.getEntityControllerManager().getAIController(entity);
if (controller != null) {
Game.getLoop().detach(controller);
}
// 4. unregister animation controller from update
final IAnimationController animation = Game.getEntityControllerManager().getAnimationController(entity);
if (animation != null) {
Game.getLoop().detach(animation);
}
// 5. unregister movement controller from update
if (entity instanceof IMobileEntity) {
final IMovementController<? extends IMobileEntity> movementController = Game.getEntityControllerManager().getMovementController((IMobileEntity) entity);
if (movementController != null) {
Game.getLoop().detach(movementController);
}
}
if (entity instanceof Emitter) {
Emitter em = (Emitter) entity;
em.deactivate();
}
}
Aggregations