コード例 #1
0
    private static void DOWNLOAD_MAP_REPLY(byte[] bytes)
    {
        DownloadMapReply input = DownloadMapReply.Parser.ParseFrom(bytes);

        if (!input.Ret)
        {
            string msg = "下载地图失败!";
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Error);
            GameRoomManager.Instance.Log("MSG: DOWNLOAD_MAP_REPLY Error - " + msg);
            return;
        }

        if (input.PackageIndex == 0)
        {// 第一条此类消息
            mapDataBuffers.Clear();
            GameRoomManager.Instance.Log($"MSG: DOWNLOAD_MAP_REPLY - 开始下载地图!地图名:{input.RoomName}");
        }
        mapDataBuffers.Add(input.MapData.ToByteArray());

        bool ret = false;

        if (input.IsLastPackage)
        {// 最后一条此类消息了
            int totalSize = 0;
            foreach (var package in mapDataBuffers)
            {
                totalSize += package.Length;
            }
            // 同时确保文件名的唯一性和可读性
            string mapName = $"{input.RoomName}_{input.RoomId}";

            // 把服务器传过来的地图数据写入本地文件
            BinaryWriter writer = GameRoomManager.Instance.HexmapHelper.BeginSaveBuffer(mapName);
            if (writer == null)
            {
                return;
            }

            foreach (var package in mapDataBuffers)
            {
                GameRoomManager.Instance.HexmapHelper.SaveBuffer(writer, package);
            }

            GameRoomManager.Instance.HexmapHelper.EndSaveBuffer(ref writer);
            GameRoomManager.Instance.Log($"MSG: DOWNLOAD_MAP_REPLY - 下载地图成功!地图名:{mapName} - Total Map Size:{totalSize}");

            // 从本地文件读取地图,并显示出来
            GameRoomManager.Instance.HexmapHelper.Load(mapName);
            GameRoomManager.Instance.Log($"MSG: DOWNLOAD_MAP_REPLY - 显示地图!地图名:{mapName}");

            // 设置房间ID和名字
            GameRoomManager.Instance.RoomId   = input.RoomId;
            GameRoomManager.Instance.RoomName = input.RoomName;
            string msg = $"进入战场 - {input.RoomName}";
            GameRoomManager.Instance.Log("MSG: DOWNLOAD_MAP_REPLY OK - " + msg);
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Success);

            // 2-补充内容,获取城市信息
            DownloadCities output = new DownloadCities()
            {
                RoomId = input.RoomId,
            };
            GameRoomManager.Instance.SendMsg(ROOM.DownloadCities, output.ToByteArray());

            // 3-补充内容,获取单位信息
            DownloadActors output2 = new DownloadActors()
            {
                RoomId = input.RoomId,
            };
            GameRoomManager.Instance.SendMsg(ROOM.DownloadActors, output2.ToByteArray());

            // 4-刷新玩家身上的资源
            UpdateRes output3 = new UpdateRes()
            {
                RoomId  = input.RoomId,
                OwnerId = GameRoomManager.Instance.CurrentPlayer.TokenId,
            };
            GameRoomManager.Instance.SendMsg(ROOM.UpdateRes, output3.ToByteArray());

            // 5-刷新玩家身上的行动点
            UpdateActionPoint output4 = new UpdateActionPoint()
            {
                RoomId  = input.RoomId,
                OwnerId = GameRoomManager.Instance.CurrentPlayer.TokenId,
            };
            GameRoomManager.Instance.SendMsg(ROOM.UpdateActionPoint, output4.ToByteArray());
        }
    }
コード例 #2
0
    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}");
            }
        }
    }