Package rea.gaming
Class GameInstance
java.lang.Object
rea.gaming.GameEventSource
rea.gaming.GameInstance
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
-
Field Summary
Fields inherited from class rea.gaming.GameEventSource
gameChanged, inventoryUpdate, messageUpdate, players, sceneUpdate
-
Constructor Summary
ConstructorsConstructorDescriptionGameInstance
(Gameplay gameplay) Create a game instance with a given gameplay -
Method Summary
Modifier and TypeMethodDescriptionAdd a character to the game if it is possible, i.e.boolean
Check if the game can be deleted, i.e.boolean
canStart()
Check if the game can start, i.e.void
End the game, if it is currently playing, otherwise raise anIllegalStateException
.(package private) String
executeBack
(Character character, Object ignored) Backtracking to the previous place on the game mapexecuteCommand
(Player player, Action action, Object object) Command execution by an identified player.(package private) String
executeDrop
(Character character, Object object) Drop an item in the inventory.(package private) String
executeHold
(Character character, Object object) Hold an item in the inventory.(package private) String
executeLook
(Character character, Object object) Look at an object in the game.(package private) String
executeMove
(Character character, Object object) Moving forward on the game map to another place or position(package private) String
executePick
(Character character, Object object) Pick as item in the place.(package private) String
executeTalk
(Character character, Object object) Talk to everyone on the same place.(package private) String
executeUse
(Character character, Object object) Use the holding item on an object in the game.Get avatars for players in this gameGet the current stage of the gameThe game map used by this game instance.getName()
Get the name of the gameint
Number of players currently in the gameGet time when the game started.Get the time when the game ended.boolean
Is game complete?boolean
Has the game started yet?void
playerReady
(Player player) The player indicates to be ready to start the game.void
Start the game, if it is not currently playing.Methods inherited from class rea.gaming.GameEventSource
addGameChangedListener, addInventoryUpdateListener, addMessageUpdateListener, addSceneUpdateListener, broadcastGameChanged, multicastMessageUpdate, multicastSceneUpdate, unicastInventoryUpdate, unicastMessageUpdate
-
Constructor Details
-
GameInstance
Create a game instance with a given gameplay- Parameters:
gameplay
- for the game instance
-
-
Method Details
-
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
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
Get the current stage of the game- Returns:
- current stage of the game
-
addPlayer
Add a character to the game if it is possible, i.e. the game is in theGameStage.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. -
playerReady
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 callingstartPlayingGame()
.- Parameters:
player
- player ready to start the game
-
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 theGameStage.CREATED
stage and there are no players yet, or if it is in theGameStage.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 theGameStage.CREATED
stage and there are enough players.- Returns:
true
if the game can start,false
otherwise.
-
getPlayingSince
Get time when the game started.- Returns:
- time when the game started,
or
null
if the game has not started yet.
-
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 theGameStage.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 anIllegalStateException
. -
executeCommand
Command execution by an identified player. Commands areAction
on an object (e.g. aPosition
to move to, aItem
to pick up, etc.). The command is executed if the game is in theGameStage.PLAYING
stage (Otherwise, anIllegalStateException
exception is raised.), and the player is in the game. (Otherwise, anIllegalArgumentException
exception is raised.) Errors reported by commands are returned as a string. A successful command execution returnsnull
.- Parameters:
player
- player executing the commandaction
- to be executedobject
- of the action- Returns:
- status of the command execution, or
null
if the command was executed successfully.
-
executeLook
Look at an object in the game. Its description is sent to the player.- Parameters:
character
- looking at the objectobject
- 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
Talk to everyone on the same place.- Parameters:
character
- doing the talkingobject
- 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
Hold an item in the inventory. An item need to be held to be used.- Parameters:
character
- holding the itemobject
- item being held- Returns:
null
if successful. otherwise an error message- Implementation Note:
- The inventory is updated by sending an inventory update event.
-
executeUse
Use the holding item on an object in the game.- Parameters:
character
- using the hold itemobject
- 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
Pick as item in the place.- Parameters:
character
- picking an objectobject
- 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
Drop an item in the inventory.- Parameters:
character
- dropping an itemobject
- 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
Moving forward on the game map to another place or position- Parameters:
character
- moving to a different place (through a passage) or positionobject
- 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
Backtracking to the previous place on the game map- Parameters:
character
- backtrackingignored
- 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.
-