Search in sources :

Example 21 with IEntity

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

the class MapObjectLoaderTests method testPropMapObjectLoader.

@Test
public void testPropMapObjectLoader() {
    PropMapObjectLoader loader = new PropMapObjectLoader();
    IEnvironment environment = mock(IEnvironment.class);
    IMapObject mapObject = mock(IMapObject.class);
    when(mapObject.getType()).thenReturn(MapObjectType.PROP.name());
    when(mapObject.getId()).thenReturn(111);
    when(mapObject.getName()).thenReturn("testProp");
    when(mapObject.getLocation()).thenReturn(new Point(100, 100));
    when(mapObject.getDimension()).thenReturn(new Dimension(200, 200));
    when(mapObject.getCustomProperty(MapObjectProperty.PROP_MATERIAL)).thenReturn(Material.PLASTIC.name());
    when(mapObject.getCustomPropertyBool(MapObjectProperty.PROP_INDESTRUCTIBLE)).thenReturn(true);
    when(mapObject.getCustomPropertyBool(MapObjectProperty.COLLISION)).thenReturn(true);
    when(mapObject.getCustomPropertyInt(MapObjectProperty.HEALTH)).thenReturn(100);
    when(mapObject.getCustomPropertyFloat(MapObjectProperty.COLLISIONBOX_WIDTH)).thenReturn(100.0f);
    when(mapObject.getCustomPropertyFloat(MapObjectProperty.COLLISIONBOX_HEIGHT)).thenReturn(100.0f);
    when(mapObject.getCustomProperty(MapObjectProperty.COLLISION_ALGIN)).thenReturn("LEFT");
    when(mapObject.getCustomProperty(MapObjectProperty.COLLISION_VALGIN)).thenReturn("MIDDLE");
    when(mapObject.getCustomPropertyInt(MapObjectProperty.TEAM)).thenReturn(1);
    Collection<IEntity> entities = loader.load(environment, mapObject);
    Optional<IEntity> opt = entities.stream().findFirst();
    assertTrue(opt.isPresent());
    IEntity entity = entities.stream().findFirst().get();
    assertNotNull(entity);
    assertEquals(entity.getMapId(), 111);
    assertEquals(entity.getName(), "testProp");
    assertEquals(entity.getLocation().getX(), 100, 0.0001);
    assertEquals(entity.getLocation().getY(), 100, 0.0001);
    Prop prop = (Prop) entity;
    assertEquals(prop.getMaterial(), Material.PLASTIC);
    assertTrue(prop.isIndestructible());
    assertTrue(prop.hasCollision());
    assertEquals(prop.getAttributes().getHealth().getMaxValue().intValue(), 100);
    assertEquals(prop.getAttributes().getHealth().getCurrentValue().intValue(), 100);
    assertEquals(prop.getCollisionBoxWidth(), 100.0, 0.0001);
    assertEquals(prop.getCollisionBoxHeight(), 100.0, 0.0001);
    assertEquals(prop.getCollisionBoxAlign(), Align.LEFT);
    assertEquals(prop.getCollisionBoxValign(), Valign.MIDDLE);
    assertEquals(prop.getTeam(), 1);
}
Also used : IEntity(de.gurkenlabs.litiengine.entities.IEntity) Prop(de.gurkenlabs.litiengine.entities.Prop) IMapObject(de.gurkenlabs.litiengine.environment.tilemap.IMapObject) Point(java.awt.Point) Dimension(java.awt.Dimension) Test(org.junit.jupiter.api.Test)

Example 22 with IEntity

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

the class MapObjectLoaderTests method testColliderMapObjectLoader.

@Test
public void testColliderMapObjectLoader() {
    CollisionBoxMapObjectLoader loader = new CollisionBoxMapObjectLoader();
    IEnvironment environment = mock(IEnvironment.class);
    IMapObject mapObject = mock(IMapObject.class);
    when(mapObject.getType()).thenReturn(MapObjectType.COLLISIONBOX.name());
    when(mapObject.getId()).thenReturn(111);
    when(mapObject.getName()).thenReturn("testCollider");
    when(mapObject.getLocation()).thenReturn(new Point(100, 100));
    when(mapObject.getDimension()).thenReturn(new Dimension(200, 200));
    when(mapObject.getWidth()).thenReturn(200);
    when(mapObject.getHeight()).thenReturn(200);
    Collection<IEntity> entities = loader.load(environment, mapObject);
    Optional<IEntity> opt = entities.stream().findFirst();
    assertTrue(opt.isPresent());
    IEntity entity = entities.stream().findFirst().get();
    assertNotNull(entity);
    assertEquals(entity.getMapId(), 111);
    assertEquals(entity.getName(), "testCollider");
    assertEquals(entity.getLocation().getX(), 100, 0.0001);
    assertEquals(entity.getLocation().getY(), 100, 0.0001);
    CollisionBox collider = (CollisionBox) entity;
    assertEquals(collider.getCollisionBoxWidth(), 200.0, 0.0001);
    assertEquals(collider.getCollisionBoxHeight(), 200.0, 0.0001);
}
Also used : IEntity(de.gurkenlabs.litiengine.entities.IEntity) IMapObject(de.gurkenlabs.litiengine.environment.tilemap.IMapObject) Point(java.awt.Point) Dimension(java.awt.Dimension) CollisionBox(de.gurkenlabs.litiengine.entities.CollisionBox) Test(org.junit.jupiter.api.Test)

Example 23 with IEntity

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

the class TriggerTests method testInteractTrigger.

@Test
public void testInteractTrigger() {
    Trigger trigger = new Trigger(TriggerActivation.INTERACT, "testrigger", "testmessage");
    IEntity entity = mock(IEntity.class);
    when(entity.getMapId()).thenReturn(123);
    IEntity target = mock(IEntity.class);
    when(target.getMapId()).thenReturn(456);
    when(target.sendMessage(any(Object.class), any(String.class))).thenReturn("answer");
    trigger.addTarget(456);
    IEnvironment env = mock(IEnvironment.class);
    Game.loadEnvironment(env);
    Game.init(Game.COMMADLINE_ARG_NOGUI);
    when(env.get(456)).thenReturn(target);
    assertFalse(trigger.isActivated());
    trigger.sendMessage(entity, Trigger.INTERACT_MESSAGE);
    assertTrue(trigger.isActivated());
    verify(target, times(1)).sendMessage(trigger, trigger.getMessage());
}
Also used : Trigger(de.gurkenlabs.litiengine.entities.Trigger) IEntity(de.gurkenlabs.litiengine.entities.IEntity) Test(org.junit.jupiter.api.Test)

Example 24 with IEntity

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

the class EmitterMapObjectLoader method load.

@Override
public Collection<IEntity> load(IEnvironment environment, IMapObject mapObject) {
    if (MapObjectType.get(mapObject.getType()) != MapObjectType.EMITTER) {
        throw new IllegalArgumentException("Cannot load a mapobject of the type " + mapObject.getType() + " with a loader of the type " + EmitterMapObjectLoader.class);
    }
    EmitterData data = createEmitterData(mapObject);
    CustomEmitter emitter = new CustomEmitter(data);
    loadDefaultProperties(emitter, mapObject);
    Collection<IEntity> entities = super.load(environment, mapObject);
    entities.add(emitter);
    return entities;
}
Also used : IEntity(de.gurkenlabs.litiengine.entities.IEntity) CustomEmitter(de.gurkenlabs.litiengine.graphics.particles.xml.CustomEmitter) EmitterData(de.gurkenlabs.litiengine.graphics.particles.xml.EmitterData)

Example 25 with IEntity

use of de.gurkenlabs.litiengine.entities.IEntity 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

IEntity (de.gurkenlabs.litiengine.entities.IEntity)26 Test (org.junit.jupiter.api.Test)9 IMapObject (de.gurkenlabs.litiengine.environment.tilemap.IMapObject)6 Trigger (de.gurkenlabs.litiengine.entities.Trigger)5 Dimension (java.awt.Dimension)5 Point (java.awt.Point)5 CollisionBox (de.gurkenlabs.litiengine.entities.CollisionBox)4 ICombatEntity (de.gurkenlabs.litiengine.entities.ICombatEntity)4 Prop (de.gurkenlabs.litiengine.entities.Prop)4 MapArea (de.gurkenlabs.litiengine.environment.tilemap.MapArea)4 LightSource (de.gurkenlabs.litiengine.graphics.LightSource)4 StaticShadow (de.gurkenlabs.litiengine.graphics.StaticShadow)4 Creature (de.gurkenlabs.litiengine.entities.Creature)3 Spawnpoint (de.gurkenlabs.litiengine.environment.tilemap.Spawnpoint)3 Direction (de.gurkenlabs.litiengine.entities.Direction)2 IMobileEntity (de.gurkenlabs.litiengine.entities.IMobileEntity)2 Emitter (de.gurkenlabs.litiengine.graphics.particles.Emitter)2 Rectangle2D (java.awt.geom.Rectangle2D)2 Consumer (java.util.function.Consumer)2 AttributeModifier (de.gurkenlabs.litiengine.attributes.AttributeModifier)1