查看: 5096|回复: 5

[原创发布] [脚本] 自由移动 (5.28 重做)

[复制链接]
梦石
0
星屑
1907
在线时间
1555 小时
注册时间
2013-4-13
回帖
783
发表于 2020-5-26 08:56:03 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 沉滞的剑 于 2020-5-28 20:49 编辑

脚本功能: 允许角色自由移动

适用于: 需要让主角更自然地在场景中进行移动

支持键盘/鼠标和手柄

如果有类似成熟的脚本话 请给个链接好不好 我去学习一下 谢谢~

如果有bug欢迎反馈~

更新日志:

5.8 重做, 将人物改为矩形碰撞检测

  1. const Base = {};
  2. // Check if it is an object
  3. {
  4.   Base.isObject = (object) => {
  5.     return ["object", "function"].includes(typeof object) && object !== null;
  6.   };
  7. }

  8. // Get value from object by path
  9. {
  10.   const _get = (_object, _path, defaultValue) => {
  11.     if (_path.length) {
  12.       if (!Base.isObject(_object)) {
  13.         return defaultValue;
  14.       }
  15.       const currentPath = _path.shift();
  16.       return _get(_object[currentPath], _path, defaultValue);
  17.     }
  18.     return _object === undefined ? defaultValue : _object;
  19.   };
  20.   Base.get = (object, path, defaultValue) => {
  21.     return _get(object, path.split("."), defaultValue);
  22.   };
  23. }
  24. // Set value from object by path
  25. {
  26.   const _set = (_object, _path, value) => {
  27.     const currentPath = _path.shift();
  28.     if (_path.length) {
  29.       if (!Base.isObject(_object[currentPath])) {
  30.         _object[currentPath] = {};
  31.       }
  32.       _set(_object[currentPath], _path, value);
  33.     } else {
  34.       _object[currentPath] = value;
  35.     }
  36.   };
  37.   Base.set = (object, path, value) => {
  38.     return _set(object, path.split("."), value);
  39.   };
  40. }

  41. // Filter small values within a range
  42. {
  43.   Base.stair = (num, threadhold, offset = 0) => (Math.abs(num + offset) < threadhold ? offset : num);
  44. }
  45. // Input
  46. {
  47.   const { get, stair } = Base;

  48.   Input._axesX = [];
  49.   Input._axesY = [];
  50.   Input._updateGamepadState = function (gamepad) {
  51.     const { buttons, axes, index } = gamepad;
  52.     const newState = [];
  53.     const lastState = get(this, `_gamepadStates.${index}`, []);
  54.     const threshold = 0.3;

  55.     buttons.forEach((v, i) => {
  56.       newState[i] = v.pressed;
  57.     });

  58.     newState[12] = axes[1] < -threshold;
  59.     newState[13] = axes[1] > threshold;
  60.     newState[14] = axes[0] < -threshold;
  61.     newState[15] = axes[0] > threshold;

  62.     newState.forEach((v, i) => {
  63.       if (lastState[i] !== v) {
  64.         const buttonName = this.gamepadMapper[i];
  65.         this._currentState[buttonName] = !!newState[i];
  66.       }
  67.     });
  68.     this._gamepadStates[index] = newState;
  69.     Input._axesX[index] = stair(axes[0], threshold);
  70.     Input._axesY[index] = stair(axes[1], threshold);
  71.   };

  72.   Object.defineProperties(Input, {
  73.     axesX: {
  74.       get: () => Input._axesX.find(Boolean) || 0,
  75.     },
  76.     axesY: {
  77.       get: () => Input._axesY.find(Boolean) || 0,
  78.     },
  79.     keyX: {
  80.       get: () => {
  81.         let x = 0;
  82.         if (Input._currentState["left"]) x -= 1;
  83.         if (Input._currentState["right"]) x += 1;
  84.         return x;
  85.       },
  86.     },
  87.     keyY: {
  88.       get: () => {
  89.         let y = 0;
  90.         if (Input._currentState["up"]) y -= 1;
  91.         if (Input._currentState["down"]) y += 1;
  92.         return y;
  93.       },
  94.     },
  95.     velocity: {
  96.       get: () => Math.min(Math.sqrt(Input.axesX ** 2 + Input.axesY ** 2), 100),
  97.     },
  98.     inputX: {
  99.       get: () => (Input.velocity ? Input.axesX : Input.keyX),
  100.     },
  101.     inputY: {
  102.       get: () => (Input.velocity ? Input.axesY : Input.keyY),
  103.     },
  104.   });
  105. }

  106. // Touch Input
  107. {
  108.   Scene_Map.prototype.processMapTouch = function () {
  109.     if (TouchInput.isTriggered() || this._touchCount > 0) {
  110.       if (TouchInput.isPressed()) {
  111.         if (this._touchCount === 0 || this._touchCount >= 15) {
  112.           var x = $gameMap.canvasToMapXTouch(TouchInput.x);
  113.           var y = $gameMap.canvasToMapYTouch(TouchInput.y);
  114.           $gameTemp.setDestination(x, y);
  115.         }
  116.         this._touchCount++;
  117.       } else {
  118.         this._touchCount = 0;
  119.       }
  120.     }
  121.   };

  122.   Scene_Map.prototype.updateDestination = function () {
  123.     if (this.isMapTouchOk()) {
  124.       this.processMapTouch();
  125.     } else {
  126.       this._touchCount = 0;
  127.     }
  128.   };

  129.   Game_Map.prototype.canvasToMapXTouch = function (x) {
  130.     var tileWidth = this.tileWidth();
  131.     var originX = this._displayX * tileWidth;
  132.     var mapX = (originX + x) / tileWidth - 0.5;
  133.     return mapX;
  134.   };

  135.   Game_Map.prototype.canvasToMapYTouch = function (y) {
  136.     var tileHeight = this.tileHeight();
  137.     var originY = this._displayY * tileHeight;
  138.     var mapY = (originY + y) / tileHeight - 0.5;
  139.     return mapY;
  140.   };
  141. }

  142. // Player
  143. {
  144.   const DIR = {
  145.     DOWN_LEFT: 1,
  146.     DOWN: 2,
  147.     DOWN_RIGHT: 3,
  148.     LEFT: 4,
  149.     IDLE: 0,
  150.     RIGHT: 6,
  151.     UP_LEFT: 7,
  152.     UP: 8,
  153.     UP_RIGHT: 9,
  154.   };

  155.   const UNIT_STEP = 0.1;

  156.   Game_Player.prototype.getInputDirection = function () {
  157.     return Input.dir8;
  158.   };

  159.   Game_Player.prototype.reverseDir = function (d) {
  160.     return (10 - d) % 10;
  161.   };

  162.   Game_Player.prototype.executeMove = function (dx, dy) {
  163.     if (Math.abs(dx) > Math.abs(dy)) {
  164.       if (dx > 0) {
  165.         this.setDirection(DIR.RIGHT);
  166.       } else {
  167.         this.setDirection(DIR.LEFT);
  168.       }
  169.     } else {
  170.       if (dy > 0) {
  171.         this.setDirection(DIR.DOWN);
  172.       } else {
  173.         this.setDirection(DIR.UP);
  174.       }
  175.     }
  176.     const check = [];
  177.     if (dx > 0) check.push(DIR.RIGHT);
  178.     if (dx < 0) check.push(DIR.LEFT);
  179.     if (dy > 0) check.push(DIR.DOWN);
  180.     if (dy < 0) check.push(DIR.UP);
  181.     for (let i of check) {
  182.       if (!this.rectCanPass(dx, dy, i)) {
  183.         if ([DIR.RIGHT, DIR.LEFT].includes(i)) dx = 0;
  184.         if ([DIR.DOWN, DIR.UP].includes(i)) dy = 0;
  185.       }
  186.     }
  187.     if (!Math.abs(dx) && !Math.abs(dy)) {
  188.       $gameTemp.clearDestination();
  189.       return;
  190.     }
  191.     this._realX = this._x;
  192.     this._realY = this._y;
  193.     this._x += dx;
  194.     this._y += dy;
  195.   };

  196.   Object.defineProperties(Game_Player.prototype, {
  197.     rect: {
  198.       get: function () {
  199.         return new Rectangle(this._x + 0.06, this._y + 0.06, 0.75, 0.75);
  200.       },
  201.     },
  202.   });

  203.   Game_Player.prototype.rectCanPass = function (dx, dy, direction) {
  204.     const { left, right, top, bottom } = this.rect;
  205.     const rd = this.reverseDir(direction);
  206.     if (!this.canPass(left, top, rd)) return false;
  207.     if (!this.canPass(left, bottom, rd)) return false;
  208.     if (!this.canPass(right, top, rd)) return false;
  209.     if (!this.canPass(right, bottom, rd)) return false;
  210.     if (!this.canPass(left + dx, top + dy, direction)) return false;
  211.     if (!this.canPass(left + dx, bottom + dy, direction)) return false;
  212.     if (!this.canPass(right + dx, top + dy, direction)) return false;
  213.     if (!this.canPass(right + dx, bottom + dy, direction)) return false;
  214.     return true;
  215.   };

  216.   Game_Player.prototype.canPass = function (x, y, d) {
  217.     x = Math.floor(x);
  218.     y = Math.floor(y);
  219.     if (!$gameMap.isValid(x, y)) {
  220.       return false;
  221.     }
  222.     if (this.isThrough() || this.isDebugThrough()) {
  223.       return true;
  224.     }
  225.     if (!this.isMapPassable(x, y, d)) {
  226.       return false;
  227.     }
  228.     if (this.isCollidedWithCharacters(x, y)) {
  229.       return false;
  230.     }
  231.     return true;
  232.   };

  233.   Game_Player.prototype.isMapPassable = function (x, y, d) {
  234.     return $gameMap.isPassable(x, y, d);
  235.   };

  236.   Game_Player.prototype.moveByInput = function () {
  237.     if (!this.isMoving() && this.canMove()) {
  238.       var direction = this.getInputDirection();
  239.       let dx, dy;
  240.       if (direction > 0) {
  241.         $gameTemp.clearDestination();
  242.         dx = +(Input.inputX * UNIT_STEP).toFixed(3);
  243.         dy = +(Input.inputY * UNIT_STEP).toFixed(3);
  244.       } else if ($gameTemp.isDestinationValid()) {
  245.         dx = $gameTemp.destinationX() - this._x;
  246.         dy = $gameTemp.destinationY() - this._y;
  247.         if (Math.abs(dx) < 0.1 && Math.abs(dy) < 0.1) {
  248.           $gameTemp.clearDestination();
  249.           return;
  250.         }
  251.         if (Math.abs(dx) > Math.abs(dy)) {
  252.           dx = dx / Math.abs(dx);
  253.           dy = dy / Math.abs(dx);
  254.         } else {
  255.           dx = dx / Math.abs(dy);
  256.           dy = dy / Math.abs(dy);
  257.         }
  258.         dx *= UNIT_STEP;
  259.         dy *= UNIT_STEP;
  260.       }
  261.       if (dx || dy) {
  262.         this.executeMove(dx, dy);
  263.       }
  264.     }
  265.   };

  266.   Game_Player.prototype.updateNonmoving = function (wasMoving) {
  267.     if (!$gameMap.isEventRunning()) {
  268.       if (wasMoving) {
  269.         $gameParty.onPlayerWalk();
  270.         this.checkEventTriggerHere([1, 2]);
  271.         if ($gameMap.setupStartingEvent()) {
  272.           return;
  273.         }
  274.       }
  275.       if (this.triggerAction()) {
  276.         return;
  277.       }
  278.       if (wasMoving) {
  279.         this.updateEncounterCount();
  280.       }
  281.     }
  282.   };

  283.   Game_Player.prototype.distancePerFrame = function () {
  284.     return Math.pow(2, this.realMoveSpeed()) / 256 / 2;
  285.   };
  286. }

  287. // NPC
  288. {
  289.   Game_CharacterBase.prototype.pos = function (x, y) {
  290.     return Math.abs(this._x - x) <= 0.5 && Math.abs(this._y - y) <= 0.5;
  291.   };

  292.   Game_Event.prototype.isCollidedWithEvents = function (x, y) {
  293.     var events = $gameMap.eventsXyNt(x, y).filter((item) => item !== this);
  294.     return events.length > 0;
  295.   };
  296. }
复制代码

评分

参与人数 4赞 +4 收起 理由
x18489 + 1 塞糖
遮那 + 1 精品文章
3268006598 + 1 赞赞
马铃薯条 + 1 塞糖

查看全部评分

夏普的道具店

塞露提亚-道具屋的经营妙方同人作品
发布帖:点击这里
梦石
0
星屑
3553
在线时间
918 小时
注册时间
2018-5-18
回帖
474
发表于 2020-5-26 11:23:49 | 显示全部楼层
顶一顶,铁子厉害了
这个人很馋,什么都没有留下。。。
回复

使用道具 举报

梦石
0
星屑
25555
在线时间
1137 小时
注册时间
2019-3-5
回帖
1303
发表于 2020-5-26 15:39:52 | 显示全部楼层
不知道你说的成熟插件是指八方向移动还是像素移动,前者的话可以参考https://rpg.blue/thread-480682-1-1.html的第七楼,我总结了一些目前发现的开源插件。
回复

使用道具 举报

梦石
0
星屑
2582
在线时间
363 小时
注册时间
2015-8-15
回帖
66
发表于 2020-5-27 10:42:41 | 显示全部楼层
移动颗粒为0.25单元格,这个最大的问题在于通行
回复

使用道具 举报

梦石
0
星屑
100
在线时间
7 小时
注册时间
2022-1-29
回帖
2
发表于 2022-6-30 17:33:20 | 显示全部楼层
梦寐以求的插件!!!!
回复

使用道具 举报

梦石
0
星屑
45819
在线时间
184 小时
注册时间
2022-1-8
回帖
167
发表于 2022-7-1 07:14:52 | 显示全部楼层
感谢大佬分享!
我在平台上传的插件均可免费商用,转载
插件传送门:https://rpg.blue/forum.php?mod=v ... eid%26typeid%3D1306
接单中,可接RPGMaker MV/MZ插件定制,QQ:3268006598
回复

使用道具 举报

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

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部