/// <summary> /// Spawns someone elses player character into our game world /// </summary> /// <param name="PlayerName">The name of the character being added</param> /// <param name="PlayerLocation">The characters initial starting location</param> public void AddRemotePlayer(string PlayerName, bool CharacterAlive, Vector3 PlayerLocation, Quaternion PlayerRotation, int CurrentHP, int MaxHP) { //Requests to add players we already know about should be ignored if (RemotePlayers.ContainsKey(PlayerName)) { Log.Chat("Ignoring request to add already known player " + PlayerName, true); return; } //Spawn a new remote player object into the game world using the prefab manager, make sure they are set at the right starting location GameObject RemotePlayer = GameObject.Instantiate(PrefabManager.Instance.RemotePlayerPrefab, PlayerLocation, PlayerRotation); RemotePlayerController RemoteController = RemotePlayer.GetComponent <RemotePlayerController>(); RemoteController.SetValues(PlayerLocation, PlayerRotation, CurrentHP, MaxHP); //Update the character name displayed above the new characters head TextMesh PlayerNameDisplay = RemotePlayer.transform.Find("Character Name").GetComponent <TextMesh>(); PlayerNameDisplay.text = PlayerName; //Map this new player into the dictionary by its player name RemotePlayers.Add(PlayerName, RemotePlayer); //If the remote player is not alive they need to have their character immediately disabled and replaced with a ragdoll if (!CharacterAlive) { KillRemotePlayer(PlayerName); } }
void OnInitClientGame(InitServerGameInfo info) { if (!initRecieved) { PlayerInfo localInfo = info.clientLocalPlayer; if (localInfo != null) { Player localPlayer = lm.AddPlayer(ControllerType.Local, localInfo.id, localInfo.name); localPlayer.transform.position = new Vector3(localInfo.positionX, localInfo.positionY, localInfo.positionZ); foreach (PlayerInfo remoteInfo in info.elsePlayers) { if (remoteInfo.id != localInfo.id) { Player remotePlayer = lm.AddPlayer(ControllerType.Remote, remoteInfo.id, remoteInfo.name); // 状态设置 RemotePlayerController rpc = remotePlayer.GetComponent <RemotePlayerController>(); rpc.ApplyRemoteInfo(remoteInfo); } } initRecieved = true; } } }
private void Awake() { if (instance != null) { Destroy(this); } instance = this; if (PacketManager.instance == null) { GameObject o = GameObject.Find("net"); switch (GameManager.instance.NetPart) { case SocketPart.Server: o.GetComponentInChildren <Server>().ServerClose(); break; case SocketPart.Client: o.GetComponentInChildren <Client>().CloseSocket(); break; default: break; } } PacketManager.instance.AddPacketDataReceiver(this); InitPlayer(); }
public void AddPacketDataReceiver(IPacketDataReceiver receiver) { if (receiver as RemotePlayerController) { rpc = (RemotePlayerController)receiver; } packetReceiver.Add(receiver); }
protected virtual void SendNetworkPackage() { int count = this.inputBuffer.Count; FrameInput[] buffer = new FrameInput[count]; for (int i = 0; i < count; ++i) { float horizontalAxis = 0f; float horizontalAxisRaw = 0f; float verticalAxis = 0f; float verticalAxisRaw = 0f; NetworkButtonPress buttons = NetworkButtonPress.None; foreach (KeyValuePair <InputReferences, InputEvents> pair in this.inputBuffer[i]) { InputReferences inputReference = pair.Key; InputEvents inputEvent = pair.Value; if (inputReference.inputType == InputType.HorizontalAxis) { horizontalAxis = inputEvent.axis; horizontalAxisRaw = inputEvent.axisRaw; } else if (inputReference.inputType == InputType.VerticalAxis) { verticalAxis = inputEvent.axis; verticalAxisRaw = inputEvent.axisRaw; } else if (inputReference.inputType == InputType.Button && inputEvent.button) { buttons |= inputReference.engineRelatedButton.ToNetworkButtonPress(); } } buffer[i] = new FrameInput( horizontalAxis, horizontalAxisRaw, verticalAxis, verticalAxisRaw, buttons, this.optionSelections[i] == null ? -2 : this.optionSelections[i].Value ); } InputBufferMessage msg = new InputBufferMessage(this.player, this.currentFrame, buffer); if (UFE.config.networkOptions.fakeNetwork) { RemotePlayerController remoteController = UFE.GetController(UFE.GetRemotePlayer()) as RemotePlayerController; remoteController.OnMessageReceived(msg.Serialize(), new NetworkMessageInfo()); } else { UFE.multiplayerAPI.SendNetworkMessage(msg); } }
void OnRemotePlayerJoin(PlayerJoin join) { if (initRecieved) { PlayerInfo remoteInfo = join.info; if (remoteInfo != null) { Player remotePlayer = lm.AddPlayer(ControllerType.Remote, remoteInfo.id, remoteInfo.name); RemotePlayerController rpc = remotePlayer.GetComponent <RemotePlayerController>(); rpc.ApplyRemoteInfo(remoteInfo); } } }
void OnShoot(PlayerShoot shoot) { Player p = lm.GetPlayer(shoot.id); if (p != null) { if (p.playerType == PlayerType.Remote) { RemotePlayerController rpc = p.GetComponent <RemotePlayerController>(); Vector3 targePoint = new Vector3(shoot.targetPointX, shoot.targetPointY, shoot.targetPointZ); rpc.Shoot(targePoint); } } }
void OnShoot(PlayerShoot shoot, Connection conn) { if (conn.state == ConnState.InGame) { Player p = lm.GetPlayer(shoot.id); if (p != null) { if (p.playerType == PlayerType.Remote) { RemotePlayerController rpc = p.GetComponent <RemotePlayerController>(); Vector3 targePoint = new Vector3(shoot.targetPointX, shoot.targetPointY, shoot.targetPointZ); rpc.Shoot(targePoint); SendShoot(shoot); } } } }
void OnUpdateGameInfo(ServerGameState state) { if (initRecieved) { foreach (PlayerInfo info in state.info) { Player player = lm.GetPlayer(info.id); if (player != null) { if (player.playerType == PlayerType.Remote) { // 状态设置 RemotePlayerController rpc = player.GetComponent <RemotePlayerController>(); rpc.ApplyRemoteInfo(info); } } } } }
void OnClientLocalState(ClientLocalPlayerInfo state, Connection conn) { if (conn.state == ConnState.InGame) { Player p = lm.GetPlayer(conn.playerId); if (p != null) { RemotePlayerController rp = p.GetComponent <RemotePlayerController>(); if (rp != null) { rp.ApplyRemoteInfo(state.info); } else {//???不应该发生 Debug.LogError("ServerAgent.OnClientLocalState >> player has no remote controller component"); } } else { //???不应该发生 Debug.LogError("ServerAgent.OnClientLocalState >> player doesn't exist"); } } }