SampleServer

From Gardenwiki

Jump to: navigation, search

Contents

SampleManager

This class extends GameManager and is the main entity that lives on the server and manages your game's state. It interacts with the players by making changes to the game object and it manages the flow of the game. The following methods are called as the game procedes through its normal lifetime:

startGame()

This is called automatically when all of the players have arrived in the game "room" and it triggers the normal starting procedure.

  • gameWillStart() - This is the method you would override to make modifications to the game object prior to the game being started. This might include generating a new board and setting it in the game object or resetting the player's scores to zero or whatever is appropriate to your game.
  • gameDidStart() - This is called after the game starting events have been dispatched to the clients. If your game involved taking turns or reacting to a timer or some other periodic in-game activity, this is where you would begin that process. Indeed the TurnGameManager (which we don't document here but is useful for turn-based games) makes use of this method to start the turn-taking process.

endGame()

This method is called by your code when some game-ending condition has taken place. Perhaps all the cards or tiles were used up, or maybe a timer has finally expired. Whatever the case, your code calls this method which triggers the standard game ending process.

  • gameWillEnd() - This is a method you can override to take care of any final processing before the game transitions to the GameObject.GAME_OVER state. In general, this method is used less often than gameDidEnd() (documented next) as that's where you would unregister timers and do whatever other cleanup that might need to be done after the game ends. However, for completeness and in the off chance that something needs to be done before the game actually ends, this method exists.
  • gameDidEnd() - This method is called after the event has been dispatched to the client letting them know that the game is over. This is a good place to compute final scores, assign a winner and generally wrap the game up. It should be noted that it is possible for gameWillEnd() and gameDidEnd() to be called even though your code did not call endGame() explicitly. If all players involved in the game leave the game room, the game will be cancelled (it will transition to the CANCELLED state instead of the GAME_OVER state) and the normal game-ending callbacks will be called to clean up after the game. Your game ending code should check the GameObject.state when being called and avoid doing things like computing scores and whatnot if the game was cancelled.

resetGame()

In the event that you wish to restart a game without actually ending it and triggering the standard ending procedures, it is possible to "reset" a game which transitions through the start-up process again (gameWillStart() and gameDidStart() will be called as in a normal startup, but gameWillEnd() and gameDidEnd() will not be called).

  • gameWillReset() - This is the method you would override to clear anything out that needed to be cleared out before your game was restarted if you want to make use of the game "reset" mechanism.

Back to tutorial.

Personal tools