找回密码
 注册用户
查看: 280|回复: 0

[RMMZ] 【汉化】距离与直线视野检查

[复制链接]

88

主题

7

回帖

1277

积分

资深会员

积分
1277
发表于 2024-6-2 23:15:38 | 显示全部楼层 |阅读模式
本帖最后由 烁灵 于 2024-6-20 17:46 编辑

可判断事件与玩家是否接近,包含直线视野。


原地址:Proximity Sensor (RMMZ) | RPG Maker Forums (rpgmakerweb.com)

设置预览:
fl.png


脚本:命名为 r88_ProximitySensor.js
  1. /*:
  2. @target MZ
  3. @plugindesc 距离检测与直线视线.
  4. 汉化 by 烁灵 更多脚本请访问 www.hknmtt.com
  5. @author reflector88
  6. @url https://reflector88.itch.io/
  7. @help

  8. "Proximity Sensor 1.2"
  9. 本脚本将可在事件接近玩家时开启 开关或独立开关
  10. 可用来制作巡逻敌人、隐身物品、陷阱等,同时提供直线视线功能

  11. Update
  12. -V1.1 Added "Facing in a Line"
  13. -V1.2 Switches now toggle; improved compatibility
  14. ____________________________________________________________________________
  15. 设置
  16. 1. 在事件指令中选择 "插件指令"

  17. 2. 选择你想用的距离检测类型:
  18.     "所有" - 在所有方向上检测.
  19.     "面对" - 只检查事件面对的方向.
  20.     "十字线" - 在十字线上检测.
  21.     "面对线" - 在事件面对的方向,并且与事件在同一条线上

  22. 3. 设置对象和距离
  23.     (比如 "小于等于 3" 检查玩家是否与事件距离 3 格以内)

  24. 4. 设置要开启的开关/独立开关

  25. 5. 设置区域ID或地形标识来表示视野障碍
  26. ____________________________________________________________________________

  27. TERMS OF USE
  28. This plugin is free to use in both commercial and non-commercial projects,
  29. though please credit me.


  30. @param Obstacle Region ID
  31. @text 障碍物区域ID
  32. @type number
  33. @desc 阻挡视线的区域ID
  34. @default 1

  35. @param Obstacle Terrain Tag
  36. @text 障碍物地形标识
  37. @type number
  38. @desc 阻挡视线的地形标识
  39. @default 1

  40. @command Basic
  41. @text 所有方向
  42. @desc 检查事件是否接近玩家

  43.     @arg Operator
  44.     @text 计算方式
  45.     @type select
  46.     @option 小于
  47.     @value less than
  48.     @option 小于等于
  49.     @value less than or equal to
  50.     @option 等于
  51.     @value equals
  52.     @option 大于等于
  53.     @value greater than or equal to
  54.     @option 大于
  55.     @value greater than
  56.     @default less than or equal to
  57.     @desc 计算方式.

  58.     @arg Distance
  59.     @text 距离
  60.     @type number
  61.     @default 3
  62.     @desc 比较与玩家的距离

  63.     @arg Self-Switch
  64.     @text 独立开关
  65.     @type select
  66.     @option 0 @option A @option B @option C @option D
  67.     @default A
  68.     @desc 开启的独立开关

  69.     @arg Switch
  70.     @text 开关
  71.     @type switch
  72.     @default 0
  73.     @desc 开启的开关

  74. @command Facing
  75. @text 面对
  76. @desc 检查事件是否在事件面向的方向上接近玩家

  77.     @arg Operator
  78.     @text 计算方式
  79.     @type select
  80.     @option 小于
  81.     @value less than
  82.     @option 小于等于
  83.     @value less than or equal to
  84.     @option 等于
  85.     @value equals
  86.     @option 大于等于
  87.     @value greater than or equal to
  88.     @option 大于
  89.     @value greater than
  90.     @default less than or equal to
  91.     @desc 计算方式.

  92.     @arg Distance
  93.     @text 距离
  94.     @type number
  95.     @default 3
  96.     @desc 比较与玩家的距离

  97.     @arg Self-Switch
  98.     @text 独立开关
  99.     @type select
  100.     @option 0 @option A @option B @option C @option D
  101.     @default A
  102.     @desc 开启的独立开关

  103.     @arg Switch
  104.     @text 开关
  105.     @type switch
  106.     @default 0
  107.     @desc 开启的开关

  108. @command Orthogonal
  109. @text 十字线
  110. @desc 检查事件在所在的十字线上是否接近玩家

  111.     @arg Operator
  112.     @text 计算方式
  113.     @type select
  114.     @option 小于
  115.     @value less than
  116.     @option 小于等于
  117.     @value less than or equal to
  118.     @option 等于
  119.     @value equals
  120.     @option 大于等于
  121.     @value greater than or equal to
  122.     @option 大于
  123.     @value greater than
  124.     @default less than or equal to
  125.     @desc 计算方式.

  126.     @arg Distance
  127.     @text 距离
  128.     @type number
  129.     @default 3
  130.     @desc 比较与玩家的距离

  131.     @arg Self-Switch
  132.     @text 独立开关
  133.     @type select
  134.     @option 0 @option A @option B @option C @option D
  135.     @default A
  136.     @desc 开启的独立开关

  137.     @arg Switch
  138.     @text 开关
  139.     @type switch
  140.     @default 0
  141.     @desc 开启的开关

  142. @command FacingLine
  143. @text 面对线
  144. @desc 检查事件在事件面对的线上是否接近玩家.

  145.     @arg Operator
  146.     @text 计算方式
  147.     @type select
  148.     @option 小于
  149.     @value less than
  150.     @option 小于等于
  151.     @value less than or equal to
  152.     @option 等于
  153.     @value equals
  154.     @option 大于等于
  155.     @value greater than or equal to
  156.     @option 大于
  157.     @value greater than
  158.     @default less than or equal to
  159.     @desc 计算方式.

  160.     @arg Distance
  161.     @text 距离
  162.     @type number
  163.     @default 3
  164.     @desc 比较与玩家的距离

  165.     @arg Self-Switch
  166.     @text 独立开关
  167.     @type select
  168.     @option 0 @option A @option B @option C @option D
  169.     @default A
  170.     @desc 开启的独立开关

  171.     @arg Switch
  172.     @text 开关
  173.     @type switch
  174.     @default 0
  175.     @desc 开启的开关
  176. */

  177. (() => {
  178.     'use strict';
  179.     var r88 = r88 || {};
  180.     r88.PS = r88.PS || {};
  181.     r88.PS.parameters = PluginManager.parameters('r88_ProximitySensor');
  182.     r88.PS.regionId = r88.PS.parameters["Obstacle Region ID"];
  183.     r88.PS.terrainTag = r88.PS.parameters["Obstacle Terrain Tag"];

  184.     function r88_isProx(args) {
  185.         const playerX = $gamePlayer.x;
  186.         const playerY = $gamePlayer.y;
  187.         const eventX = $gameMap.event(this._eventId).x;
  188.         const eventY = $gameMap.event(this._eventId).y;
  189.         const operator = args['Operator'];
  190.         const distance = args['Distance'];
  191.         const proximity = (Math.sqrt(Math.pow(eventX - playerX, 2) +
  192.             Math.pow(eventY - playerY, 2)));

  193.         switch (operator) {
  194.             case 'less than': return proximity < distance;
  195.             case 'less than or equal to': return proximity <= distance;
  196.             case 'equals': return proximity === distance;
  197.             case 'greater than or equal to': return proximity >= distance;
  198.             case 'greater than': return proximity > distance;
  199.             default: return false;
  200.         }
  201.     }

  202.     function r88_isFacing() {
  203.         const dir = $gameMap.event(this._eventId).direction();
  204.         const playerX = $gamePlayer.x;
  205.         const playerY = $gamePlayer.y;
  206.         const eventX = $gameMap.event(this._eventId).x;
  207.         const eventY = $gameMap.event(this._eventId).y;

  208.         if (dir == 2 && eventY < playerY || dir == 8 && eventY > playerY
  209.             || dir == 4 && eventX > playerX || dir == 6 && eventX < playerX) {
  210.             return true;
  211.         } else {
  212.             return false;
  213.         }
  214.     }

  215.     function r88_isOrthogonal() {
  216.         const playerX = $gamePlayer.x;
  217.         const playerY = $gamePlayer.y;
  218.         const eventX = $gameMap.event(this._eventId).x;
  219.         const eventY = $gameMap.event(this._eventId).y;

  220.         return playerX === eventX || playerY === eventY;
  221.     }

  222.     // Bresenham Algorithm for line of sight calculation
  223.     function r88_inLineOfSight() {
  224.         const tileCoords = [];
  225.         const playerX = $gamePlayer.x;
  226.         const playerY = $gamePlayer.y;
  227.         const eventX = $gameMap.event(this._eventId).x;
  228.         const eventY = $gameMap.event(this._eventId).y;
  229.         const distanceX = Math.abs(playerX - eventX);
  230.         const distanceY = Math.abs(playerY - eventY);
  231.         const incrementX = (eventX < playerX) ? 1 : -1;
  232.         const incrementY = (eventY < playerY) ? 1 : -1;
  233.         let tileX = eventX;
  234.         let tileY = eventY;
  235.         let error = distanceX - distanceY;

  236.         while (tileX !== playerX || tileY !== playerY) {
  237.             tileCoords.push([tileX, tileY]);

  238.             const error2 = 2 * error;
  239.             if (error2 > -distanceY) {
  240.                 error -= distanceY;
  241.                 tileX += incrementX;
  242.             }
  243.             if (error2 < distanceX) {
  244.                 error += distanceX;
  245.                 tileY += incrementY;
  246.             }
  247.         }

  248.         for (let i = 0; i < tileCoords.length; i++) {
  249.             if ($gameMap.regionId(tileCoords[i][0], tileCoords[i][1]) == r88.PS.regionId ||
  250.                 $gameMap.terrainTag(tileCoords[i][0], tileCoords[i][1]) == r88.PS.terrainTag)
  251.                 return false;
  252.         }
  253.         return true;

  254.     }

  255.     function r88_FlipSwitch(args) {
  256.         if (r88_inLineOfSight.call(this)) {

  257.             if (args['Self-Switch'] !== '0') {
  258.                 $gameSelfSwitches.setValue([this._mapId, this._eventId,
  259.                 args['Self-Switch']], !$gameSelfSwitches.value([this._mapId, this._eventId,
  260.                 args['Self-Switch']]));
  261.             }
  262.             $gameSwitches.setValue(args['Switch'], !$gameSwitches.value(args['Switch']));
  263.         }
  264.     }

  265.     // User-defined plugin commands
  266.     function r88_basicProx(args) {
  267.         if (r88_isProx.call(this, args)) {
  268.             r88_FlipSwitch.call(this, args);
  269.         }
  270.     }

  271.     function r88_facingProx(args) {
  272.         if (r88_isProx.call(this, args) && r88_isFacing.call(this)) {
  273.             r88_FlipSwitch.call(this, args);
  274.         }
  275.     }

  276.     function r88_orthogonalProx(args) {
  277.         if (r88_isProx.call(this, args) && r88_isOrthogonal.call(this)) {
  278.             r88_FlipSwitch.call(this, args);
  279.         }
  280.     }

  281.     function r88_facingLineProx(args) {
  282.         if (r88_isProx.call(this, args) && r88_isFacing.call(this) && r88_isOrthogonal.call(this)) {
  283.             r88_FlipSwitch.call(this, args);
  284.         }
  285.     }


  286.     PluginManager.registerCommand("r88_ProximitySensor", "Basic", r88_basicProx);
  287.     PluginManager.registerCommand("r88_ProximitySensor", "Facing", r88_facingProx);
  288.     PluginManager.registerCommand("r88_ProximitySensor", "Orthogonal", r88_orthogonalProx);
  289.     PluginManager.registerCommand("r88_ProximitySensor", "FacingLine", r88_facingLineProx);

  290. })();
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册用户

本版积分规则

Archiver|QQ群: 48625831|爱上RPG|哈库纳玛塔塔 |网站地图 Clicky

GMT+8, 2024-10-23 13:29 , Processed in 0.066615 second(s), 21 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

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