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()
  • Field Details

  • Constructor Details

    • GameState

      public GameState()
  • Method Details

    • addTo

      public void addTo(GameModule mod)
      Expects to be added to a GameModule. Adds New, Load, Close, and Save entries to the File menu of the controls window
    • isModified

      public boolean isModified()
      Returns:
      true if the game state is different from when it was last saved
    • isSaveEnabled

      public boolean isSaveEnabled()
      Returns:
      true if saveGame action is enabled (mainly to detect if logging can start)
    • addGameComponent

      public void addGameComponent(GameComponent theComponent)
      Add a GameComponent to the list of objects that will be notified when a game is started/ended
    • removeGameComponent

      public void removeGameComponent(GameComponent theComponent)
      Remove a GameComponent 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.
      Use getGameComponents() instead.
      Returns:
      an enumeration of all GameComponent objects that have been added to this GameState
    • getGameComponents

      public Collection<GameComponent> getGameComponents()
      Returns:
      a Collection of all GameComponent objects that have been added to this GameState
    • addGameSetupStep

      public void addGameSetupStep(GameSetupStep step)
    • removeGameSetupStep

      public void removeGameSetupStep(GameSetupStep step)
      Remove a GameSetupStep
    • getUnfinishedSetupSteps

      public Iterator<GameSetupStep> getUnfinishedSetupSteps()
      Returns:
      an iterator of all GameSetupSteps that are not yet finished
    • setupRefresh

      public void setupRefresh()
    • 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. Invoke GameComponent.setup(boolean) on all registered GameComponent 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 to GameModule.decode(java.lang.String) and translated into a Command, which is then executed. The command read from the file should be that returned by getRestoreCommand(). This version for binary compatibility w/ old style.
    • loadGame

      public void loadGame(boolean continuation)
      Read the game from a savefile. The contents of the file is sent to GameModule.decode(java.lang.String) and translated into a Command, which is then executed. The command read from the file should be that returned by getRestoreCommand().
      Parameters:
      continuation - if true then do the "old-style" version of load continuation
    • saveString

      protected String saveString()
    • checkForOldSaveFile

      protected boolean checkForOldSaveFile(File f)
    • closeGame

      public void closeGame()
      Closes the game.
    • 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

      public void addPiece(GamePiece p)
      Add a GamePiece to the current game. The GameState keeps track of all GamePieces in the system, regardless of which Map they belong to (if any)
    • getPieceForId

      public GamePiece getPieceForId(String id)
      Returns:
      the GamePiece in the current game with the given id
    • removePiece

      public void removePiece(String id)
      Remove a GamePiece from the current game
    • getNewPieceId

      public String getNewPieceId()
      Returns:
      a String identifier guaranteed to be different from the id of all GamePieces in the game
      See Also:
      GamePiece.getId()
    • loadContinuation

      public void loadContinuation(File f) throws IOException
      Throws:
      IOException
    • getPieces

      @Deprecated(since="2020-08-06", forRemoval=true) public Enumeration<GamePiece> getPieces()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use getAllPieces() instead.
      Returns:
      an Enumeration of all GamePieces in the game
    • getAllPieces

      public Collection<GamePiece> getAllPieces()
      Returns:
      a Collection of all GamePieces in the game
    • getRestoreCommand

      public Command getRestoreCommand()
      Returns:
      a Command that, when executed, will restore the game to its current state -- this command can then be written to a save game file. Invokes GameComponent.getRestoreCommand() on each registered GameComponent, and then creates an AddPiece command for each game piece.
    • encode

      public String encode(Command c)
      A GameState recognizes instances of GameState.SetupCommand
      Specified by:
      encode in interface CommandEncoder
    • decode

      public Command decode(String theCommand)
      A GameState recognizes instances of GameState.SetupCommand
      Specified by:
      decode in interface CommandEncoder
    • saveGameRefresh

      public void saveGameRefresh(ZipArchive archive) throws IOException
      Throws:
      IOException
    • saveGame

      public void saveGame(File f) throws IOException
      Throws:
      IOException
    • loadGameInForeground

      public void loadGameInForeground(File f)
    • loadGameInForeground

      public void loadGameInForeground(String shortName, InputStream in) throws IOException
      Throws:
      IOException
    • loadGameInBackground

      public void loadGameInBackground(File f)
    • loadGameInBackground

      public void loadGameInBackground(String shortName, InputStream in)
    • getRestorePiecesCommand

      public Command getRestorePiecesCommand()
      Returns:
      a Command that, when executed, will add all pieces currently in the game. Used when saving a game. Pieces are grouped by map, and within a map by visual layer.
    • decodeSavedGame

      public Command decodeSavedGame(File saveFile) throws IOException
      Read a saved game and translate it into a Command. Executing the command will load the saved game.
      Parameters:
      saveFile - Save file name
      Returns:
      Command
      Throws:
      IOException - I/O Exception
    • decodeSavedGame

      public Command decodeSavedGame(InputStream in) throws IOException
      Throws:
      IOException
    • getSavedGameDirectoryPreference

      public DirectoryConfigurer getSavedGameDirectoryPreference()