Skip to content

Commit 67dcd3d

Browse files
committed
Don't use custom rectangle class
1 parent 33b0c7e commit 67dcd3d

File tree

3 files changed

+41
-161
lines changed

3 files changed

+41
-161
lines changed

src/main/java/gregtech/api/mui/widget/FlappyGreg.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import gregtech.api.GTValues;
44
import gregtech.api.mui.GTGuiTextures;
5-
import gregtech.api.util.Rectangle;
5+
import gregtech.api.util.GTUtility;
66

77
import net.minecraft.client.renderer.GlStateManager;
88
import net.minecraft.util.text.TextFormatting;
@@ -23,6 +23,7 @@
2323
import org.jetbrains.annotations.NotNull;
2424
import org.jetbrains.annotations.Nullable;
2525

26+
import java.awt.geom.Rectangle2D;
2627
import java.util.List;
2728

2829
public class FlappyGreg extends Widget<FlappyGreg> implements Interactable {
@@ -37,11 +38,11 @@ public class FlappyGreg extends Widget<FlappyGreg> implements Interactable {
3738
protected static final int MAX_OBSTACLES = 5;
3839
protected static final float OBSTACLE_MOVEMENT_SPEED = 2.0f;
3940
protected static final int OBSTACLE_COLOR = Color.GREEN.main;
40-
protected final List<Rectangle> obstacles = new ObjectArrayList<>(MAX_OBSTACLES * 2);
41+
protected final List<Rectangle2D.Float> obstacles = new ObjectArrayList<>(MAX_OBSTACLES * 2);
4142
protected float obstacleWidth;
4243

4344
protected static final float GREG_SIZE = 18.0f;
44-
protected Rectangle gregArea = new Rectangle();
45+
protected Rectangle2D.Float gregArea = new Rectangle2D.Float();
4546
protected float gregYSpeed = TERMINAL_VELOCITY;
4647

4748
@Nullable
@@ -101,28 +102,29 @@ protected void initializeState() {
101102
int width = area.width;
102103
int height = area.height;
103104

104-
gregArea = new Rectangle((width / 3.0f) - (GREG_SIZE / 2.0f), (height / 2.0f) - (GREG_SIZE / 2.0f), GREG_SIZE);
105+
gregArea.setRect((width / 3.0f) - (GREG_SIZE / 2.0f), (height / 2.0f) - (GREG_SIZE / 2.0f), GREG_SIZE,
106+
GREG_SIZE);
105107

106108
float lastXPos = width * 0.95f;
107109
this.obstacleWidth = width / 20.0f;
108110
for (int i = 0; i < MAX_OBSTACLES; i++) {
109-
Rectangle top = new Rectangle();
111+
Rectangle2D.Float top = new Rectangle2D.Float();
110112
obstacles.add(top);
111113

112-
Rectangle bottom = new Rectangle();
114+
Rectangle2D.Float bottom = new Rectangle2D.Float();
113115
obstacles.add(bottom);
114116

115-
top.setX(lastXPos);
116-
bottom.setX(lastXPos);
117-
top.setWidth(obstacleWidth);
118-
bottom.setWidth(obstacleWidth);
117+
top.x = lastXPos;
118+
bottom.x = lastXPos;
119+
top.width = obstacleWidth;
120+
bottom.width = obstacleWidth;
119121

120122
final float topYEnd = (height * 0.55f) - (height * 0.20f * GTValues.RNG.nextFloat());
121-
top.setHeight(topYEnd);
123+
top.height = topYEnd;
122124

123-
final float bottomYStart = topYEnd + (gregArea.getHeight() * 1.75f);
124-
bottom.setHeight(height - bottomYStart);
125-
bottom.setY(bottomYStart);
125+
final float bottomYStart = topYEnd + (gregArea.height * 1.75f);
126+
bottom.height = height - bottomYStart;
127+
bottom.y = bottomYStart;
126128

127129
lastXPos += (width / 2.5f) + ((width / 15.0f) * GTValues.RNG.nextFloat());
128130
}
@@ -146,7 +148,7 @@ public void onUpdate() {
146148
@SideOnly(Side.CLIENT)
147149
protected void updateGregPosition() {
148150
if (gregArea.getY() < (getArea().height - gregArea.getHeight())) {
149-
gregArea.setY(gregArea.getY() + gregYSpeed);
151+
gregArea.y = gregArea.y + gregYSpeed;
150152
}
151153

152154
if (gregYSpeed < TERMINAL_VELOCITY) {
@@ -156,8 +158,12 @@ protected void updateGregPosition() {
156158

157159
@SideOnly(Side.CLIENT)
158160
protected void checkObstacleCollisions() {
159-
for (Rectangle obstacle : obstacles) {
160-
if (obstacle.collides(gregArea, OBSTACLE_MOVEMENT_SPEED)) {
161+
Rectangle2D.Float gregTest = new Rectangle2D.Float();
162+
gregTest.setRect(gregArea);
163+
gregTest.x += OBSTACLE_MOVEMENT_SPEED;
164+
165+
for (Rectangle2D.Float obstacle : obstacles) {
166+
if (GTUtility.rectanglesCollide(obstacle, gregTest)) {
161167
gameState = GameState.COLLIDED;
162168
break;
163169
}
@@ -166,17 +172,17 @@ protected void checkObstacleCollisions() {
166172

167173
@SideOnly(Side.CLIENT)
168174
protected void updateObstaclePositions() {
169-
for (Rectangle obstacle : obstacles) {
170-
obstacle.decrementX(OBSTACLE_MOVEMENT_SPEED);
175+
for (Rectangle2D.Float obstacle : obstacles) {
176+
obstacle.x -= OBSTACLE_MOVEMENT_SPEED;
171177
}
172178
}
173179

174180
@SideOnly(Side.CLIENT)
175181
protected void checkWinState() {
176182
if (obstacles.isEmpty()) return;
177183

178-
Rectangle obstacle = obstacles.get(obstacles.size() - 1);
179-
if ((obstacle.getX() + obstacleWidth + WIN_DISTANCE) <= gregArea.getX()) {
184+
Rectangle2D.Float obstacle = obstacles.get(obstacles.size() - 1);
185+
if ((obstacle.x + obstacleWidth + WIN_DISTANCE) <= gregArea.getX()) {
180186
gameState = GameState.FINISHED;
181187
onFinish();
182188
}
@@ -193,14 +199,13 @@ public void draw(ModularGuiContext context, WidgetThemeEntry<?> widgetTheme) {
193199

194200
// Obstacles
195201
Stencil.applyAtZero(area, context);
196-
for (Rectangle obstacle : obstacles) {
197-
GuiDraw.drawRect(obstacle.getX(), obstacle.getY(), obstacle.getWidth(), obstacle.getHeight(),
198-
OBSTACLE_COLOR);
202+
for (Rectangle2D.Float obstacle : obstacles) {
203+
GuiDraw.drawRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height, OBSTACLE_COLOR);
199204
}
200205
Stencil.remove();
201206

202207
// "Character"
203-
GTGuiTextures.GREGTECH_LOGO.draw(gregArea.getX(), gregArea.getY(), gregArea.getWidth(), gregArea.getHeight());
208+
GTGuiTextures.GREGTECH_LOGO.draw(gregArea.x, gregArea.y, gregArea.width, gregArea.height);
204209

205210
WidgetTheme theme = widgetTheme.getTheme();
206211
switch (gameState) {

src/main/java/gregtech/api/util/GTUtility.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.jetbrains.annotations.Nullable;
7171
import org.jetbrains.annotations.Range;
7272

73+
import java.awt.geom.Rectangle2D;
7374
import java.time.MonthDay;
7475
import java.time.ZoneId;
7576
import java.util.AbstractList;
@@ -1129,4 +1130,14 @@ public static boolean isToday(@NotNull MonthDay monthDay, @Nullable ZoneId tz) {
11291130
public static boolean isToday(@NotNull MonthDay monthDay) {
11301131
return isToday(monthDay, null);
11311132
}
1133+
1134+
/**
1135+
* Check if two rectangles would touch or intersect.
1136+
*/
1137+
public static boolean rectanglesCollide(@NotNull Rectangle2D a, @NotNull Rectangle2D b) {
1138+
return (a.getX() <= b.getX() + b.getWidth()) &&
1139+
(a.getX() + a.getWidth() >= b.getX()) &&
1140+
(a.getY() <= b.getY() + b.getHeight()) &&
1141+
(a.getY() + a.getHeight() >= b.getY());
1142+
}
11321143
}

src/main/java/gregtech/api/util/Rectangle.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)