Example #1
0
        //新玩家
        public int NewPlayer(string id, GamePlayerSession session)
        {
            lock (this)
            {
                if (_players.ContainsKey(id))
                {
                    return(GameServerReturnCode.DUPLICATE_USERNAME);
                }

                //新玩家
                var user = new GamePlayer();
                user.id = id;

                //寻找新位置,回复HP
                ResetPositionAndStatus(user);

                //生成随机颜色
                user.r = (float)_rnd.NextDouble();
                user.g = (float)_rnd.NextDouble();
                user.b = (float)_rnd.NextDouble();

                //创建session设置
                session.user = user;

                //给其他玩家分发玩家加入消息
                DispatchMsg(new GamePlayerMessage(MsgCode.USER_JOIN, user), "", id);

                _players.Add(id, session);

                //给自己全量同步
                DispatchMsg(GenerateSyncMessage(), id);

                return(GameServerReturnCode.OK);
            }
        }
Example #2
0
        public void Start()
        {
            var listener = NetworkFactory.CreateNetworkServer <TcpNetworkServer>();

            listener.OnNewClient += (client) =>
            {
                GamePlayerSession session = new GamePlayerSession(client, this);
                //立刻读取请求
                //var msg = client.ReadSync();
                //if (msg != null && msg is ClientJoinMessage)
                //{
                //    var id = ((ClientJoinMessage)msg).id;
                //    this.NewPlayer(id, client);
                //}
                //else
                //{
                //    client.Close(); //非法传入,直接关闭
                //}
            };
            listener.StartListen(ConstSettings.DefaultServerEndPoint);

            //接收所有的客户端请求
            LoopFun();
        }