TA的每日心情 | 开心 2022-8-5 09:24 |
---|
注册会员
- 积分
- 77
- 金钱
- 69
- 元宝
- 10
|
本帖最后由 feijimj 于 2022-7-13 14:32 编辑
根据龙哥寻路贴子LOMCN论坛寻路算法转载
代码本身是传2的代码,但是都是吉米的所以挪过来用是可以的,只需代码轻微改动,然后我就把带弄到我的原版代码里了!
因为龙已经在教程里说得很详细了,所以在这里就不赘述了!
这里要说的是照搬过来以后出现的寻路过程中出现卡顿的问题,群里茶密大佬告诉我是移动的格子和位置不统一的问题,可是我昨天下午debug了一下午也没看出问题来~!
下面说说改动的文件,希望大佬帮我分析一下
PathFinder.cs
修改了调用的类库,其他未做更改
BigMapDialog.cs
地图添加Image_MouseClick,Image_MouseClick事件代码,鼠标右键触发事件
- private void Image_MouseClick(object sender, MouseEventArgs e)
- {
- //if (SelectedInfo != GameScene.Game.MapControl.MapInfo) return;
- //if (MapObject.User.Buffs.All(z => z.Type != BuffType.Developer))
- //if (!SelectedInfo.AllowRT || !SelectedInfo.AllowTT || !GameScene.Game.MapControl.MapInfo.AllowRT || !GameScene.Game.MapControl.MapInfo.AllowTT) return;
- if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
- {
- //TODO Teleport Ring
- int x = (int)((e.Location.X - Image.DisplayArea.X) / ScaleX);
- int y = (int)((e.Location.Y - Image.DisplayArea.Y) / ScaleY);
- //CEnvir.Enqueue(new C.TeleportRing { Location = new Point(x, y), Index = SelectedInfo.Index });
-
- var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, new Point(x, y));
- if (path == null || path.Count == 0)
- {
- GameScene.Game.ReceiveChat("寻路失败,请再试...", MessageType.System);
- GameScene.Game.MapControl.AutoPath = false;
- }
- else
- {
- GameScene.Game.MapControl.CurrentPath = path;
- GameScene.Game.MapControl.AutoPath = true;
- }
- }
- }
复制代码
Draw部分和龙哥贴里一样
MapControl.cs
在public void ProcessInput()方法中添加
- if (AutoPath)
- {
- if (CurrentPath == null || CurrentPath.Count == 0)
- {
- AutoPath = false;
- return;
- }
- //var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, new Point(x, y));
- var path = GameScene.Game.MapControl.PathFinder.FindPath(MapObject.User.CurrentLocation, CurrentPath.Last().Location);
-
- if (path != null && path.Count > 0)
- GameScene.Game.MapControl.CurrentPath = path;
- else
- {
- AutoPath = false;
- return;
- }
- Node currentNode = CurrentPath.SingleOrDefault(x => User.CurrentLocation == x.Location);
- if (currentNode != null)
- {
- while (true)
- {
- Node first = CurrentPath.First();
- CurrentPath.Remove(first);
- if (first == currentNode)
- break;
- }
- }
- if (CurrentPath.Count > 0)
- {
- MirDirection dir = Functions.DirectionFromPoint(User.CurrentLocation, CurrentPath.First().Location);
- //EEOO Edit FindPath-Move
-
- int steps = 1;
- if (GameScene.Game.CanRun
- && CEnvir.Now >= User.NextRunTime
- && User.BagWeight <= User.Stats[Stat.BagWeight]
- && User.WearWeight <= User.Stats[Stat.WearWeight]
- && (Math.Abs(User.CurrentLocation.X- CurrentPath.Last().Location.X)>1
- || Math.Abs(User.CurrentLocation.Y - CurrentPath.Last().Location.Y)>1))
- {
- steps++;
- if (User.Horse != HorseType.None)
- steps++;
- }
- for (int i = 1; i <= steps; i++)
- {
- if (CanMove(dir, i)) continue;
- MirDirection best1 = MouseDirectionBest(dir, 1);
- if (best1 == dir)
- {
- if (i == 1)
- {
- if (dir != User.Direction)
- MapObject.User.AttemptAction(new ObjectAction(MirAction.Standing, dir, MapObject.User.CurrentLocation));
- return;
- }
- steps = i - 1;
- }
- else
- {
- steps = 1;
- }
- dir = best1;
- break;
- }
- MapObject.User.AttemptAction(new ObjectAction(MirAction.Moving, dir, Functions.Move(MapObject.User.CurrentLocation, dir, steps), steps, MagicType.None));
-
- }
- }
复制代码
修改了寻路部分,在每次中重新设置路径,然后遇到怪物和障碍物时,能自动避让,但是问题就是卡顿?求大佬帮忙分析一下~!
需要贴啥代码您说...我贴出来!
|
|