public void PlayerJoinRoom(int PlayerConnectionID, Vector3 Position) { Player player = PlayerManager.Instance.GetPlayer(PlayerConnectionID); player.OnPlayerChangeRooms(this); if (!PlayerConnectionIDsInRoom.Contains(PlayerConnectionID)) { PlayerConnectionIDsInRoom.Add(PlayerConnectionID); } if (NetworkDetails.IsLocalConnectionID(PlayerConnectionID)) { for (int i = 0; i < PlayerConnectionIDsInRoom.Count; i++) { PlayerManager.Instance.GetPlayer(PlayerConnectionIDsInRoom[i]).SetIsVisible(true); } } #if SERVER NetworkPacketSender.SendRoomData(PlayerConnectionID, RoomIndex); NetworkPacketSender.AddPlayerToRoom(PlayerConnectionID, this, Position); #endif player.transform.position = Position; player.SetIsVisible(true); }
public void PlayerReady(int ConnectionID) { NetworkPacketSender.SendLevelData(ConnectionID); NetworkPacketSender.SendPlayerData(ConnectionID); NetworkPacketSender.SendSpawnPlayer(ConnectionID); LevelManager.Instance.GetRoom(0).PlayerJoinRoom(ConnectionID, Vector3.zero); }
IEnumerator NetworkUpdate() { bool ServerActive = true; while (ServerActive) { int recHostId; int recConnectionId; int recChannelId; byte[] recBuffer = new byte[ReceivePacketSize]; int dataSize; byte error; NetworkEventType recNetworkEvent = NetworkTransport.Receive(out recHostId, out recConnectionId, out recChannelId, recBuffer, ReceivePacketSize, out dataSize, out error); switch (recNetworkEvent) { case NetworkEventType.Nothing: break; case NetworkEventType.ConnectEvent: AddConnection(recConnectionId); break; case NetworkEventType.DataEvent: NetworkPacket RecPacket = ScriptableObject.CreateInstance <NetworkPacket>(); RecPacket.SetPacketTarget(BitConverter.ToInt32(recBuffer, 0)); RecPacket.PacketHeader = (NetworkPacketHeader)BitConverter.ToInt32(recBuffer, 4); RecPacket.SetIsTargetRoom(BitConverter.ToBoolean(recBuffer, 8)); RecPacket.SetPacketData(recBuffer, 13, BitConverter.ToInt32(recBuffer, 9)); Debug.Log("Received Packet: " + RecPacket.PacketHeader + " Size: " + RecPacket.GetDataSize()); NetworkPacketReader.ReadPacket(RecPacket, recConnectionId, true); if (RecPacket.GetPacketTarget() != (int)PacketTargets.ServerOnly) { if (RecPacket.GetPacketTarget() == (int)PacketTargets.RelayToAllClients) { RelayPacketToAllClients(RecPacket, recConnectionId, recChannelId); } else if (RecPacket.GetIsTargetRoom()) { NetworkPacketSender.RelayPacketToPlayersInRoom(RecPacket, LevelManager.Instance.GetRoom(RecPacket.GetPacketTarget()), GetQOSType(recChannelId), false); } else { SendPacketToClient(RecPacket, recChannelId); } } Debug_AmountOfPacketsReceivedPerSecond++; Debug_TotalSizeOfPacketsReceivedPerSecond += RecPacket.GetTotalPacketSize(); break; case NetworkEventType.DisconnectEvent: RemoveConnection(recHostId); Debug.Log("remote client event disconnected"); break; } yield return(new WaitForEndOfFrame()); } }
public override void ReceiveInputs(byte[] PlayerInputData, int AmountOfInputs, int InputID, float DeltaTime) { InputType[] PlayerInputs = new InputType[AmountOfInputs]; int CurrentByteIndex = 16; for (int i = 0; i < AmountOfInputs; i++) { PlayerInputs[i] = (InputType)BitConverter.ToInt32(PlayerInputData, CurrentByteIndex); CurrentByteIndex += 4; switch (PlayerInputs[i]) { case InputType.Left: { CurrentVelocity -= Speed * Vector3.right; } break; case InputType.Right: { CurrentVelocity += Speed * Vector3.right; } break; case InputType.Jump: { CurrentVelocity += JumpForce * Vector3.up; } break; case InputType.ArmDirection: { SetArmDirection(Serializer.DeserializeToVector3(PlayerInputData, CurrentByteIndex)); CurrentByteIndex += 12; } break; } } transform.position += CurrentVelocity * DeltaTime; NetworkPacketSender.SendUpdatePlayer(GetPlayerConnectionID(), InputID, transform.position, transform.forward, CurrentArmDirection, CurrentRoom); //NetworkPacketSender.SendPlayerArmDirection(GetPlayerConnectionID(), InputID, CurrentArmDirection, CurrentRoom); //NetworkPacketSender.SendPlayerPosition(GetPlayerConnectionID(), InputID, transform.position, CurrentRoom); CurrentVelocity = Vector3.zero; }
public void PlayerLeaveRoom(int PlayerConnectionID) { #if SERVER NetworkPacketSender.RemovePlayerFromRoom(PlayerConnectionID, this); #endif PlayerConnectionIDsInRoom.Remove(PlayerConnectionID); if (NetworkDetails.IsLocalConnectionID(PlayerConnectionID)) { for (int i = 0; i < PlayerConnectionIDsInRoom.Count; i++) { PlayerManager.Instance.GetPlayer(PlayerConnectionIDsInRoom[i]).SetIsVisible(false); } } else { PlayerManager.Instance.GetPlayer(PlayerConnectionID).SetIsVisible(false); } }
void FixedUpdate() { CurrentAmountOfInputs = 0; if (Input.GetKey(KeyCode.A)) { CurrentAmountOfInputs++; InputsToSendToServer.AddRange(BitConverter.GetBytes((int)InputType.Left)); CurrentVelocity -= Speed * Vector3.right; } if (Input.GetKey(KeyCode.D)) { CurrentAmountOfInputs++; InputsToSendToServer.AddRange(BitConverter.GetBytes((int)(InputType.Right))); CurrentVelocity += Speed * Vector3.right; } if (Input.GetKeyDown(KeyCode.Space)) { CurrentAmountOfInputs++; InputsToSendToServer.AddRange(BitConverter.GetBytes((int)InputType.Jump)); CurrentVelocity += JumpForce * Vector3.up; } UpdateArmDirection(); if (LastArmDirection != CurrentArmDirection) { CurrentAmountOfInputs++; InputsToSendToServer.AddRange(BitConverter.GetBytes((int)(InputType.ArmDirection))); InputsToSendToServer.AddRange(Serializer.GetBytes(CurrentArmDirection)); LastArmDirection = CurrentArmDirection; } if (CurrentAmountOfInputs > 0) { CurrentInputID++; transform.position += CurrentVelocity * Time.deltaTime; CurrentVelocity = Vector3.zero; NetworkPacketSender.SendPlayerInput(GetPlayerConnectionID(), InputsToSendToServer, CurrentAmountOfInputs, CurrentInputID, Time.deltaTime); InputsToSendToServer.Clear(); } }
/// <summary> /// 连接 /// </summary> public override void Connect(string ip, int port, int connId = 0) { //连接过 先断开之前的 DisConnect(); IP = ip; Port = port; ConnId = connId; TimeOut = NetDefine.CONNECT_TIMEOUT; _isConnectFinish = false; _recvData = new byte[5 * 1024]; //TODO TcpClient = new TcpClient(); TcpClient.BeginConnect(IP, Port, AsyncConnectCallBack, null); //TCP 开始连接 TcpClient.SendTimeout = NetDefine.SEND_TIMEOUT; _packetSender = new NetworkPacketSender(this); _packetParser = new NetworkPacketParser <PacketTuple <IMessage> >(new ProtobufParser()); State = EConnetState.EConneting; ConnetTime = GetTimeStamp(); //更新时间戳 Throw = ThrowException; }
public override void Ragdoll() { base.Ragdoll(); NetworkPacketSender.SendRagdollPlayer(GetPlayerConnectionID()); }
public void RequestCurrentPlayers() { NetworkPacketSender.RequestCurrentPlayers(); }
public static void ReadPacket(NetworkPacket Packet, int SenderConnectionID, bool isServerReading) { switch (Packet.PacketHeader) { case NetworkPacketHeader.ConnectionID: { int LocalConnectionID = BitConverter.ToInt32(Packet.GetPacketData(), 0); NetworkDetails.SetLocalConnectionID(LocalConnectionID); NetworkPacketSender.SendPlayerReady(LocalConnectionID); } break; case NetworkPacketHeader.RequestConnectionID: { NetworkPacket ConnectionIDPacket = ScriptableObject.CreateInstance <NetworkPacket>(); ConnectionIDPacket.SetPacketTarget(SenderConnectionID); ConnectionIDPacket.PacketHeader = NetworkPacketHeader.ConnectionID; ConnectionIDPacket.SetPacketData(BitConverter.GetBytes(SenderConnectionID), 0, sizeof(int)); NetworkManager.Instance.SendPacketToClient(ConnectionIDPacket, QosType.Reliable); } break; case NetworkPacketHeader.PlayerReady: { int ReadyPlayerConnectionID = BitConverter.ToInt32(Packet.GetPacketData(), 0); BattleRoyale_GameManager.Instance.PlayerReady(ReadyPlayerConnectionID); } break; case NetworkPacketHeader.StartGame: break; case NetworkPacketHeader.SpawnPlayer: { int PlayerConnectionID = BitConverter.ToInt32(Packet.GetPacketData(), 0); PlayerManager.Instance.SpawnPlayer(PlayerConnectionID); } break; case NetworkPacketHeader.PlayerPosition: { int PlayerID = BitConverter.ToInt32(Packet.GetPacketData(), 0); int InputID = BitConverter.ToInt32(Packet.GetPacketData(), 4); Vector3 position = Serializer.DeserializeToVector3(Packet.GetPacketData(), 8); PlayerManager.Instance.GetPlayer(PlayerID).SetPosition(position); } break; case NetworkPacketHeader.ArmDirection: { int PlayerID = BitConverter.ToInt32(Packet.GetPacketData(), 0); int InputID = BitConverter.ToInt32(Packet.GetPacketData(), 4); Vector3 ArmDirection = Serializer.DeserializeToVector3(Packet.GetPacketData(), 8); PlayerManager.Instance.GetPlayer(PlayerID).SetArmDirection(ArmDirection); } break; case NetworkPacketHeader.PlayerTransform: { int PlayerID = BitConverter.ToInt32(Packet.GetPacketData(), 0); Vector3 position = Serializer.DeserializeToVector3(Packet.GetPacketData(), 4); Vector3 rotation = Serializer.DeserializeToVector3(Packet.GetPacketData(), 16); PlayerManager.Instance.GetPlayer(PlayerID).SetTransform(position, rotation); } break; case NetworkPacketHeader.PlayerUpdate: { int PlayerID = BitConverter.ToInt32(Packet.GetPacketData(), 0); int InputID = BitConverter.ToInt32(Packet.GetPacketData(), 4); Vector3 position = Serializer.DeserializeToVector3(Packet.GetPacketData(), 8); Vector3 ForwardVector = Serializer.DeserializeToVector3(Packet.GetPacketData(), 20); Vector3 CurrentArmDir = Serializer.DeserializeToVector3(Packet.GetPacketData(), 32); } break; case NetworkPacketHeader.RagdollPlayer: { int PlayerToRagdollID = BitConverter.ToInt32(Packet.GetPacketData(), 0); PlayerManager.Instance.GetPlayer(PlayerToRagdollID).Ragdoll(); } break; case NetworkPacketHeader.PlayerInputUpdate: { int PlayerID = BitConverter.ToInt32(Packet.GetPacketData(), 0); float DeltaTime = BitConverter.ToSingle(Packet.GetPacketData(), 4); int InputID = BitConverter.ToInt32(Packet.GetPacketData(), 8); int AmountOfInputs = BitConverter.ToInt32(Packet.GetPacketData(), 12); PlayerManager.Instance.GetPlayer(PlayerID).ReceiveInputs(Packet.GetPacketData(), AmountOfInputs, InputID, DeltaTime); } break; case NetworkPacketHeader.AddPlayerToRoom: { int PlayerID = BitConverter.ToInt32(Packet.GetPacketData(), 0); int RoomIndex = BitConverter.ToInt32(Packet.GetPacketData(), 4); Vector3 PlayerPosition = Serializer.DeserializeToVector3(Packet.GetPacketData(), 8); LevelManager.Instance.GetRoom(RoomIndex).PlayerJoinRoom(PlayerID, PlayerPosition); } break; case NetworkPacketHeader.RemovePlayerFromRoom: { int PlayerID = BitConverter.ToInt32(Packet.GetPacketData(), 0); int RoomIndex = BitConverter.ToInt32(Packet.GetPacketData(), 4); LevelManager.Instance.GetRoom(RoomIndex).PlayerLeaveRoom(PlayerID); } break; case NetworkPacketHeader.SpawnPlayerInRoom: { //int PlayerID = BitConverter.ToInt32(Packet.GetPacketData(), 0); //int RoomIndex = BitConverter.ToInt32(Packet.GetPacketData(), 4); //LevelManager.Instance.GetRoom(RoomIndex).PlayerJoinRoom(PlayerID); } break; case NetworkPacketHeader.RequestLevel: { NetworkPacketSender.SendLevelData(SenderConnectionID); } break; case NetworkPacketHeader.LevelData: { LevelManager.Instance.ReadInLevelBytes(Packet.GetPacketData()); } break; case NetworkPacketHeader.RequestRoomData: { int RoomIndex = BitConverter.ToInt32(Packet.GetPacketData(), 0); NetworkPacketSender.SendRoomData(SenderConnectionID, RoomIndex); } break; case NetworkPacketHeader.RoomData: { int RoomIndex = BitConverter.ToInt32(Packet.GetPacketData(), 0); LevelManager.Instance.GetRoom(RoomIndex).ReadInRoomAsBytes(Packet.GetPacketData()); } break; case NetworkPacketHeader.RequestCurrentPlayers: { NetworkPacketSender.SendPlayerData(SenderConnectionID); } break; case NetworkPacketHeader.PlayerData: { PlayerManager.Instance.ReadInPlayersAsBytes(Packet.GetPacketData()); } break; } }