鸟寅

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
public async Task MoveToAsync(float destX, float destY)
        {
            var x = PlayerEntity.GetProp<float>(CsPropDef.X);
            var y = PlayerEntity.GetProp<float>(CsPropDef.Y);
            var src = new Vector2(x, y);
            var dest = new Vector2(destX, destY);

            var seeks = PathSeeker.GetInstance(SelectedRole.curScence.ToString()).Seek(src, dest);
            if (seeks == null)
                return;
            var pathQueue = new Queue<Vector2>(seeks);

            while (pathQueue.Count != 0)
            {
                dest = pathQueue.Dequeue();
                await MoveToAsync(src, dest);
                src = dest;
            }
        }

        private async Task MoveToAsync(Vector2 src, Vector2 dest)
        {
            MoveTo(src.X, src.Y, dest.X, dest.Y);

            var distance = Vector2.Distance(src, dest);
            var time = (int)(distance * 1.01f / PlayerEntity.GetProp<float>(CsPropDef.RunSpeed));

            await TaskEx.Delay(time + 1);

            PlayerEntity.props[CsPropDef.X] = dest.X;
            PlayerEntity.props[CsPropDef.Y] = dest.Y;
        }

        /// <summary>
        /// 移动到NPC
        /// </summary>
        /// <param name="npcId"></param>
        /// <returns></returns>
        public async Task<ObjId> MoveToNpc(int npcId)
        {
            var pos = GetNpcPos(npcId);
            await MoveToAsync(pos.X, pos.Y);
            var obj = GetObjByIdFromScene(npcId);
            return obj;
        }

        /// <summary>
        /// 移动到怪物并攻击
        /// </summary>
        /// <param name="monsterId"> </param>
        /// <returns></returns>
        public async Task MoveToMonsterAtt(int monsterId)
        {
            var pos = GetNpcPos(monsterId);
            await MoveToAsync(pos.X, pos.Y);
            var obj = GetObjByIdFromScene(monsterId);
            SelectTarget(obj);

            Castskill(GetDefaultSkill());
            //比对杀怪数量
            await MonsterDead();
            await MonsterAttWrong();
        }

        /// <summary>
        /// 打怪失败返回消息
        /// </summary>
        /// <returns>2-距离超出、4-CD中、其他-重新选择目标(目标无敌或者目标无效)</returns>
        private async Task<int> MonsterAttWrong()
        {
            var tcs = new TaskCompletionSource<VarList>();
            EventHandler<DataEventArgs<VarList>> d = (s, e) => tcs.TrySetResult(e.Arg1);
            OnMonsterCannotAttack += d;
            var acc = await tcs.Task;
            OnMonsterCannotAttack -= d;
            return acc.Int32(1);
        }

        /// <summary>
        /// 杀怪完成
        /// </summary>
        /// <returns></returns>
        public async Task MonsterDead()
        {
            var tcs = new TaskCompletionSource<object>();
            EventHandler d = (s, e) => tcs.TrySetResult(s);
            OnMonsterDead += d;
            var acc = await tcs.Task;
            OnMonsterDead -= d;
        }

  

posted on 2012-02-22 09:26  nywd  阅读(409)  评论(2编辑  收藏  举报