SampleObject

From Gardenwiki

Jump to: navigation, search

SampleObject

This class extends GameObject and defines the data that will be shared between the clients and server when playing your game. The standard game object defines a few pieces of information which are used to manage the flow of the game. Here is an excerpt:

public class GameObject extends PlaceObject
{
    /** The game state, one of PRE_GAME, IN_PLAY, GAME_OVER, or CANCELLED. */
    public int state = PRE_GAME;

    /** The usernames of the players involved in this game. */
    public Name[] players;
}

state transitions from PRE_GAME to IN_PLAY and then to either GAME_OVER or CANCELLED. As the events arrive indicating a change in this attribute, the game manager and controller call methods appropriate to each state (for example, gameDidStart() or gameDidEnd() which are described later).

players is an array that contains the usernames of all of the players in the game. This is generally used to ensure that events received are in fact received from game participants and at the right times (in the case of turn-based games, for example) and not malicious users who hack their client and try to do funny things. The client user interface generally also uses this to display the names of the players and configure the UI depending on whether or not the client is a player or is just watching.

Additional information specific to your game would also be contained in this object. For example, one might have an object representing the board and a distributed set containing the pieces that are on the board. Perhaps an array defining the scores for each player. For example:

public class MyGameObject extends GameObject
{
    /** Contains Piece objects for all the pieces in play. */
    public DSet<Piece> pieces;

    /** Contains the current score for each player. */
    public int[] score;
}

Back to tutorial or on to SampleManager.

Personal tools