Package rea.gaming

Class GameInstance


public class GameInstance extends GameEventSource
A single instance of a role playing game with given gameplay and players. The players are added to the instance with addPlayer(Character). which return a Player that can be used to identify the character in the game. The game can be started with startPlayingGame() and ended with endPlayingGame(). If the number of players is less than the minimum number of players, the game cannot be started. but if it reaches the maximum number of players, it is automatically started. This class is also responsible for executing commands on the game, such as moving, picking up items, etc. through the executeCommand(Player, Action, Object) method. This public method is invoked by players and delegates in package methods specific to each action. Action commands report updates by sending events to the players. Players and events listeners are managed by methods inherited from GameEventSource.
Author:
José Paulo Leal jpleal@dc.fc.up.pt
  • Constructor Details

    • GameInstance

      public GameInstance(Gameplay gameplay)
      Create a game instance with a given gameplay
      Parameters:
      gameplay - for the game instance
  • Method Details

    • getGameMap

      public GameMap getGameMap()
      The game map used by this game instance. Differentes instances of the same game will have different map instances, altough similar in structure and content.
      Returns:
      game map
    • getName

      public String getName()
      Get the name of the game
      Returns:
      name of the game
    • getPlayerCount

      public int getPlayerCount()
      Number of players currently in the game
      Returns:
      number of players currently in the game
    • getCurrentStage

      public GameStage getCurrentStage()
      Get the current stage of the game
      Returns:
      current stage of the game
    • addPlayer

      public Player addPlayer(Character character)
      Add a character to the game if it is possible, i.e. the game is in the GameStage.CREATED stage and the number of players is less than the maximum number of players. If the character can be added, it is added to the game map and the game is broadcasted and an @{link Player} that can be used to identify the character in the game is returned. Otherwise, if the character cannot be added, null is returned.
      Parameters:
      character - instance of Character to be added
      Returns:
      an Player if it was successfully added, or null otherwise
    • playerReady

      public void playerReady(Player player)
      The player indicates to be ready to start the game. It was already added to the game and registered handlers for game events. If maximum number of players is reached, the game is automatically started. A game can also be started by calling startPlayingGame().
      Parameters:
      player - player ready to start the game
    • getAvatars

      public Set<Avatar> getAvatars()
      Get avatars for players in this game
      Returns:
      set of avatar
    • canDelete

      public boolean canDelete()
      Check if the game can be deleted, i.e. if it is in the GameStage.CREATED stage and there are no players yet, or if it is in the GameStage.ENDED stage.
      Returns:
      true if the game can be deleted, false otherwise.
    • canStart

      public boolean canStart()
      Check if the game can start, i.e. if it is in the GameStage.CREATED stage and there are enough players.
      Returns:
      true if the game can start, false otherwise.
    • getPlayingSince

      public Date getPlayingSince()
      Get time when the game started.
      Returns:
      time when the game started, or null if the game has not started yet.
    • getPlayingUntil

      public Date getPlayingUntil()
      Get the time when the game ended.
      Returns:
      time when the game ended, or null if the game has not ended yet.
    • isComplete

      public boolean isComplete()
      Is game complete?
      Returns:
      true if complete;false otherwise.
    • startPlayingGame

      public void startPlayingGame()
      Start the game, if it is not currently playing. This is only possible if the game is in the GameStage.CREATED stage. Otherwise, a non-checkable exception is raised. The game change is broadcasted to all players and the start place scene is multicast to the players.
      Implementation Note:
      (@link IllegalStateException) raised if game cannot be created.
    • isNotPlayingYet

      public boolean isNotPlayingYet()
      Has the game started yet?
      Returns:
      true if the game has not started yet, false otherwise.
    • endPlayingGame

      public void endPlayingGame()
      End the game, if it is currently playing, otherwise raise an IllegalStateException.
    • executeCommand

      public String executeCommand(Player player, Action action, Object object)
      Command execution by an identified player. Commands are Action on an object (e.g. a Position to move to, a Item to pick up, etc.). The command is executed if the game is in the GameStage.PLAYING stage (Otherwise, an IllegalStateException exception is raised.), and the player is in the game. (Otherwise, an IllegalArgumentException exception is raised.) Errors reported by commands are returned as a string. A successful command execution returns null.
      Parameters:
      player - player executing the command
      action - to be executed
      object - of the action
      Returns:
      status of the command execution, or null if the command was executed successfully.
    • executeLook

      String executeLook(Character character, Object object)
      Look at an object in the game. Its description is sent to the player.
      Parameters:
      character - looking at the object
      object - to be looked at
      Returns:
      null if successful, otherwise an error message
      Implementation Note:
      The description is sent to the player as a message update event.
    • executeTalk

      String executeTalk(Character character, Object object)
      Talk to everyone on the same place.
      Parameters:
      character - doing the talking
      object - what she told
      Returns:
      null if successful, otherwise an error message
      Implementation Note:
      The message is sent to the players in the same place as a message update event.
    • executeHold

      String executeHold(Character character, Object object)
      Hold an item in the inventory. An item need to be held to be used.
      Parameters:
      character - holding the item
      object - item being held
      Returns:
      null if successful. otherwise an error message
      Implementation Note:
      The inventory is updated by sending an inventory update event.
    • executeUse

      String executeUse(Character character, Object object)
      Use the holding item on an object in the game.
      Parameters:
      character - using the hold item
      object - on which its used
      Returns:
      null if successful. otherwise an error message
      Implementation Note:
      The game map is updated by sending a scene update event.
    • executePick

      String executePick(Character character, Object object)
      Pick as item in the place.
      Parameters:
      character - picking an object
      object - item being picked
      Returns:
      null if successful. otherwise an error message
      Implementation Note:
      The game map is updated by sending a scene update event., The inventory is updated by sending an inventory update event.
    • executeDrop

      String executeDrop(Character character, Object object)
      Drop an item in the inventory.
      Parameters:
      character - dropping an item
      object - item being dropped
      Returns:
      null if successful. otherwise an error message
      Implementation Note:
      The game map is updated by sending a scene update event., The inventory is updated by sending an inventory update event.
    • executeMove

      String executeMove(Character character, Object object)
      Moving forward on the game map to another place or position
      Parameters:
      character - moving to a different place (through a passage) or position
      object - passage or position where the character is moved
      Returns:
      null if successful. otherwise an error message
      Implementation Note:
      The game map is updated by sending a scene update event for the current place and the new place.
    • executeBack

      String executeBack(Character character, Object ignored)
      Backtracking to the previous place on the game map
      Parameters:
      character - backtracking
      ignored - argument kep for consistency
      Returns:
      null if successful. otherwise an error message
      Implementation Note:
      The game map is updated by sending a scene update event for the current place and the new place.