@@ -24,21 +24,20 @@ import kotlin.math.ceil
2424import kotlin.math.min
2525import kotlin.math.sqrt
2626
27- // --- Configuration ---
28- // Identical constants to the Node.js script
27+
2928const val SCALE = 4f
3029const val TILE_WIDTH = 100f * SCALE
3130const val TILE_HEIGHT = TILE_WIDTH / 2f
3231const val SOURCE_ANCHOR_Y_OFFSET = (150f * SCALE ) + (TILE_WIDTH / 4f )
3332
34- data class PlacedTree (
33+ private data class PlacedTree (
3534 val id : String ,
3635 val image : ImageBitmap ,
3736 val drawX : Float ,
3837 val drawY : Float
3938)
4039
41- data class ForestLayout (
40+ private data class ForestLayout (
4241 val trees : List <PlacedTree >,
4342 val totalWidth : Float ,
4443 val totalHeight : Float
@@ -48,21 +47,17 @@ data class ForestLayout(
4847fun IsometricForest (treeIds : List <String >) {
4948 val context = LocalPlatformContext .current
5049
51- // State to hold the calculated forest
5250 var forestLayout by remember { mutableStateOf<ForestLayout ?>(null ) }
5351 var isLoading by remember { mutableStateOf(true ) }
5452
55- // Logic: Load & Calculate Positions (Background Thread)
5653 LaunchedEffect (treeIds) {
5754 isLoading = true
5855
5956 forestLayout = withContext(Dispatchers .Default ) {
6057 if (treeIds.isEmpty()) return @withContext null
6158
62- // 1. Calculate Grid Dimensions
6359 val gridSize = ceil(sqrt(treeIds.size.toDouble())).toInt()
6460
65- // 2. Load Images Parallel
6661 val imageLoader = ImageLoader (context)
6762
6863 val deferredLoad = treeIds.mapIndexed { index, id ->
@@ -91,7 +86,7 @@ fun IsometricForest(treeIds: List<String>) {
9186
9287 val results = deferredLoad.awaitAll().filterNotNull()
9388
94- // 3. Sort by Depth (Painter's Algorithm)
89+ // Sort by Depth (Painter's Algorithm)
9590 val sortedResults = results.sortedWith(Comparator { a, b ->
9691 val (posA, _) = a
9792 val (posB, _) = b
@@ -101,7 +96,6 @@ fun IsometricForest(treeIds: List<String>) {
10196 if (depthA != depthB) depthA - depthB else posA.first - posB.first
10297 })
10398
104- // 4. Calculate Canvas Bounds (Virtual Size)
10599 val isoWidth = (gridSize * 2 ) * (TILE_WIDTH / 2f )
106100 val isoHeight = gridSize * TILE_HEIGHT
107101
@@ -113,7 +107,7 @@ fun IsometricForest(treeIds: List<String>) {
113107 val originX = totalWidth / 2f
114108 val originY = 150f * SCALE
115109
116- // 5. Create PlacedTree objects with exact coordinates
110+ // Create PlacedTree objects with exact coordinates
117111 val placements = sortedResults.map { (pos, bitmap, id) ->
118112 val (gridX, gridY) = pos
119113
@@ -141,7 +135,6 @@ fun IsometricForest(treeIds: List<String>) {
141135 isLoading = false
142136 }
143137
144- // --- Rendering ---
145138 Box (modifier = Modifier .fillMaxSize(), contentAlignment = Alignment .Center ) {
146139 if (isLoading) {
147140 CircularProgressIndicator ()
@@ -153,33 +146,28 @@ fun IsometricForest(treeIds: List<String>) {
153146 val screenHeight = constraints.maxHeight.toFloat()
154147
155148 Canvas (modifier = Modifier .fillMaxSize()) {
156- // 1. Calculate Scale to Fit
157- // We want the whole 'layout.totalWidth' to fit inside 'screenWidth' (and same for height)
149+ // Calculate Scale to Fit
158150 val scaleX = screenWidth / layout.totalWidth
159151 val scaleY = screenHeight / layout.totalHeight
160152
161153 // Use the smaller scale to ensure it fits both dimensions (Fit Center)
162154 val scale = min(scaleX, scaleY)
163155
164- // 2. Calculate Centering Offsets
165- // How much empty space is left?
156+ // Calculate Centering Offsets
166157 val usedWidth = layout.totalWidth * scale
167158 val usedHeight = layout.totalHeight * scale
168159
169160 val translateX = (screenWidth - usedWidth) / 2f
170161 val translateY = (screenHeight - usedHeight) / 2f
171162
172- // 3. Apply Transform and Draw
173163 withTransform({
174164 translate(left = translateX, top = translateY)
175- // We must define both scaleX and scaleY explicitly
176165 scale(
177166 scaleX = scale,
178167 scaleY = scale,
179168 pivot = Offset .Zero
180169 )
181170 }) {
182- // "Very efficient": No math here, just drawing pre-calc coordinates
183171 layout.trees.forEach { tree ->
184172 drawImage(
185173 image = tree.image,
0 commit comments