Skip to content

Commit

Permalink
Merge pull request #24 from fastjengine/drawable-improvement
Browse files Browse the repository at this point in the history
Drawable Improvement 1 - Paint Support in Polygon2D
  • Loading branch information
lucasstarsz authored Jun 2, 2021
2 parents ba31f8e + 288968e commit 543a07b
Show file tree
Hide file tree
Showing 26 changed files with 945 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import tech.fastj.math.Maths;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Display;
import tech.fastj.graphics.DrawUtil;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.game.Model2D;
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.game.Text2D;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.util.PsdfUtil;

import tech.fastj.systems.control.Scene;

Expand Down Expand Up @@ -125,7 +126,7 @@ private Polygon2D createPlayerHealthBar() {
}

private Model2D createPlayer() {
return new Model2D(DrawUtil.load2DModel(FilePaths.PathToResources + "player.psdf"));
return new Model2D(PsdfUtil.loadPsdf(FilePaths.PathToResources + "player.psdf"));
}

private void newWave() {
Expand All @@ -150,7 +151,7 @@ private Model2D createEnemy() {
Maths.randomAtEdge(-500f, 1220f)
);

return (Model2D) new Model2D(DrawUtil.load2DModel(FilePaths.PathToResources + "enemy.psdf"))
return (Model2D) new Model2D(PsdfUtil.loadPsdf(FilePaths.PathToResources + "enemy.psdf"))
.setTranslation(randomPosition)
.addBehavior(new EnemyMovement(this), this)
.addAsGameObject(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.DrawUtil;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.Drawable;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.game.Polygon2D;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.DrawUtil;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.game.Text2D;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Display;
import tech.fastj.graphics.DrawUtil;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.ui.UIElement;
Expand Down Expand Up @@ -63,7 +63,7 @@ public void load(Display display) {

/* With all that out of the way, we can create our player! */
player = new Polygon2D(boxPoints) // the Polygon2D class is for game objects that are polygons, so it makes sense to use for our square player.
.setColor(Color.RED) // Just to show it off, we'll set the player's color to red.
.setPaint(Color.red) // Just to show it off, we'll set the player's color to red.
.addBehavior(playerScript, this) // Then, we'll add our two behaviors -- the player script...
.addBehavior(rotationScript, this) // ...and the rotation script. The second param is the scene itself -- read the docs for more info.
.addAsGameObject(this); // and lastly, we need to add the game object to the scene.
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* <p>
* This game library is split into two main parts:
* <ul>
* <li>the framework with which to build games (every package except {@link io.github.lucasstarsz.fastj.engine}), and</li>
* <li>the engine running it (just package {@code engine}).</li>
* <li>the framework with which to build games (every package except {@link tech.fastj.engine}), and</li>
* <li>the engine running it (just package {@link tech.fastj.engine}).</li>
* </ul>
* <p>
* For more information, check out
* <a href="https://github.com/lucasstarsz/FastJ-Engine" target="_blank">the github repository.</a>
* <a href="https://github.com/fastjengine/FastJ" target="_blank">the github repository.</a>
*/
module fastj.library {
requires transitive java.desktop;
Expand All @@ -23,6 +23,7 @@
exports tech.fastj.graphics.game;
exports tech.fastj.graphics.ui;
exports tech.fastj.graphics.ui.elements;
exports tech.fastj.graphics.util;

exports tech.fastj.systems.input;
exports tech.fastj.systems.input.keyboard;
Expand All @@ -31,4 +32,5 @@
exports tech.fastj.systems.behaviors;
exports tech.fastj.systems.control;
exports tech.fastj.systems.tags;
exports tech.fastj.graphics.util.gradients;
}
4 changes: 2 additions & 2 deletions src/main/java/tech/fastj/engine/FastJEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tech.fastj.engine.internals.Timer;
import tech.fastj.math.Point;
import tech.fastj.graphics.Display;
import tech.fastj.graphics.DisplayUtil;
import tech.fastj.graphics.util.DisplayUtil;

import tech.fastj.systems.behaviors.BehaviorManager;
import tech.fastj.systems.control.LogicManager;
Expand All @@ -25,7 +25,7 @@
* This class contains the methods needed to initialize and run a game using the FastJ Game Engine. With this, you'll
* have access to the engine's features in their full force.
* <p>
* <a href="https://github.com/lucasstarsz/FastJ-Engine">The FastJ Game Engine</a>
* <a href="https://github.com/fastjengine/FastJ">The FastJ Game Engine</a>
*
* @author Andrew Dey
* @version 1.0.0
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tech/fastj/graphics/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import tech.fastj.math.Pointf;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.ui.UIElement;
import tech.fastj.graphics.util.DisplayUtil;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.input.keyboard.Keyboard;
import tech.fastj.systems.input.mouse.Mouse;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/tech/fastj/graphics/Drawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tech.fastj.math.Pointf;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.ui.UIElement;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.control.Scene;
import tech.fastj.systems.tags.TaggableEntity;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/tech/fastj/graphics/game/Model2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import tech.fastj.math.Maths;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Boundary;
import tech.fastj.graphics.DrawUtil;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.util.PsdfUtil;

import tech.fastj.systems.control.Scene;

Expand All @@ -14,7 +15,7 @@
/**
* {@code Drawable} subclass for grouping an array of {@code Polygon2D}s under a single object.
* <p>
* This class is compatible with loading from a .PSDF file, using the {@code DrawUtil.load2DModel()} method.
* This class is compatible with loading from a .PSDF file, using the {@link PsdfUtil#loadPsdf(String)} method.
*
* @author Andrew Dey
* @version 1.0.0
Expand Down
73 changes: 39 additions & 34 deletions src/main/java/tech/fastj/graphics/game/Polygon2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import tech.fastj.math.Maths;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Boundary;
import tech.fastj.graphics.DrawUtil;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.control.Scene;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
Expand All @@ -23,8 +24,8 @@
*/
public class Polygon2D extends GameObject {

/** {@link Color} representing the default color value of {@code (0, 0, 0)}. */
public static final Color DefaultColor = Color.black;
/** {@link Paint} representing the default paint value as the color black: {@code (0, 0, 0)}. */
public static final Paint DefaultPaint = Color.black;
/** {@code boolean} representing the default "should fill" value of {@code true}. */
public static final boolean DefaultFill = true;
/** {@code boolean} representing the default "should render" value of {@code true}. */
Expand All @@ -33,8 +34,8 @@ public class Polygon2D extends GameObject {
private Path2D.Float renderPath;
private Pointf[] points;

private Color color;
private boolean paintFilled;
private Paint paint;
private boolean shouldFill;

private float rotation;
private Pointf scale;
Expand All @@ -44,24 +45,24 @@ public class Polygon2D extends GameObject {
/**
* {@code Polygon2D} constructor that takes in a set of points.
* <p>
* This constructor defaults the color to {@link #DefaultColor}, the fill to {@link #DefaultFill}, and sets the
* This constructor defaults the paint to {@link #DefaultPaint}, the fill to {@link #DefaultFill}, and sets the
* {@code show} boolean to {@link #DefaultShow}.
*
* @param pts {@code Pointf} array that defines the points for the polygon.
*/
public Polygon2D(Pointf[] pts) {
this(pts, DefaultColor, DefaultFill, DefaultShow);
this(pts, DefaultPaint, DefaultFill, DefaultShow);
}

/**
* {@code Polygon2D} constructor that takes in a set of points, a color, a fill variable, and a show variable.
* {@code Polygon2D} constructor that takes in a set of points, a paint, a fill variable, and a show variable.
*
* @param pts {@code Pointf} array that defines the points for the polygon.
* @param color {@code Color} variable that sets the color of the polygon.
* @param paint {@code Paint} variable that sets the paint of the polygon.
* @param fill Boolean that determines whether the polygon should be filled, or only outlined.
* @param show Boolean that determines whether the polygon should be shown on screen.
*/
public Polygon2D(Pointf[] pts, Color color, boolean fill, boolean show) {
public Polygon2D(Pointf[] pts, Paint paint, boolean fill, boolean show) {
super();
points = pts;

Expand All @@ -72,26 +73,26 @@ public Polygon2D(Pointf[] pts, Color color, boolean fill, boolean show) {
scale = GameObject.DefaultScale.copy();
translation = new Pointf(getBound(Boundary.TopLeft));

setColor(color);
setPaint(paint);
setFilled(fill);

setCollisionPath(renderPath);
setShouldRender(show);
}

/**
* {@code Polygon2D} constructor that takes in a set of points, a color, fill variable, a show variable, and the
* {@code Polygon2D} constructor that takes in a set of points, a paint variable, fill variable, a show variable, and the
* translation, rotation, and scale of the polygon.
*
* @param pts {@code Pointf} array that defines the points for the polygon.
* @param setLocation {@code Pointf} to set the initial location of the polygon.
* @param setRotation {@code Pointf} to set the initial rotation of the polygon.
* @param setScale {@code Pointf} to set the initial scale of the polygon.
* @param color {@code Color} variable that sets the color of the polygon.
* @param paint {@code Paint} variable that sets the paint of the polygon.
* @param fill Boolean that determines whether the polygon should be filled, or only outlined.
* @param show Boolean that determines whether the polygon should be shown on screen.
*/
public Polygon2D(Pointf[] pts, Pointf setLocation, float setRotation, Pointf setScale, Color color, boolean fill, boolean show) {
public Polygon2D(Pointf[] pts, Pointf setLocation, float setRotation, Pointf setScale, Paint paint, boolean fill, boolean show) {
super();
points = pts;

Expand All @@ -106,7 +107,7 @@ public Polygon2D(Pointf[] pts, Pointf setLocation, float setRotation, Pointf set
setRotation(setRotation);
setScale(setScale);

setColor(color);
setPaint(paint);
setFilled(fill);

setCollisionPath(renderPath);
Expand All @@ -132,22 +133,22 @@ public Pointf[] getOriginalPoints() {
}

/**
* Gets the color set for this polygon.
* Gets the paint for this polygon.
*
* @return The {@code Color} set for this polygon.
* @return The {@code Paint} set for this polygon.
*/
public Color getColor() {
return color;
public Paint getPaint() {
return paint;
}

/**
* Sets the color for the polygon.
* Sets the paint for this polygon.
*
* @param newColor The {@code Color} to be used for the polygon.
* @param newPaint The {@code Paint} to be used for the polygon.
* @return This instance of the {@code Polygon2D}, for method chaining.
*/
public Polygon2D setColor(Color newColor) {
color = newColor;
public Polygon2D setPaint(Paint newPaint) {
paint = newPaint;
return this;
}

Expand All @@ -158,7 +159,7 @@ public Polygon2D setColor(Color newColor) {
* outlined.
*/
public boolean isFilled() {
return paintFilled;
return shouldFill;
}

/**
Expand All @@ -168,7 +169,7 @@ public boolean isFilled() {
* @return This instance of the {@code Polygon2D}, for method chaining.
*/
public Polygon2D setFilled(boolean fill) {
paintFilled = fill;
shouldFill = fill;
return this;
}

Expand Down Expand Up @@ -271,22 +272,26 @@ public void scale(Pointf scaleMod, Pointf centerpoint) {
public void render(Graphics2D g) {
if (!shouldRender()) return;

g.setColor(color);
Paint oldPaint = g.getPaint();

if (paintFilled) {
g.setPaint(paint);

if (shouldFill) {
g.fill(renderPath);
} else {
g.draw(renderPath);
}

g.setPaint(oldPaint);
}

@Override
public void destroy(Scene originScene) {
points = null;
renderPath = null;

color = null;
paintFilled = false;
paint = null;
shouldFill = false;

scale = null;
translation = null;
Expand Down Expand Up @@ -321,8 +326,8 @@ public boolean equals(Object other) {
}
Polygon2D polygon2D = (Polygon2D) other;

return paintFilled == polygon2D.paintFilled
&& Objects.equals(color, polygon2D.color)
return shouldFill == polygon2D.shouldFill
&& DrawUtil.paintEquals(paint, polygon2D.paint)
&& Objects.equals(translation, polygon2D.translation)
&& Objects.equals(scale, polygon2D.scale)
&& Maths.floatEquals(polygon2D.rotation, rotation)
Expand All @@ -332,7 +337,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
int result = Objects.hash(renderPath, color, paintFilled, rotation, scale, translation);
int result = Objects.hash(renderPath, paint, shouldFill, rotation, scale, translation);
result = 31 * result + Arrays.hashCode(points);
return result;
}
Expand All @@ -342,8 +347,8 @@ public String toString() {
return "Polygon2D{" +
"renderPath=" + renderPath +
", points=" + Arrays.toString(points) +
", color=" + color +
", paintFilled=" + paintFilled +
", paint=" + paint +
", paintFilled=" + shouldFill +
", rotation=" + rotation +
", scale=" + scale +
", translation=" + translation +
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tech/fastj/graphics/game/Text2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tech.fastj.engine.CrashMessages;
import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.DrawUtil;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.control.Scene;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tech/fastj/graphics/ui/elements/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Camera;
import tech.fastj.graphics.DrawUtil;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.game.Text2D;
import tech.fastj.graphics.ui.UIElement;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.fastj.graphics;
package tech.fastj.graphics.util;

import tech.fastj.math.Point;
import tech.fastj.graphics.Display;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
Expand Down
Loading

0 comments on commit 543a07b

Please sign in to comment.