summaryrefslogtreecommitdiff
path: root/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-10-26 21:38:22 -0700
committerElizabeth Hunt <me@liz.coffee>2025-10-26 21:39:58 -0700
commita8e5e723b7e1891c9b352261a3ee4c3d3563e8cf (patch)
tree853df79c877d37d7e5d25f52b301aedcc3d5db55 /composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
parent395aa7d1c312e495517701be11c21425d9a5838e (diff)
downloadabstraction-engine-kt-main.tar.gz
abstraction-engine-kt-main.zip
Checkpoint twoHEADmain
Diffstat (limited to 'composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt')
-rw-r--r--composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt57
1 files changed, 48 insertions, 9 deletions
diff --git a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
index 0b771e4..8047f3e 100644
--- a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
+++ b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
@@ -1,19 +1,42 @@
package coffee.liz.ecs
+import kotlin.math.sqrt
+
/**
* Rectangle in cartesian space.
*/
data class Rect(
val topLeft: Vec2,
- val dimensions: Vec2
+ val dimensions: Vec2,
) {
+ fun contains(point: Vec2): Boolean =
+ point.x >= topLeft.x &&
+ point.x <= topLeft.x + dimensions.x &&
+ point.y >= topLeft.y &&
+ point.y <= topLeft.y + dimensions.y
+
fun overlaps(other: Rect): Boolean {
- val xOverlap = topLeft.x <= other.topLeft.x + other.dimensions.x &&
+ val xOverlap =
+ topLeft.x <= other.topLeft.x + other.dimensions.x &&
topLeft.x + dimensions.x >= other.topLeft.x
- val yOverlap = topLeft.y <= other.topLeft.y + other.dimensions.y &&
+ val yOverlap =
+ topLeft.y <= other.topLeft.y + other.dimensions.y &&
topLeft.y + dimensions.y >= other.topLeft.y
return xOverlap && yOverlap
}
+
+ fun split(newDimensions: Vec2): List<List<Rect>> = List(
+ (dimensions.y / newDimensions.y).toInt()
+ ) { y ->
+ List(
+ (dimensions.x / newDimensions.x).toInt()
+ ) { x ->
+ Rect(
+ topLeft = topLeft.plus(Vec2(x * newDimensions.x, y * newDimensions.y)),
+ dimensions = newDimensions,
+ )
+ }
+ }
}
/**
@@ -21,13 +44,29 @@ data class Rect(
*/
data class Vec2(
val x: Float,
- val y: Float
+ val y: Float,
) {
fun plus(other: Vec2): Vec2 = Vec2(x + other.x, y + other.y)
+
fun minus(other: Vec2): Vec2 = Vec2(x - other.x, y - other.y)
- fun times(scalar: Float): Vec2 = Vec2(x * scalar, y * scalar)
- fun div(scalar: Float): Vec2 = Vec2(x / scalar, y / scalar)
+
+ fun scale(x: Float, y: Float): Vec2 = Vec2(this.x * x, this.y * y)
+
fun distanceTo(other: Vec2): Float = (this.minus(other)).length()
- fun normalize(): Vec2 = this.div(length())
- fun length(): Float = kotlin.math.sqrt(x * x + y * y)
-} \ No newline at end of file
+
+ fun normalize(): Vec2 = this.scale(1 / length(), 1 / length())
+
+ fun length(): Float = sqrt(x * x + y * y)
+}
+
+/**
+ * Cardinal directions.
+ */
+enum class CardinalDirection(
+ val direction: Vec2,
+) {
+ NORTH(Vec2(0f, -1f)),
+ SOUTH(Vec2(0f, 1f)),
+ WEST(Vec2(-1f, 0f)),
+ EAST(Vec2(1f, 0f)),
+}