void AddSpaceshipToClient(uint clientID) { if (otherClientsTransformsByID.ContainsKey(clientID)) { return; } int playerIndex = (spaceshipController.ObjectID == 0) ? 1 : 0; EnemySpaceship enemySpaceship = CreateSpaceship(enemySpaceshipPrefabs[playerIndex], playerSpawnPoints[playerIndex]) as EnemySpaceship; EnemySpaceshipTransformData enemySpaceshipTransformData; enemySpaceshipTransformData.spaceship = enemySpaceship; enemySpaceshipTransformData.previousTransform = null; enemySpaceshipTransformData.lastTransform = null; TransformPacket transformPacket = new TransformPacket(); TransformData transformData; transformData.flags = (int)TransformFlag.None; transformData.inputSequenceID = 0; transformData.position = null; transformData.rotation = null; transformData.scale = null; transformPacket.Payload = transformData; PacketsManager.Instance.SendPacket(transformPacket, null, UdpNetworkManager.Instance.GetSenderID(), spaceshipController.ObjectID, reliable: true); otherClientsTransformsByID.Add(clientID, enemySpaceshipTransformData); }
void UpdateServerClients() { using (var dicIterator = inputsByClientID.GetEnumerator()) while (dicIterator.MoveNext()) { TransformPacket transformPacket = new TransformPacket(); TransformData transformData; EnemySpaceship enemySpaceship = dicIterator.Current.Value.spaceship; Vector3 accumulatedInputs = Vector3.zero; List <InputPacket> inputs = dicIterator.Current.Value.inputs; if (inputs.Count > 0) { for (int i = 0; i < inputs.Count; i++) { float[] movement = inputs[i].Payload.movement; Vector3 inputVector = new Vector3(movement[0], movement[1], movement[2]); accumulatedInputs += inputVector; } enemySpaceship.Move(accumulatedInputs); float[] spaceshipPosition = { enemySpaceship.transform.position.x, enemySpaceship.transform.position.y, enemySpaceship.transform.position.z, }; transformData.flags = (int)TransformFlag.PositionBit | (int)TransformFlag.InputSequenceIDBit; transformData.inputSequenceID = inputs[inputs.Count - 1].Payload.sequenceID; transformData.position = spaceshipPosition; transformData.rotation = null; transformData.scale = null; transformPacket.Payload = transformData; inputs.Clear(); PacketsManager.Instance.SendPacket(transformPacket, null, dicIterator.Current.Key, enemySpaceship.ObjectID); } } }
void OnDataReceivedByServer(ushort userPacketTypeIndex, uint senderID, Stream stream) { if (userPacketTypeIndex == (ushort)UserPacketType.Input) { InputPacket inputPacket = new InputPacket(); inputPacket.Deserialize(stream); inputsByClientID[senderID].inputs.Add(inputPacket); } if (userPacketTypeIndex == (ushort)UserPacketType.ShotInput) { ShotInputPacket shotInputPacket = new ShotInputPacket(); shotInputPacket.Deserialize(stream); using (var dicIterator = enemySpaceshipsByClientID.GetEnumerator()) while (dicIterator.MoveNext()) { EnemySpaceship enemySpaceship = dicIterator.Current.Value; Vector3 hitPos = new Vector3(shotInputPacket.Payload.hitPosition[0], shotInputPacket.Payload.hitPosition[1], shotInputPacket.Payload.hitPosition[2]); if (enemySpaceship.transform.position == hitPos) { NotificationPacket notificationPacket = new NotificationPacket(); NotificationData notificationData; uint receiverID = dicIterator.Current.Key; notificationData.playerStatus = (uint)PlayerStatus.Dead; notificationPacket.Payload = notificationData; PacketsManager.Instance.SendPacket(notificationPacket, UdpConnectionManager.Instance.GetClientIP(receiverID), senderID, enemySpaceship.ObjectID, reliable: true); } } } }
void UpdateOtherClients() { using (var dicIterator = otherClientsTransformsByID.GetEnumerator()) while (dicIterator.MoveNext()) { EnemySpaceshipTransformData enemySpaceshipTransformData = dicIterator.Current.Value; if (enemySpaceshipTransformData.previousTransform != null) { EnemySpaceship enemySpaceship = enemySpaceshipTransformData.spaceship; float[] lastPositionValues = enemySpaceshipTransformData.lastTransform.Payload.position; float[] previousPositionValues = enemySpaceshipTransformData.previousTransform.Payload.position; Vector3 lastPosition = new Vector3(lastPositionValues[0], lastPositionValues[1], lastPositionValues[2]); Vector3 previousPosition = new Vector3(previousPositionValues[0], previousPositionValues[1], previousPositionValues[2]); if ((lastPosition - enemySpaceship.transform.position).sqrMagnitude > NeglibiglePosDiffSqrMagnitude) { Vector3 direction = (lastPosition - previousPosition).normalized; enemySpaceship.Move(direction); } } } }