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

Project1

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

[通用发布] 【MV发布】瞎糊一个真·八方向脚本试试js怎么玩

[复制链接]

Lv4.逐梦者 (版主)

无限の剣制

梦石
0
星屑
9997
在线时间
5019 小时
注册时间
2013-2-28
帖子
5030

开拓者贵宾

跳转到指定楼层
1
发表于 2015-11-11 00:01:50 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

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

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

x
本帖最后由 VIPArcher 于 2015-11-11 09:32 编辑

JAVASCRIPT 代码复制
  1. //=============================================================================
  2. // VIPArcher_Dir8.js
  3. //=============================================================================
  4. /*:
  5.  * @plugindesc 真 · 八方向移动
  6.  * @author VIPArcher
  7.  *
  8.  * @param Dir8_flag
  9.  * @desc 使用八方向素材的标志
  10.  * @default %
  11.  *
  12.  * @param Dir8_Affix
  13.  * @desc 八方向素材文件后缀
  14.  * @default _8D
  15.  *
  16.  * @help 使用八方向的行走图素材文件名前面添加一个标志,
  17.  * 与之对应的8方向的素材与其同名并在后面添加后缀以区别。
  18.  * 例如默认的设置就是使用八方向的素材命名为:
  19.  * !$%VIPArcher.png
  20.  *
  21.  * 那么与之对应的八方向的素材就应命名为:
  22.  * !$%VIPArcher_8D.png
  23.  *
  24.  */
  25. (function() {
  26.     var parameters = PluginManager.parameters('VIPArcher_Dir8');
  27.     var dir8_flag = String(parameters['Dir8_flag'] || '%');
  28.     var dir8_affix = String(parameters['Dir8_Affix'] || '_8D');
  29.     ImageManager.isDir8Character = function(filename) {
  30.         var reg = new RegExp("^\[\\!\\$\\" + dir8_flag + "]+")
  31.         var sign = filename.match(reg);
  32.         return sign && sign[0].contains(dir8_flag);
  33.     };
  34.     Game_Player.prototype.moveByInput = function() {
  35.         if (!this.isMoving() && this.canMove()) {
  36.             var direction = Input.dir8
  37.             if (direction > 0) {
  38.                 $gameTemp.clearDestination();
  39.             } else if ($gameTemp.isDestinationValid()){
  40.                 var x = $gameTemp.destinationX();
  41.                 var y = $gameTemp.destinationY();
  42.                 direction = this.findDirectionTo(x, y);
  43.             }
  44.             if (direction > 0) {
  45.                 if (direction % 2 == 0){
  46.                     this.moveStraight(direction);
  47.                     return;
  48.                 }
  49.                 if (direction < 5){
  50.                     this.moveDiagonally(direction + 3 , 2);
  51.                 } else{
  52.                     this.moveDiagonally(direction - 3 , 8);
  53.                 }
  54.             }
  55.         }
  56.     };
  57.     var dir8_move_diagonal = Game_Player.prototype.moveDiagonally;
  58.     Game_Player.prototype.moveDiagonally = function(horz, vert) {
  59.         if (!this.canPass(this._x, this._y, horz) && !this.canPass(this._x, this._y, vert)){
  60.             this.setMovementSuccess(false);
  61.             return;
  62.         }
  63.         if (this.canPass(this._x, this._y, horz) && !this.canPass(this._x, this._y, vert)){
  64.             this.moveStraight(horz);
  65.             return;
  66.         }
  67.         if (this.canPass(this._x, this._y, vert) && !this.canPass(this._x, this._y, horz)){
  68.             this.moveStraight(vert);
  69.             return;
  70.         }
  71.         if (!this.canPassDiagonally(this._x, this._y, horz, vert)) {
  72.             if (Math.random() > 0.5){
  73.                 this.setDirection(vert); this.moveStraight(vert)
  74.             } else {
  75.                 this.setDirection(horz); this.moveStraight(horz)
  76.             }
  77.             return;
  78.         }
  79.         dir8_move_diagonal.call(this, horz, vert);
  80.         if (horz > 5) {
  81.             vert > 5 ? this.setDirection(9) : this.setDirection(7);
  82.         }
  83.         else {
  84.             vert > 5 ? this.setDirection(3) : this.setDirection(1);
  85.         };
  86.     };
  87.     Sprite_Character.prototype.characterPatternY = function() {
  88.         if (this._character.direction() % 2 == 0){
  89.             if (this.dir8_bitmap){ this.bitmap = this.dir4_bitmap; }
  90.             return (this._character.direction() - 2) / 2;
  91.         } else {
  92.             if (this.dir8_bitmap){ this.bitmap = this.dir8_bitmap; }
  93.             return parseInt((this._character.direction() + 1) / 3);
  94.         }
  95.     };
  96.     var dir8_set_CharacterBitmap = Sprite_Character.prototype.setCharacterBitmap
  97.     Sprite_Character.prototype.setCharacterBitmap = function() {
  98.         dir8_set_CharacterBitmap.call(this);
  99.         this._isDir8Character = ImageManager.isDir8Character(this._characterName);
  100.         if (this._isDir8Character) {
  101.             this.dir4_bitmap = this.bitmap;
  102.             this.dir8_bitmap = ImageManager.loadCharacter(this._characterName + dir8_affix);
  103.         };
  104.     };
  105. })();

更新记录

对js啥都不懂的萌新瞎玩了一会js。发个插件试试。我自己并没有做过多测试。
有啥问题就直接回帖子反馈吧,反正我也不懂能不能结决
中文注释有点low啊

Lv2.观梦者

梦石
0
星屑
676
在线时间
224 小时
注册时间
2006-12-7
帖子
839
13
发表于 2015-11-29 15:16:46 | 只看该作者
本帖最后由 doranikofu 于 2015-11-29 16:02 编辑

话说又折腾了一下发现这个只定义了Game_player
貌似对跟随人物和npc都无效吧?。。。
试着把movediagnal从Game_player改高到Game_character或者Game_characterbase都没用。。。这个能通用改到跟随和npc么。。

再就是目前插件是靠读取两个文件分8向,感觉可以考虑整合成一个文件改index实现8向 默认素材就是4×2的 index+4就可以读取下一行的图做斜向图 就看要怎么加进去了
这样不用再读一个文件显示斜向图

点评

最近发现拼图神器 ImageMagick 超级好用。。。拼图神马的完全不是问题  发表于 2015-11-30 00:57
那我改改发到MV区吧。  发表于 2015-11-29 16:44
嗯。我之前也是想整合一个文件,不过有人说改文件名轻松点,拼一个文件麻烦啥啥啥的。  发表于 2015-11-29 16:42
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
676
在线时间
224 小时
注册时间
2006-12-7
帖子
839
12
发表于 2015-11-23 03:34:49 | 只看该作者
折腾了一下把三个行走图脚本整合了
八方向(LZ的),待机动作(如下),增加动画帧数(如下)

增加帧数
http://mvplugins.com/plugin/Shaz/More%20Character%20Frames
待机/行走/跑步动画
http://mvplugins.com/plugin/Galv/Galvs%20Character%20Animations

看脚本的时候就觉得脚本之前没有太多重复定义应该问题不大,果然前两个脚本可以直接共用。问题只在第三个因为一些写法比较暴力。
手写行走图文件名加$似乎是通过MV的内置脚本定义成了大行走图,从MV的编辑器里面都能看出来区别,这种文件不会被划分成4×2的小图。
于是哥折腾了半天稍微做点改动,就可以公用了。。。菜鸟不知道有没有地方有错,还没有测试复杂的图片。

下面是瞎掰的合并了的八方向和增加帧数js,待机动作在这个基础上直接外挂就可以一起用了

JAVASCRIPT 代码复制
  1. //=============================================================================
  2. // VIPArcher_Dir8.js
  3. //=============================================================================
  4. /*:
  5.  * @plugindesc 真 · 八方向移动
  6.  * @author VIPArcher
  7.  *
  8.  * @param Dir8_flag
  9.  * @desc 使用八方向素材的标志
  10.  * @default %
  11.  *
  12.  * @param Dir8_Affix
  13.  * @desc 八方向素材文件后缀
  14.  * @default _8D
  15.  *
  16.  * @help 使用八方向的行走图素材文件名前面添加一个标志,
  17.  * 与之对应的8方向的素材与其同名并在后面添加后缀以区别。
  18.  * 例如默认的设置就是使用八方向的素材命名为:
  19.  * !$%VIPArcher.png
  20.  *
  21.  * 那么与之对应的八方向的素材就应命名为:
  22.  * !$%VIPArcher_8D.png
  23.  *
  24.  */
  25.  
  26. // merge the two plugins
  27. //=============================================================================
  28. // More Character Frames
  29. // by Shaz
  30. // Last Updated: 2015.09.21
  31. //=============================================================================
  32.  
  33. /*:
  34.  * @plugindesc Allows more than 3 Frames
  35.  * @author Shaz
  36.  *
  37.  * @help This plugin does not provide plugin commands.
  38.  *
  39.  * Add [D L R U] to your character sheet name (with $ prefix) to specify haw
  40.  * many frames in each direction (Down, Left, Right, Up).
  41.  * Spritesheets with this added to the file name will use a looping frame
  42.  * animation rather than the back-and-forth default animation.
  43.  *
  44.  * The first frame should be the idle/still pose.
  45.  *
  46.  * eg. $Ralph [8 8 8 8].png
  47.  *     is a character sheet consisting of 4 rows with 8 frames per row.
  48.  *     Animation will go from frame 1 to 8 then start at 1 again.
  49.  *
  50.  *        !!! For merging with idle action plugin, do not put $ in file name. Simply define the frame.
  51.  *     这里不要用插件默认的$前缀文件,为了和其他插件兼容我们要保持MV默认的4*2划分行走图方式
  52.  *     只需要在文件名后面加上[a b c d],分别定义4个方向的帧数
  53.  */
  54.  
  55.  
  56. (function() {
  57.     var parameters = PluginManager.parameters('VIPArcher_Dir8');
  58.     var dir8_flag = String(parameters['Dir8_flag'] || '%');
  59.     var dir8_affix = String(parameters['Dir8_Affix'] || '_8D');
  60.     ImageManager.isDir8Character = function(filename) {
  61.         var reg = new RegExp("^\[\\!\\$\\" + dir8_flag + "]+")
  62.         var sign = filename.match(reg);
  63.         return sign && sign[0].contains(dir8_flag);
  64.     };
  65.  
  66. //multiframe parameters 插入的多帧脚本内容,没有修改
  67.   ImageManager.isMultiFrameCharacter = function(filename) {
  68.     var frames = filename.match(/\[(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\]/);
  69.     return frames && frames.length === 5;
  70.   };
  71.  
  72.   ImageManager.getCharacterFrameCount = function(filename) {
  73.     var frames = filename.match(/\[(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\]/);
  74.     if (!frames) {
  75.       return [3, 3, 3, 3];
  76.     } else {
  77.       return frames.splice(1, 4);
  78.     }
  79.   };
  80. //multiframe parameters end 插入结束
  81.  
  82.     Game_Player.prototype.moveByInput = function() {
  83.         if (!this.isMoving() && this.canMove()) {
  84.             var direction = Input.dir8
  85.             if (direction > 0) {
  86.                 $gameTemp.clearDestination();
  87.             } else if ($gameTemp.isDestinationValid()){
  88.                 var x = $gameTemp.destinationX();
  89.                 var y = $gameTemp.destinationY();
  90.                 direction = this.findDirectionTo(x, y);
  91.             }
  92.             if (direction > 0) {
  93.                 if (direction % 2 == 0){
  94.                     this.moveStraight(direction);
  95.                     return;
  96.                 }
  97.                 if (direction < 5){
  98.                     this.moveDiagonally(direction + 3 , 2);
  99.                 } else{
  100.                     this.moveDiagonally(direction - 3 , 8);
  101.                 }
  102.             }
  103.         }
  104.     };
  105.     var dir8_move_diagonal = Game_Player.prototype.moveDiagonally;
  106.     Game_Player.prototype.moveDiagonally = function(horz, vert) {
  107.         if (!this.canPass(this._x, this._y, horz) && !this.canPass(this._x, this._y, vert)){
  108.             this.setMovementSuccess(false);
  109.             return;
  110.         }
  111.         if (this.canPass(this._x, this._y, horz) && !this.canPass(this._x, this._y, vert)){
  112.             this.moveStraight(horz);
  113.             return;
  114.         }
  115.         if (this.canPass(this._x, this._y, vert) && !this.canPass(this._x, this._y, horz)){
  116.             this.moveStraight(vert);
  117.             return;
  118.         }
  119.         if (!this.canPassDiagonally(this._x, this._y, horz, vert)) {
  120.             if (Math.random() > 0.5){
  121.                 this.setDirection(vert); this.moveStraight(vert)
  122.             } else {
  123.                 this.setDirection(horz); this.moveStraight(horz)
  124.             }
  125.             return;
  126.         }
  127.         dir8_move_diagonal.call(this, horz, vert);
  128.         if (horz > 5) {
  129.             vert > 5 ? this.setDirection(9) : this.setDirection(7);
  130.         }
  131.         else {
  132.             vert > 5 ? this.setDirection(3) : this.setDirection(1);
  133.         };
  134.     };
  135.     Sprite_Character.prototype.characterPatternY = function() {
  136.         if (this._character.direction() % 2 == 0){
  137.             if (this.dir8_bitmap){ this.bitmap = this.dir4_bitmap; }
  138.             return (this._character.direction() - 2) / 2;
  139.         } else {
  140.             if (this.dir8_bitmap){ this.bitmap = this.dir8_bitmap; }
  141.             return parseInt((this._character.direction() + 1) / 3);
  142.         }
  143.     };
  144.     var dir8_set_CharacterBitmap = Sprite_Character.prototype.setCharacterBitmap
  145.     Sprite_Character.prototype.setCharacterBitmap = function() {
  146.         dir8_set_CharacterBitmap.call(this);
  147.         this._isDir8Character = ImageManager.isDir8Character(this._characterName);
  148.         if (this._isDir8Character) {
  149.             this.dir4_bitmap = this.bitmap;
  150.             this.dir8_bitmap = ImageManager.loadCharacter(this._characterName + dir8_affix);
  151.         };
  152.     };
  153.  
  154. // multiframe code===============插入的多帧代码部分
  155.   var _Game_CharacterBase_initMembers = Game_CharacterBase.prototype.initMembers;
  156.   Game_CharacterBase.initMembers = function() {
  157.     _Game_CharacterBase_initMembers.call(this);
  158.     this._isMultiFrame = false;
  159.     this._frames = [3, 3, 3, 3];
  160.   };
  161.  
  162.   var _Game_CharacterBase_maxPattern = Game_CharacterBase.prototype.maxPattern;
  163.   Game_CharacterBase.prototype.maxPattern = function() {
  164.     if (!this._isMultiFrame) {
  165.       return _Game_CharacterBase_maxPattern.call(this);
  166.     } else {
  167. //consider diaganol directions  重写定义 考虑八向脚本兼容
  168.         return this._direction <5 ? this._frames[(parseInt((this._direction + 1)/ 2) - 1)] :  this._frames[(parseInt((this._direction + 1)/ 3))];
  169.     }
  170.   };
  171.  
  172.   var _Game_CharacterBase_pattern = Game_CharacterBase.prototype.pattern;
  173.   Game_CharacterBase.prototype.pattern = function() {
  174.     if (!this._isMultiFrame) {
  175.       return _Game_CharacterBase_pattern.call(this);
  176.     } else {
  177. //      return this._pattern < this._frames[this._direction / 2 - 1] ? this._pattern : 0;
  178. //      为了简单暂时只用了第一行的帧数作为上限,稍微改一下应该就好了,懒,而且应该不太会用到不同方向不同帧数
  179.                 return this._pattern < this._frames[0] ? this._pattern : 0;
  180.     }
  181.   };
  182.  
  183.   var _Game_CharacterBase_isOriginalPattern = Game_CharacterBase.prototype.isOriginalPattern;
  184.   Game_CharacterBase.prototype.isOriginalPattern = function() {
  185.     if (!this._isMultiFrame) {
  186.       return _Game_CharacterBase_isOriginalPattern.call(this);
  187.     } else {
  188.       return this.pattern() === 0;
  189.     }
  190.   };
  191.  
  192.   var _Game_CharacterBase_resetPattern = Game_CharacterBase.prototype.resetPattern;
  193.   Game_CharacterBase.prototype.resetPattern = function() {
  194.     if (!this._isMultiFrame) {
  195.       _Game_CharacterBase_resetPattern.call(this);
  196.     } else {
  197.       this.setPattern(0);
  198.     }
  199.   };
  200.  
  201.   var _Game_CharacterBase_setImage = Game_CharacterBase.prototype.setImage;
  202.   Game_CharacterBase.prototype.setImage = function(characterName, characterIndex) {
  203.     _Game_CharacterBase_setImage.call(this, characterName, characterIndex);
  204.     this._isMultiFrame = ImageManager.isMultiFrameCharacter(characterName);
  205.     this._frames = ImageManager.getCharacterFrameCount(characterName);
  206.   };
  207.  
  208.   var _Game_CharacterBase_setTileImage = Game_CharacterBase.prototype.setTileImage;
  209.   Game_CharacterBase.prototype.setTileImage = function(tileId) {
  210.     _Game_CharacterBase_setTileImage.call(this, tileId);
  211.     this._isMultiFrame = false;
  212.     this._frames = [3, 3, 3, 3];
  213.   };
  214.  
  215.   Game_CharacterBase.prototype.isMultiFrame = function() {
  216.     return this._isMultiFrame;
  217.   };
  218.  
  219.   Game_CharacterBase.prototype.getDirectionFrames = function() {
  220. //    return this._frames[this._direction / 2 - 1]; 重新定义帧数 兼容八方向
  221.         return this._direction <5 ? this._frames[(parseInt((this._direction + 1)/ 2) - 1)] :  this._frames[(parseInt((this._direction + 1)/ 3))];
  222.   };
  223.  
  224.   var _Game_Event_initMembers = Game_Event.prototype.initMembers;
  225.   Game_Event.prototype.initMembers = function() {
  226.     _Game_Event_initMembers.call(this);
  227.     if (this._isMultiFrame) {
  228.       this._originalPattern = 0;
  229.     }
  230.   };
  231.  
  232.   var _Sprite_Character_patternWidth = Sprite_Character.prototype.patternWidth;
  233.   Sprite_Character.prototype.patternWidth = function() {
  234.     if (!this._character.isMultiFrame()) {
  235.       return _Sprite_Character_patternWidth.call(this);
  236.     } else {
  237. // 除4以还原默认的4*2划分方式
  238.       return this.bitmap.width / (4*this._character.getDirectionFrames());
  239.     }
  240.   };
  241. //doranikofu edit redefine block 原始插件没有重定义的部分,需要按照新定义的行走图动画的帧数去修改index的读取位点,默认是3帧
  242. Sprite_Character.prototype.characterBlockX = function() {
  243.     if (this._isBigCharacter) {
  244.         return 0;
  245.     } else {
  246.         var index = this._character.characterIndex();
  247.         return (index % 4) * (this._character.getDirectionFrames());
  248.     }
  249. };
  250.   //==========
  251.  
  252. })();
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
676
在线时间
224 小时
注册时间
2006-12-7
帖子
839
11
发表于 2015-11-19 14:21:21 | 只看该作者
话说楼主打算做增加帧数的功能么
试了下英文论坛的发现完全冲突,那个破坏力太强了直接把整个行走图划分了读,不是按默认的一个行走图分四个人物的
http://mvplugins.com/plugin/Shaz/More%20Character%20Frames
这种模式跟另外一个待机/走路/跑步的插件也冲突
http://mvplugins.com/plugin/Galv/Galvs%20Character%20Animations

点评

和这样的脚本冲突不是肯定的事嘛=。=  发表于 2015-11-19 14:22
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
676
在线时间
224 小时
注册时间
2006-12-7
帖子
839
10
发表于 2015-11-19 14:20:48 | 只看该作者
话说楼主打算做增加帧数的功能么
试了下英文论坛的发现完全冲突,那个破坏力太强了直接把整个行走图划分了读,不是按默认的一个行走图分四个人物的
http://mvplugins.com/plugin/Shaz/More%20Character%20Frames
这种模式跟另外一个待机/走路/跑步的插件也冲突
http://mvplugins.com/plugin/Galv/Galvs%20Character%20Animations
回复 支持 反对

使用道具 举报

Lv5.捕梦者 (暗夜天使)

只有笨蛋才会看到

梦石
1
星屑
21120
在线时间
9349 小时
注册时间
2012-6-19
帖子
7107

开拓者短篇九导演组冠军

9
发表于 2015-11-19 09:14:53 | 只看该作者
VIPArcher 发表于 2015-11-19 09:04
瞎试了一下,发现js和ruby的 / 和 % 还是有区别的。所以大概可以这样写
for (var j = 1; j  0 ? directio ...

哦对,忘了这茬,用 Number.prototype.mod ( n )  代替 %
回复 支持 反对

使用道具 举报

Lv4.逐梦者 (版主)

无限の剣制

梦石
0
星屑
9997
在线时间
5019 小时
注册时间
2013-2-28
帖子
5030

开拓者贵宾

8
 楼主| 发表于 2015-11-19 09:04:31 | 只看该作者
喵呜喵5 发表于 2015-11-18 17:53
寻路在 Game_Character 的 findDirectionTo 中,算法为 A*

为了分析方便单独拎出来:


瞎试了一下,发现js和ruby的 / 和 % 还是有区别的。所以大概可以这样写
JAVASCRIPT 代码复制
  1. for (var j = 1; j <= 9; j++) {
  2.     var direction = j;
  3.     if (direction == 5) continue;
  4.     var x2 = x1 + (direction - 4 > 0 ? direction - 4 : direction - 1) % 3 - 1;
  5.     var y2 = y1 + Math.floor((direction - 4) / 3);
回复 支持 反对

使用道具 举报

Lv5.捕梦者 (暗夜天使)

只有笨蛋才会看到

梦石
1
星屑
21120
在线时间
9349 小时
注册时间
2012-6-19
帖子
7107

开拓者短篇九导演组冠军

7
发表于 2015-11-18 17:53:30 | 只看该作者
本帖最后由 喵呜喵5 于 2015-11-18 18:04 编辑
千葉玖濑 发表于 2015-11-12 00:04
用鼠标的话会不会斜向寻路?还是只会走直线然后拐弯?我瞎糊的就不行 ...


寻路在 Game_Character 的 findDirectionTo 中,算法为 A*

为了分析方便单独拎出来:

JAVASCRIPT 代码复制
  1. Game_Character.prototype.findDirectionTo = function(goalX, goalY) {
  2.     var searchLimit = this.searchLimit();
  3.     var mapWidth = $gameMap.width();
  4.     var nodeList = [];
  5.     var openList = [];
  6.     var closedList = [];
  7.     var start = {};
  8.     var best = start;
  9.     if (this.x === goalX && this.y === goalY) {
  10.         return 0
  11.     }
  12.     start.parent = null;
  13.     start.x = this.x;
  14.     start.y = this.y;
  15.     start.g = 0;
  16.     start.f = $gameMap.distance(start.x, start.y, goalX, goalY);
  17.     nodeList.push(start);
  18.     openList.push(start.y * mapWidth + start.x);
  19.     while (nodeList.length > 0) {
  20.         var bestIndex = 0;
  21.         for (var i = 0; i < nodeList.length; i++) {
  22.             if (nodeList[i].f < nodeList[bestIndex].f) {
  23.                 bestIndex = i
  24.             }
  25.         }
  26.         var current = nodeList[bestIndex];
  27.         var x1 = current.x;
  28.         var y1 = current.y;
  29.         var pos1 = y1 * mapWidth + x1;
  30.         var g1 = current.g;
  31.         nodeList.splice(bestIndex, 1);
  32.         openList.splice(openList.indexOf(pos1), 1);
  33.         closedList.push(pos1);
  34.         if (current.x === goalX && current.y === goalY) {
  35.             best = current;
  36.             goaled = true;
  37.             break
  38.         }
  39.         if (g1 >= searchLimit) {
  40.             continue
  41.         }
  42.         for (var j = 0; j < 4; j++) {
  43.             var direction = 2 + j * 2;
  44.             var x2 = $gameMap.roundXWithDirection(x1, direction);
  45.             var y2 = $gameMap.roundYWithDirection(y1, direction);
  46.             var pos2 = y2 * mapWidth + x2;
  47.             if (closedList.contains(pos2)) {
  48.                 continue
  49.             }
  50.             if (!this.canPass(x1, y1, direction)) {
  51.                 continue
  52.             }
  53.             var g2 = g1 + 1;
  54.             var index2 = openList.indexOf(pos2);
  55.             if (index2 < 0 || g2 < nodeList[index2].g) {
  56.                 var neighbor;
  57.                 if (index2 >= 0) {
  58.                     neighbor = nodeList[index2]
  59.                 } else {
  60.                     neighbor = {};
  61.                     nodeList.push(neighbor);
  62.                     openList.push(pos2)
  63.                 }
  64.                 neighbor.parent = current;
  65.                 neighbor.x = x2;
  66.                 neighbor.y = y2;
  67.                 neighbor.g = g2;
  68.                 neighbor.f = g2 + $gameMap.distance(x2, y2, goalX, goalY);
  69.                 if (!best || neighbor.f - neighbor.g < best.f - best.g) {
  70.                     best = neighbor
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     var node = best;
  76.     while (node.parent && node.parent !== start) {
  77.         node = node.parent
  78.     }
  79.     var deltaX1 = $gameMap.deltaX(node.x, start.x);
  80.     var deltaY1 = $gameMap.deltaY(node.y, start.y);
  81.     if (deltaY1 > 0) {
  82.         return 2
  83.     } else if (deltaX1 < 0) {
  84.         return 4
  85.     } else if (deltaX1 > 0) {
  86.         return 6
  87.     } else if (deltaY1 < 0) {
  88.         return 8
  89.     }
  90.     var deltaX2 = this.deltaXFrom(goalX);
  91.     var deltaY2 = this.deltaYFrom(goalY);
  92.     if (Math.abs(deltaX2) > Math.abs(deltaY2)) {
  93.         return deltaX2 > 0 ? 4 : 6
  94.     } else if (deltaY2 !== 0) {
  95.         return deltaY2 > 0 ? 8 : 2
  96.     }
  97.     return 0
  98. };


42~45行是选择要寻路的点,

具体就是四次循环得出 2/4/6/8 这四个方向然后根据当前方向算出朝指定方向移动后的 x2 和 y2 值

JAVASCRIPT 代码复制
  1. for (var j = 0; j < 4; j++) {
  2.             var direction = 2 + j * 2;
  3.             var x2 = $gameMap.roundXWithDirection(x1, direction);
  4.             var y2 = $gameMap.roundYWithDirection(y1, direction);


要八方向的话稍微改改就好,仅提供一个简单的思路,出自兰触(https://rpg.blue/forum.php?mod=viewthread&tid=368249):

JAVASCRIPT 代码复制
  1. for (var j = 1; j <= 9; j++) {
  2.             var direction = j;
  3.             if (direction == 5) continue;
  4.             var x2 = x1 + (direction - 4) % 3 - 1;
  5.             var y2 = y1 - ((direction - 4) / 3);


50行的 canPass 函数是判定是否能从当前位置向指定方向移动一步的,因为加了八方向,所以自己根据自己的需要稍微更改一下判定条件,比如如果左边有个老太太是否还能向左上角移动啥的

JAVASCRIPT 代码复制
  1. if (!this.canPass(x1, y1, direction)) {


79行开始的代码就是最终找到路径

JAVASCRIPT 代码复制
  1. var deltaX1 = $gameMap.deltaX(node.x, start.x);
  2.     var deltaY1 = $gameMap.deltaY(node.y, start.y);
  3.     if (deltaY1 > 0) {
  4.         return 2
  5.     } else if (deltaX1 < 0) {
  6.         return 4
  7.     } else if (deltaX1 > 0) {
  8.         return 6
  9.     } else if (deltaY1 < 0) {
  10.         return 8
  11.     }
  12.     var deltaX2 = this.deltaXFrom(goalX);
  13.     var deltaY2 = this.deltaYFrom(goalY);
  14.     if (Math.abs(deltaX2) > Math.abs(deltaY2)) {
  15.         return deltaX2 > 0 ? 4 : 6
  16.     } else if (deltaY2 !== 0) {
  17.         return deltaY2 > 0 ? 8 : 2
  18.     }
  19.     return 0


node.x 和 node.y 就是下一步要移动的XY坐标,但是这个函数原本的返回值是移动方向的方位数(2/4/6/8)而不是最终的坐标,所以继续用兰触公式转换一下

JAVASCRIPT 代码复制
  1. return 5 + 3 * -(node.y - start.y) + (node.x - start.x)


这样寻路部分的逻辑就支持八方向寻路了


不过要真的让角色支持八方向寻路,还有一个要改的地方是这里:

JAVASCRIPT 代码复制
  1. Game_Player.prototype.executeMove = function(direction) {
  2.     this.moveStraight(direction)
  3. };


上面整个寻路过程最终得出的方位数是传到这个函数中的,原生的 moveStraight 应该不支持八方向,因此这里也要修改一下让direction为1379时角色能走的动


==========

以上纯粹是理论,是否正确还需要测试。因为我完全没有用过MV,只看过MV的源代码

评分

参与人数 2星屑 +300 收起 理由
VIPArcher + 150 我很赞同
千葉玖濑 + 150 我也找到了这个然而不知道怎么改寻路算法or.

查看全部评分

回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
676
在线时间
224 小时
注册时间
2006-12-7
帖子
839
6
发表于 2015-11-18 15:15:15 | 只看该作者
测试可用 就是开始说明没太看懂
刚开始加了!$在前面发现rmmv的编辑器都识别有问题,不是按4×2的读的
说明白点就是要
%标准四向行走图
%四个斜向的行走图_8D
两个文件对应上就ok

评分

参与人数 1星屑 +10 收起 理由
VIPArcher + 10 嗯。语文不好,不太会表述&gt;.&lt;.

查看全部评分

回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
2258
在线时间
1602 小时
注册时间
2014-9-28
帖子
2792

开拓者

5
发表于 2015-11-13 01:27:29 | 只看该作者
楼主你不知道RPG Maker MV 讨论区是专门发布MV脚本的么!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-6-9 05:23

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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