Skip to content

Commit ff5a091

Browse files
committed
enhance(piranhas): fish hovering and human move click-lock
1 parent 85496af commit ff5a091

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/main/kotlin/sc/gui/view/GameView.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,19 @@ abstract class GameBoard<GameState: IGameState>: View(), ChangeListener<IGameSta
105105
}
106106
}
107107

108+
/** For keyboard based controls - accessibility! */
108109
protected abstract fun handleKeyPress(state: GameState, keyEvent: KeyEvent): Boolean
109110

111+
/** Show human controls if it is a human move. */
110112
protected fun checkHumanControls() {
111113
if(awaitingHumanMove.value)
112114
renderHumanControls(gameState ?: return)
113115
}
114116

117+
/** Called when a human player needs to make a move to render needed UI elements. */
115118
protected abstract fun renderHumanControls(state: GameState)
116119

120+
/** Send move if it is humans turn. */
117121
protected fun sendHumanMove(move: IMove): Boolean {
118122
if(awaitingHumanMove.value) {
119123
fire(HumanMoveAction(move.also {

src/main/kotlin/sc/gui/view/game/PiranhasBoard.kt

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package sc.gui.view.game
22

3+
import javafx.application.Platform
34
import javafx.geometry.Pos
45
import javafx.scene.Node
56
import javafx.scene.effect.ColorAdjust
7+
import javafx.scene.effect.Glow
68
import javafx.scene.input.KeyEvent
79
import javafx.scene.layout.GridPane
810
import sc.api.plugins.Coordinates
@@ -29,11 +31,14 @@ class PiranhasBoard: GameBoard<GameState>() {
2931
}
3032
}
3133

32-
var selected: Coordinates? = null
34+
var selected: Node? = null
3335
val hovers = ArrayList<Node>()
3436

3537
override fun onNewState(oldState: GameState?, state: GameState?) {
38+
logger.debug { "New State: $state" }
3639
grid.children.clear()
40+
hovers.clear()
41+
selected = null
3742

3843
(0 until PiranhaConstants.BOARD_LENGTH).forEach { y ->
3944
grid.add(PieceImage(gridSize, "squid").apply { opacity = 0.0 }, 0, y)
@@ -42,33 +47,43 @@ class PiranhasBoard: GameBoard<GameState>() {
4247

4348
state?.let { state ->
4449
state.board.forEach { (pos: Coordinates, field: FieldState) ->
45-
val piece = PieceImage(gridSize,
50+
val piece = PieceImage(
51+
gridSize,
4652
field.team?.let { team -> "${team}_${field.size}" } ?: field.name.lowercase())
4753
grid.add(piece, pos.x, pos.y)
4854
if(field.team == null)
4955
return@forEach
5056
piece.onHover {
5157
if(selected == null) {
52-
grid.children.removeAll(hovers)
53-
hovers.clear()
54-
addHovers(state, pos, field)
58+
Platform.runLater {
59+
addHovers(state, pos, field)
60+
}
5561
}
5662
}
5763
piece.onLeftClick {
58-
grid.children.removeAll(hovers)
59-
hovers.clear()
60-
if(selected == pos) {
61-
selected = null
62-
return@onLeftClick
64+
if(field.team == state.currentTeam && awaitingHumanMove.value) {
65+
logger.debug { "Clicked own fish on $pos" }
66+
selected?.effect = null
67+
if(selected == piece) {
68+
grid.children.removeAll(hovers)
69+
hovers.clear()
70+
selected = null
71+
return@onLeftClick
72+
}
73+
selected = piece
74+
piece.effect = Glow(0.6)
75+
addHovers(state, pos, field)
6376
}
64-
selected = pos
65-
addHovers(state, pos, field)
6677
}
6778
}
6879
}
6980
}
7081

7182
fun addHovers(state: GameState, pos: Coordinates, field: FieldState) {
83+
logger.trace { "Clearing hovers and adding for $pos in turn ${state.turn}" }
84+
grid.children.removeAll(hovers)
85+
hovers.clear()
86+
7287
val board = state.board
7388
GameRuleLogic.possibleMovesFor(board, pos).forEach { move ->
7489
val target = GameRuleLogic.targetField(board, move)
@@ -91,7 +106,7 @@ class PiranhasBoard: GameBoard<GameState>() {
91106
}
92107

93108
override fun renderHumanControls(state: GameState) {
94-
// TODO "Not yet implemented"
109+
// not needed for piranhas, handled abovene
95110
}
96111

97112
}

0 commit comments

Comments
 (0)