Skip to content

Commit

Permalink
(fastjengine#68) add light2d
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed May 7, 2022
1 parent 60dc5a1 commit b3d097f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main/java/tech/fastj/graphics/game/Light2D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tech.fastj.graphics.game;

import tech.fastj.math.Pointf;
import tech.fastj.graphics.Drawable;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.control.Scene;
import tech.fastj.systems.control.SimpleManager;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RadialGradientPaint;

public class Light2D extends GameObject {

private final AlphaComposite alphaComposite;
private final RadialGradientPaint gradient;

protected Light2D(Pointf location, Pointf size, RadialGradientPaint gradient, AlphaComposite alphaComposite) {
this.alphaComposite = alphaComposite;
this.gradient = gradient;
setCollisionPath(DrawUtil.createPath(DrawUtil.createBox(location, size)));
}

public static Light2DBuilder create(Pointf location, Pointf size) {
return new Light2DBuilder(location, size, Drawable.DefaultShouldRender);
}

public static Light2DBuilder create(Pointf location, Pointf size, boolean shouldRender) {
return new Light2DBuilder(location, size, shouldRender);
}

@Override
public void destroy(Scene origin) {
super.destroyTheRest(origin);
}

@Override
public void destroy(SimpleManager origin) {
super.destroyTheRest(origin);
}

@Override
public void render(Graphics2D g) {
Composite oldComposite = g.getComposite();
Paint oldPaint = g.getPaint();

g.setComposite(alphaComposite);
g.setPaint(gradient);
g.fill(getCollisionPath());

g.setComposite(oldComposite);
g.setPaint(oldPaint);
}
}
62 changes: 62 additions & 0 deletions src/main/java/tech/fastj/graphics/game/Light2DBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package tech.fastj.graphics.game;

import tech.fastj.math.Pointf;
import tech.fastj.graphics.gradients.Gradients;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.RadialGradientPaint;
import java.util.Objects;

/** A builder class for creating {@link Light2D} objects. */
public class Light2DBuilder {
private final boolean shouldRender;
private final Pointf location;
private final Pointf size;

private AlphaComposite alphaComposite = AlphaComposite.SrcOver;
private RadialGradientPaint gradientPaint;

Light2DBuilder(Pointf location, Pointf size, boolean shouldRender) {
this.location = location;
this.size = size;
this.shouldRender = shouldRender;
}

public Light2DBuilder withGradient(RadialGradientPaint gradientPaint) {
this.gradientPaint = Objects.requireNonNull(gradientPaint, "The radial gradient paint should not be null.");
return this;
}

public Light2DBuilder withGradient(Color centerColor, Color outerColor) {
Objects.requireNonNull(centerColor, "The center color should not be null.");
Objects.requireNonNull(outerColor, "The outer color should not be null.");
this.gradientPaint = Gradients.radialGradient(Pointf.add(location, size.copy().divide(2f)), evaluateRadius())
.withColors(centerColor, outerColor)
.build();
return this;
}

public Light2DBuilder withAlphaComposite(AlphaComposite alphaComposite) {
this.alphaComposite = Objects.requireNonNull(alphaComposite, "The alpha composite should not be null.");
return this;
}

/**
* Creates a new {@link Light2D} object, using the data provided by earlier method calls.
*
* @return The resulting {@code Light2D}.
*/
public Light2D build() {
return (Light2D) new Light2D(location, size, gradientPaint, alphaComposite)
.setShouldRender(shouldRender);
}

private float evaluateRadius() {
Pointf horizontalSide = Pointf.subtract(Pointf.add(location, size.x, 0f), location);
Pointf verticalSide = Pointf.subtract(Pointf.add(location, 0f, size.y), location);

Pointf smallestSide = horizontalSide.squareMagnitude() < verticalSide.squareMagnitude() ? horizontalSide : verticalSide;
return smallestSide.magnitude() / 2.0f;
}
}

0 comments on commit b3d097f

Please sign in to comment.