/// <summary> /// Reconciliate the client with the server data /// </summary> private void Reconciliate() { if (reconciliationInfoList.Count() > 0) { //Get the position of the client at this specific frame PlayerSyncMessageModel clientInfo = reconciliationInfoList.Where(i => i.serverTick == lastReceivedMessage.serverTick).FirstOrDefault(); //If there is more than 50 tick that the ball has not been updated depending to the server position if (reconciliationInfoList.Count() > 50) { rigidbodyReference.velocity = lastReceivedMessage.velocity; rigidbodyReference.transform.position = lastReceivedMessage.position; clientTick = lastReceivedMessage.serverTick; clientInfo = lastReceivedMessage; } if (!Equals(clientInfo, null)) { //Check for position divergence if (Vector3.Distance(clientInfo.position, lastReceivedMessage.position) >= 0.05f) { //Update data rigidbodyReference.velocity = lastReceivedMessage.velocity; rigidbodyReference.transform.position = lastReceivedMessage.position; } //Empty the list reconciliationInfoList.Clear(); } } }
//////////////////////////////////prototyping////////////////////////////////////////////////////////////// /// <summary> /// This function recieves a movement message from a client and sends it to the rest of the clients. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == NetworkTags.InGame.REP_SYNC_POS) { //Get message data PlayerSyncMessageModel syncMessage = e.GetMessage().Deserialize <PlayerSyncMessageModel>(); //for every client other than the one that sent the message foreach (IClient client in clients.Where(x => x != e.Client)) { PlayerSyncMessageModel movementMessageData = new PlayerSyncMessageModel { networkID = syncMessage.networkID, serverTick = currentTick, position = syncMessage.position, velocity = syncMessage.velocity }; //create the message using (Message m = Message.Create( NetworkTags.InGame.REP_SYNC_POS, //Tag movementMessageData) //Data ) { //Send the message in TCP mode (Reliable) client.SendMessage(m, SendMode.Reliable); } } } } }
/// <summary> /// This function receives messages meant to move the representations with their controllable player counterparts. /// The function distinguishes between messages for different represenations using the networkID /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UpdateFromServerState(object sender, DarkRift.Client.MessageReceivedEventArgs e) { //if we can a message with the tag for moving the representation if (e.Tag == NetworkTags.InGame.REP_SYNC_POS) { //Get message data PlayerSyncMessageModel syncMessage = e.GetMessage().Deserialize <PlayerSyncMessageModel>(); //If this is the first time we receive the message if (Object.Equals(null, lastReceivedMessage) && id == syncMessage.networkID) { //Update data rigidbodyReference.velocity = syncMessage.velocity; rigidbodyReference.transform.position = syncMessage.position; clientTick = syncMessage.serverTick; lastReceivedMessage = syncMessage; } //If the message regards this object and is older than the previous one if (id == syncMessage.networkID && syncMessage.serverTick > lastReceivedMessage.serverTick) { lastReceivedMessage = syncMessage; } } }
private void SendBallPositionToClients() { //Create the message PlayerSyncMessageModel bouncyBallPositionMessageData = new PlayerSyncMessageModel { networkID = base.id, serverTick = 0, position = rigidbodyReference.transform.position, velocity = rigidbodyReference.velocity }; //create the message using (Message m = Message.Create( NetworkTags.InGame.REP_SYNC_POS, //Tag bouncyBallPositionMessageData) //Data ) { Client.SendMessage(m, SendMode.Reliable); } }
/// <summary> /// Send ball server position to all clients /// </summary> private void SendBallPositionToClients() { //Create the message PlayerSyncMessageModel bouncyBallPositionMessageData = new PlayerSyncMessageModel { networkID = base.id, serverTick = GameServerManager.instance.currentTick, position = rigidbodyReference.transform.position, velocity = rigidbodyReference.velocity }; //create the message using (Message m = Message.Create( NetworkTags.InGame.BOUNCY_BALL_SYNC_POS, //Tag bouncyBallPositionMessageData) //Data ) { foreach (IClient client in GameServerManager.instance.serverReference.Server.ClientManager.GetAllClients()) { client.SendMessage(m, SendMode.Reliable); } } }