From a8e5e723b7e1891c9b352261a3ee4c3d3563e8cf Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 26 Oct 2025 21:38:22 -0700 Subject: Checkpoint two --- .../kotlin/coffee/liz/ecs/grid/Components.kt | 8 ++++ .../commonMain/kotlin/coffee/liz/ecs/grid/Grid.kt | 54 ++++++++++++++++++++++ .../kotlin/coffee/liz/ecs/grid/GridSystem.kt | 17 +++++++ 3 files changed, 79 insertions(+) create mode 100644 composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Components.kt create mode 100644 composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/Grid.kt create mode 100644 composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid/GridSystem.kt (limited to 'composeApp/src/commonMain/kotlin/coffee/liz/ecs/grid') 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(dimensions.y.toInt()) { + MutableList(dimensions.x.toInt()) { mutableSetOf() } + }, +) { + fun at(position: Vec2): MutableSet { + 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) { + 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( + private val grid: Grid +): System { + override val dependencies = setOf(PhysicsSystem::class) + + override fun update(world: World, state: Outside, deltaTime: Duration) { + + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2