private void CheckState(MessageXieYi xieyi) { string messageInfo = SerializeHelper.ConvertToString(xieyi.MessageContent); RoomActorUpdate roomActorUpdate = new RoomActorUpdate(); roomActorUpdate.SetSendInfo(messageInfo); if (roomActorUpdate.userIndex == DataController.instance.MyLocateIndex) { RoomActorState state = (RoomActorState)int.Parse(roomActorUpdate.update); Debug.Log("我的状态:" + state); //判断我的状态为预准备 switch (state) { case RoomActorState.PrepareModel: GameLoadingUI.Show(); //PrepareLocalModel(); break; case RoomActorState.ModelPrepared: break; case RoomActorState.Invincible: GameRunUI.instance.Open(); //MyJoystickManager.instance.Open(); break; } } }
public void Update() { if (serverEvent.Count > 0) { MessageXieYi xieyi = serverEvent.Dequeue(); if ((MessageConvention)xieyi.XieYiFirstFlag == MessageConvention.getRoommateInfo) { ErrorType error = ClassGroup.CheckIsError(xieyi); if (error != ErrorType.none) { Debug.LogError(error); } else { HomeUI.Close(); Debug.Log("根据玩家数据刷新房间UI"); ReflashBtnReady(); ReflashRoomInfo(); ReflashRoomState(); } } if ((MessageConvention)xieyi.XieYiFirstFlag == MessageConvention.updateActorState) { ErrorType error = ClassGroup.CheckIsError(xieyi); if (error != ErrorType.none) { Debug.LogError(error); } else { string messageInfo = SerializeHelper.ConvertToString(xieyi.MessageContent); RoomActorUpdate roomActorUpdate = new RoomActorUpdate(); roomActorUpdate.SetSendInfo(messageInfo); RoomActorState state = (RoomActorState)int.Parse(roomActorUpdate.update); if (state == RoomActorState.Ready || state == RoomActorState.NoReady || state == RoomActorState.Offline) { ReflashRoomState(); } } } if ((MessageConvention)xieyi.XieYiFirstFlag == MessageConvention.updateRoom) { ErrorType error = ClassGroup.CheckIsError(xieyi); if (error != ErrorType.none) { Debug.LogError(error); } else { UpdateUIInfo(); } } } }
public static void SendState(RoomActorState state) { RoomActorUpdate netUpdate = new RoomActorUpdate() { userIndex = DataController.instance.MyLocateIndex, update = (int)state + "" }; string info = netUpdate.GetSendInfo(); byte[] message = SerializeHelper.ConvertToByte(info); SocketManager.instance.SendSave((byte)MessageConvention.updateActorState, message, false); }
private bool CheckIsAllFixedState(RoomActorState state) { bool isFixed = true; for (int i = 0; i < ActorList.Count; i++) { if (ActorList[i] == null || ActorList[i].CurState != state) { isFixed = false; break; } } return(isFixed); }
private void OnTriggerEnter(Collider other) { CharacterCommon checkSelf = other.GetComponent <CharacterCommon>(); if (checkSelf != null) { if (checkSelf.myIndex == info.userIndex)//子弹碰撞的是子弹的主人,不处理,不停止 { return; } } //自身被射检测以后再判断其他 if (!isDestroy) { isDestroy = true; bool isSend = (ownIndex == DataController.instance.MyLocateIndex); GameObject obj = other.gameObject; BulletInfo bulletInfo = new BulletInfo() { userIndex = DataController.instance.MyLocateIndex }; switch (obj.tag) { case nameof(Tag.Box): Box box = obj.GetComponent <Box>(); bulletInfo.shootTag = ShootTag.Box; bulletInfo.shootInfo = box.myInfo.myIndex + ""; if (isSend) //本人射击 { TeamType type = DataController.instance.ActorList[DataController.instance.MyLocateIndex].MyTeam; box.ChangeTexture(type); } break; case nameof(Tag.Member): CharacterCommon member = obj.GetComponent <CharacterCommon>(); bulletInfo.shootTag = ShootTag.Character; bulletInfo.shootInfo = member.myIndex + ""; RoomActorState state = DataController.instance.ActorList[member.myIndex].CurState; string tip = ""; switch (state) { case RoomActorState.Dead: tip = "该玩家已死亡"; break; case RoomActorState.Invincible: tip = "该玩家当前无敌"; break; } if (!string.IsNullOrEmpty(tip)) { UIManager.instance.ShowAlertTip(tip); isSend = false; } break; default: //墙体及障碍物 isSend = false; break; } Debug.Log("射中:" + bulletInfo.shootTag); if (isSend) { GameManager.instance.SendNetInfo(bulletInfo); } // PoolDestory(); } }
public void UpdateState(RoomActorUpdate roomActorUpdate) { int index = roomActorUpdate.userIndex; RoomActorState upState = (RoomActorState)int.Parse(roomActorUpdate.update); if (ActorList[index] == null) { return; } //if (ActorList[index].CurState != RoomActorState.ReConnect) { Log4Debug("站位 " + index + " 更新当前状态:" + ActorList[index].CurState + " -> " + (RoomActorState)int.Parse(roomActorUpdate.update)); ActorList[index].CurState = upState; byte[] message = SerializeHelper.ConvertToByte(roomActorUpdate.GetSendInfo()); BoardcastMessage(MessageConvention.updateActorState, message); if (CheckIsAllFixedState(RoomActorState.Ready)) { ChangeRoomState(RoomActorState.Ready); } if (CurState == RoomActorState.Gaming) { switch (ActorList[index].CurState) { case RoomActorState.ReConnect: break; case RoomActorState.WaitForStart: Log4Debug("模型未准备好的玩家准备好进入游戏了。"); string starttime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); byte[] start = SerializeHelper.ConvertToByte(starttime); MessageXieYi xieyi = new MessageXieYi((byte)MessageConvention.startGaming, 0, start); AsyncIOCPServer.instance.SendSave(UserTokenInfo[index], xieyi.ToBytes()); roomActorUpdate = new RoomActorUpdate() { userIndex = index, update = (int)RoomActorState.Gaming + "" }; UpdateState(roomActorUpdate); //广播,修改玩家状态用来准备本机数据 break; case RoomActorState.Dead: ActorList[index].timerDead = new Timer(new TimerCallback(SetAfterDead), index, RoomActor.DeadLastTime, 0); break; } } } //else//reConnect时修改状态 //{ // if (upState == RoomActorState.WaitForStart && RoomInfo.CurState == RoomActorState.Gaming) // { // ActorList[index].CurState = RoomActorState.Gaming; // roomActorUpdate.update = (int)ActorList[index].CurState + ""; // byte[] message = SerializeHelper.ConvertToByte(roomActorUpdate.GetSendInfo()); // BoardcastMessage(MessageConvention.updateActorState, message); // } // else // { // Log4Debug("想要在重连时更新状态:" + upState); // } //} }
public void ChangeRoomState(RoomActorState state) { byte[] message = new byte[] { }; RoomActorUpdate roomActorUpdate = new RoomActorUpdate(); Log4Debug("房间更改状态为:" + CurState + "->" + state); CurState = state; switch (CurState) { case RoomActorState.NoReady: foreach (var item in ActorList) { if (item.Value == null) { continue; } //初始化房间玩家部分信息 item.Value.InitActor(); //广播房间用户准备状态为未准备 roomActorUpdate = new RoomActorUpdate() { userIndex = item.Key, update = (int)item.Value.CurState + "" }; UpdateState(roomActorUpdate); } break; case RoomActorState.Ready: Log4Debug("玩家都准备了。"); //设置所有玩家默认值 foreach (var item in ActorList) { if (item.Value == null) { continue; } item.Value.MyModelInfo.pos = (NetVector3)GameTypeManager.BackStandPos(RoomInfo.RoomType, item.Key); item.Value.MyModelInfo.rotate = new NetVector3(0, GameTypeManager.BackLookAt(RoomInfo.RoomType, item.Key), 0); roomActorUpdate = new RoomActorUpdate() { userIndex = item.Key, update = (int)RoomActorState.PrepareModel + "" }; UpdateState(roomActorUpdate); //广播,修改玩家状态用来准备本机数据 } //方块初始化 BoxList = new ConcurrentDictionary <int, BoxInfo>(); //倒计时进入游戏 ChangeRoomState(RoomActorState.WaitForStart); break; case RoomActorState.WaitForStart: // Log4Debug("开始倒计时"); //时间初始化 PassedCountDownTime = 0; CountDownTimer = new Timer(new TimerCallback(GameCountDowning), null, 0, 1 * 1000); break; case RoomActorState.Gaming: Log4Debug("开始游戏:" + FrameIndex); foreach (var item in ActorList) { if (item.Value == null) { continue; } if (item.Value.CurState == RoomActorState.WaitForStart) { roomActorUpdate = new RoomActorUpdate() { userIndex = item.Key, update = (int)RoomActorState.Gaming + "" }; UpdateState(roomActorUpdate); //广播,修改玩家状态用来准备本机数据 } else { Log4Debug("用户:" + item.Value.Register.name + "在倒计时期间内未准备好模型。"); } } //保存帧同步 FrameIndex = 0; FrameCount = (int)(RoomInfo.GameTime / RoomInfo.frameTime); FrameGroup = new ConcurrentDictionary <int, FrameInfo>(); //FrameGroup = new Dictionary<int, FrameInfo>(); //for (int i = 0; i < FrameCount; i++) //{ // FrameGroup.Add(i, new FrameInfo() { frameIndex = i, frameData = new List<byte[]>() }); //} //线程池调用时间间隔逻辑 ThFrameCount = new Thread(GameFrameReconding); ThFrameCount.IsBackground = true; ThFrameCount.Start(); //ThreadPool.QueueUserWorkItem(new WaitCallback(GameFrameReconding), null); // string starttime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); byte[] start = SerializeHelper.ConvertToByte(starttime); BoardcastMessage(MessageConvention.startGaming, start); //发送游戏时间 PassedGameTime = 0; break; case RoomActorState.GameEnd: Log4Debug("退出计数"); TeamType winTeam = GetWinnerTeam(); Log4Debug("游戏结束" + winTeam + "胜利"); message = SerializeHelper.ConvertToByte((int)winTeam + ""); BoardcastMessage(MessageConvention.endGaming, message); // InitRoom(); break; } }