door automatic opening/closing now works

This commit is contained in:
minjaesong
2022-07-28 15:48:01 +09:00
parent c903d48073
commit 56fbfb578f
6 changed files with 117 additions and 73 deletions

View File

@@ -67,7 +67,7 @@ open class ActorWithBody : Actor {
protected set
open var tooltipText: String? = null // null: display nothing
val mouseUp: Boolean
get() = hitbox.containsPoint(Terrarum.mouseX, Terrarum.mouseY)
get() = hitbox.containsPoint((world?.width ?: 0) * TILE_SIZED, Terrarum.mouseX, Terrarum.mouseY)
var hitboxTranslateX: Int = 0// relative to spritePosX
protected set

View File

@@ -158,21 +158,24 @@ class Hitbox {
return this
}
// TODO consider ROUNDWORLD
fun containsPoint(x: Double, y: Double) = (x - hitboxStart.x) in 0.0..width && (y - hitboxStart.y) in 0.0..height
fun containsPoint(p: Point2d) = containsPoint(p.x, p.y)
infix fun intersects(position: Point2d) =
(position.x >= startX && position.x <= startX + width) &&
(position.y >= startY && position.y <= startY + height)
infix fun intersects(other: Hitbox) =
(this.startX <= other.startX && other.startX <= this.endX) ||
(this.startX <= other.endX && other.endX <= this.endX) &&
(this.startY <= other.startY && other.startY <= this.endY) ||
(this.startY <= other.endY && other.endY <= this.endY)
/** *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)
/** *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 intersects(px: Double, py: Double) =
(px in startX..endX) &&
(py in startY..endY)
infix fun intersects(position: Point2d) = intersects(position.x, position.y)
infix fun intersects(other: Hitbox) = intersects(other.startX, other.startY, other.endX, other.endY)
fun intersects(otherStartX: Double, otherStartY: Double, otherEndX: Double, otherEndY: Double) =
(this.endX >= otherStartX && otherEndX >= this.startX) &&
(this.endY >= otherStartY && otherEndY >= this.startY)
fun toVector(): Vector2 = Vector2(startX, startY)