private void OnCommandFromClient(NetworkMessage netMsg) { netMsg.ReadMessage <ClientCommand>(ClientCommand.identity); ClientCommand.identity.Apply(ClientSyncEvent.identity); if (ClientCommand.identity.id == 0) { int connectionId = netMsg.conn.connectionId; ClientSyncEvent connectionSyncEvent = ClientSyncEvent.identity; ClientSyncEvent.identity = new ClientSyncEvent(); if (playTimeOver > 0f) { foreach (KeyValuePair <int, ClientSyncEvent> pair in ImServices.players) { int playingId = pair.Key; ClientSyncEvent playingSyncEvent = pair.Value; NetworkServer.SendToClient(connectionId, ImServices.MSG_COMMAND, playingSyncEvent); NetworkServer.SendToClient(playingId, ImServices.MSG_COMMAND, connectionSyncEvent); } ImServices.players[connectionId] = connectionSyncEvent; float time = playTimeOver - Time.time; NetworkServer.SendToClient(connectionId, ImServices.MSG_COMMAND, connectionSyncEvent); NetworkServer.SendToClient(connectionId, ImServices.MSG_GAME, new IntegerMessage(time > 0f ? (int)time : 0)); } else { ImServices.players[connectionId] = connectionSyncEvent; List <ClientSyncEvent> list = new List <ClientSyncEvent>(ImServices.players.Values); if (list.Count == playGameNum) { for (int i = 0; i < list.Count; ++i) { NetworkServer.SendToAll(ImServices.MSG_COMMAND, list[i]); } NetworkServer.SendToAll(ImServices.MSG_GAME, new IntegerMessage(playTimeMax)); playTimeOver = Time.time + playTimeMax; } Debug.LogWarning("Player: " + list.Count); } } else { NetworkServer.SendToAll(ImServices.MSG_COMMAND, ClientSyncEvent.identity); } }
void ServerEvent(ClientSyncEvent _event) { //Debug.Log("animId : " + _event.animId + " 送出的 ID : " + _event.connectionId + " MYID : " + MyID + " : " + AllPlayerControl.Count); if (_event.animId == 0) { #region 建立角色 PlayerControl control = null; if (AllPlayerControl.ContainsKey(_event.connectionId)) { control = AllPlayerControl[_event.connectionId]; } else { control = Instantiate <PlayerControl>(PlayerControlPrefab); control.gameObject.name = "PlayerID : " + _event.connectionId; if (_event.connectionId == MyID) { control.InitInput(); } control.ModelType = _event.isLucky ? PlayerControl.PlayerType.Cat : PlayerControl.PlayerType.Ghost; control.InitPoint(_event.mapping); AllPlayerControl.Add(_event.connectionId, control); } //建立 場上還沒出來的角色 GameObject model = null; if (AllPlayer.ContainsKey(_event.connectionId)) { model = AllPlayer[_event.connectionId].gameObject; } else { model = PlayerLoader.Instance.CreatePlayer(_event.catId); } if (model != null && control != null) { control.ConnectionId = _event.connectionId; control.ControlModel = model; control.ModelType = _event.isLucky ? PlayerControl.PlayerType.Cat : PlayerControl.PlayerType.Ghost; control.gameObject.layer = LayerMask.NameToLayer("Ignore Raycast"); model.transform.SetParent(control.transform, false); model.transform.localPosition = Vector3.zero; var player = model.GetComponent <Player>(); control.ModelControl = player; control.NowControlType = PlayerControl.ControlType.Move; if (AllPlayer.ContainsKey(_event.connectionId) == false) { AllPlayer.Add(_event.connectionId, player); } player.ChangeModel(control.ModelType); } #endregion } //每次 Send 都會檢查 if (_event.animId != 0 && AllPlayerControl.ContainsKey(_event.connectionId) && AllPlayer.ContainsKey(_event.connectionId)) { int nowCatID = -1; #region 檢查 交替 //尋找目前場上的貓 foreach (var player in AllPlayerControl) { if (player.Value.ModelType == PlayerControl.PlayerType.Cat) { nowCatID = player.Key; break; } } int newCatID = _event.isLucky ? _event.connectionId : nowCatID; bool isChange = nowCatID != newCatID; //if (isChange) // Debug.Log("目前的貓ID : " + nowCatID + " 新貓ID : " + _event.connectionId + " MYID : " + MyID); if (isChange) { PlayerControl newCatControl = AllPlayerControl[_event.connectionId]; PlayerControl nowCatControl = AllPlayerControl[nowCatID]; { #region 鬼 => 貓 //讓貓繼承 原本的路徑方向 繼續跑 Vector3 catPos = nowCatControl.transform.position; Vector3 catforward = nowCatControl.transform.forward; if (nowCatControl.IsAI && nowCatControl.gameObject.activeInHierarchy) { nowCatControl.gameObject.SetActive(false); AllPlayerControl.Remove(-1); } //讓鬼 接著貓的路徑繼續奔跑 newCatControl.ModelType = PlayerControl.PlayerType.Cat; newCatControl.transform.position = catPos; newCatControl.transform.forward = catforward; newCatControl.ChangeCD = Time.time + 3f; AllPlayer[newCatID].ChangeModel(PlayerControl.PlayerType.Cat); ShowDieEffect.Instance.CatShowDieEffect(newCatControl.transform); #endregion } { #region 貓 => 鬼 if (nowCatControl.IsAI == false) { nowCatControl.transform.position = _event.running; nowCatControl.transform.forward = _event.runningForward; nowCatControl.ModelType = PlayerControl.PlayerType.Ghost; nowCatControl.InitPoint(_event.mapping - 1); nowCatControl.NowControlType = PlayerControl.ControlType.Move; AllPlayer[nowCatID].ChangeModel(PlayerControl.PlayerType.Ghost); } #endregion } #endregion } } if (_event.animId == 1) { if (AllPlayerControl.ContainsKey(_event.connectionId) && AllPlayer.ContainsKey(_event.connectionId)) { AllPlayer[_event.connectionId].Turn(); AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.Move; AllPlayerControl[_event.connectionId].Clockwise = _event.mapping == 1; if (_event.runningForward != Vector3.zero) { AllPlayerControl[_event.connectionId].transform.forward = _event.runningForward; } } } if (_event.animId == 2) { if (AllPlayerControl.ContainsKey(_event.connectionId) && AllPlayer.ContainsKey(_event.connectionId)) { AllPlayer[_event.connectionId].Move(); if (_event.connectionId != MyID) { AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.Rotate; } } } if (_event.animId == 3) { if (AllPlayerControl.ContainsKey(_event.connectionId) && AllPlayer.ContainsKey(_event.connectionId)) { if (_event.isLucky) { AllPlayerControl[_event.connectionId].transform.position = _event.running; AllPlayerControl[_event.connectionId].transform.forward = _event.runningForward; SpeedUp.Attach(AllPlayerControl[_event.connectionId].gameObject); } else { if (_event.connectionId != MyID) { AllPlayer[_event.connectionId].Attack(); AllPlayerControl[_event.connectionId].OBTimer = _event.fvaule; AllPlayerControl[_event.connectionId].RunFinalPoint = _event.running; AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.Run; AllPlayerControl[_event.connectionId].TargetMoveIndex = _event.mapping; } } } } if (_event.animId == 4) { //鬼的判定 if (_event.isLucky == false) { AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.ReadyRun; } else { AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.Rotate; } AllPlayerControl[_event.connectionId].GhostRotateVal = _event.fvaule; if (_event.running != Vector3.zero) { AllPlayerControl[_event.connectionId].transform.position = _event.running; } if (_event.runningForward != Vector3.zero) { AllPlayerControl[_event.connectionId].transform.forward = _event.runningForward; } AllPlayerControl[_event.connectionId].RandomF = _event.mapping == 1; AllPlayerControl[_event.connectionId].CacheForward = AllPlayerControl[_event.connectionId].transform.forward; AllPlayerControl[_event.connectionId].CacheRight = AllPlayerControl[_event.connectionId].transform.right; } }
public void Apply(ClientSyncEvent syncEvent) { switch (id) { case 0: // initial syncEvent.animId = 0; syncEvent.catId = catId; syncEvent.connectionId = connectionId; syncEvent.isLucky = false; syncEvent.mapping = Random.Range(1, 5); syncEvent.fvaule = fVaule; syncEvent.running = Vector3.zero; syncEvent.runningForward = Vector3.zero; break; case 1: // idle syncEvent.animId = 1; syncEvent.catId = catId; syncEvent.connectionId = connectionId; syncEvent.isLucky = connectionId == GM.currentLuckyCatId; syncEvent.mapping = mapping; syncEvent.fvaule = fVaule; syncEvent.running = running; syncEvent.runningForward = runningForward; break; case 2: // running syncEvent.animId = 2; syncEvent.catId = catId; syncEvent.connectionId = connectionId; syncEvent.isLucky = connectionId == GM.currentLuckyCatId; syncEvent.mapping = mapping; syncEvent.fvaule = fVaule; syncEvent.running = running; syncEvent.runningForward = runningForward; break; case 3: // jumping && Run if (Time.time >= CatChangeCDTimer && GM.currentLuckyCatId == gotchaId) { CatChangeCDTimer = Time.time + ChangeCDTime; GM.currentLuckyCatId = connectionId; syncEvent.mapping = Random.Range(1, 5); } else { syncEvent.mapping = mapping; } syncEvent.animId = 3; syncEvent.catId = catId; syncEvent.connectionId = connectionId; syncEvent.isLucky = connectionId == GM.currentLuckyCatId; syncEvent.running = running; syncEvent.runningForward = runningForward; syncEvent.fvaule = fVaule; break; case 4: // Rotate syncEvent.animId = 4; syncEvent.mapping = mapping; syncEvent.catId = catId; syncEvent.connectionId = connectionId; syncEvent.isLucky = connectionId == GM.currentLuckyCatId; syncEvent.fvaule = fVaule; syncEvent.running = running; syncEvent.runningForward = runningForward; break; default: Debug.LogError("undefined " + id); break; } }