Skip to content

Commit e1a53d0

Browse files
Added base reserve ammo
1 parent db44f37 commit e1a53d0

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

composeApp/src/jvmMain/kotlin/ua/valeriishymchuk/lobmapeditor/domain/GameScenario.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ sealed interface GameScenario<T : GameScenario<T>> {
4747
add("player", JsonPrimitive(index + 1))
4848
add("team", JsonPrimitive(player.team.id))
4949
add("ammoReserve", JsonPrimitive(player.ammo))
50+
add("baseAmmoReserve", JsonPrimitive(player.baseAmmo))
5051
})
5152
}
5253
})
@@ -157,7 +158,9 @@ sealed interface GameScenario<T : GameScenario<T>> {
157158
val teamId = playerObj.getAsJsonPrimitive("team").asInt
158159
Player(
159160
team = PlayerTeam.fromId(teamId),
160-
playerObj.getAsJsonPrimitive("ammoReserve").asInt
161+
playerObj.getAsJsonPrimitive("ammoReserve")?.asInt ?: 500,
162+
playerObj.getAsJsonPrimitive("baseAmmoReserve")?.asInt
163+
?: playerObj.getAsJsonPrimitive("ammoReserve")?.asInt ?: 500
161164
)
162165
}.toList()
163166

composeApp/src/jvmMain/kotlin/ua/valeriishymchuk/lobmapeditor/domain/player/Player.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ package ua.valeriishymchuk.lobmapeditor.domain.player
22

33
data class Player(
44
val team: PlayerTeam,
5-
val ammo: Int
5+
val ammo: Int,
6+
val baseAmmo: Int
67
)

composeApp/src/jvmMain/kotlin/ua/valeriishymchuk/lobmapeditor/services/ProjectsService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class ProjectsService(override val di: DI) : DIAware {
7777
),
7878
units = emptyList(),
7979
players = listOf(
80-
Player(PlayerTeam.RED, 500),
81-
Player(PlayerTeam.BLUE, 500),
80+
Player(PlayerTeam.RED, 500, 500),
81+
Player(PlayerTeam.BLUE, 500, 500),
8282
),
8383
),
8484
File(dir, "map.json")

composeApp/src/jvmMain/kotlin/ua/valeriishymchuk/lobmapeditor/ui/component/project/tool/ToolConfig.kt

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,30 @@ private fun PlayerToolConfig() {
281281
)
282282
}
283283

284+
285+
284286
LaunchedEffect(currentPlayerReference) {
285287
// println("Changed currentPlayerReference to ${currentPlayerReference.key}")
286288
// val textValue = ammoTextFieldValue.text.toIntOrNull() ?: return@LaunchedEffect
287289
ammoTextFieldValue = ammoTextFieldValue.copy(text = currentPlayer.ammo.toString())
288290
}
289291

292+
var baseAmmoTextFieldValue by remember {
293+
mutableStateOf(
294+
TextFieldValue(
295+
text = currentPlayer.baseAmmo.toString(),
296+
selection = TextRange(currentPlayer.baseAmmo.toString().length)
297+
)
298+
)
299+
}
300+
301+
302+
LaunchedEffect(currentPlayerReference) {
303+
// println("Changed currentPlayerReference to ${currentPlayerReference.key}")
304+
// val textValue = ammoTextFieldValue.text.toIntOrNull() ?: return@LaunchedEffect
305+
baseAmmoTextFieldValue = baseAmmoTextFieldValue.copy(text = currentPlayer.baseAmmo.toString())
306+
}
307+
290308
val playerPopupManager = remember { PopupManager() }
291309
val teamPopupManager = remember { PopupManager() }
292310
val newOwnerPopupManager = remember { PopupManager() }
@@ -398,6 +416,51 @@ private fun PlayerToolConfig() {
398416
}
399417
)
400418

419+
Spacer(Modifier.height(10.dp))
420+
421+
Text("Base ammo:")
422+
423+
TextField(
424+
value = baseAmmoTextFieldValue,
425+
onValueChange = { newValue ->
426+
baseAmmoTextFieldValue = newValue
427+
baseAmmoTextFieldValue = baseAmmoTextFieldValue.copy(
428+
text = newValue.text
429+
.replace(Regex("[^0-9]"), "").let { str ->
430+
val value = str.toFloatOrNull() ?: return@let str
431+
val coercedValue = max(value, 0f)
432+
if (coercedValue == value) return@let str
433+
coercedValue.toString()
434+
}
435+
)
436+
437+
438+
val finalText: Int = baseAmmoTextFieldValue.text.ifEmpty { "0" }.toIntOrNull() ?: 0
439+
440+
val oldList = scenario!!.players
441+
val newList = scenario!!.players.mapIndexed { id, player ->
442+
if (currentPlayerReference.key != id) return@mapIndexed player
443+
player.copy(baseAmmo = finalText)
444+
}
445+
val command = UpdatePlayerListCommand(
446+
oldList,
447+
newList
448+
)
449+
editorService.executeCompound(command)
450+
},
451+
modifier = Modifier.onFocusChanged { focus ->
452+
if (!focus.isFocused) {
453+
editorService.flushCompound()
454+
}
455+
},
456+
leadingIcon = {
457+
Row {
458+
Text("Base ammo", color = JewelTheme.globalColors.text.info)
459+
Spacer(Modifier.width(4.dp))
460+
}
461+
}
462+
)
463+
401464

402465
data class PlayerMapping(
403466
val player: Player,
@@ -476,7 +539,7 @@ private fun PlayerToolConfig() {
476539
onClick = {
477540
val oldList = scenario!!.players
478541
val newList = scenario!!.players.toMutableList()
479-
newList.add(Player(PlayerTeam.RED, 500))
542+
newList.add(Player(PlayerTeam.RED, 500, 500))
480543
val command = UpdatePlayerListCommand(
481544
oldList,
482545
newList

0 commit comments

Comments
 (0)