use of de.gurkenlabs.litiengine.entities.IMobileEntity in project litiengine by gurkenlabs.
the class EnvironmentTests method testMobileEntity.
@Test
public void testMobileEntity() {
IMobileEntity mobileEntity = mock(IMobileEntity.class);
when(mobileEntity.getMapId()).thenReturn(456);
when(mobileEntity.getName()).thenReturn("test");
when(mobileEntity.getRenderType()).thenReturn(RenderType.NORMAL);
this.testEnvironment.add(mobileEntity);
assertNotNull(this.testEnvironment.get(456));
assertNotNull(this.testEnvironment.getMobileEntity(456));
assertNotNull(this.testEnvironment.getMobileEntity("test"));
assertEquals(1, this.testEnvironment.getMobileEntities().size());
assertEquals(1, this.testEnvironment.getByType(IMobileEntity.class).size());
assertEquals(0, this.testEnvironment.getByType(ICombatEntity.class).size());
assertEquals(1, this.testEnvironment.getEntities().size());
this.testEnvironment.remove(mobileEntity);
assertNull(this.testEnvironment.get(456));
assertNull(this.testEnvironment.getMobileEntity(456));
assertNull(this.testEnvironment.getMobileEntity("test"));
assertEquals(0, this.testEnvironment.getMobileEntities().size());
assertEquals(0, this.testEnvironment.getByType(IMobileEntity.class).size());
assertEquals(0, this.testEnvironment.getEntities().size());
}
use of de.gurkenlabs.litiengine.entities.IMobileEntity in project litiengine by gurkenlabs.
the class EnvironmentTests method testEntitiesByTag.
@Test
public void testEntitiesByTag() {
IMobileEntity entityWithTags = mock(IMobileEntity.class);
when(entityWithTags.getMapId()).thenReturn(456);
when(entityWithTags.getRenderType()).thenReturn(RenderType.NORMAL);
IMobileEntity anotherEntityWithTags = mock(IMobileEntity.class);
when(anotherEntityWithTags.getMapId()).thenReturn(123);
when(anotherEntityWithTags.getRenderType()).thenReturn(RenderType.NORMAL);
ArrayList<String> tags = new ArrayList<>();
tags.add("tag1");
tags.add("tag2");
when(entityWithTags.getTags()).thenReturn(tags);
when(anotherEntityWithTags.getTags()).thenReturn(tags);
this.testEnvironment.add(entityWithTags);
this.testEnvironment.add(anotherEntityWithTags);
assertEquals(2, this.testEnvironment.getByTag("tag1").size());
assertEquals(2, this.testEnvironment.getByTag("tag2").size());
assertEquals(0, this.testEnvironment.getByTag("invalidTag").size());
this.testEnvironment.remove(entityWithTags);
this.testEnvironment.remove(anotherEntityWithTags);
assertEquals(0, this.testEnvironment.getByTag("tag1").size());
assertEquals(0, this.testEnvironment.getByTag("tag2").size());
}
use of de.gurkenlabs.litiengine.entities.IMobileEntity in project litiengine by gurkenlabs.
the class EntityNavigator method update.
@Override
public void update() {
if (!this.isNavigating()) {
return;
}
if (this.path == null) {
return;
}
for (final Predicate<IMobileEntity> pred : this.cancelNavigationConditions) {
if (pred.test(this.getEntity())) {
this.stop();
return;
}
}
final PathIterator pi = this.path.getPath().getPathIterator(null);
if (pi.isDone()) {
this.stop();
return;
}
// although at max 6 elements are returned, sometimes the path
// implementation tries to access index 20 ... don't know why, but this
// prevents it
final double[] startCoordinates = new double[22];
final double[] coordinates = new double[22];
for (int i = 0; i <= this.currentSegment; i++) {
if (pi.isDone()) {
this.stop();
return;
}
pi.currentSegment(startCoordinates);
pi.next();
}
if (pi.isDone()) {
this.stop();
return;
}
pi.currentSegment(coordinates);
final double distance = GeometricUtilities.distance(this.entity.getCollisionBox().getCenterX(), this.entity.getCollisionBox().getCenterY(), coordinates[0], coordinates[1]);
if (distance < ACCEPTABLE_ERROR) {
++this.currentSegment;
return;
}
final double angle = GeometricUtilities.calcRotationAngleInDegrees(this.entity.getCollisionBox().getCenterX(), this.entity.getCollisionBox().getCenterY(), coordinates[0], coordinates[1]);
final float pixelsPerTick = this.entity.getTickVelocity();
Game.getPhysicsEngine().move(this.entity, (float) angle, (float) (distance < pixelsPerTick ? distance : pixelsPerTick));
}
use of de.gurkenlabs.litiengine.entities.IMobileEntity in project litiengine by gurkenlabs.
the class Environment method load.
/**
* Loads the specified entiy by performing the following steps:
* <ol>
* <li>add to physics engine</li>
* <li>register entity for update</li>
* <li>register animation controller for update</li>
* <li>register movement controller for update</li>
* <li>register AI controller for update</li>
* </ol>
*
* @param entity
*/
private void load(final IEntity entity) {
// 1. add to physics engine
this.loadPhysicsEntity(entity);
// 2. register for update or activate
this.loadUpdatableOrEmitterEntity(entity);
// 3. register animation controller for update
final IAnimationController animation = Game.getEntityControllerManager().getAnimationController(entity);
if (animation != null) {
Game.getLoop().attach(animation);
}
// 4. register movement controller for update
if (entity instanceof IMobileEntity) {
final IMovementController<? extends IMobileEntity> movementController = Game.getEntityControllerManager().getMovementController((IMobileEntity) entity);
if (movementController != null) {
Game.getLoop().attach(movementController);
}
}
// 5. register ai controller for update
final IEntityController<? extends IEntity> controller = Game.getEntityControllerManager().getAIController(entity);
if (controller != null) {
Game.getLoop().attach(controller);
}
if (entity instanceof LightSource || entity instanceof StaticShadow) {
this.updateColorLayers(entity);
}
}
use of de.gurkenlabs.litiengine.entities.IMobileEntity 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);
}
}
Aggregations