summaryrefslogtreecommitdiff
path: root/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid
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/grid
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/grid')
-rw-r--r--composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Components.kt8
-rw-r--r--composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Grid.kt54
-rw-r--r--composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/GridSystem.kt17
3 files changed, 79 insertions, 0 deletions
diff --git a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Components.kt b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Components.kt
new file mode 100644
index 0000000..b559aa7
--- /dev/null
+++ b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Components.kt
@@ -0,0 +1,8 @@
+package coffee.liz.ecs.grid
+
+import coffee.liz.ecs.Component
+import coffee.liz.ecs.Vec2
+
+data class GridPosition(
+ val position: Vec2,
+) : Component
diff --git a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Grid.kt b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Grid.kt
new file mode 100644
index 0000000..f4eef43
--- /dev/null
+++ b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Grid.kt
@@ -0,0 +1,54 @@
+package coffee.liz.ecs.grid
+
+import coffee.liz.ecs.Entity
+import coffee.liz.ecs.Rect
+import coffee.liz.ecs.Vec2
+
+data class Grid(
+ val dimensions: Vec2,
+ val grid: MutableList<MutableList<MutableSet<Entity>>> =
+ MutableList(dimensions.y.toInt()) {
+ MutableList(dimensions.x.toInt()) { mutableSetOf() }
+ },
+) {
+ fun at(position: Vec2): MutableSet<Entity> {
+ if (!Rect(Vec2(0f, 0f), dimensions).contains(position)) {
+ throw IllegalArgumentException()
+ }
+ return grid[position.y.toInt()][position.x.toInt()]
+ }
+
+ fun add(entity: Entity) {
+ if (!entity.has(GridPosition::class)) {
+ throw IllegalArgumentException("Entity must have a GridPosition component to be added to the grid")
+ }
+ val position = entity.get(GridPosition::class).position
+ at(position).add(entity)
+ }
+
+ fun reassignCellEntities(entities: Collection<Entity>) {
+ val remainingEntities = entities.filter { it.has(GridPosition::class) }.toMutableSet()
+ for (y in grid.indices) {
+ for (x in grid[y].indices) {
+ val vec = Vec2(x.toFloat(), y.toFloat())
+ val entities = at(vec)
+ entities.retainAll(
+ entities
+ .filter {
+ if (!it.has(GridPosition::class)) {
+ return@filter false
+ }
+ val currentPosition = it.get(GridPosition::class).position
+ (vec == currentPosition).also { stillInCell ->
+ if (!stillInCell) {
+ at(currentPosition).add(it)
+ }
+ }
+ }.toSet(),
+ )
+ remainingEntities.removeAll(entities)
+ }
+ }
+ remainingEntities.forEach { add(it) }
+ }
+}
diff --git a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/GridSystem.kt b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/GridSystem.kt
new file mode 100644
index 0000000..50d784d
--- /dev/null
+++ b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/GridSystem.kt
@@ -0,0 +1,17 @@
+package coffee.liz.ecs.grid
+
+import coffee.liz.ecs.System
+import coffee.liz.ecs.World
+import coffee.liz.ecs.physics.PhysicsSystem
+import kotlin.reflect.KClass
+import kotlin.time.Duration
+
+class GridSystem<Outside>(
+ private val grid: Grid
+): System<Outside> {
+ override val dependencies = setOf(PhysicsSystem::class)
+
+ override fun update(world: World<Outside>, state: Outside, deltaTime: Duration) {
+
+ }
+} \ No newline at end of file