Search in sources :

Example 6 with ICombatEntity

use of de.gurkenlabs.litiengine.entities.ICombatEntity 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);
    }
}
Also used : Emitter(de.gurkenlabs.litiengine.graphics.particles.Emitter) ICombatEntity(de.gurkenlabs.litiengine.entities.ICombatEntity) Creature(de.gurkenlabs.litiengine.entities.Creature) 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)

Example 7 with ICombatEntity

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

the class DebugRenderer method renderEntityDebugInfo.

public static void renderEntityDebugInfo(final Graphics2D g, final IEntity entity) {
    if (!Game.getConfiguration().debug().isDebugEnabled()) {
        return;
    }
    if (Game.getConfiguration().debug().renderEntityNames()) {
        drawMapId(g, entity);
    }
    if (Game.getConfiguration().debug().renderHitBoxes() && entity instanceof ICombatEntity) {
        g.setColor(Color.RED);
        Game.getRenderEngine().renderOutline(g, ((ICombatEntity) entity).getHitBox());
    }
    if (Game.getConfiguration().debug().renderBoundingBoxes()) {
        g.setColor(Color.RED);
        Game.getRenderEngine().renderOutline(g, entity.getBoundingBox());
    }
    if (Game.getConfiguration().debug().renderCollisionBoxes() && entity instanceof ICollisionEntity) {
        final ICollisionEntity collisionEntity = (ICollisionEntity) entity;
        g.setColor(collisionEntity.hasCollision() ? Color.RED : Color.ORANGE);
        Game.getRenderEngine().renderOutline(g, collisionEntity.getCollisionBox());
    }
}
Also used : ICombatEntity(de.gurkenlabs.litiengine.entities.ICombatEntity) ICollisionEntity(de.gurkenlabs.litiengine.entities.ICollisionEntity)

Example 8 with ICombatEntity

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

the class LightSource method renderShadows.

/**
 * Renders the shadows using simple vector math. The steps are as follows:
 *
 * <pre>
 * for each entity
 *     if entity is not moving:
 *         ignore entity
 *     if entity is too far from mouse:
 *         ignore entity
 *
 *     determine unit vector from mouse to entity center
 *     get perpendicular of unit vector
 *
 *     Create Points A + B:
 *         extrude perpendicular in either direction, by the half-size of the entity
 *     Create Points C + D:
 *         extrude A + B away from mouse position
 *
 *     construct polygon with points A, B, C, D
 *
 *     render with RadialGradientPaint to give it a "fade-out" appearance
 * </pre>
 *
 * @param g
 *          the graphics to use for rendering
 * @param center
 *          the center
 */
private void renderShadows(final Graphics2D g) {
    if (!Game.getEnvironment().getCombatEntities().stream().anyMatch(isInRange(this.getCenter(), SHADOW_GRADIENT_SIZE))) {
        return;
    }
    // we'll use a radial gradient
    final Paint gradientPaint = new RadialGradientPaint(Game.getCamera().getViewPortDimensionCenter(this), SHADOW_GRADIENT_SIZE, SHADOW_GRADIENT_FRACTIONS, SHADOW_GRADIENT_COLORS);
    // old Paint object for resetting it later
    final Paint oldPaint = g.getPaint();
    g.setPaint(gradientPaint);
    // for each entity
    for (final ICombatEntity mob : Game.getEnvironment().getCombatEntities()) {
        if (mob.isDead() || !isInRange(this.getCenter(), SHADOW_GRADIENT_SIZE).test(mob)) {
            continue;
        }
        final Shape obstructedVision = this.getObstructedVisionArea(mob, Game.getCamera().getViewPortDimensionCenter(this));
        // fill the polygon with the gradient paint
        g.fill(obstructedVision);
    }
    // reset to old Paint object
    g.setPaint(oldPaint);
}
Also used : ICombatEntity(de.gurkenlabs.litiengine.entities.ICombatEntity) Shape(java.awt.Shape) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint) RadialGradientPaint(java.awt.RadialGradientPaint)

Example 9 with ICombatEntity

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

the class EnvironmentEventTests method testOnAdded.

@Test
public void testOnAdded() {
    ICombatEntity combatEntity = mock(ICombatEntity.class);
    when(combatEntity.getMapId()).thenReturn(123);
    when(combatEntity.getRenderType()).thenReturn(RenderType.NORMAL);
    Consumer<IEntity> addedConsumer = (Consumer<IEntity>) mock(Consumer.class);
    this.testEnvironment.onEntityAdded(addedConsumer);
    this.testEnvironment.add(combatEntity);
    verify(addedConsumer, times(1)).accept(combatEntity);
}
Also used : ICombatEntity(de.gurkenlabs.litiengine.entities.ICombatEntity) Consumer(java.util.function.Consumer) IEntity(de.gurkenlabs.litiengine.entities.IEntity) Test(org.junit.jupiter.api.Test)

Example 10 with ICombatEntity

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

the class EnvironmentTests method testGetByName.

@Test
public void testGetByName() {
    ICombatEntity combatEntity = mock(ICombatEntity.class);
    when(combatEntity.getMapId()).thenReturn(1);
    when(combatEntity.getName()).thenReturn("test");
    when(combatEntity.getRenderType()).thenReturn(RenderType.NORMAL);
    ICombatEntity combatEntity2 = mock(ICombatEntity.class);
    when(combatEntity2.getRenderType()).thenReturn(RenderType.NORMAL);
    this.testEnvironment.add(combatEntity);
    this.testEnvironment.add(combatEntity2);
    assertNotNull(this.testEnvironment.get("test"));
    assertNull(this.testEnvironment.get(""));
    assertNull(this.testEnvironment.get(null));
}
Also used : ICombatEntity(de.gurkenlabs.litiengine.entities.ICombatEntity) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ICombatEntity (de.gurkenlabs.litiengine.entities.ICombatEntity)15 Test (org.junit.jupiter.api.Test)8 IEntity (de.gurkenlabs.litiengine.entities.IEntity)4 Rectangle2D (java.awt.geom.Rectangle2D)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 CollisionBox (de.gurkenlabs.litiengine.entities.CollisionBox)2 Creature (de.gurkenlabs.litiengine.entities.Creature)2 IMobileEntity (de.gurkenlabs.litiengine.entities.IMobileEntity)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 LightSource (de.gurkenlabs.litiengine.graphics.LightSource)2 StaticShadow (de.gurkenlabs.litiengine.graphics.StaticShadow)2 Emitter (de.gurkenlabs.litiengine.graphics.particles.Emitter)2 Consumer (java.util.function.Consumer)2 ICollisionEntity (de.gurkenlabs.litiengine.entities.ICollisionEntity)1 Paint (java.awt.Paint)1 RadialGradientPaint (java.awt.RadialGradientPaint)1 Shape (java.awt.Shape)1