Skip to content

Commit

Permalink
(#10, #12) Cleaned up drawable/transform code
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jun 24, 2021
1 parent fe257b1 commit 41a9f69
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.Drawable;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.behaviors.Behavior;
import tech.fastj.systems.input.keyboard.Keyboard;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.game.Text2D;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.behaviors.Behavior;
import tech.fastj.systems.control.SceneManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Display;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.game.GameObject;
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.ui.UIElement;
import tech.fastj.graphics.ui.elements.Button;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.behaviors.Behavior;
import tech.fastj.systems.control.Scene;
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/tech/fastj/graphics/Drawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public abstract class Drawable extends TaggableEntity {
protected Path2D.Float collisionPath;
protected Path2D.Float transformedCollisionPath;
private boolean shouldRender;
private Pointf[] boundaries;
protected final Transform2D transform;
private Pointf initialCenter;

Expand Down Expand Up @@ -109,23 +108,6 @@ public Pointf[] getBounds() {
return DrawUtil.createBox((Rectangle2D.Float) transformedCollisionPath.getBounds2D());
}

/**
* Sets the boundaries of the {@code Drawable} to the specified {@code Pointf} array.
* <p>
* The specified array must be exactly 4 points. If there is any deviancy from this, the game will crash out with an
* error specifying this.
*
* @param bounds The {@code Pointf} array that the boundaries of the {@code Drawable} will be set to.
*/
protected void setBounds(Pointf[] bounds) {
if (bounds.length != 4) {
FastJEngine.error(CrashMessages.illegalAction(getClass()),
new IllegalArgumentException("The boundaries for a Drawable must only have 4 points."));
}

boundaries = bounds;
}

/**
* Gets one of the boundaries of the {@code Drawable}, based on the specified {@code Boundary} parameter.
*
Expand Down Expand Up @@ -374,7 +356,6 @@ protected void destroyTheRest(Scene origin) {
clearTags();

collisionPath = null;
boundaries = null;
}

private void updateTransformedCollisionPath() {
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/tech/fastj/graphics/game/Model2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public Model2D(Polygon2D[] polygonArray, boolean show) {
polyArr = polygonArray;

setCollisionPoints();
setBounds(createBounds());


setShouldRender(show);
Expand All @@ -77,14 +76,10 @@ public Model2D(Polygon2D[] polygonArray, Pointf location, float rotVal, Pointf s

setCollisionPoints();

setBounds(createBounds());

setTranslation(location);
setRotation(rotVal);
setScale(scaleVal);

setBounds(createBounds());

setShouldRender(shouldBeShown);
}

Expand Down
26 changes: 6 additions & 20 deletions src/main/java/tech/fastj/graphics/game/Polygon2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.awt.Paint;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
import java.util.Objects;

Expand Down Expand Up @@ -61,7 +60,6 @@ public Polygon2D(Pointf[] pts, Paint paint, boolean fill, boolean show) {
points = pts;

renderPath = DrawUtil.createPath(points);
setBoundaries(renderPath);
setCollisionPath(renderPath);

setPaint(paint);
Expand All @@ -74,20 +72,19 @@ public Polygon2D(Pointf[] pts, Paint paint, boolean fill, boolean show) {
* {@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 pts {@code Pointf} array that defines the points for the polygon.
* @param setTranslation {@code Pointf} to set the initial translation 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 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.
* @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 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 setTranslation, float setRotation, Pointf setScale, Paint paint, boolean fill, boolean show) {
super();
points = pts;

renderPath = DrawUtil.createPath(points);
setBoundaries(renderPath);
setCollisionPath(renderPath);

setTranslation(setTranslation);
Expand Down Expand Up @@ -199,12 +196,10 @@ public void modifyPoints(Pointf[] pts, boolean resetTranslation, boolean resetRo
transform.resetRotation();
}
if (resetScale) {
System.out.println("reset?");
transform.resetScale();
}
}

setBoundaries(renderPath);
setCollisionPath(renderPath);
}

Expand Down Expand Up @@ -242,15 +237,6 @@ public void destroy(Scene originScene) {

}

/**
* Sets the boundaries of the polygon, using the specified {@code Path2D.Float}.
*
* @param p The {@code Path2D.Float} which the boundaries will be based off of.
*/
private void setBoundaries(Path2D.Float p) {
super.setBounds(DrawUtil.createBox((Rectangle2D.Float) p.getBounds2D()));
}

/**
* Checks for equality between the {@code Polygon2D} and the other specified.
*
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/tech/fastj/graphics/game/Text2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.util.DrawUtil;

import tech.fastj.systems.control.Scene;

Expand Down Expand Up @@ -232,7 +231,6 @@ private void setMetrics(Graphics2D g) {

Pointf translation = getTranslation();
final Rectangle2D.Float bounds = new Rectangle2D.Float(translation.x, translation.y, textWidth, textHeight);
setBounds(DrawUtil.createBox(bounds));

setCollisionPath(createMetricsPath(bounds));

Expand Down
1 change: 0 additions & 1 deletion src/main/java/tech/fastj/graphics/ui/elements/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public Button(Scene origin, Pointf location, Pointf initialSize) {

this.location = location;
Pointf[] buttonCoords = DrawUtil.createBox(this.location, initialSize);
super.setBounds(buttonCoords);

renderPath = DrawUtil.createPath(buttonCoords);
super.setCollisionPath(renderPath);
Expand Down

0 comments on commit 41a9f69

Please sign in to comment.