void OnDataReceived(ushort userPacketTypeIndex, uint senderID, Stream stream) { if (userPacketTypeIndex != (ushort)UserPacketType.Transform || senderID != UdpNetworkManager.Instance.GetSenderID()) { return; } TransformPacket transformPacket = new TransformPacket(); transformPacket.Deserialize(stream); int lastSequenceIDProcessedByServer = inputsSent.FindIndex(ip => ip.Payload.sequenceID == transformPacket.Payload.inputSequenceID); if (lastSequenceIDProcessedByServer != -1) { float[] positionReceived = transformPacket.Payload.position; Vector3 serverAuthoritativePosition = new Vector3(positionReceived[0], positionReceived[1], positionReceived[2]); Vector3 accumulatedMovement = Vector3.zero; for (int i = lastSequenceIDProcessedByServer + 1; i < inputsSent.Count; i++) { float[] movement = inputsSent[i].Payload.movement; Vector3 movementVector = new Vector3(movement[0], movement[1], movement[2]); accumulatedMovement += movementVector * Speed * Time.fixedDeltaTime; } inputsSent.RemoveRange(0, lastSequenceIDProcessedByServer); ReconcilePosition(serverAuthoritativePosition, accumulatedMovement); } }
void OnDataReceivedByClient(ushort userPacketTypeIndex, uint senderID, Stream stream) { if (senderID == UdpNetworkManager.Instance.GetSenderID()) { return; } if (userPacketTypeIndex == (uint)UserPacketType.Transform) { AddSpaceshipToClient(senderID); TransformPacket transformPacket = new TransformPacket(); transformPacket.Deserialize(stream); EnemySpaceshipTransformData enemySpaceshipTransformData = otherClientsTransformsByID[senderID]; if (enemySpaceshipTransformData.lastTransform == null || transformPacket.Payload.inputSequenceID > enemySpaceshipTransformData.lastTransform.Payload.inputSequenceID) { enemySpaceshipTransformData.previousTransform = enemySpaceshipTransformData.lastTransform; enemySpaceshipTransformData.lastTransform = transformPacket; otherClientsTransformsByID[senderID] = enemySpaceshipTransformData; } } if (userPacketTypeIndex == (uint)UserPacketType.Notification) { NotificationPacket notificationPacket = new NotificationPacket(); notificationPacket.Deserialize(stream); if (notificationPacket.Payload.playerStatus == (uint)PlayerStatus.Dead) { spaceshipController.Die(); } } }