private void OnActorAddReply(byte[] bytes) { ActorAddReply input = ActorAddReply.Parser.ParseFrom(bytes); if (!input.Ret) { string msg = "创建Actor失败!"; GameRoomManager.Instance.Log("MSG: OnActorAddReply - " + msg); return; } if (input.CellIndex == 0) { Debug.LogError("RoomLogic OnActorAddReply Error - CellIndex is lost!!!"); return; } if (GameRoomManager.Instance == null || GameRoomManager.Instance.HexmapHelper == null) { Debug.LogError("What the F**k! with GameRoomManager and HexmapHelper!!!"); return; } var av = GameRoomManager.Instance.HexmapHelper.CreateUnit(input.RoomId, input.OwnerId, input.ActorId, input.PosX, input.PosZ, input.Orientation, input.Species, input.CellIndex, input.ActorInfoId, input.Name, input.Hp, input.HpMax, input.AttackPower, input.DefencePower, input.Speed, input.FieldOfVision, input.ShootingRange, input.AttackDuration, input.AttackInterval, input.AmmoBase, input.AmmoBaseMax); // 每增加一个单位, 都要根据现有的AI-代理权情况来给这个单位设置一下, 批量处理在另外的地方 var ab = GameRoomManager.Instance.RoomLogic.ActorManager.GetActor(input.ActorId); if (ab != null) { ab.HasAiRights = GameRoomManager.Instance.GetAiPlayer(input.OwnerId); ab.HighAiState = (StateEnum)input.HighAiState; ab.HighAiTargetCell = input.HighAiCellIndexTo; ab.HighAiTargetId = input.HighAiTargetId; ab.HighAiDurationTime = input.HighAiDurationTime; ab.HighAiTotalTime = input.HighAiTotalTime; } }
private static void DOWNLOAD_ACTORS(byte[] bytes) { DownloadActors input = DownloadActors.Parser.ParseFrom(bytes); RoomLogic roomLogic = ServerRoomManager.Instance.GetRoomLogic(input.RoomId); if (roomLogic == null) { string msg = $"Battlefield is not found!"; // 战场没有找到 ServerRoomManager.Instance.Log("MSG: DOWNLOAD_ACTORS Error - " + msg + $" - {input.RoomId}"); DownloadCitiesReply output = new DownloadCitiesReply() { RoomId = input.RoomId, Ret = false, ErrMsg = msg, }; ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadActorsReply, output.ToByteArray()); return; } // 把房间内已有的所有actor都发给本人 foreach (var keyValue in roomLogic.ActorManager.AllActors) { ActorBehaviour ab = keyValue.Value; if (ab.CellIndex == 0) { Debug.LogError("DOWNLOAD_ACTORS Erro - Actor position is lost!!!"); continue; } ActorAddReply output = new ActorAddReply() { RoomId = ab.RoomId, OwnerId = ab.OwnerId, ActorId = ab.ActorId, PosX = ab.PosX, PosZ = ab.PosZ, CellIndex = ab.CellIndex, Orientation = ab.Orientation, Species = ab.Species, ActorInfoId = ab.ActorInfoId, Name = ab.Name, Hp = ab.Hp, HpMax = ab.HpMax, AttackPower = ab.AttackPower, DefencePower = ab.DefencePower, Speed = ab.Speed, FieldOfVision = ab.FieldOfVision, ShootingRange = ab.ShootingRange, AttackDuration = ab.AttackDuration, AttackInterval = ab.AttackInterval, AmmoBase = ab.AmmoBase, AmmoBaseMax = ab.AmmoBaseMax, // High AI params HighAiState = ab.HighAiState, HighAiTargetId = ab.HighAiTargetId, HighAiCellIndexTo = ab.HighAiCellIndexTo, HighAiDurationTime = ab.HighAiDurationTime, HighAiTotalTime = ab.HighAiTotalTime, Ret = true, }; ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.ActorAddReply, output.ToByteArray()); // 更新AI状态, 注: 尽管参数与ActorAiStateReply一样, 但是这里是高级AI状态 if (ab.HighAiDurationTime > 0) { ab.HighAiDurationTime -= (float)(DateTime.Now - ab.HighAiStartTime).TotalSeconds; if (ab.HighAiDurationTime < 0f) { ServerRoomManager.Instance.Log($"RoomMsgReply DOWNLOAD_ACTORS Error - HighAiDurationTime is less than 0 - Name:{ab.Name} - Time:{ab.HighAiDurationTime}"); ab.HighAiDurationTime = 0f; } } } { PlayerInfo pi = ServerRoomManager.Instance.GetPlayer(_args); if (pi == null) { string msg = $"当前玩家没有找到!"; ServerRoomManager.Instance.Log("MSG: DOWNLOAD_ACTORS Error - " + msg); DownloadCitiesReply output = new DownloadCitiesReply() { RoomId = input.RoomId, Ret = false, ErrMsg = msg, }; ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadActorsReply, output.ToByteArray()); return; } pi.IsReady = true; // 客户端准备好了,可以检测心跳了 long OwnerId = pi.Enter.TokenId; { DownloadActorsReply output = new DownloadActorsReply() { RoomId = input.RoomId, TotalCount = roomLogic.UrbanManager.AllCities.Count, MyCount = roomLogic.UrbanManager.CountOfThePlayer(OwnerId), Ret = true, }; ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadActorsReply, output.ToByteArray()); ServerRoomManager.Instance.Log($"MSG: DOWNLOAD_ACTORS OK - Player:{pi.Enter.Account} - City Count:{output.MyCount}/{output.TotalCount}"); } } }
private void OnActorAdd(SocketAsyncEventArgs args, byte[] bytes) { ActorAdd input = ActorAdd.Parser.ParseFrom(bytes); // 这条消息是在房间内部接收的,所以要判断这条消息是否属于这个房间。如果放在外部判断,可以通过外部的函数找到该房间,然后直接调用该房间的函数 // 放在哪里都可以,目前放在这里是因为可以测试一下,同一条消息,多个函数注册是否会有BUG if (input.RoomId != _roomId) { return; } var piir = GetPlayerInRoom(input.OwnerId); if (piir == null) { ActorAddReply output = new ActorAddReply() { Ret = false, }; ServerRoomManager.Instance.SendMsg(args, ROOM_REPLY.ActorAddReply, output.ToByteArray()); ServerRoomManager.Instance.Log($"MSG: ActorAdd - Player not found in server! Id:{input.OwnerId}"); // 当前玩家不在本本战场! } else { if (input.CellIndex == 0) { Debug.LogError("OnActorAdd F**k!!! Actor position is lost!!!"); } ActorBehaviour ab = new ActorBehaviour() { RoomId = input.RoomId, OwnerId = input.OwnerId, ActorId = input.ActorId, PosX = input.PosX, PosZ = input.PosZ, CellIndex = input.CellIndex, Orientation = input.Orientation, Species = input.Species, ActorInfoId = input.ActorInfoId, }; ab.LoadFromTable(); ActorManager.AddActor(ab, this); // 转发给房间内的所有玩家 ActorAddReply output = new ActorAddReply() { RoomId = ab.RoomId, OwnerId = ab.OwnerId, ActorId = ab.ActorId, Orientation = ab.Orientation, PosX = ab.PosX, PosZ = ab.PosZ, CellIndex = ab.CellIndex, Species = ab.Species, ActorInfoId = ab.ActorInfoId, Name = ab.Name, Hp = ab.Hp, HpMax = ab.HpMax, AttackPower = ab.AttackPower, DefencePower = ab.DefencePower, Speed = ab.Speed, FieldOfVision = ab.FieldOfVision, ShootingRange = ab.ShootingRange, AttackDuration = ab.AttackDuration, AttackInterval = ab.AttackInterval, AmmoBase = ab.AmmoBase, AmmoBaseMax = ab.AmmoBaseMax, Ret = true, }; BroadcastMsg(ROOM_REPLY.ActorAddReply, output.ToByteArray()); } }