use of de.gurkenlabs.litiengine.entities.EntityRenderEvent in project litiengine by gurkenlabs.
the class RenderEngine method renderEntity.
/**
* Renders the specified entity at its current location in the environment.
*
* <p>
* This method uses the {@code IEntityAnimationController} to render the appropriate {@code
* Animation}.<br>
* If the entity implements the {@code IRenderable} interface, its render method is being called afterwards.
*
* <p>
* To listen to events about this process, you can add a {@code EntityRenderListener} or {@code
* EntityRenderedListener} to the render engine.
*
* @param g
* The graphics object to render on.
* @param entity
* The entity to be rendered.
* @see IEntity#animations()
* @see IAnimationController#getCurrentImage()
* @see IRenderable#render(Graphics2D)
* @see #canRender(IEntity)
* @see EntityRenderListener#canRender(IEntity)
* @see EntityRenderedListener#rendered(EntityRenderEvent)
*/
public void renderEntity(final Graphics2D g, final IEntity entity) {
if (entity == null) {
return;
}
if (!this.canRender(entity)) {
return;
}
final EntityRenderEvent renderEvent = new EntityRenderEvent(g, entity);
if (entity instanceof EntityRenderListener) {
((EntityRenderListener) entity).rendering(renderEvent);
}
for (final EntityRenderListener listener : this.entityRenderListener) {
listener.rendering(renderEvent);
}
final IEntityAnimationController<?> animationController = entity.animations();
if (animationController != null) {
final BufferedImage img = animationController.getCurrentImage();
if (img != null) {
if (animationController.isAutoScaling()) {
final double ratioX = entity.getWidth() / img.getWidth();
final double ratioY = entity.getHeight() / img.getHeight();
ImageRenderer.renderScaled(g, img, Game.world().camera().getViewportLocation(entity.getLocation()), ratioX, ratioY);
} else {
// center the image relative to the entity dimensions -> the pivot point for rendering is
// the center of the entity
double deltaX = (entity.getWidth() - img.getWidth()) / 2.0;
double deltaY = (entity.getHeight() - img.getHeight()) / 2.0;
final AffineTransform transform = animationController.getAffineTransform();
if (transform != null) {
// center the scaled image relative to the desired render location if the transform
// provides a scaling element
deltaX += (img.getWidth() - (img.getWidth() * transform.getScaleX())) / 2.0;
deltaY += (img.getHeight() - (img.getHeight() * transform.getScaleY())) / 2.0;
}
Point2D renderLocation = Game.world().camera().getViewportLocation(entity.getX() + deltaX, entity.getY() + deltaY);
ImageRenderer.renderTransformed(g, img, renderLocation.getX(), renderLocation.getY(), transform);
if (Game.config().debug().renderBoundingBoxes()) {
g.setColor(new Color(255, 0, 0, 50));
renderOutline(g, new Rectangle2D.Double(entity.getX(), entity.getY(), img.getWidth(), img.getWidth()));
ShapeRenderer.renderOutlineTransformed(g, new Rectangle2D.Double(renderLocation.getX(), renderLocation.getY(), img.getWidth(), img.getWidth()), animationController.getAffineTransform(), 0.25f);
}
}
}
}
if (entity instanceof IRenderable) {
((IRenderable) entity).render(g);
}
if (entity instanceof EntityRenderListener) {
((EntityRenderListener) entity).rendered(renderEvent);
}
for (final EntityRenderListener listener : this.entityRenderListener) {
listener.rendered(renderEvent);
}
for (final EntityRenderedListener listener : this.entityRenderedListener) {
listener.rendered(renderEvent);
}
}
use of de.gurkenlabs.litiengine.entities.EntityRenderEvent in project litiengine by gurkenlabs.
the class DebugRenderer method renderEntityDebugInfo.
public static void renderEntityDebugInfo(final Graphics2D g, final IEntity entity) {
if (!Game.config().debug().isDebugEnabled()) {
return;
}
if (Game.config().debug().renderEntityNames()) {
drawMapId(g, entity);
}
if (Game.config().debug().renderHitBoxes() && entity instanceof ICombatEntity) {
g.setColor(Color.RED);
Game.graphics().renderOutline(g, ((ICombatEntity) entity).getHitBox());
}
if (Game.config().debug().renderBoundingBoxes()) {
g.setColor(Color.RED);
Game.graphics().renderOutline(g, entity.getBoundingBox());
if (entity instanceof SoundSource) {
final int range = ((SoundSource) entity).getRange();
final float[] dash1 = { 10f };
final BasicStroke dashed = new BasicStroke(.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
Game.graphics().renderOutline(g, new Ellipse2D.Double(entity.getBoundingBox().getCenterX() - range, entity.getBoundingBox().getCenterY() - range, range * 2d, range * 2d), dashed);
}
}
if (Game.config().debug().renderCollisionBoxes() && entity instanceof ICollisionEntity) {
final ICollisionEntity collisionEntity = (ICollisionEntity) entity;
g.setColor(collisionEntity.hasCollision() ? Color.RED : Color.ORANGE);
Game.graphics().renderOutline(g, collisionEntity.getCollisionBox());
}
final EntityRenderEvent event = new EntityRenderEvent(g, entity);
for (EntityRenderedListener listener : entityDebugListeners) {
listener.rendered(event);
}
}
Aggregations