Main Page Modules Namespaces C++ Classes Compound List File List Index
TNL: TNLTest::Player Class Reference

# TNLTest::Player Class Reference

`#include <testGame.h>`

Inheritance diagram for TNLTest::Player:

## Detailed Description

The Player is an example of an object that can move around and update its position to clients in the system.

Each client in the server controls a Player instance constructed for it upon joining the game. Client users click on the game window, which generates an event to the server to move that client's player to the desired point.

## Public Types

enum | PlayerType { PlayerTypeAI, PlayerTypeAIDummy, PlayerTypeClient, PlayerTypeMyClient
}
| Enumeration of possible player types in the game. More...
enum | MaskBits { InitialMask = (1 << 0), PositionMask = (1 << 1) }
| Mask bits used for determining which object states need to be updated to all the clients. More...

## Public Member Functions

| Player (PlayerType pt=PlayerTypeClient)
| Player constructor, assigns a random position in the playing field to the player, and if it is AI controlled will pick a first destination point.
| ~Player ()
| Player destructor removes the player from the game.
void | performScopeQuery (TNL::GhostConnection *connection)
| performScopeQuery is called to determine which objects are "in scope" for the client that controls this Player instance.
TNL::U32 | packUpdate (TNL::GhostConnection *connection, TNL::U32 updateMask, TNL::BitStream *stream)
| packUpdate writes the Player's ghost update from the server to the client.
void | unpackUpdate (TNL::GhostConnection *connection, TNL::BitStream *stream)
| unpackUpdate reads the data the server wrote in packUpdate from the packet.
void | serverSetPosition (Position startPos, Position endPos, TNL::F32 t, TNL::F32 tDelta)
| serverSetPosition is called on the server when it receives notice from a client to change the position of the player it controls.
void | update (TNL::F32 timeDelta)
| Move this object along its path.
void | onGhostAvailable (TNL::GhostConnection *theConnection)
| onGhostAvailable is called on the server when it knows that this Player has been constructed on the specified client as a result of being "in scope".
bool | onGhostAdd (TNL::GhostConnection *theConnection)
| onGhostAdd is called for every NetObject on the client after the ghost has been constructed and its initial unpackUpdate is called.
void | addToGame (TestGame *theGame)
| Adds this Player to the list of Player instances in the specified game.
| TNL_DECLARE_RPC (rpcPlayerWillMove,(TNL::StringPtr testString))
| rpcPlayerWillMove is used in TNLTest to demonstrate ghost->parent NetObject RPCs.
| TNL_DECLARE_RPC (rpcPlayerDidMove,(TNL::Float< 6 > x, TNL::Float< 6 > y))
| rpcPlayerDidMove is used in TNLTest to demostrate a broadcast RPC from the server to all of the ghosts on clients scoping this object.
| TNL_DECLARE_RPC (rpcPlayerIsInScope,(TNL::Float< 6 > x, TNL::Float< 6 > y))
| rpcPlayerIsInScope is the RPC method called by onGhostAvailable to demonstrate targeted NetObject RPCs.
| TNL_DECLARE_CLASS (Player)
| This macro invocation declares the Player class to be known to the TNL class management system.

## Data Fields

Position | startPos
Position | endPos
| All players move along a line from startPos to endPos.
Position | renderPos
| Position at which to render the player - computed during update().
TNL::F32 | t
| Parameter of how far the player is along the line from startPos to endPos.
TNL::F32 | tDelta
| Change in t per second (ie, velocity).
TestGame * | game
| The game object this player is associated with.
PlayerType | myPlayerType
| What type of player is this?

## Private Types

typedef TNL::NetObject | Parent

## Member Typedef Documentation

typedef TNL::NetObject TNLTest::Player::Parent`[private]`

Reimplemented from TNL::NetObject.

## Member Enumeration Documentation

enum TNLTest::Player::PlayerType

Enumeration of possible player types in the game.

Enumerator:

  PlayerTypeAI | The player is an AI controlled player on the server.
  PlayerTypeAIDummy | The player is the ghost of an AI controlled player on the server.
  PlayerTypeClient | The player is owned by a client. If this is a ghost, it is owned by a user other than this client.
  PlayerTypeMyClient | The player controlled by this client.

enum TNLTest::Player::MaskBits

Mask bits used for determining which object states need to be updated to all the clients.

Enumerator:

  InitialMask | This mask bit is never set explicitly, so it can be used for initialization data.
  PositionMask | This mask bit is set when the position information changes on the server.

## Constructor & Destructor Documentation

TNLTest::Player::Player | ( | PlayerType | pt = `PlayerTypeClient` | ) |

Player constructor, assigns a random position in the playing field to the player, and if it is AI controlled will pick a first destination point.

TNLTest::Player::~Player | ( |  | ) |

Player destructor removes the player from the game.

## Member Function Documentation

void TNLTest::Player::performScopeQuery | ( | TNL::GhostConnection * | connection | ) | `[virtual]`

performScopeQuery is called to determine which objects are "in scope" for the client that controls this Player instance.

In the TNLTest program, all objects in a circle of radius 0.25 around the scope object are considered to be in scope.

Reimplemented from TNL::NetObject.

TNL::U32 TNLTest::Player::packUpdate | ( | TNL::GhostConnection * | connection,
|  | TNL::U32 | updateMask,
|  | TNL::BitStream * | stream |
| ) |  |  | `[virtual]`

packUpdate writes the Player's ghost update from the server to the client.

This function takes advantage of the fact that the InitialMask is only ever set for the first update of the object to send once-only state to the client.

Reimplemented from TNL::NetObject.

void TNLTest::Player::unpackUpdate | ( | TNL::GhostConnection * | connection,
|  | TNL::BitStream * | stream |
| ) |  |  | `[virtual]`

unpackUpdate reads the data the server wrote in packUpdate from the packet.

Reimplemented from TNL::NetObject.

void TNLTest::Player::serverSetPosition | ( | Position | startPos,
|  | Position | endPos,
|  | TNL::F32 | t,
|  | TNL::F32 | tDelta |
| ) |  |  |

serverSetPosition is called on the server when it receives notice from a client to change the position of the player it controls.

serverSetPosition will call setMaskBits(PositionMask) to notify the network system that this object has changed state.

void TNLTest::Player::update | ( | TNL::F32 | timeDelta | ) |

Move this object along its path.

If it hits the end point, and it's an AI, it will generate a new destination.

void TNLTest::Player::onGhostAvailable | ( | TNL::GhostConnection * | theConnection | ) | `[virtual]`

onGhostAvailable is called on the server when it knows that this Player has been constructed on the specified client as a result of being "in scope".

In TNLTest this method call is used to test the per-ghost targeted RPC functionality of NetObject subclasses by calling rpcPlayerIsInScope on the Player ghost on the specified connection.

Reimplemented from TNL::NetObject.

bool TNLTest::Player::onGhostAdd | ( | TNL::GhostConnection * | theConnection | ) | `[virtual]`

onGhostAdd is called for every NetObject on the client after the ghost has been constructed and its initial unpackUpdate is called.

A return value of false from this function would indicate than an error had occured, notifying the network system that the connection should be terminated.

Reimplemented from TNL::NetObject.

void TNLTest::Player::addToGame | ( | TestGame * | theGame | ) |

Adds this Player to the list of Player instances in the specified game.

TNLTest::Player::TNL_DECLARE_RPC | ( | rpcPlayerWillMove | ,
|  | (TNL::StringPtr testString) |  |
| ) |  |  |

rpcPlayerWillMove is used in TNLTest to demonstrate ghost->parent NetObject RPCs.

TNLTest::Player::TNL_DECLARE_RPC | ( | rpcPlayerDidMove | ,
|  | (TNL::Float< 6 > x, TNL::Float< 6 > y) |  |
| ) |  |  |

rpcPlayerDidMove is used in TNLTest to demostrate a broadcast RPC from the server to all of the ghosts on clients scoping this object.

It also demonstrates the usage of the Float<> template argument, in this case using 6 bits for each X and Y position. Float arguments to RPCs are between 0 and 1.

TNLTest::Player::TNL_DECLARE_RPC | ( | rpcPlayerIsInScope | ,
|  | (TNL::Float< 6 > x, TNL::Float< 6 > y) |  |
| ) |  |  |

rpcPlayerIsInScope is the RPC method called by onGhostAvailable to demonstrate targeted NetObject RPCs.

onGhostAvailable uses the TNL_RPC_CONSTRUCT_NETEVENT macro to construct a NetEvent from the RPC invocation and then posts it only to the client for which the object just came into scope.

TNLTest::Player::TNL_DECLARE_CLASS | ( | Player |  | ) |

This macro invocation declares the Player class to be known to the TNL class management system.

## Field Documentation

Position TNLTest::Player::startPos

Position TNLTest::Player::endPos

All players move along a line from startPos to endPos.

Position TNLTest::Player::renderPos

Position at which to render the player - computed during update().

TNL::F32 TNLTest::Player::t

Parameter of how far the player is along the line from startPos to endPos.

TNL::F32 TNLTest::Player::tDelta

Change in t per second (ie, velocity).

TestGame* TNLTest::Player::game

The game object this player is associated with.

PlayerType TNLTest::Player::myPlayerType

What type of player is this?