use of de.gurkenlabs.litiengine.entities.StaticShadow in project litiengine by gurkenlabs.
the class EnvironmentTests method testStaticShadow.
@Test
void testStaticShadow() {
StaticShadow testShadow = new StaticShadow(0, 0, 1, 1, StaticShadowType.NONE);
testShadow.setMapId(1);
testShadow.setName("test");
this.testEnvironment.add(testShadow);
assertNotNull(this.testEnvironment.getStaticShadow(1));
assertNotNull(this.testEnvironment.getStaticShadow("test"));
assertEquals(1, this.testEnvironment.getEntities(StaticShadow.class).size());
assertEquals(1, this.testEnvironment.getEntities().size());
this.testEnvironment.remove(testShadow);
assertNull(this.testEnvironment.getStaticShadow(1));
assertNull(this.testEnvironment.getStaticShadow("test"));
assertEquals(0, this.testEnvironment.getEntities(StaticShadow.class).size());
assertEquals(0, this.testEnvironment.getEntities().size());
}
use of de.gurkenlabs.litiengine.entities.StaticShadow in project litiengine by gurkenlabs.
the class AmbientLight method renderLightSource.
private void renderLightSource(final Graphics2D g, final LightSource light, Rectangle2D section) {
final double mapWidth = this.getEnvironment().getMap().getSizeInPixels().width;
final double mapHeight = this.getEnvironment().getMap().getSizeInPixels().height;
double longerDimension = mapWidth < mapHeight ? mapHeight : mapWidth;
final Point2D lightCenter = light.getCenter();
final Point2D lightFocus = new Point2D.Double(lightCenter.getX() + light.getBoundingBox().getWidth() * light.getFocusOffsetX(), lightCenter.getY() + light.getBoundingBox().getHeight() * light.getFocusOffsetY());
Shape fillShape;
Area lightArea = null;
if (light.getLightShapeType() == LightSource.Type.RECTANGLE) {
g.setColor(new Color(light.getColor().getRed(), light.getColor().getGreen(), light.getColor().getBlue(), light.getColor().getAlpha()));
fillShape = new Rectangle2D.Double(light.getBoundingBox().getX() - section.getX(), light.getBoundingBox().getY() - section.getY(), light.getBoundingBox().getWidth(), light.getBoundingBox().getHeight());
g.fill(fillShape);
return;
}
// into and out of rooms)
for (final StaticShadow col : this.getEnvironment().getStaticShadows()) {
if (!light.getBoundingBox().intersects(col.getBoundingBox())) {
continue;
}
if (lightArea == null) {
lightArea = new Area(light.getLightShape());
}
if (!lightArea.intersects(col.getBoundingBox())) {
continue;
}
final Area boxInLight = new Area(col.getBoundingBox());
final Line2D[] bounds = GeometricUtilities.getLines(col.getBoundingBox());
for (final Line2D line : bounds) {
final Vector2D lineVector = new Vector2D(line.getP1(), line.getP2());
final Vector2D lightVector = new Vector2D(lightFocus, line.getP1());
if (light.getCenter().getY() < line.getY1() && light.getCenter().getY() < line.getY2() && col.getBoundingBox().contains(light.getCenter()) || lineVector.normalVector().dotProduct(lightVector) >= 0) {
continue;
}
final Path2D shadowParallelogram = new Path2D.Double();
final Point2D shadowPoint1 = GeometricUtilities.project(lightFocus, line.getP1(), longerDimension);
final Point2D shadowPoint2 = GeometricUtilities.project(lightFocus, line.getP2(), longerDimension);
// construct a shape from our points
shadowParallelogram.moveTo(line.getP1().getX(), line.getP1().getY());
shadowParallelogram.lineTo(shadowPoint1.getX(), shadowPoint1.getY());
shadowParallelogram.lineTo(shadowPoint2.getX(), shadowPoint2.getY());
shadowParallelogram.lineTo(line.getP2().getX(), line.getP2().getY());
shadowParallelogram.closePath();
final Area shadowArea = new Area(shadowParallelogram);
if (light.getCenter().getY() < col.getBoundingBox().getMaxY() && !col.getBoundingBox().contains(light.getCenter())) {
shadowArea.add(boxInLight);
}
shadowArea.intersect(lightArea);
lightArea.subtract(shadowArea);
}
}
final Paint oldPaint = g.getPaint();
// render parts that lie within the shadow with a gradient from the light
// color to transparent
final Shape lightShape = light.getLightShape();
final double radius = lightShape.getBounds2D().getWidth() > lightShape.getBounds2D().getHeight() ? lightShape.getBounds2D().getWidth() : lightShape.getBounds2D().getHeight();
final Color[] transColors = new Color[] { light.getColor(), new Color(light.getColor().getRed(), light.getColor().getGreen(), light.getColor().getBlue(), 0) };
final Point2D center = new Point2D.Double(lightShape.getBounds2D().getCenterX() - section.getX(), lightShape.getBounds2D().getCenterY() - section.getY());
final Point2D focus = new Point2D.Double(center.getX() + lightShape.getBounds2D().getWidth() * light.getFocusOffsetX(), center.getY() + lightShape.getBounds2D().getHeight() * light.getFocusOffsetY());
RadialGradientPaint paint = new RadialGradientPaint(center, (float) (radius / 2d), focus, new float[] { 0.0f, 1.00f }, transColors, CycleMethod.NO_CYCLE);
g.setPaint(paint);
if (lightArea != null) {
lightArea.transform(AffineTransform.getTranslateInstance(-section.getX(), -section.getY()));
fillShape = lightArea;
} else {
fillShape = new Rectangle2D.Double(light.getBoundingBox().getX() - section.getX(), light.getBoundingBox().getY() - section.getY(), light.getBoundingBox().getWidth(), light.getBoundingBox().getHeight());
}
g.fill(fillShape);
g.setPaint(oldPaint);
}
use of de.gurkenlabs.litiengine.entities.StaticShadow 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 ICollisionEntity) {
Game.physics().remove((ICollisionEntity) entity);
}
// 2. unregister from update
if (entity instanceof IUpdateable) {
Game.loop().detach((IUpdateable) entity);
}
if (entity instanceof IMobileEntity) {
this.removeGravity((IMobileEntity) entity);
}
// 3. detach all controllers
entity.detachControllers();
if (entity instanceof Emitter) {
Emitter em = (Emitter) entity;
em.deactivate();
}
if (this.loaded && (entity instanceof LightSource || entity instanceof StaticShadow)) {
this.updateLighting(entity);
}
entity.removed(this);
}
use of de.gurkenlabs.litiengine.entities.StaticShadow in project litiengine by gurkenlabs.
the class Environment method updateLighting.
private void updateLighting(IEntity entity) {
if (entity instanceof StaticShadow) {
StaticShadow shadow = (StaticShadow) entity;
this.updateLighting(shadow.getArea() != null ? shadow.getArea().getBounds2D() : shadow.getBoundingBox());
return;
}
this.updateLighting(entity.getBoundingBox());
}
use of de.gurkenlabs.litiengine.entities.StaticShadow in project litiengine by gurkenlabs.
the class CollisionBoxMapObjectLoader method load.
@Override
public Collection<IEntity> load(Environment environment, IMapObject mapObject) {
Collection<IEntity> entities = new ArrayList<>();
if (!this.isMatchingType(mapObject)) {
return entities;
}
final CollisionBox col = this.createCollisionBox(mapObject);
loadDefaultProperties(col, mapObject);
col.setCollisionBoxWidth(col.getWidth());
col.setCollisionBoxHeight(col.getHeight());
entities.add(col);
if (col.isObstructingLight()) {
entities.add(new StaticShadow(col));
}
return entities;
}
Aggregations