Package VASSAL.build.module
Class GameState
java.lang.Object
VASSAL.build.module.GameState
- All Implemented Interfaces:
CommandEncoder
public class GameState extends Object implements CommandEncoder
The GameState contains methods to track and read/write the complete enumerated game state of the game
currently being played. Its main methods deal with saving/loading games and starting/ending games: see
saveGameAs()
, loadGame()
, setup(boolean)
. In each case, appropriate calls
are made to designated methods in all relevant GameComponents. Can also be queried if a game is in progress
isGameStarted()
and if the game state has been modified since the last save isModified()
.
Only one game can be open at once in a single Player, or in a single Editor/Player pair.- See Also:
GameModule.getGameState()
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
GameState.SetupCommand
-
Field Summary
Fields Modifier and Type Field Description static String
BEGIN_SAVE
protected Action
closeGame
static String
END_SAVE
protected List<GameComponent>
gameComponents
protected String
lastSave
protected File
lastSaveFile
protected String
loadComments
protected Action
loadGame
protected Action
newGame
protected Map<String,GamePiece>
pieces
protected DirectoryConfigurer
savedGameDirectoryPreference
static String
SAVEFILE_ZIP_ENTRY
protected Action
saveGame
protected Action
saveGameAs
protected List<GameSetupStep>
setupSteps
-
Constructor Summary
Constructors Constructor Description GameState()
-
Method Summary
Modifier and Type Method Description void
addGameComponent(GameComponent theComponent)
Add aGameComponent
to the list of objects that will be notified when a game is started/endedvoid
addGameSetupStep(GameSetupStep step)
Add aGameSetupStep
void
addPiece(GamePiece p)
Add aGamePiece
to the current game.void
addTo(GameModule mod)
Expects to be added to a GameModule.protected boolean
checkForOldSaveFile(File f)
Command
decode(String theCommand)
A GameState recognizes instances ofGameState.SetupCommand
Command
decodeSavedGame(File saveFile)
Read a saved game and translate it into a Command.Command
decodeSavedGame(InputStream in)
String
encode(Command c)
A GameState recognizes instances ofGameState.SetupCommand
Collection<GamePiece>
getAllPieces()
Collection<GameComponent>
getGameComponents()
Enumeration<GameComponent>
getGameComponentsEnum()
Deprecated, for removal: This API element is subject to removal in a future version.String
getNewPieceId()
GamePiece
getPieceForId(String id)
Enumeration<GamePiece>
getPieces()
Deprecated, for removal: This API element is subject to removal in a future version.UsegetAllPieces()
instead.Command
getRestoreCommand()
Return aCommand
that, when executed, will restore the game to its current state.Command
getRestorePiecesCommand()
DirectoryConfigurer
getSavedGameDirectoryPreference()
Iterator<GameSetupStep>
getUnfinishedSetupSteps()
boolean
isGameStarted()
Return true if a game is currently in progressboolean
isModified()
boolean
isUpdating()
void
loadContinuation(File f)
void
loadGame()
Read the game from a savefile.void
loadGameInBackground(File f)
void
loadGameInBackground(String shortName, InputStream in)
void
removeGameComponent(GameComponent theComponent)
Remove aGameComponent
from the list of objects that will be notified when a game is started/endedvoid
removeGameSetupStep(GameSetupStep step)
Remove aGameSetupStep
void
removePiece(String id)
Remove aGamePiece
from the current gamevoid
saveGame()
Saves the game to an existing file, or prompts for a new one.void
saveGame(File f)
void
saveGameAs()
Prompts the user for a file into which to save the gameprotected String
saveString()
void
setModified(boolean modified)
void
setup(boolean gameStarting)
Start/end a game.void
setup(boolean gameStarting, boolean gameUpdating)
Start a game for updating (via editor).void
updateDone()
Indicated game update is completed and game is saved.
-
Field Details
-
pieces
-
gameComponents
-
setupSteps
-
loadGame
-
saveGame
-
saveGameAs
-
newGame
-
closeGame
-
lastSave
-
lastSaveFile
-
savedGameDirectoryPreference
-
loadComments
-
SAVEFILE_ZIP_ENTRY
- See Also:
- Constant Field Values
-
BEGIN_SAVE
- See Also:
- Constant Field Values
-
END_SAVE
- See Also:
- Constant Field Values
-
-
Constructor Details
-
GameState
public GameState()
-
-
Method Details
-
addTo
Expects to be added to a GameModule. AddsNew
,Load
,Close
, andSave
entries to theFile
menu of the controls window -
isModified
public boolean isModified()- Returns:
- true if the game state is different from when it was last saved
-
addGameComponent
Add aGameComponent
to the list of objects that will be notified when a game is started/ended -
removeGameComponent
Remove aGameComponent
from the list of objects that will be notified when a game is started/ended -
getGameComponentsEnum
@Deprecated(since="2020-08-06", forRemoval=true) public Enumeration<GameComponent> getGameComponentsEnum()Deprecated, for removal: This API element is subject to removal in a future version.UsegetGameComponents()
instead.- Returns:
- an enumeration of all
GameComponent
objects that have been added to this GameState
-
getGameComponents
- Returns:
- a Collection of all
GameComponent
objects that have been added to this GameState
-
addGameSetupStep
Add aGameSetupStep
-
removeGameSetupStep
Remove aGameSetupStep
-
getUnfinishedSetupSteps
- Returns:
- an iterator of all
GameSetupStep
s that are not yet finished
-
setup
public void setup(boolean gameStarting, boolean gameUpdating)Start a game for updating (via editor). NOTE: This method is not for use in custom code. -
updateDone
public void updateDone()Indicated game update is completed and game is saved. -
isUpdating
public boolean isUpdating() -
setup
public void setup(boolean gameStarting)Start/end a game. Prompt to save if the game state has been modified since last save. InvokeGameComponent.setup(boolean)
on all registeredGameComponent
objects. -
isGameStarted
public boolean isGameStarted()Return true if a game is currently in progress -
loadGame
public void loadGame()Read the game from a savefile. The contents of the file is sent toGameModule.decode(java.lang.String)
and translated into aCommand
, which is then executed. The command read from the file should be that returned bygetRestoreCommand()
. -
saveString
-
checkForOldSaveFile
-
saveGame
public void saveGame()Saves the game to an existing file, or prompts for a new one. -
saveGameAs
public void saveGameAs()Prompts the user for a file into which to save the game -
setModified
public void setModified(boolean modified) -
addPiece
-
getPieceForId
- Returns:
- the
GamePiece
in the current game with the given id
-
removePiece
Remove aGamePiece
from the current game -
getNewPieceId
- Returns:
- a String identifier guaranteed to be different from the id of all GamePieces in the game
- See Also:
GamePiece.getId()
-
loadContinuation
- Throws:
IOException
-
getPieces
Deprecated, for removal: This API element is subject to removal in a future version.UsegetAllPieces()
instead.- Returns:
- an Enumeration of all
GamePiece
s in the game
-
getAllPieces
- Returns:
- a Collection of all
GamePiece
s in the game
-
getRestoreCommand
Return aCommand
that, when executed, will restore the game to its current state. InvokesGameComponent.getRestoreCommand()
on each registeredGameComponent
-
encode
A GameState recognizes instances ofGameState.SetupCommand
- Specified by:
encode
in interfaceCommandEncoder
-
decode
A GameState recognizes instances ofGameState.SetupCommand
- Specified by:
decode
in interfaceCommandEncoder
-
saveGame
- Throws:
IOException
-
loadGameInBackground
-
loadGameInBackground
-
getRestorePiecesCommand
- Returns:
- a Command that, when executed, will add all pieces currently in the game. Used when saving a game.
-
decodeSavedGame
Read a saved game and translate it into a Command. Executing the command will load the saved game.- Parameters:
saveFile
- Sqve file name- Returns:
- Command
- Throws:
IOException
- I/O Exception
-
decodeSavedGame
- Throws:
IOException
-
getSavedGameDirectoryPreference
-
getGameComponents()
instead.