Search in sources :

Example 6 with IMobileEntity

use of de.gurkenlabs.litiengine.entities.IMobileEntity 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();
    }
}
Also used : Emitter(de.gurkenlabs.litiengine.graphics.particles.Emitter) IMobileEntity(de.gurkenlabs.litiengine.entities.IMobileEntity) IUpdateable(de.gurkenlabs.litiengine.IUpdateable) ICollisionEntity(de.gurkenlabs.litiengine.entities.ICollisionEntity) IAnimationController(de.gurkenlabs.litiengine.graphics.animation.IAnimationController) CollisionBox(de.gurkenlabs.litiengine.entities.CollisionBox)

Example 7 with IMobileEntity

use of de.gurkenlabs.litiengine.entities.IMobileEntity in project litiengine by gurkenlabs.

the class EntityControllerManager method disposeControllers.

public void disposeControllers(final IEntity entity) {
    final IEntityController<? extends IEntity> aiController = this.getAIController(entity);
    if (aiController != null) {
        Game.getLoop().detach(aiController);
        this.aiControllers.remove(entity);
    }
    if (entity instanceof IMobileEntity) {
        final IMovementController<? extends IMobileEntity> controller = this.getMovementController((IMobileEntity) entity);
        if (controller != null) {
            Game.getLoop().detach(controller);
            this.movementControllers.remove(entity);
        }
    }
    final IAnimationController animationController = this.getAnimationController(entity);
    if (animationController != null) {
        animationController.dispose();
        this.animationControllers.remove(entity);
    }
}
Also used : IMobileEntity(de.gurkenlabs.litiengine.entities.IMobileEntity) IAnimationController(de.gurkenlabs.litiengine.graphics.animation.IAnimationController)

Example 8 with IMobileEntity

use of de.gurkenlabs.litiengine.entities.IMobileEntity in project litiengine by gurkenlabs.

the class Environment method remove.

@Override
public void remove(final IEntity entity) {
    if (entity == null) {
        return;
    }
    if (this.entities.get(entity.getRenderType()) != null) {
        this.entities.get(entity.getRenderType()).entrySet().removeIf(e -> e.getValue().getMapId() == entity.getMapId());
    }
    for (String tag : entity.getTags()) {
        if (this.entitiesByTag.containsKey(tag)) {
            this.entitiesByTag.get(tag).remove(entity);
            if (this.entitiesByTag.get(tag).isEmpty()) {
                this.entitiesByTag.remove(tag);
            }
        }
    }
    if (entity instanceof Emitter) {
        Emitter emitter = (Emitter) entity;
        this.groundRenderable.remove(emitter.getGroundRenderable());
        this.overlayRenderable.remove(emitter.getOverlayRenderable());
        this.emitters.remove(emitter);
    }
    if (entity instanceof MapArea) {
        this.mapAreas.remove(entity);
    }
    if (entity instanceof Prop) {
        this.props.remove(entity);
    }
    if (entity instanceof Creature) {
        this.creatures.remove(entity);
    }
    if (entity instanceof CollisionBox) {
        this.colliders.remove(entity);
        this.staticShadows.removeIf(x -> x.getOrigin() != null && x.getOrigin().equals(entity));
    }
    if (entity instanceof LightSource) {
        this.lightSources.remove(entity);
        this.updateColorLayers(entity);
    }
    if (entity instanceof Trigger) {
        this.triggers.remove(entity);
    }
    if (entity instanceof Spawnpoint) {
        this.spawnPoints.remove(entity);
    }
    if (entity instanceof StaticShadow) {
        this.staticShadows.remove(entity);
        this.updateColorLayers(entity);
    }
    if (entity instanceof IMobileEntity) {
        this.mobileEntities.values().remove(entity);
    }
    if (entity instanceof ICombatEntity) {
        this.combatEntities.values().remove(entity);
    }
    this.unload(entity);
    for (Consumer<IEntity> cons : this.entityRemovedConsumers) {
        cons.accept(entity);
    }
}
Also used : Emitter(de.gurkenlabs.litiengine.graphics.particles.Emitter) Creature(de.gurkenlabs.litiengine.entities.Creature) ICombatEntity(de.gurkenlabs.litiengine.entities.ICombatEntity) Prop(de.gurkenlabs.litiengine.entities.Prop) IEntity(de.gurkenlabs.litiengine.entities.IEntity) MapArea(de.gurkenlabs.litiengine.environment.tilemap.MapArea) Trigger(de.gurkenlabs.litiengine.entities.Trigger) StaticShadow(de.gurkenlabs.litiengine.graphics.StaticShadow) IMobileEntity(de.gurkenlabs.litiengine.entities.IMobileEntity) LightSource(de.gurkenlabs.litiengine.graphics.LightSource) Spawnpoint(de.gurkenlabs.litiengine.environment.tilemap.Spawnpoint) CollisionBox(de.gurkenlabs.litiengine.entities.CollisionBox)

Aggregations

IMobileEntity (de.gurkenlabs.litiengine.entities.IMobileEntity)8 CollisionBox (de.gurkenlabs.litiengine.entities.CollisionBox)3 LightSource (de.gurkenlabs.litiengine.graphics.LightSource)3 StaticShadow (de.gurkenlabs.litiengine.graphics.StaticShadow)3 IAnimationController (de.gurkenlabs.litiengine.graphics.animation.IAnimationController)3 Emitter (de.gurkenlabs.litiengine.graphics.particles.Emitter)3 Creature (de.gurkenlabs.litiengine.entities.Creature)2 ICombatEntity (de.gurkenlabs.litiengine.entities.ICombatEntity)2 IEntity (de.gurkenlabs.litiengine.entities.IEntity)2 Prop (de.gurkenlabs.litiengine.entities.Prop)2 Trigger (de.gurkenlabs.litiengine.entities.Trigger)2 MapArea (de.gurkenlabs.litiengine.environment.tilemap.MapArea)2 Spawnpoint (de.gurkenlabs.litiengine.environment.tilemap.Spawnpoint)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 IUpdateable (de.gurkenlabs.litiengine.IUpdateable)1 ICollisionEntity (de.gurkenlabs.litiengine.entities.ICollisionEntity)1 PathIterator (java.awt.geom.PathIterator)1 ArrayList (java.util.ArrayList)1