查看: 70|回复: 0

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

[复制链接]
  • TA的每日心情
    开心
    2024-5-10 09:55
  • 签到天数: 37 天

    连续签到: 3 天

    [LV.5]常住居民I

    2028

    主题

    32

    回帖

    7260

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    0
    卡币
    5184
    OK点
    16
    积分
    7260
    发表于 同元九百九十六年二月三十日(春) | 显示全部楼层 |阅读模式
    但是这里并没有用什么诸如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.     var passedXs = new Array();
    19.     var passedYs = new Array();
    20.     var dirOfNext;
    21.     var _is_passedX;
    22.     var _is_passedY;
    23.     var _xNow;
    24.     var _yNow;
    25.     var _leftOrRight;
    26.     var _upOrDown;
    27.     //var _Markov;
    28.     Game_Character.prototype.addPassedArray = function(x,y)
    29.     {
    30.         passedXs[passedXs.length] = x;
    31.         passedYs[passedYs.length] = y;
    32.         if (passedXs.length > 3)
    33.             passedXs.shift();
    34.         if (passedYs.length > 3)
    35.             passedYs.shift();
    36.     };
    37.     Game_Character.prototype.isPassed = function(_x,_y,d)
    38.     {
    39.         const x2 = $gameMap.roundXWithDirection(_x, d);
    40.         const y2 = $gameMap.roundYWithDirection(_y, d);
    41.         for (var i = 0; i < passedYs.length; i++)
    42.         {
    43.             if (passedXs[i] == x2 && passedYs[i] == y2)
    44.             {
    45.                 return true;
    46.             }
    47.         }
    48.         return false;
    49.     };
    50.     Game_Character.prototype.hasDir = function(dir)
    51.     {
    52.         if (dir == 2 || dir == 4 || dir == 6|| dir == 8)
    53.             return true;
    54.         else
    55.             return false;
    56.     }
    57.     Game_Character.prototype.newPath = function(_directOfLength,sx,sy,_x,_y)
    58.     {
    59.         var dirX = sx > 0 ? 6 : 4
    60.         var dirY = sy > 0 ? 2 : 8
    61.         var dirOfNextX = sx > 0 ? 4 : 6
    62.         var dirOfNextY = sy > 0 ? 8 : 2
    63.         if (_directOfLength)
    64.         {
    65.             this.moveStraight(dirX);
    66.             if (!this.isMovementSucceeded())
    67.             {
    68.                 this.moveStraight(dirY);
    69.                 this.addPassedArray(_x,_y);
    70.                 return dirOfNextX
    71.             }
    72.             else
    73.             {
    74.                 this.addPassedArray(_x,_y);
    75.                 return dirOfNextY
    76.             }
    77.         }
    78.         else
    79.         {
    80.             this.moveStraight(dirY);
    81.             if (!this.isMovementSucceeded())
    82.             {
    83.                 this.moveStraight(dirX);
    84.                 this.addPassedArray(_x,_y);
    85.                 return dirOfNextY
    86.             }
    87.             else
    88.             {
    89.                 this.addPassedArray(_x,_y);
    90.                 return dirOfNextX
    91.             }
    92.         }
    93.     };
    94.     Game_Character.prototype.moveTowardCharacter = function(character)
    95.     {
    96.         /*
    97.         if (Math.random() < 0.05)
    98.         {
    99.             _Markov = Math.random();
    100.             if (_Markov < 0.25)
    101.                 dirOfNext = 2;
    102.             else if(_Markov < 0.5)
    103.                 dirOfNext = 4;
    104.             else if(_Markov < 0.5)
    105.                 dirOfNext = 6;
    106.             else if(_Markov < 0.5)
    107.                 dirOfNext = 8;
    108.         }
    109.         */
    110.         const sx = this.deltaXFrom(character.x);
    111.         const sy = this.deltaYFrom(character.y);
    112.         //console.log(dirOfNext);
    113.         //console.log(passedXs);
    114.         //console.log(passedYs);
    115.         if (this.hasDir(dirOfNext))
    116.         {
    117.             console.log("0000000000000");
    118.             if (this.canPass(this._x, this._y, dirOfNext))
    119.                 this.moveStraight(dirOfNext);
    120.             dirOfNext = 0;
    121.         }
    122.         else if (sx == 0)
    123.         {
    124.             _is_passedY = sy > 0 ? 8 : 2;
    125.             if (this.isPassed(this._x,this._y,_is_passedY))
    126.             {   
    127.                 _is_passedY = sy > 0 ? 2 : 8;
    128.                 this.addPassedArray(this._x,this._y);
    129.                 _leftOrRight = Math.random();
    130.                 if(_leftOrRight 0 ? 8 : 2;
    131.                     this.moveStraight(6);
    132.                     if (this.isMovementSucceeded())
    133.                     {
    134.                         this.addPassedArray(_xNow,_yNow);
    135.                     }
    136.                 }
    137.             }
    138.         }
    139.         else if (sy == 0)
    140.         {
    141.             _is_passedX = sx > 0 ? 4 : 6;
    142.             if (this.isPassed(this._x,this._y,_is_passedX))
    143.             {
    144.                 _is_passedX = sx > 0 ? 6 : 4;
    145.                 this.addPassedArray(this._x,this._y);
    146.                 _upOrDown = Math.random();
    147.                 if(_upOrDown 0 ? 4 : 6;
    148.                     this.moveStraight(2);
    149.                     if (this.isMovementSucceeded())
    150.                     {
    151.                         this.addPassedArray(_xNow,_yNow);
    152.                     }
    153.                 }
    154.             }
    155.         }
    156.         else if (Math.abs(sx) > Math.abs(sy))
    157.         {
    158.             _is_passedX = sx > 0 ? 4 : 6;
    159.             if (this.isPassed(this._x,this._y,_is_passedX))
    160.             {
    161.                 _is_passedX = sx > 0 ? 6 : 4;
    162.                 this.addPassedArray(this._x,this._y);
    163.                 dirOfNext = sy > 0 ? 8 : 2;
    164.             }
    165.             this.moveStraight(_is_passedX);
    166.             if (!this.isMovementSucceeded()&& sy !== 0)
    167.             {
    168.                 _is_passedY = (sy > 0 ? 8 : 2);
    169.                 if (this.isPassed(this._x,this._y,_is_passedY))
    170.                 {   
    171.                     _is_passedY = sy > 0 ? 2 : 8;
    172.                     this.addPassedArray(this._x,this._y);
    173.                     dirOfNext = sx > 0 ? 4 : 6;
    174.                 }
    175.                 this.moveStraight(_is_passedY);
    176.                 if (!this.isMovementSucceeded())
    177.                 {   
    178.                     dirOfNext = this.newPath(false, sx, sy, this._x, this._y);//强制离开此格
    179.                 }
    180.             }
    181.         }
    182.         else if (sy !== 0)
    183.         {
    184.             _is_passedY = sy > 0 ? 8 : 2;
    185.             if (this.isPassed(this._x,this._y,_is_passedY))
    186.             {   
    187.                 _is_passedY = sy > 0 ? 2 : 8;
    188.                 this.addPassedArray(this._x,this._y);
    189.                 dirOfNext = sx > 0 ? 4 : 6;
    190.                 console.log(dirOfNext);
    191.             }
    192.             this.moveStraight(_is_passedY);
    193.             if (!this.isMovementSucceeded() && sx !== 0)
    194.             {
    195.                 _is_passedX = sx > 0 ? 4 : 6;
    196.                 if (this.isPassed(this._x,this._y,_is_passedX))
    197.                 {   
    198.                     _is_passedX = sx > 0 ? 6 : 4;
    199.                     this.addPassedArray(this._x,this._y);
    200.                     dirOfNext = sy > 0 ? 8 : 2;
    201.                 }
    202.                 this.moveStraight(_is_passedX);
    203.                 if (!this.isMovementSucceeded())
    204.                 {   
    205.                     dirOfNext = this.newPath(true,sx, sy,this._x,this._y);
    206.                 }
    207.             }
    208.         }
    209.     };
    210. })();复制代码
    复制代码
                 本帖来自P1论坛作者wtyliangting,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=483088  若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
    天天去同能,天天有童年!
    回复 论坛版权

    使用道具 举报

    ahome_bigavatar:guest
    ahome_bigavatar:welcomelogin
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2024-5-20 11:30 , Processed in 0.050189 second(s), 41 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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