protected override void OnReceiveDataEvent(uint packetID, ushort packetTypeID, Stream stream) { //Debug.Log("Applying velocity received to " + gameObject.name); switch (packetTypeID) { case (ushort)UserPacketType.Velocity: VelocityPacket velocityPacket = new VelocityPacket(); velocityPacket.Deserialize(stream); //Debug.Log("Applying velocity received to " + gameObject.name); Vector3 velocityReceived = new Vector3( velocityPacket.payload.velocity.x, velocityPacket.payload.velocity.y, velocityPacket.payload.velocity.z ); //rb.position += velocityReceived; transform.Translate(velocityReceived, Space.Self); if (NetworkManager.Instance.isServer) { NetworkMessageManager.Instance.SendPosition(transform.position, (uint)objectID); } break; case (ushort)UserPacketType.Position: PositionPacket positionPacket = new PositionPacket(); positionPacket.Deserialize(stream); Vector3 position = new Vector3( positionPacket.payload.position.x, positionPacket.payload.position.y, positionPacket.payload.position.z ); //rb.position = position; transform.position = position; break; case (ushort)UserPacketType.Shoot: Shoot(); break; case (ushort)UserPacketType.Damage: DamagePacket damagePacket = new DamagePacket(); damagePacket.Deserialize(stream); TakeDamage((int)damagePacket.payload.damage); break; case (ushort)UserPacketType.ShipDestroyed: Instantiate(explosionEffect, transform.position, Quaternion.identity); Destroy(gameObject); break; } }
protected override void OnReceiveDataEvent(uint packetID, ushort packetTypeID, Stream stream) { switch (packetTypeID) { case (ushort)UserPacketType.Velocity: VelocityPacket velocityPacket = new VelocityPacket(); velocityPacket.Deserialize(stream); //Debug.Log("Applying velocity received to " + gameObject.name); Vector3 velocityReceived = new Vector3( velocityPacket.payload.velocity.x, velocityPacket.payload.velocity.y, velocityPacket.payload.velocity.z ); //rb.position += velocityReceived; transform.Translate(velocityReceived, Space.Self); if (NetworkManager.Instance.isServer) { NetworkMessageManager.Instance.SendPosition(transform.position, (uint)objectID, velocityPacket.payload.sequence); } break; case (ushort)UserPacketType.Position: PositionPacket positionPacket = new PositionPacket(); positionPacket.Deserialize(stream); Vector3 position = new Vector3( positionPacket.payload.position.x, positionPacket.payload.position.y, positionPacket.payload.position.z ); //rb.position = position; //Debug.Log("Position before reconciliation: " + position); if (positionPacket.payload.sequence > lastSequenceServerReceived) { lastSequenceServerReceived = positionPacket.payload.sequence; Vector3 reconciliationPosition = Vector3.zero; for (uint currentInputKey = positionPacket.payload.sequence; currentInputKey < sequence; currentInputKey++) { if (inputs.ContainsKey(currentInputKey)) { //Debug.Log("Removing input with ID " + currentInputKey); reconciliationPosition += inputs[currentInputKey]; inputs.Remove(positionPacket.payload.sequence); } } transform.position = position; } break; case (ushort)UserPacketType.Shoot: Shoot(); break; case (ushort)UserPacketType.Damage: DamagePacket damagePacket = new DamagePacket(); damagePacket.Deserialize(stream); TakeDamage((int)damagePacket.payload.damage); break; case (ushort)UserPacketType.ShipDestroyed: if (NetworkManager.Instance.isServer) { NetworkMessageManager.Instance.SendShipdestroyedPacket((uint)objectID); } Instantiate(explosionEffect, transform.position, Quaternion.identity); Destroy(gameObject); break; } }