Пример #1
0
        public static GameObjInfo Player(Character player)                      //转换成玩家
        {
            GameObjInfo ret = new GameObjInfo();

            ret.GameObjType = Communication.Proto.GameObjType.Character;
            Basic(player, ref ret);

            ret.TeamID     = player.TeamID;
            ret.Ap         = player.AP;
            ret.BulletType = ConvertTool.ToCommunicationBulletType(player.bulletType);

            Prop?holdProp = player.HoldProp;                            // 防止判断后被突然置null

            ret.PropType = holdProp == null ? Communication.Proto.PropType.Null : ConvertTool.ToCommunicationPropType(holdProp.GetPropType());

            ret.IsDying      = player.IsResetting;
            ret.JobType      = ConvertTool.ToCommunicationJobType(player.jobType);
            ret.CD           = player.CD;
            ret.MaxBulletNum = player.MaxBulletNum;
            ret.BulletNum    = player.BulletNum;
            ret.MaxHp        = player.MaxHp;
            ret.Hp           = player.HP;
            ret.LifeNum      = player.LifeNum;

            return(ret);
        }
Пример #2
0
 private static void Basic(GameObject gameObj, ref GameObjInfo ret)                      //拷贝基本信息
 {
     ret.Guid            = gameObj.ID;
     ret.X               = gameObj.Position.x;
     ret.Y               = gameObj.Position.y;
     ret.FacingDirection = gameObj.FacingDirection;
     ret.MoveSpeed       = gameObj.MoveSpeed;
     ret.CanMove         = gameObj.CanMove;
     ret.IsMoving        = gameObj.IsMoving;
     ret.ShapeType       = ConvertTool.ToCommunicationShapeType(gameObj.Shape);
 }
Пример #3
0
        public static GameObjInfo Prop(Prop prop)                               //转换成道具
        {
            GameObjInfo ret = new GameObjInfo();

            ret.GameObjType = Communication.Proto.GameObjType.Prop;
            Basic(prop, ref ret);

            ret.PropType = ConvertTool.ToCommunicationPropType(prop.GetPropType());
            ret.IsLaid   = prop.Laid;

            return(ret);
        }
Пример #4
0
        public static GameObjInfo Bullet(Bullet bullet)                 //转换成子弹
        {
            GameObjInfo ret = new GameObjInfo();

            ret.GameObjType = Communication.Proto.GameObjType.Bullet;
            Basic(bullet, ref ret);

            ret.TeamID     = bullet.Parent == null ? Team.invalidTeamID : bullet.Parent.TeamID;
            ret.Ap         = bullet.AP;
            ret.BulletType = ConvertTool.ToCommunicationBulletType(bullet.BulletType);

            return(ret);
        }
Пример #5
0
        public static GameObjInfo Prop(Prop prop)                               //转换成道具
        {
            GameObjInfo ret = new GameObjInfo();

            ret.GameObjType = Communication.Proto.GameObjType.Prop;
            Basic(prop, ref ret);

            ret.PropType = ConvertTool.ToCommunicationPropType(prop.GetPropType());
            ret.IsLaid   = prop.Laid;
            ret.TeamID   = prop.Parent == null ? Team.invalidTeamID : prop.Parent.TeamID;

            return(ret);
        }
Пример #6
0
        protected override void OnReceive(MessageToServer msg)
        {
#if DEBUG
            Console.WriteLine($"Recieve message: from teamID {msg.TeamID}, playerID {msg.PlayerID}: {msg.MessageType}");
#endif
            switch (msg.MessageType)
            {
            case MessageType.AddPlayer:
                SendAddPlayerResponse(msg, AddPlayer(msg));
                break;

            case MessageType.Move:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.MovePlayer(communicationToGameID[msg.TeamID, msg.PlayerID], msg.TimeInMilliseconds, msg.Angle);
                }
                break;

            case MessageType.Attack:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.Attack(communicationToGameID[msg.TeamID, msg.PlayerID], msg.TimeInMilliseconds, msg.Angle);
                }
                break;

            case MessageType.Pick:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.Pick(communicationToGameID[msg.TeamID, msg.PlayerID], ConvertTool.ToGamePropType(msg.PropType));
                }
                break;

            case MessageType.Use:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.Use(communicationToGameID[msg.TeamID, msg.PlayerID]);
                }
                break;

            case MessageType.Throw:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.Throw(communicationToGameID[msg.TeamID, msg.PlayerID], msg.TimeInMilliseconds, msg.Angle);
                }
                break;

            case MessageType.Send:
                SendMessageToTeammate(msg);
                break;
            }
        }
Пример #7
0
        private void OnReceive(MessageToServer msg)
        {
            switch (msg.MessageType)
            {
            case MessageType.AddPlayer:
                SendAddPlayerResponse(msg, AddPlayer(msg));
                break;

            case MessageType.Move:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.MovePlayer(communicationToGameID[msg.TeamID, msg.PlayerID], msg.TimeInMilliseconds, msg.Angle);
                }
                break;

            case MessageType.Attack:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.Attack(communicationToGameID[msg.TeamID, msg.PlayerID], msg.TimeInMilliseconds, msg.Angle);
                }
                break;

            case MessageType.Pick:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.Pick(communicationToGameID[msg.TeamID, msg.PlayerID], ConvertTool.ToGamePropType(msg.PropType));
                }
                break;

            case MessageType.Use:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.Use(communicationToGameID[msg.TeamID, msg.PlayerID]);
                }
                break;

            case MessageType.Throw:
                if (ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))
                {
                    game.Throw(communicationToGameID[msg.TeamID, msg.PlayerID], msg.TimeInMilliseconds, msg.Angle);
                }
                break;

            case MessageType.Send:
                SendMessageToTeammate(msg);
                break;
            }
        }
Пример #8
0
        private object messageToAllClientsLock = new object();                                             //全局消息锁,保证EndGame消息是最后一个消息
        private void SendMessageToAllClients(MessageType msgType, bool requireGaming = true)               //向所有的客户端发送消息
        {
            var gameObjList = game.GetGameObject();
            var cellColor   = game.GameMap.CellColor;

            var rows = game.GameMap.Cols;
            var cols = game.GameMap.Cols;

            //记录颜色信息,避免重复构造颜色信息
            Google.Protobuf.Collections.RepeatedField <MessageToClient.Types.OneDimVec> msgCellColors = new Google.Protobuf.Collections.RepeatedField <MessageToClient.Types.OneDimVec>();
            for (int x = 0; x < rows; ++x)
            {
                msgCellColors.Add(new MessageToClient.Types.OneDimVec());
                for (int y = 0; y < cols; ++y)
                {
                    msgCellColors[x].RowColors.Add(ConvertTool.ToCommunicationColorType(cellColor[x, y]));
                }
            }

            //记录所有游戏对象信息,避免重复构造游戏对象信息
            Google.Protobuf.Collections.RepeatedField <GameObjInfo> msgGameObjs = new Google.Protobuf.Collections.RepeatedField <GameObjInfo>();
            foreach (var gameObj in gameObjList)
            {
                msgGameObjs.Add(CopyInfo.Auto((GameObject)gameObj));
            }

            //记录所有GUID信息
            Google.Protobuf.Collections.RepeatedField <MessageToClient.Types.OneTeamGUIDs> playerGUIDs = new Google.Protobuf.Collections.RepeatedField <MessageToClient.Types.OneTeamGUIDs>();
            for (int x = 0; x < options.TeamCount; ++x)
            {
                playerGUIDs.Add(new MessageToClient.Types.OneTeamGUIDs());
                for (int y = 0; y < options.PlayerCountPerTeam; ++y)
                {
                    playerGUIDs[x].TeammateGUIDs.Add(communicationToGameID[x, y]);
                }
            }

            lock (messageToAllClientsLock)
            {
                if (!game.GameMap.Timer.IsGaming && requireGaming)
                {
                    return;
                }
                for (int i = 0; i < options.TeamCount; ++i)
                {
                    int teamScore = game.GetTeamScore(i);
                    for (int j = 0; j < options.PlayerCountPerTeam; ++j)
                    {
                        MessageToClient msg = new MessageToClient();
                        msg.TeamID      = i;
                        msg.PlayerID    = j;
                        msg.MessageType = msgType;
                        msg.SelfInfo    = CopyInfo.Player(game.GetPlayerFromTeam(communicationToGameID[i, j]));

                        for (int k = 0; k < options.TeamCount; ++k)
                        {
                            msg.PlayerGUIDs.Add(playerGUIDs[k]);
                        }

                        msg.SelfTeamColor = ConvertTool.ToCommunicationColorType(Map.TeamToColor(i));

                        foreach (var infos in msgGameObjs)
                        {
                            msg.GameObjs.Add(infos);
                        }

                        for (int x = 0; x < rows; ++x)
                        {
                            msg.CellColors.Add(msgCellColors[x]);
                        }

                        msg.TeamScore = teamScore;

                        serverCommunicator.SendMessage(msg);

                        mwr?.WriteOne(msg);
                    }
                }
            }
        }
Пример #9
0
        private bool AddPlayer(MessageToServer msg)
        {
            if (game.GameMap.Timer.IsGaming)                    //如果正在游戏,不能加入角色
            {
                return(false);
            }

            if (!ValidTeamIDAndPlayerID(msg.TeamID, msg.PlayerID))             //PlayerID或TeamID不正确
            {
                return(false);
            }

            lock (addPlayerLock)
            {
                if (communicationToGameID[msg.TeamID, msg.PlayerID] != GameObject.invalidID)                        //已经有这个玩家了,不能再添加了
                {
                    return(false);
                }

                Game.PlayerInitInfo playerInitInfo = new Game.PlayerInitInfo(GetBirthPointIdx(msg.TeamID, msg.PlayerID), ConvertTool.ToGameJobType(msg.JobType), msg.TeamID);
                if (playerInitInfo.jobType == THUnity2D.JobType.InvalidJobType)
                {
                    return(false);                                                                                  //非法职业
                }
                bool legalJob = false;
                foreach (var enumMem in typeof(THUnity2D.JobType).GetFields())
                {
                    if (playerInitInfo.jobType.ToString() == enumMem.Name)
                    {
                        legalJob = true;
                        break;
                    }
                }
                if (!legalJob)
                {
                    return(false);                                  //非法职业,职业数值超出枚举范围
                }
                long newPlayerID = game.AddPlayer(playerInitInfo);
                if (newPlayerID == GameObject.invalidID)
                {
                    return(false);
                }
                communicationToGameID[msg.TeamID, msg.PlayerID] = newPlayerID;

                return(true);
            }
        }