@@ -32,6 +32,17 @@ public static void main(String[] args) {
32
32
launch (args );
33
33
}
34
34
35
+ private void saveState (
36
+ ActionEncoder .StateAction stateAction ,
37
+ SimpleObjectProperty <GameState > gameStateO ,
38
+ SimpleObjectProperty <List <String >> actionsO
39
+ ) {
40
+ gameStateO .setValue (stateAction .gameState ());
41
+ List <String > newActions = new ArrayList <>(actionsO .getValue ());
42
+ newActions .add (stateAction .action ());
43
+ actionsO .setValue (newActions );
44
+ }
45
+
35
46
private TileDecks getShuffledTileDecks (Long seed ) {
36
47
List <Tile > tiles = new ArrayList <>(Tiles .TILES );
37
48
if (seed != null ) {
@@ -73,7 +84,6 @@ public void start(Stage primaryStage) {
73
84
tileDecks ,
74
85
textMaker
75
86
);
76
- gameState = gameState .withStartingTilePlaced ();
77
87
78
88
SimpleObjectProperty <GameState > gameStateO = new SimpleObjectProperty <>(gameState );
79
89
ObservableValue <List <MessageBoard .Message >> observableMessagesO = gameStateO .map (
@@ -85,42 +95,44 @@ public void start(Stage primaryStage) {
85
95
ObservableValue <TileDecks > tileDecksO = gameStateO .map (GameState ::tileDecks );
86
96
ObservableValue <Integer > leftNormalTilesO = tileDecksO .map (tDecks -> tDecks .normalTiles ().size ());
87
97
ObservableValue <Integer > leftMenhirTilesO = tileDecksO .map (tDecks -> tDecks .menhirTiles ().size ());
88
- ObservableValue <String > textToDisplayO = gameStateO .map (gameState1 ->
89
- switch (gameState1 .nextAction ()){
98
+ ObservableValue <String > textToDisplayO = gameStateO .map (gState ->
99
+ switch (gState .nextAction ()){
90
100
case GameState .Action .OCCUPY_TILE -> textMaker .clickToOccupy ();
91
101
case GameState .Action .RETAKE_PAWN -> textMaker .clickToUnoccupy ();
92
102
default -> "" ;
93
103
}
94
104
);
95
105
106
+ SimpleObjectProperty <List <String >> actionsO = new SimpleObjectProperty <>(List .of ());
107
+
96
108
Consumer <Occupant > onOccupantClick = occupant -> {
97
109
// todo handle things
98
110
GameState currentGameState = gameStateO .getValue ();
99
111
if (currentGameState .nextAction () == GameState .Action .OCCUPY_TILE ) {
100
112
assert currentGameState .board ().lastPlacedTile () != null ;
101
113
int lastPlacedTileId = currentGameState .board ().lastPlacedTile ().id ();
102
114
if (occupant != null && Zone .tileId (occupant .zoneId ()) != lastPlacedTileId ) return ;
103
- gameStateO .setValue (currentGameState .withNewOccupant (occupant ));
115
+ ActionEncoder .StateAction stateAction = ActionEncoder .withNewOccupant (currentGameState , occupant );
116
+ saveState (stateAction , gameStateO , actionsO );
104
117
}
105
118
else if (currentGameState .nextAction () == GameState .Action .RETAKE_PAWN ) {
106
119
// todo check owner stuff etc
107
- gameStateO .setValue (currentGameState .withOccupantRemoved (occupant ));
120
+ ActionEncoder .StateAction stateAction = ActionEncoder .withOccupantRemoved (currentGameState , occupant );
121
+ saveState (stateAction , gameStateO , actionsO );
108
122
}
109
123
};
110
124
111
- SimpleObjectProperty <List <String >> actions = new SimpleObjectProperty <>(List .of ());
112
-
113
125
Consumer <String > onEnteredAction = action -> {
114
126
ActionEncoder .StateAction newSt = ActionEncoder .decodeAndApply (gameStateO .getValue (), action );
115
127
if (newSt != null ) {
116
- gameStateO . setValue (newSt . gameState () );
128
+ saveState (newSt , gameStateO , actionsO );
117
129
}
118
130
};
119
131
120
132
Node playersNode = PlayersUI .create (gameStateO , new TextMakerFr (playersNames ));
121
133
Node messagesNode = MessageBoardUI .create (observableMessagesO , highlightedTilesO );
122
134
Node decksNode = DecksUI .create (tileToPlaceO , leftNormalTilesO , leftMenhirTilesO , textToDisplayO , onOccupantClick );
123
- Node actionsNode = ActionsUI .create (actions , onEnteredAction );
135
+ Node actionsNode = ActionsUI .create (actionsO , onEnteredAction );
124
136
125
137
SimpleObjectProperty <Rotation > nextRotationO = new SimpleObjectProperty <>(Rotation .NONE );
126
138
Consumer <Rotation > onRotationClick = r -> {
@@ -139,7 +151,8 @@ else if (currentGameState.nextAction() == GameState.Action.RETAKE_PAWN) {
139
151
nextRotationO .getValue (),
140
152
pos
141
153
);
142
- gameStateO .setValue (currentGameState .withPlacedTile (placedTile ));
154
+ ActionEncoder .StateAction stateAction = ActionEncoder .withPlacedTile (currentGameState , placedTile );
155
+ saveState (stateAction , gameStateO , actionsO );
143
156
};
144
157
145
158
ObservableValue <Set <Occupant >> visibleOccupants = gameStateO .map (gState -> {
@@ -179,5 +192,7 @@ else if (currentGameState.nextAction() == GameState.Action.RETAKE_PAWN) {
179
192
primaryStage .setTitle ("ChaCuN" );
180
193
primaryStage .show ();
181
194
195
+ gameStateO .setValue (gameStateO .getValue ().withStartingTilePlaced ());
196
+
182
197
}
183
198
}
0 commit comments