16BPP.net
 
 
 
 
 Previous Page   UDP Pong - Arena   Next Page 

UDP Pong - Arena

This is where the Pong game logic lives on the Server.

 

 

At the top of the class we have our member variables.  State needs to be encased in a ThreadSafe since it will be accessed by multiple threads.  The Paddle objects are contained for us in the PlayerInfo classes.  We store a reference to the running Server instance.  We also have our own message queue for Packets that have been dispatched to us.

TryAddPlayer() is called when we get a new Client connecting to the Server.  It will only add two players to this Arena.  The function will return true if the Client was added successfully, in any other case it will return false.

Start() will shift the State of the Arena to where it can accept new players and will begin running the underlying _arenaThread which runs the _arenaRun() method.

_arenaRun() is the heart of this class.  It is another looping function.  In the beginning of the loop it will check for new NetworkMessages and will make decisions based upon it's current State.  While we are waiting for two players, it will try to handle a connection setup.  Once two players have connected it will send them the GameStart Packets.  Once the GameStart Packets have been Ack'd by the Clients it will then shift the State to InGame, start the _gameTimer, and begin processing/handling PaddlePosition & GameState Packets.  In case a Bye message has been sent by one of the clients it will then stop the loop and notify the other Client the match is over.  At the end of the loop it will check for player time outs or see if a stop has been requested by the Server.  Once the loop has been exited it will queue up a ByePacket to any connected clients and notify the Server the game is done.

JoinThread() will give the underlying _arenaThread 1/10 of a second to complete execution.

EnqueMessage() is called by the PongServer to tell the Arena it's gotten something from one of it's Clients.

_sendTo() sets a Packet to be sent to a Client.  It will record the time when we wanted to send it.

_timedOut() is used to check the last time we've received a Packet from them versus how long a timeout should be (20 seconds in our case)

_handleConnection() is what instantiates the connection between the Client and the Server (though it's stored in the Arena code).  It will assign a Paddle to a Client and respond to Heartbeat messages.

_sendAcceptJoin() is a small helper method to tell a Client which Paddle they are.

_notifyGameStart() will tell a Client a game has started, and will retry every time the timeout has been reached.

_sendGameState() will tell a Client about the current state of the match.  This is the Paddle positions, scores, and Ball position.

_playSoundEffect() will queue up a PlaySoundEffectPacket to a Client.

_handlePaddleUpdate() will look at a PaddlePosition message from a Client and update it's Paddle's Y position.

The next two methods are for collisions.  _checkForBallCollision() will do all the math to see what the Ball is hitting something, and take an appropriate action (bounce, score, sound, etc.).  _processBallHitWithPaddle() is used to alter the Ball's velocity for Ball-Paddle collisions.

 Previous Page   UDP Pong - Arena   Next Page