设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 2175|回复: 1
打印 上一主题 下一主题

[原创发布] [不怎么成功的尝试]增强接近玩家使其能绕开障碍

[复制链接]

Lv2.观梦者

梦石
0
星屑
351
在线时间
68 小时
注册时间
2020-7-17
帖子
8
跳转到指定楼层
1
发表于 2020-8-28 22:39:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
但是这里并没有用什么诸如B*之类寻路算法,一方面是我怕影响性能,但更重要的是我很懒(我真的不想探究Game_Map.prototype.checkPassage这个里面的东西了www)...
请注意,这个虽然能一定程度上使其绕过障碍,但如果你和事件离得很远,或者你们之间直线距离很短但实际距离很长,那这个事件很有可能转圈圈或者左右横跳。
个人第一次写给别人看的代码,我得承认里面确实有很多地方考虑不够,比如那些var究竟会不会有什么坏影响我也吃不准,各位大佬如果要用的话还是可以继续改进的。


  1. //=============================================================================
  2. // RPG Maker MZ - Moving Toward Player More Intelligent
  3. //=============================================================================

  4. /*:
  5. * @target MZ
  6. * @plugindesc Moving toward player more intelligent.
  7. * @author 那只野生loli
  8. *
  9. * @help
  10. *
  11. * This plugin make character moving toward player more intelligent.
  12. * It was rewrited Game_Character.prototype.moveTowardCharacter.
  13. *
  14. *
  15. * It does not provide plugin commands.
  16. */

  17. (() => {
  18.    
  19.     var passedXs = new Array();
  20.     var passedYs = new Array();
  21.     var dirOfNext;
  22.     var _is_passedX;
  23.     var _is_passedY;
  24.     var _xNow;
  25.     var _yNow;
  26.     var _leftOrRight;
  27.     var _upOrDown;
  28.     //var _Markov;

  29.     Game_Character.prototype.addPassedArray = function(x,y)
  30.     {
  31.         passedXs[passedXs.length] = x;
  32.         passedYs[passedYs.length] = y;
  33.         if (passedXs.length > 3)
  34.             passedXs.shift();
  35.         if (passedYs.length > 3)
  36.             passedYs.shift();
  37.     };

  38.     Game_Character.prototype.isPassed = function(_x,_y,d)
  39.     {
  40.         const x2 = $gameMap.roundXWithDirection(_x, d);
  41.         const y2 = $gameMap.roundYWithDirection(_y, d);
  42.         
  43.         for (var i = 0; i < passedYs.length; i++)
  44.         {
  45.             if (passedXs[i] == x2 && passedYs[i] == y2)
  46.             {
  47.                 return true;
  48.             }
  49.         }
  50.         return false;
  51.     };

  52.     Game_Character.prototype.hasDir = function(dir)
  53.     {
  54.         if (dir == 2 || dir == 4 || dir == 6|| dir == 8)
  55.             return true;
  56.         else
  57.             return false;
  58.     }

  59.     Game_Character.prototype.newPath = function(_directOfLength,sx,sy,_x,_y)
  60.     {
  61.         var dirX = sx > 0 ? 6 : 4
  62.         var dirY = sy > 0 ? 2 : 8
  63.         var dirOfNextX = sx > 0 ? 4 : 6
  64.         var dirOfNextY = sy > 0 ? 8 : 2
  65.         
  66.         if (_directOfLength)
  67.         {
  68.             this.moveStraight(dirX);
  69.             if (!this.isMovementSucceeded())
  70.             {
  71.                 this.moveStraight(dirY);
  72.                 this.addPassedArray(_x,_y);
  73.                 return dirOfNextX
  74.             }
  75.             else
  76.             {
  77.                 this.addPassedArray(_x,_y);
  78.                 return dirOfNextY
  79.             }
  80.         }
  81.         else
  82.         {
  83.             this.moveStraight(dirY);
  84.             if (!this.isMovementSucceeded())
  85.             {
  86.                 this.moveStraight(dirX);
  87.                 this.addPassedArray(_x,_y);
  88.                 return dirOfNextY
  89.             }
  90.             else
  91.             {
  92.                 this.addPassedArray(_x,_y);
  93.                 return dirOfNextX
  94.             }
  95.         }
  96.     };

  97.     Game_Character.prototype.moveTowardCharacter = function(character)
  98.     {
  99.         /*
  100.         if (Math.random() < 0.05)
  101.         {
  102.             _Markov = Math.random();
  103.             if (_Markov < 0.25)
  104.                 dirOfNext = 2;
  105.             else if(_Markov < 0.5)
  106.                 dirOfNext = 4;
  107.             else if(_Markov < 0.5)
  108.                 dirOfNext = 6;
  109.             else if(_Markov < 0.5)
  110.                 dirOfNext = 8;
  111.         }
  112.         */
  113.         const sx = this.deltaXFrom(character.x);
  114.         const sy = this.deltaYFrom(character.y);
  115.         //console.log(dirOfNext);
  116.         //console.log(passedXs);
  117.         //console.log(passedYs);
  118.         if (this.hasDir(dirOfNext))
  119.         {
  120.             console.log("0000000000000");
  121.             if (this.canPass(this._x, this._y, dirOfNext))
  122.                 this.moveStraight(dirOfNext);
  123.             dirOfNext = 0;
  124.         }

  125.         else if (sx == 0)
  126.         {
  127.             _is_passedY = sy > 0 ? 8 : 2;
  128.             if (this.isPassed(this._x,this._y,_is_passedY))
  129.             {   
  130.                 _is_passedY = sy > 0 ? 2 : 8;
  131.                 this.addPassedArray(this._x,this._y);
  132.                 _leftOrRight = Math.random();
  133.                 if(_leftOrRight<0.5)
  134.                     dirOfNext = 4;
  135.                 else
  136.                     dirOfNext = 6;
  137.                 console.log(dirOfNext);
  138.             }

  139.             this.moveStraight(_is_passedY);
  140.             if (!this.isMovementSucceeded())
  141.             {
  142.                 _xNow = this._x;
  143.                 _yNow = this._y;
  144.                 _leftOrRight = Math.random();
  145.                 if(_leftOrRight<0.5)
  146.                 {
  147.                     dirOfNext = sy > 0 ? 8 : 2;
  148.                     this.moveStraight(4);
  149.                     if (this.isMovementSucceeded())
  150.                     {
  151.                         this.addPassedArray(_xNow,_yNow);
  152.                     }
  153.                 }
  154.                 else
  155.                 {
  156.                     dirOfNext = sy > 0 ? 8 : 2;
  157.                     this.moveStraight(6);
  158.                     if (this.isMovementSucceeded())
  159.                     {
  160.                         this.addPassedArray(_xNow,_yNow);
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.         
  166.         else if (sy == 0)
  167.         {
  168.             _is_passedX = sx > 0 ? 4 : 6;
  169.             if (this.isPassed(this._x,this._y,_is_passedX))
  170.             {
  171.                 _is_passedX = sx > 0 ? 6 : 4;
  172.                 this.addPassedArray(this._x,this._y);
  173.                 _upOrDown = Math.random();
  174.                 if(_upOrDown<0.5)
  175.                     dirOfNext = 8;
  176.                 else
  177.                     dirOfNext = 2;
  178.             }
  179.             
  180.             this.moveStraight(_is_passedX);
  181.             if (!this.isMovementSucceeded())
  182.             {
  183.                 _xNow = this._x;
  184.                 _yNow = this._y;
  185.                 _upOrDown = Math.random();
  186.                 if(_upOrDown<0.5)
  187.                 {
  188.                     dirOfNext = sx > 0 ? 4 : 6;
  189.                     this.moveStraight(8);
  190.                     if (this.isMovementSucceeded())
  191.                     {
  192.                         this.addPassedArray(_xNow,_yNow);
  193.                     }
  194.                 }
  195.                 else
  196.                 {
  197.                     dirOfNext = sx > 0 ? 4 : 6;
  198.                     this.moveStraight(2);
  199.                     if (this.isMovementSucceeded())
  200.                     {
  201.                         this.addPassedArray(_xNow,_yNow);
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.         
  207.         else if (Math.abs(sx) > Math.abs(sy))
  208.         {
  209.             _is_passedX = sx > 0 ? 4 : 6;
  210.             if (this.isPassed(this._x,this._y,_is_passedX))
  211.             {
  212.                 _is_passedX = sx > 0 ? 6 : 4;
  213.                 this.addPassedArray(this._x,this._y);
  214.                 dirOfNext = sy > 0 ? 8 : 2;
  215.             }
  216.             this.moveStraight(_is_passedX);
  217.             if (!this.isMovementSucceeded()&& sy !== 0)
  218.             {
  219.                 _is_passedY = (sy > 0 ? 8 : 2);
  220.                 if (this.isPassed(this._x,this._y,_is_passedY))
  221.                 {   
  222.                     _is_passedY = sy > 0 ? 2 : 8;
  223.                     this.addPassedArray(this._x,this._y);
  224.                     dirOfNext = sx > 0 ? 4 : 6;
  225.                 }
  226.                 this.moveStraight(_is_passedY);
  227.                 if (!this.isMovementSucceeded())
  228.                 {   
  229.                     dirOfNext = this.newPath(false, sx, sy, this._x, this._y);//强制离开此格

  230.                 }
  231.             }
  232.         }
  233.         
  234.         else if (sy !== 0)
  235.         {
  236.             _is_passedY = sy > 0 ? 8 : 2;
  237.             if (this.isPassed(this._x,this._y,_is_passedY))
  238.             {   
  239.                 _is_passedY = sy > 0 ? 2 : 8;
  240.                 this.addPassedArray(this._x,this._y);
  241.                 dirOfNext = sx > 0 ? 4 : 6;
  242.                 console.log(dirOfNext);
  243.             }
  244.             this.moveStraight(_is_passedY);
  245.             if (!this.isMovementSucceeded() && sx !== 0)
  246.             {
  247.                 _is_passedX = sx > 0 ? 4 : 6;
  248.                 if (this.isPassed(this._x,this._y,_is_passedX))
  249.                 {   
  250.                     _is_passedX = sx > 0 ? 6 : 4;
  251.                     this.addPassedArray(this._x,this._y);
  252.                     dirOfNext = sy > 0 ? 8 : 2;
  253.                 }
  254.                 this.moveStraight(_is_passedX);
  255.                 if (!this.isMovementSucceeded())
  256.                 {   
  257.                     dirOfNext = this.newPath(true,sx, sy,this._x,this._y);
  258.                 }
  259.             }
  260.         }
  261.     };

  262. })();
复制代码

MovingTowardPlayerMoreIntelligent.zip

1.54 KB, 下载次数: 57

Lv3.寻梦者

梦石
0
星屑
1232
在线时间
1017 小时
注册时间
2011-4-30
帖子
1516
2
发表于 2020-9-10 00:19:38 | 只看该作者
其实直接用mz默认的寻路就不错,
this.findDirectionTo(x,y)
返回的是一个方向值

之前写过一个mv的事件向实现应该可以参考
https://rpg.blue/thread-405972-1-1.html

并行时
this.moveStraight(this.findDirectionTo(10, 5))
this.pos(10,5)&&this.setMoveRoute(null)

非并行时防卡(卡时跳出)
this.moveStraight(this.findDirectionTo(10, 5));!this.isMovementSucceeded()&&this.setMoveRoute(null)
this.pos(10,5)&&this.setMoveRoute(null)
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-10 18:19

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表