Пример #1
0
        private IEnumerator CheckUDPState()
        {
            var wt = new WaitForSeconds(5);

            while (true)
            {
                if (udpClient != null && udpClient.Connected)
                {
                    var now  = Time.time;
                    var last = udpClient.lastReceiveTime;
                    if (udpClient.receiveYet)
                    {
                        if (now - last > 5)
                        {
                            //UDP 连接断开之后 走TCP进行数据广播
                            Util.ShowMsg("Client UDP Connect Lost");
                            UDPLost();
                            //通知服务器切断UDP连接
                            var cg = CGPlayerCmd.CreateBuilder();
                            cg.Cmd = "UDPLost";
                            this.BroadcastMsg(cg);
                            break;
                        }
                    }
                }
                yield return(wt);
            }
        }
Пример #2
0
    IEnumerator InitData(string ip, string account)
    {
        if (RemoteConnection == null)
        {
            RemoteConnection            = new RemoteClient(ml);
            RemoteConnection.evtHandler = EvtHandler;
            RemoteConnection.msgHandler = MsgHandler;

            RemoteConnection.Connect(ip, ClientApp.Instance.remotePort);
        }


        var cg = CGPlayerCmd.CreateBuilder();

        cg.Cmd     = "Login2";
        cg.Account = account;

        Bundle bundle;
        var    data = KBEngine.Bundle.GetPacketFull(cg, out bundle);

        yield return(StartCoroutine(RemoteConnection.SendWaitResponse(data.data, data.fid, (packet) =>
        {
            var proto = packet.protoBody as GCPlayerCmd;
            var cmds = proto.Result.Split(' ');
            Debug.LogError(proto);
        }, bundle)));
    }
Пример #3
0
        //MakeMove 0
        public async Task MakeMove(int id, CGPlayerCmd cmd)         //有玩家走了一步,更新数据,广播给所有玩家,开始下一手
        {
            await _messageQueue;
            var   pos = Convert.ToInt32(cmd.Cmd.Split(' ')[1]);

            chessState[pos] = id;

            curTurn++;
            var playerWho = curTurn % 2;
            var p         = players[playerWho];


            var cmd2 = GCPlayerCmd.CreateBuilder();

            cmd2.Result = string.Format("MakeMove {0} {1}", id, pos);
            BroadcastToAll(cmd2);

            if (CheckWin())
            {
                await GameOver();
            }
            else
            {
                var cmd3 = GCPlayerCmd.CreateBuilder();
                cmd3.Result = string.Format("NewTurn {0} {1}", curTurn, p.Id);
                BroadcastToAll(cmd3);
            }
        }
Пример #4
0
        public override IEnumerator RunLogic()
        {
            yield return(new WaitForSeconds(2));

            yield return(GetAttr().StartCoroutine(Util.SetBurn(GetAttr().gameObject)));

            yield return(null);

            //等网络属性同步上去了再删除对象 Hp = 0 接着等一会删除对象
            if (NetworkUtil.IsNetMaster())
            {
                yield return(new WaitForSeconds(5));

                var cg = CGPlayerCmd.CreateBuilder();
                cg.Cmd = "RemoveEntity";
                var ety = EntityInfo.CreateBuilder();
                ety.Id        = GetAttr().GetNetView().GetServerID();
                cg.EntityInfo = ety.Build();
                var world = WorldManager.worldManager.GetActive();
                world.BroadcastMsg(cg);
                ObjectManager.objectManager.DestroyByLocalId(GetAttr().GetComponent <KBEngine.KBNetworkView>().GetLocalId());
            }
            else
            {
                ObjectManager.objectManager.DestroyByLocalId(GetAttr().GetComponent <KBEngine.KBNetworkView>().GetLocalId());
            }
        }
Пример #5
0
        private void Start()
        {
            var runner = transform.parent.GetComponent <SkillLayoutRunner>();
            var isMe   = runner.stateMachine.attacker.GetComponent <NpcAttribute>().GetNetView().IsMe;

            if (isMe)
            {
                var unitData = Util.GetUnitData(false, MonsterID, 0);
                var cg       = CGPlayerCmd.CreateBuilder();
                cg.Cmd = "AddEntity";

                var entityInfo = EntityInfo.CreateBuilder();
                entityInfo.UnitId = MonsterID;
                var ip = NetworkUtil.ConvertPos(this.transform.position);
                entityInfo.X        = ip[0];
                entityInfo.Y        = ip[1] + 50;
                entityInfo.Z        = ip[2];
                entityInfo.SpawnId  = 0;
                entityInfo.EType    = EntityType.CHEST;
                entityInfo.HP       = unitData.HP;
                entityInfo.LifeLeft = GameConst.Instance.NuclearWaitTime + GameConst.Instance.NuclearWorkTime;
                entityInfo.PlayerID =
                    runner.stateMachine.attacker.GetComponent <NpcAttribute>().GetNetView().GetServerID();
                cg.EntityInfo = entityInfo.Build();
                var scene = WorldManager.worldManager.GetActive();
                scene.BroadcastMsg(cg);
            }
        }
Пример #6
0
        public static void SyncPosDirHP()
        {
            var me = ObjectManager.objectManager.GetMyPlayer();

            if (me == null)
            {
                return;
            }
            var pos    = me.transform.position;
            var dir    = (int)me.transform.localRotation.eulerAngles.y;
            var meAttr = me.GetComponent <NpcAttribute>();

            var cg = CGPlayerCmd.CreateBuilder();

            cg.Cmd = "UpdateData";
            var ainfo = AvatarInfo.CreateBuilder();

            ainfo.X   = (int)(pos.x * 100);
            ainfo.Z   = (int)(pos.z * 100);
            ainfo.Y   = (int)(pos.y * 100);
            ainfo.Dir = dir;
            ainfo.HP  = meAttr.HP;

            cg.AvatarInfo = ainfo.Build();

            var s = WorldManager.worldManager.GetActive();

            s.BroadcastMsg(cg);
        }
Пример #7
0
        public IEnumerator Connect(string ip1, int port1, int userId)
        {
            client   = new UdpClient();
            endPoint = new IPEndPoint(IPAddress.Parse(ip1), port1);
            try
            {
                client.Connect(endPoint);
            } catch (Exception ex)
            {
                Debug.LogError(ex.ToString());
            }
            myId = userId;

            while (true)
            {
                var cg = CGPlayerCmd.CreateBuilder();
                cg.Cmd = "UDP";
                var ainfo = AvatarInfo.CreateBuilder();
                ainfo.Id      = userId;
                cg.AvatarInfo = ainfo.Build();
                var packet = KBEngine.Bundle.GetPacketFid(cg);
                Send(packet.data);
                var s   = WorldManager.worldManager.GetActive();
                var ret = new bool[] { false };
                yield return(s.StartCoroutine(WaitTimeOut(packet.flowId, ret)));

                if (ret [0])
                {
                }
                else
                {
                }
            }
        }
Пример #8
0
        /// <summary>
        /// 0 right
        /// 1 up
        /// 2 left
        /// 3 down
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public async Task MoveTo(int id, CGPlayerCmd cmd)
        {
            await _messageQueue;
            var   pos    = Convert.ToInt32(cmd.Cmd.Split(' ')[1]);
            var   player = PlayerMap[id];

            switch (pos)
            {
            case 0:
                player.playerState.px += 1;
                break;

            case 1:
                player.playerState.py += 1;
                break;

            case 2:
                player.playerState.px -= 1;
                break;

            case 3:
                player.playerState.py -= 1;
                break;

            default:
                break;
            }

            //交由每帧的同步消息取去发送同步命令
            //var cmd3 = GCPlayerCmd.CreateBuilder();
            //cmd3.Result = $"MoveTo {id} {player.px} {player.py}";
            //BroadcastToAll(cmd3);
        }
Пример #9
0
    public void MoveTo(int dir)
    {
        var cmd = CGPlayerCmd.CreateBuilder();

        cmd.Cmd = "MoveTo " + dir;
        NetworkScene.Instance.SendPacket(cmd);
    }
Пример #10
0
        IEnumerator SendReady()
        {
            while (ObjectManager.objectManager.GetMyPlayer() == null)
            {
                yield return(null);
            }
            //等待NetworkLoadZone 初始化完成
            yield return(new WaitForSeconds(0.2f));

            rc.evtHandler = EvtHandler;
            rc.msgHandler = MsgHandler;
            state         = WorldState.Connected;
            myId          = NetMatchScene.Instance.myId;
            ObjectManager.objectManager.RefreshMyServerId(myId);



            var cg = CGPlayerCmd.CreateBuilder();

            cg.Cmd = "Ready";
            var data = KBEngine.Bundle.GetPacket(cg);

            rc.Send(data);
            SendUserData();
            yield return(StartCoroutine(SendCommandToServer()));
        }
Пример #11
0
        public static void GameOver()
        {
            var cg = CGPlayerCmd.CreateBuilder();

            cg.Cmd = "GameOver";
            WorldManager.worldManager.GetActive().BroadcastMsg(cg);
        }
Пример #12
0
    private IEnumerator TestConnect()
    {
        var cg = CGPlayerCmd.CreateBuilder();

        cg.Cmd = "TestUDP";
        var avtar = AvatarInfo.CreateBuilder();
        var myId  = NetworkScene.Instance.myId;

        avtar.Id      = myId;
        cg.AvatarInfo = avtar.Build();

        SendPacket(cg);
        var waitTime = 0.0f;

        Log.Net("UDP TestBegin");
        while (!Connected && waitTime < 10)
        {
            yield return(null);

            waitTime += Time.deltaTime;
        }
        Log.Net("UDP Connnect: " + Connected);
        if (Connected)
        {
            var cg1 = CGPlayerCmd.CreateBuilder();
            cg1.Cmd = "UDPConnect";
            NetworkUtil.Broadcast(cg1);
            Util.ShowMsg("开始使用UDP同步");
        }
        else
        {
            Close();
            Util.ShowMsg("UDP测试失败,使用TCP通信");
        }
    }
Пример #13
0
    public void MakeMoveMethod()
    {
        var cmd = CGPlayerCmd.CreateBuilder();

        cmd.Cmd = string.Format("MakeMove {0}", pos);
        NetworkScene.Instance.SendPacket(cmd);
    }
Пример #14
0
        public static void FastMoveAndPos()
        {
            var s = WorldManager.worldManager.GetActive();

            if (s.IsNet)
            {
                var me = ObjectManager.objectManager.GetMyPlayer();
                if (me == null)
                {
                    return;
                }
                var pos = me.transform.position;
                var dir = (int)me.transform.localRotation.eulerAngles.y;

                var cg = CGPlayerCmd.CreateBuilder();
                cg.Cmd = "Move";
                var ainfo = AvatarInfo.CreateBuilder();
                ainfo.X       = (int)(pos.x * 100);
                ainfo.Z       = (int)(pos.z * 100);
                ainfo.Y       = (int)(pos.y * 100);
                ainfo.Dir     = dir;
                cg.AvatarInfo = ainfo.Build();

                s.BroadcastMsg(cg);
            }
        }
Пример #15
0
        public static void SyncTime(int leftTime)
        {
            var cg = CGPlayerCmd.CreateBuilder();

            cg.Cmd      = "SyncTime";
            cg.LeftTime = leftTime;
            WorldManager.worldManager.GetActive().BroadcastMsg(cg);
        }
Пример #16
0
 private void OnBuy()
 {
     if (curSelect != null)
     {
         var cg = CGPlayerCmd.CreateBuilder();
         cg.Cmd = "Buy " + curSelect.id;
         NetworkUtil.Broadcast(cg);
     }
 }
Пример #17
0
        public static void RemoveEntityToNetwork(KBEngine.KBNetworkView view)
        {
            var cg  = CGPlayerCmd.CreateBuilder();
            var ety = EntityInfo.CreateBuilder();

            ety.Id        = view.GetServerID();
            cg.EntityInfo = ety.Build();
            cg.Cmd        = "RemoveEntity";
            NetworkUtil.Broadcast(cg);
        }
Пример #18
0
    public void MatchMethod()
    {
        var cmd = CGPlayerCmd.CreateBuilder();

        cmd.Cmd = "Match";
        NetworkScene.Instance.SendPacket(cmd);

        uiText.text = "Matching";
        MainUI.Instance.onbutton = null;
    }
Пример #19
0
        /// <summary>
        /// 技能的朝向
        /// 攻击目标由服务器决定
        /// </summary>
        /// <param name="skillId"></param>
        /// <param name="skillLevel"></param>
        public static void FastUseSkill(int skillId, int skillLevel)
        {
            Log.Sys("FastUseSkill: " + skillId + " lev " + skillLevel);
            var sc = WorldManager.worldManager.GetActive();

            if (sc.IsNet && sc.CanFight)
            {
                var me  = ObjectManager.objectManager.GetMyPlayer();
                var pos = me.transform.position;
                var dir = (int)me.transform.localRotation.eulerAngles.y;

                /*
                 * var tower = me.GetComponent<TankPhysicComponent>().tower;
                 * if (tower == null)
                 * {
                 *  return;
                 * }
                 * var dir = (int)tower.transform.eulerAngles.y;
                 */


                var cg     = CGPlayerCmd.CreateBuilder();
                var skInfo = SkillAction.CreateBuilder();
                skInfo.Who        = ObjectManager.objectManager.GetMyServerID();
                skInfo.SkillId    = skillId;
                skInfo.SkillLevel = skillLevel;

                var ip = NetworkUtil.ConvertPos(pos);
                skInfo.X   = ip[0];
                skInfo.Y   = ip[1];
                skInfo.Z   = ip[2];
                skInfo.Dir = dir;

                /*
                 * if (skillId == 1)
                 * {
                 *  var attribute = me.GetComponent<NpcAttribute>();
                 *  skInfo.IsStaticShoot = attribute.GetStaticShootBuff();
                 * }
                 */

                var target   = SkillLogic.FindNearestEnemy(me);
                var targetId = 0;
                if (target != null)
                {
                    targetId = target.GetComponent <NpcAttribute>().GetNetView().GetServerID();
                }
                skInfo.Target = targetId;

                cg.SkillAction = skInfo.Build();
                cg.Cmd         = "Skill";
                //sc.BroadcastMsg(cg);
                sc.BroadcastKCP(cg);
            }
        }
Пример #20
0
        public async Task Skill(int id, CGPlayerCmd cmd)
        {
            await _messageQueue;

            var player = PlayerMap[id];
            var dir    = Convert.ToInt32(cmd.Cmd.Split(' ')[1]);

            var bullet = new Bullet(this, id, player.playerState.px, player.playerState.py, dir);

            bulletMap.Add(bullet.id, bullet);
        }
Пример #21
0
        public static void Dead(int last)
        {
            var cg = CGPlayerCmd.CreateBuilder();

            cg.Cmd = "Dead";
            var dinfo = DamageInfo.CreateBuilder();

            dinfo.Attacker = last;
            dinfo.Enemy    = ObjectManager.objectManager.GetMyAttr().GetNetView().GetServerID();
            cg.DamageInfo  = dinfo.Build();
            WorldManager.worldManager.GetActive().BroadcastMsg(cg);
        }
Пример #22
0
        void SendUserData()
        {
            Debug.LogError("SendUserData");
            if (state != WorldState.Connected)
            {
                return;
            }
            if (rc == null)
            {
                return;
            }

            var me  = ObjectManager.objectManager.GetMyPlayer();
            var pos = me.transform.position;
            var dir = (int)me.transform.localRotation.eulerAngles.y;
            var cg  = CGPlayerCmd.CreateBuilder();

            cg.Cmd = "InitData";
            var ainfo = AvatarInfo.CreateBuilder();

            if (NetDebug.netDebug.IsTest)
            {
                ainfo.IsRobot = true;
            }
            else
            {
                ainfo.IsRobot = false;
            }

            ainfo.Dir  = dir;
            ainfo.Name = ServerData.Instance.playerInfo.Roles.Name;

            var pinfo = ServerData.Instance.playerInfo;

            foreach (var d in pinfo.DressInfoList)
            {
                ainfo.DressInfoList.Add(d);
            }
            ainfo.Level = ObjectManager.objectManager.GetMyProp(CharAttribute.CharAttributeEnum.LEVEL);
            ainfo.HP    = ObjectManager.objectManager.GetMyProp(CharAttribute.CharAttributeEnum.HP);
            ainfo.Job   = ServerData.Instance.playerInfo.Roles.Job;

            cg.AvatarInfo = ainfo.Build();
            var sync = me.GetComponent <MeSyncToServer>();

            sync.InitData(cg.AvatarInfo);

            Bundle bundle;
            var    data = KBEngine.Bundle.GetPacket(cg, out bundle);

            rc.Send(data, bundle);
        }
Пример #23
0
    public void StartMatch()
    {
        if (state == State.Wait)
        {
            state = State.Matching;
            var cmd = CGPlayerCmd.CreateBuilder();
            cmd.Cmd = "Match";
            NetworkScene.Instance.SendPacket(cmd);

            uiText.text = "Matching";
            MainUI.Instance.onbutton = null;
        }
    }
Пример #24
0
        IEnumerator StartMatch()
        {
            var cg = CGPlayerCmd.CreateBuilder();

            cg.Cmd = "Match";
            var data = KBEngine.Bundle.GetPacketFull(cg);

            yield return(StartCoroutine(rc.SendWaitResponse(data.data, data.fid, (packet) =>
            {
                var cmd = packet.protoBody as GCPlayerCmd;
                roomInfo = cmd.RoomInfo;
            })));
        }
Пример #25
0
        public static void FastDamage(int attackerId, int enemyId, int damage, bool isCritical)
        {
            var cg    = CGPlayerCmd.CreateBuilder();
            var dinfo = DamageInfo.CreateBuilder();

            dinfo.Attacker   = attackerId;
            dinfo.Enemy      = enemyId;
            dinfo.Damage     = damage;
            dinfo.IsCritical = isCritical;
            cg.DamageInfo    = dinfo.Build();
            cg.Cmd           = "Damage";
            WorldManager.worldManager.GetActive().BroadcastMsg(cg);
        }
Пример #26
0
        public static void RemoveBuffId(int buffId, GameObject target)
        {
            var cg    = CGPlayerCmd.CreateBuilder();
            var binfo = BuffInfo.CreateBuilder();

            binfo.BuffId = buffId;
            binfo.Target = target.GetComponent <KBEngine.KBNetworkView>().GetServerID();

            cg.BuffInfo = binfo.Build();
            cg.Cmd      = "RemoveBuff";
            var sc = WorldManager.worldManager.GetActive();

            sc.BroadcastMsg(cg);
        }
Пример #27
0
        public void UpdateDataMethod()
        {
            var cg = CGPlayerCmd.CreateBuilder();

            cg.Cmd = "UpdateData";
            var ainfo = AvatarInfo.CreateBuilder();

            ainfo.X       = p++;
            ainfo.Z       = p++;
            cg.AvatarInfo = ainfo.Build();
            var data = KBEngine.Bundle.GetPacket(cg);

            rc.Send(data);
        }
Пример #28
0
    private IEnumerator TestConnect()
    {
        var cg = CGPlayerCmd.CreateBuilder();

        cg.Cmd = "TestKCP";
        var avtar = AvatarInfo.CreateBuilder();
        var myId  = NetMatchScene.Instance.myId;

        avtar.Id      = myId;
        cg.AvatarInfo = avtar.Build();
        Bundle bundle;
        var    data = Bundle.GetPacketFull(cg, out bundle);

        Bundle.ReturnBundle(bundle);
        var pkData = data.data;

        SendPacketData(pkData);

        var waitTime = 0.0f;

        Log.Net("KCP TestBegin");
        var askTime = 0.0f;

        while (!Connected && waitTime < 10)
        {
            yield return(null);

            waitTime += Time.deltaTime;
            askTime  += Time.deltaTime;
            if (askTime >= 1f)
            {
                askTime = 0;
                SendPacketData(pkData);
            }
        }

        waitTime = 0;
        if (Connected)
        {
            Log.Net("KCP TestSuc");
            var cg1 = CGPlayerCmd.CreateBuilder();
            cg1.Cmd = "KCPConnect";
            NetworkUtil.Broadcast(cg1);
        }
        else
        {
            Close();
            Log.Net("KCP Test Fail");
        }
    }
Пример #29
0
        private void PickByMe()
        {
            var whoAttr = ObjectManager.objectManager.GetMyAttr();
            var cg      = CGPlayerCmd.CreateBuilder();

            cg.Cmd = "Pick";
            var pickAction = PickItemAction.CreateBuilder();

            pickAction.Id      = netView.GetServerID();
            pickAction.ItemId  = itemData.ObjectId;
            pickAction.ItemNum = num;
            pickAction.Who     = whoAttr.GetComponent <KBEngine.KBNetworkView>().GetServerID();
            cg.PickAction      = pickAction.Build();
            NetworkUtil.Broadcast(cg);
        }
Пример #30
0
        public static void FastUseSkill(int skillId)
        {
            var sc = WorldManager.worldManager.GetActive();

            if (sc.IsNet)
            {
                var cg     = CGPlayerCmd.CreateBuilder();
                var skInfo = SkillAction.CreateBuilder();
                skInfo.Who     = ObjectManager.objectManager.GetMyServerID();
                skInfo.SkillId = skillId;
                cg.SkillAction = skInfo.Build();
                cg.Cmd         = "Skill";
                sc.BroadcastMsg(cg);
            }
        }