• 机器人的异步代码同步实现


    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;
            }
    

      

  • 相关阅读:
    平衡二叉树
    二叉树的深度
    数字在升序数组中出现的次数
    美国最受雇主欢迎的十大编程语言
    重学数据结构(五、串)
    重学数据结构(四、数组和广义表)
    100个高频Spring面试题
    重学数据结构(三、队列)
    重学数据结构(二、栈)
    Java学习之jackson篇
  • 原文地址:https://www.cnblogs.com/nywd/p/2362522.html
Copyright © 2020-2023  润新知