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

Project1

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

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

[复制链接]

Lv3.寻梦者

梦石
0
星屑
1882
在线时间
1552 小时
注册时间
2013-4-13
帖子
917
跳转到指定楼层
1
发表于 2020-5-26 08:56:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

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

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

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

支持键盘/鼠标和手柄

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

如果有bug欢迎反馈~

更新日志:

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

JAVASCRIPT 代码复制
  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.  
  9. // Get value from object by path
  10. {
  11.   const _get = (_object, _path, defaultValue) => {
  12.     if (_path.length) {
  13.       if (!Base.isObject(_object)) {
  14.         return defaultValue;
  15.       }
  16.       const currentPath = _path.shift();
  17.       return _get(_object[currentPath], _path, defaultValue);
  18.     }
  19.     return _object === undefined ? defaultValue : _object;
  20.   };
  21.   Base.get = (object, path, defaultValue) => {
  22.     return _get(object, path.split("."), defaultValue);
  23.   };
  24. }
  25. // Set value from object by path
  26. {
  27.   const _set = (_object, _path, value) => {
  28.     const currentPath = _path.shift();
  29.     if (_path.length) {
  30.       if (!Base.isObject(_object[currentPath])) {
  31.         _object[currentPath] = {};
  32.       }
  33.       _set(_object[currentPath], _path, value);
  34.     } else {
  35.       _object[currentPath] = value;
  36.     }
  37.   };
  38.   Base.set = (object, path, value) => {
  39.     return _set(object, path.split("."), value);
  40.   };
  41. }
  42.  
  43. // Filter small values within a range
  44. {
  45.   Base.stair = (num, threadhold, offset = 0) => (Math.abs(num + offset) < threadhold ? offset : num);
  46. }
  47. // Input
  48. {
  49.   const { get, stair } = Base;
  50.  
  51.   Input._axesX = [];
  52.   Input._axesY = [];
  53.   Input._updateGamepadState = function (gamepad) {
  54.     const { buttons, axes, index } = gamepad;
  55.     const newState = [];
  56.     const lastState = get(this, `_gamepadStates.${index}`, []);
  57.     const threshold = 0.3;
  58.  
  59.     buttons.forEach((v, i) => {
  60.       newState[i] = v.pressed;
  61.     });
  62.  
  63.     newState[12] = axes[1] < -threshold;
  64.     newState[13] = axes[1] > threshold;
  65.     newState[14] = axes[0] < -threshold;
  66.     newState[15] = axes[0] > threshold;
  67.  
  68.     newState.forEach((v, i) => {
  69.       if (lastState[i] !== v) {
  70.         const buttonName = this.gamepadMapper[i];
  71.         this._currentState[buttonName] = !!newState[i];
  72.       }
  73.     });
  74.     this._gamepadStates[index] = newState;
  75.     Input._axesX[index] = stair(axes[0], threshold);
  76.     Input._axesY[index] = stair(axes[1], threshold);
  77.   };
  78.  
  79.   Object.defineProperties(Input, {
  80.     axesX: {
  81.       get: () => Input._axesX.find(Boolean) || 0,
  82.     },
  83.     axesY: {
  84.       get: () => Input._axesY.find(Boolean) || 0,
  85.     },
  86.     keyX: {
  87.       get: () => {
  88.         let x = 0;
  89.         if (Input._currentState["left"]) x -= 1;
  90.         if (Input._currentState["right"]) x += 1;
  91.         return x;
  92.       },
  93.     },
  94.     keyY: {
  95.       get: () => {
  96.         let y = 0;
  97.         if (Input._currentState["up"]) y -= 1;
  98.         if (Input._currentState["down"]) y += 1;
  99.         return y;
  100.       },
  101.     },
  102.     velocity: {
  103.       get: () => Math.min(Math.sqrt(Input.axesX ** 2 + Input.axesY ** 2), 100),
  104.     },
  105.     inputX: {
  106.       get: () => (Input.velocity ? Input.axesX : Input.keyX),
  107.     },
  108.     inputY: {
  109.       get: () => (Input.velocity ? Input.axesY : Input.keyY),
  110.     },
  111.   });
  112. }
  113.  
  114. // Touch Input
  115. {
  116.   Scene_Map.prototype.processMapTouch = function () {
  117.     if (TouchInput.isTriggered() || this._touchCount > 0) {
  118.       if (TouchInput.isPressed()) {
  119.         if (this._touchCount === 0 || this._touchCount >= 15) {
  120.           var x = $gameMap.canvasToMapXTouch(TouchInput.x);
  121.           var y = $gameMap.canvasToMapYTouch(TouchInput.y);
  122.           $gameTemp.setDestination(x, y);
  123.         }
  124.         this._touchCount++;
  125.       } else {
  126.         this._touchCount = 0;
  127.       }
  128.     }
  129.   };
  130.  
  131.   Scene_Map.prototype.updateDestination = function () {
  132.     if (this.isMapTouchOk()) {
  133.       this.processMapTouch();
  134.     } else {
  135.       this._touchCount = 0;
  136.     }
  137.   };
  138.  
  139.   Game_Map.prototype.canvasToMapXTouch = function (x) {
  140.     var tileWidth = this.tileWidth();
  141.     var originX = this._displayX * tileWidth;
  142.     var mapX = (originX + x) / tileWidth - 0.5;
  143.     return mapX;
  144.   };
  145.  
  146.   Game_Map.prototype.canvasToMapYTouch = function (y) {
  147.     var tileHeight = this.tileHeight();
  148.     var originY = this._displayY * tileHeight;
  149.     var mapY = (originY + y) / tileHeight - 0.5;
  150.     return mapY;
  151.   };
  152. }
  153.  
  154. // Player
  155. {
  156.   const DIR = {
  157.     DOWN_LEFT: 1,
  158.     DOWN: 2,
  159.     DOWN_RIGHT: 3,
  160.     LEFT: 4,
  161.     IDLE: 0,
  162.     RIGHT: 6,
  163.     UP_LEFT: 7,
  164.     UP: 8,
  165.     UP_RIGHT: 9,
  166.   };
  167.  
  168.   const UNIT_STEP = 0.1;
  169.  
  170.   Game_Player.prototype.getInputDirection = function () {
  171.     return Input.dir8;
  172.   };
  173.  
  174.   Game_Player.prototype.reverseDir = function (d) {
  175.     return (10 - d) % 10;
  176.   };
  177.  
  178.   Game_Player.prototype.executeMove = function (dx, dy) {
  179.     if (Math.abs(dx) > Math.abs(dy)) {
  180.       if (dx > 0) {
  181.         this.setDirection(DIR.RIGHT);
  182.       } else {
  183.         this.setDirection(DIR.LEFT);
  184.       }
  185.     } else {
  186.       if (dy > 0) {
  187.         this.setDirection(DIR.DOWN);
  188.       } else {
  189.         this.setDirection(DIR.UP);
  190.       }
  191.     }
  192.     const check = [];
  193.     if (dx > 0) check.push(DIR.RIGHT);
  194.     if (dx < 0) check.push(DIR.LEFT);
  195.     if (dy > 0) check.push(DIR.DOWN);
  196.     if (dy < 0) check.push(DIR.UP);
  197.     for (let i of check) {
  198.       if (!this.rectCanPass(dx, dy, i)) {
  199.         if ([DIR.RIGHT, DIR.LEFT].includes(i)) dx = 0;
  200.         if ([DIR.DOWN, DIR.UP].includes(i)) dy = 0;
  201.       }
  202.     }
  203.     if (!Math.abs(dx) && !Math.abs(dy)) {
  204.       $gameTemp.clearDestination();
  205.       return;
  206.     }
  207.     this._realX = this._x;
  208.     this._realY = this._y;
  209.     this._x += dx;
  210.     this._y += dy;
  211.   };
  212.  
  213.   Object.defineProperties(Game_Player.prototype, {
  214.     rect: {
  215.       get: function () {
  216.         return new Rectangle(this._x + 0.06, this._y + 0.06, 0.75, 0.75);
  217.       },
  218.     },
  219.   });
  220.  
  221.   Game_Player.prototype.rectCanPass = function (dx, dy, direction) {
  222.     const { left, right, top, bottom } = this.rect;
  223.     const rd = this.reverseDir(direction);
  224.     if (!this.canPass(left, top, rd)) return false;
  225.     if (!this.canPass(left, bottom, rd)) return false;
  226.     if (!this.canPass(right, top, rd)) return false;
  227.     if (!this.canPass(right, bottom, rd)) return false;
  228.     if (!this.canPass(left + dx, top + dy, direction)) return false;
  229.     if (!this.canPass(left + dx, bottom + dy, direction)) return false;
  230.     if (!this.canPass(right + dx, top + dy, direction)) return false;
  231.     if (!this.canPass(right + dx, bottom + dy, direction)) return false;
  232.     return true;
  233.   };
  234.  
  235.   Game_Player.prototype.canPass = function (x, y, d) {
  236.     x = Math.floor(x);
  237.     y = Math.floor(y);
  238.     if (!$gameMap.isValid(x, y)) {
  239.       return false;
  240.     }
  241.     if (this.isThrough() || this.isDebugThrough()) {
  242.       return true;
  243.     }
  244.     if (!this.isMapPassable(x, y, d)) {
  245.       return false;
  246.     }
  247.     if (this.isCollidedWithCharacters(x, y)) {
  248.       return false;
  249.     }
  250.     return true;
  251.   };
  252.  
  253.   Game_Player.prototype.isMapPassable = function (x, y, d) {
  254.     return $gameMap.isPassable(x, y, d);
  255.   };
  256.  
  257.   Game_Player.prototype.moveByInput = function () {
  258.     if (!this.isMoving() && this.canMove()) {
  259.       var direction = this.getInputDirection();
  260.       let dx, dy;
  261.       if (direction > 0) {
  262.         $gameTemp.clearDestination();
  263.         dx = +(Input.inputX * UNIT_STEP).toFixed(3);
  264.         dy = +(Input.inputY * UNIT_STEP).toFixed(3);
  265.       } else if ($gameTemp.isDestinationValid()) {
  266.         dx = $gameTemp.destinationX() - this._x;
  267.         dy = $gameTemp.destinationY() - this._y;
  268.         if (Math.abs(dx) < 0.1 && Math.abs(dy) < 0.1) {
  269.           $gameTemp.clearDestination();
  270.           return;
  271.         }
  272.         if (Math.abs(dx) > Math.abs(dy)) {
  273.           dx = dx / Math.abs(dx);
  274.           dy = dy / Math.abs(dx);
  275.         } else {
  276.           dx = dx / Math.abs(dy);
  277.           dy = dy / Math.abs(dy);
  278.         }
  279.         dx *= UNIT_STEP;
  280.         dy *= UNIT_STEP;
  281.       }
  282.       if (dx || dy) {
  283.         this.executeMove(dx, dy);
  284.       }
  285.     }
  286.   };
  287.  
  288.   Game_Player.prototype.updateNonmoving = function (wasMoving) {
  289.     if (!$gameMap.isEventRunning()) {
  290.       if (wasMoving) {
  291.         $gameParty.onPlayerWalk();
  292.         this.checkEventTriggerHere([1, 2]);
  293.         if ($gameMap.setupStartingEvent()) {
  294.           return;
  295.         }
  296.       }
  297.       if (this.triggerAction()) {
  298.         return;
  299.       }
  300.       if (wasMoving) {
  301.         this.updateEncounterCount();
  302.       }
  303.     }
  304.   };
  305.  
  306.   Game_Player.prototype.distancePerFrame = function () {
  307.     return Math.pow(2, this.realMoveSpeed()) / 256 / 2;
  308.   };
  309. }
  310.  
  311. // NPC
  312. {
  313.   Game_CharacterBase.prototype.pos = function (x, y) {
  314.     return Math.abs(this._x - x) <= 0.5 && Math.abs(this._y - y) <= 0.5;
  315.   };
  316.  
  317.   Game_Event.prototype.isCollidedWithEvents = function (x, y) {
  318.     var events = $gameMap.eventsXyNt(x, y).filter((item) => item !== this);
  319.     return events.length > 0;
  320.   };
  321. }

评分

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

查看全部评分

夏普的道具店

塞露提亚-道具屋的经营妙方同人作品
发布帖:点击这里

Lv3.寻梦者

梦石
0
星屑
2775
在线时间
601 小时
注册时间
2018-5-18
帖子
331
2
发表于 2020-5-26 11:23:49 | 只看该作者
顶一顶,铁子厉害了
这个人很馋,什么都没有留下。。。
回复 支持 反对

使用道具 举报

Lv4.逐梦者

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

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
2577
在线时间
362 小时
注册时间
2015-8-15
帖子
80
4
发表于 2020-5-27 10:42:41 | 只看该作者
移动颗粒为0.25单元格,这个最大的问题在于通行
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
100
在线时间
7 小时
注册时间
2022-1-29
帖子
3
5
发表于 2022-6-30 17:33:20 | 只看该作者
梦寐以求的插件!!!!
回复 支持 反对

使用道具 举报

Lv5.捕梦者

梦石
0
星屑
31074
在线时间
173 小时
注册时间
2022-1-8
帖子
230
6
发表于 2022-7-1 07:14:52 | 只看该作者
感谢大佬分享!
我在平台上传的插件均可免费商用,转载
插件传送门:https://rpg.blue/forum.php?mod=v ... eid%26typeid%3D1306
接单中,可接RPGMaker MV/MZ插件定制,QQ:3268006598
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-5-9 05:09

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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