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

Project1

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

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

[复制链接]

Lv4.逐梦者 (版主)

无限の剣制

梦石
0
星屑
9971
在线时间
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啊

Lv3.寻梦者 (版主)

…あたしは天使なんかじゃないわ

梦石
0
星屑
2208
在线时间
4033 小时
注册时间
2010-10-4
帖子
10779

开拓者贵宾

2
发表于 2015-11-11 06:28:03 | 只看该作者
泥看我就说js不用学就能瞎糊吧

点评

我信了,我就没接触过任何编程语言,基础都不懂。。要是我现在不能用JS瞎糊弄一套系统出来。我就去弹你家玻璃!  发表于 2015-11-11 09:30

评分

参与人数 1星屑 +233 收起 理由
VIPArcher + 233 我很赞同

查看全部评分

回复 支持 反对

使用道具 举报

Lv1.梦旅人 (暗夜天使)

永夜蟄居の玖瀨

梦石
0
星屑
71
在线时间
1018 小时
注册时间
2011-9-5
帖子
2813

开拓者贵宾

3
发表于 2015-11-12 00:04:03 | 只看该作者
用鼠标的话会不会斜向寻路?还是只会走直线然后拐弯?我瞎糊的就不行

点评

寻路算法要改的就我说的那几个地方啊,除了 canPass 函数其他都已经改好了啊  发表于 2015-11-18 20:48
[s]不用在意,那已经是寻路脚本的事了。[/s]  发表于 2015-11-12 09:05

回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
676
在线时间
224 小时
注册时间
2006-12-7
帖子
839
4
发表于 2015-11-12 10:14:50 | 只看该作者
马克学习一下
回复 支持 反对

使用道具 举报

Lv3.寻梦者

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

开拓者

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

点评

http://m.shabixiaoshuo.com/info-1255/ 神藏  发表于 2015-12-15 21:13
这我真不知道,那版主帮我移一下帖子吧=。=  发表于 2015-11-13 09:07
回复 支持 反对

使用道具 举报

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

查看全部评分

回复 支持 反对

使用道具 举报

Lv5.捕梦者 (暗夜天使)

只有笨蛋才会看到

梦石
1
星屑
21040
在线时间
9340 小时
注册时间
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.

查看全部评分

回复 支持 反对

使用道具 举报

Lv4.逐梦者 (版主)

无限の剣制

梦石
0
星屑
9971
在线时间
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
星屑
21040
在线时间
9340 小时
注册时间
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 )  代替 %

点评

好吧,其实我还没认真看过F1 >.<  发表于 2015-11-19 11:56
F1帮助文档  发表于 2015-11-19 09:42
(这些内置对象的额外prototype有什么地方有文档可以看到么?  发表于 2015-11-19 09:24
回复 支持 反对

使用道具 举报

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
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-5-15 16:12

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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