From 8b2cabcbdcfbf738998e35bbd423eae86f590f67 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 28 Jul 2022 23:40:55 +0900 Subject: [PATCH] removing redundant maths ops on hitbox intersect detection with ROUNDWORLD --- src/net/torvald/terrarum/gameactors/Hitbox.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/net/torvald/terrarum/gameactors/Hitbox.kt b/src/net/torvald/terrarum/gameactors/Hitbox.kt index 43acfd82a..66e42fe6c 100644 --- a/src/net/torvald/terrarum/gameactors/Hitbox.kt +++ b/src/net/torvald/terrarum/gameactors/Hitbox.kt @@ -159,14 +159,17 @@ class Hitbox { } /** *ROUNDWORLD*-aware version of intersects(Double, Double). */ - fun containsPoint(worldWidth: Double, x: Double, y: Double) = intersects(x, y) || intersects(x + worldWidth, y) || intersects(x - worldWidth, y) + fun containsPoint(worldWidth: Double, x: Double, y: Double) = + ((x in startX..endX) || (x + worldWidth in startX..endX) || (x - worldWidth in startX..endX)) && (y in startY..endY) /** *ROUNDWORLD*-aware version of intersects(Poind2d). */ fun containsPoint(worldWidth: Double, p: Point2d) = containsPoint(worldWidth, p.x, p.y) /** *ROUNDWORLD*-aware version of intersects(Hitbox). */ - fun containsHitbox(worldWidth: Double, other: Hitbox): Boolean { - val osx = other.startX; val osy = other.startY; val oex = other.endX; val oey = other.endY // to avoid redundant maths operations - return intersects(osx, osy, oex, oey) || intersects(osx + worldWidth, osy, oex + worldWidth, oey) || intersects(osx - worldWidth, osy, oex - worldWidth, oey) - } + fun containsHitbox(worldWidth: Double, other: Hitbox) = + ( + (this.endX >= other.startX && other.endX >= this.startX) || + (this.endX >= other.startX + worldWidth && other.endX + worldWidth >= this.startX) || + (this.endX >= other.startX - worldWidth && other.endX - worldWidth >= this.startX) + ) && (this.endY >= other.startY && other.endY >= this.startY) fun intersects(px: Double, py: Double) = (px in startX..endX) &&