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

Project1

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

[有事请教] 怎么做那种敌人在特定视野内看到主角就追逐的效果啊

[复制链接]

Lv2.观梦者

梦石
0
星屑
302
在线时间
53 小时
注册时间
2021-2-27
帖子
15
跳转到指定楼层
1
发表于 5 天前 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
想做个类似以前那种只要看到主角就会冒感叹号然后追逐主人公的效果,踩地雷的模式还是太难绷了

Lv6.析梦学徒

梦石
64
星屑
15227
在线时间
3972 小时
注册时间
2016-6-17
帖子
1060

极短26获奖极短24参与开拓者

2
发表于 5 天前 | 只看该作者
可以看这个发布页https://reflector88.itch.io/proximity-sensor

这个插件可以让事件设定按面对方向的照格子、十字方向之类的检测玩家是否进入判定区域
作者是reflector88

JS 代码复制
  1. /*:
  2. @target MZ
  3. @plugindesc 计算距离和视线。
  4. @author reflector88
  5. @url [url]https://reflector88.itch.io/[/url]
  6. @help
  7.  
  8. "Proximity Sensor 1.3"
  9. 当有事件发生时,该插件会激活一个开关/自开关,以便
  10. 玩家。这对漫游敌人、隐身序列和陷阱非常有用。
  11. 还可选择方向和视线。
  12.  
  13. 更新
  14. -V1.1 添加了 "面对一条线 "功能
  15. -V1.2 开关现在可以切换;兼容性得到改善
  16. -V1.3 修正了与某些像素移动脚本一起使用时的崩溃问题
  17. ____________________________________________________________________________
  18. 配置
  19. 1. 打开事件命令窗口,选择 "插件命令
  20.  
  21. 2. 选择要使用的接近类型:
  22. "所有方向"- 检查所有方向。
  23. "面向"- 仅检查事件面向的方向。
  24. "Orthogonal Directions(正交方向)"- 仅在十字形中进行检查。
  25. 面向一条直线"--"面向 "和 "正交 "的组合。
  26.  
  27. 3. 设置操作员和距离,以确定事件的视距
  28. (例如,如果玩家距离小于 3 个方砖,则检查 "小于 3") 4.
  29.  
  30. 4. 4. 设置开关和/或自开关,以便在事件接近时切换。
  31.  
  32. 5. 在地图编辑器中设置区域 ID 和/或地形标签来表示障碍物。
  33. ____________________________________________________________________________
  34.  
  35. TERMS OF USE
  36. This plugin is free to use in both commercial and non-commercial projects,
  37. though please credit me.
  38.  
  39.  
  40. @param Obstacle Region ID
  41. @type number
  42. @desc Regions with this ID will block line of sight.
  43. @default 1
  44.  
  45. @param Obstacle Terrain Tag
  46. @type number
  47. @desc Terrain with this tag will block line of sight.
  48. @default 1
  49.  
  50. @command Basic
  51. @text All Directions
  52. @desc Checks if this event is in proximity of the player.
  53.  
  54.     @arg Operator
  55.     @type select
  56.     @option less than @option less than or equal to @option equals @option greater than or equal to @option greater than
  57.     @default less than or equal to
  58.     @desc The comparison operator.
  59.  
  60.     @arg Distance
  61.     @type number
  62.     @default 3
  63.     @desc The distance to be compared with the player's proximity to this event.
  64.  
  65.     @arg Self-Switch
  66.     @type select
  67.     @option 0 @option A @option B @option C @option D
  68.     @default A
  69.     @desc Switch is toggled if the proximity condition is fulfilled.
  70.  
  71.     @arg Switch
  72.     @type switch
  73.     @default 0
  74.     @desc Switch is toggled if the proximity condition is fulfilled.
  75.  
  76. @command Facing
  77. @text Facing
  78. @desc Checks if this event is facing and in proximity of the player.
  79.  
  80.     @arg Operator
  81.     @type select
  82.     @option less than @option less than or equal to @option equals @option greater than or equal to @option greater than
  83.     @default less than or equal to
  84.     @desc The comparison operator.
  85.  
  86.     @arg Distance
  87.     @type number
  88.     @default 3
  89.     @desc The distance to be compared with the player's proximity to this event.
  90.  
  91.     @arg Self-Switch
  92.     @type select
  93.     @option 0 @option A @option B @option C @option D
  94.     @default A
  95.     @desc Switch is toggled if the proximity condition is fulfilled.
  96.  
  97.     @arg Switch
  98.     @type switch
  99.     @default 0
  100.     @desc Switch is toggled if the proximity condition is fulfilled.
  101.  
  102. @command Orthogonal
  103. @text Orthogonal Directions
  104. @desc Checks if this event is in proximity of and in line with the player.
  105.  
  106.     @arg Operator
  107.     @type select
  108.     @option less than @option less than or equal to @option equals @option greater than or equal to @option greater than
  109.     @default less than or equal to
  110.     @desc The comparison operator.
  111.  
  112.     @arg Distance
  113.     @type number
  114.     @default 3
  115.     @desc The distance to be compared with the player's proximity to this event.
  116.  
  117.     @arg Self-Switch
  118.     @type select
  119.     @option 0 @option A @option B @option C @option D
  120.     @default A
  121.     @desc Switch is toggled if the proximity condition is fulfilled.
  122.  
  123.     @arg Switch
  124.     @type switch
  125.     @default 0
  126.     @desc Switch is toggled if the proximity condition is fulfilled.
  127.  
  128. @command FacingLine
  129. @text Facing in a Line
  130. @desc Checks if this event is in proximity, in line, and facing the player.
  131.  
  132.     @arg Operator
  133.     @type select
  134.     @option less than @option less than or equal to @option equals @option greater than or equal to @option greater than
  135.     @default less than or equal to
  136.     @desc The comparison operator.
  137.  
  138.     @arg Distance
  139.     @type number
  140.     @default 3
  141.     @desc The distance to be compared with the player's proximity to this event.
  142.  
  143.     @arg Self-Switch
  144.     @type select
  145.     @option 0 @option A @option B @option C @option D
  146.     @default A
  147.     @desc Switch is toggled if the proximity condition is fulfilled.
  148.  
  149.     @arg Switch
  150.     @type switch
  151.     @default 0
  152.     @desc Switch is toggled if the proximity condition is fulfilled.
  153. */
  154.  
  155. (() => {
  156.     'use strict';
  157.     var r88 = r88 || {};
  158.     r88.PS = r88.PS || {};
  159.     r88.PS.parameters = PluginManager.parameters('r88_ProximitySensor');
  160.     r88.PS.regionId = r88.PS.parameters["Obstacle Region ID"];
  161.     r88.PS.terrainTag = r88.PS.parameters["Obstacle Terrain Tag"];
  162.  
  163.     function r88_isProx(args) {
  164.         const playerX = $gamePlayer.x;
  165.         const playerY = $gamePlayer.y;
  166.         const eventX = $gameMap.event(this._eventId).x;
  167.         const eventY = $gameMap.event(this._eventId).y;
  168.         const operator = args['Operator'];
  169.         const distance = args['Distance'];
  170.         const proximity = (Math.sqrt(Math.pow(eventX - playerX, 2) +
  171.             Math.pow(eventY - playerY, 2)));
  172.  
  173.         switch (operator) {
  174.             case 'less than': return proximity < distance;
  175.             case 'less than or equal to': return proximity <= distance;
  176.             case 'equals': return proximity === distance;
  177.             case 'greater than or equal to': return proximity >= distance;
  178.             case 'greater than': return proximity > distance;
  179.             default: return false;
  180.         }
  181.     }
  182.  
  183.     function r88_isFacing() {
  184.         const dir = $gameMap.event(this._eventId).direction();
  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.  
  190.         if (dir == 2 && eventY < playerY || dir == 8 && eventY > playerY
  191.             || dir == 4 && eventX > playerX || dir == 6 && eventX < playerX) {
  192.             return true;
  193.         } else {
  194.             return false;
  195.         }
  196.     }
  197.  
  198.     function r88_isOrthogonal() {
  199.         const playerX = $gamePlayer.x;
  200.         const playerY = $gamePlayer.y;
  201.         const eventX = $gameMap.event(this._eventId).x;
  202.         const eventY = $gameMap.event(this._eventId).y;
  203.  
  204.         return playerX === eventX || playerY === eventY;
  205.     }
  206.  
  207.     // Bresenham Algorithm for line of sight calculation
  208.     function r88_inLineOfSight() {
  209.         const tileCoords = [];
  210.         const playerX = $gamePlayer.x;
  211.         const playerY = $gamePlayer.y;
  212.         const eventX = $gameMap.event(this._eventId).x;
  213.         const eventY = $gameMap.event(this._eventId).y;
  214.         const distanceX = Math.abs(playerX - eventX);
  215.         const distanceY = Math.abs(playerY - eventY);
  216.         const incrementX = (eventX < playerX) ? 1 : -1;
  217.         const incrementY = (eventY < playerY) ? 1 : -1;
  218.         let tileX = eventX;
  219.         let tileY = eventY;
  220.         let error = distanceX - distanceY;
  221.  
  222.  
  223.         const hypotenuse = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
  224.         let tileHypotenuse = Math.sqrt(Math.pow(tileX - eventX, 2) + Math.pow(tileY - eventY, 2));
  225.  
  226.  
  227.         while (tileHypotenuse < hypotenuse) {
  228.             tileCoords.push([tileX, tileY]);
  229.  
  230.             const error2 = 2 * error;
  231.             if (error2 > -distanceY) {
  232.                 error -= distanceY;
  233.                 tileX += incrementX;
  234.             }
  235.             if (error2 < distanceX) {
  236.                 error += distanceX;
  237.                 tileY += incrementY;
  238.             }
  239.  
  240.             tileHypotenuse = Math.sqrt(Math.pow(tileX - eventX, 2) + Math.pow(tileY - eventY, 2));
  241.         }
  242.  
  243.         for (let i = 0; i < tileCoords.length; i++) {
  244.             if ($gameMap.regionId(tileCoords[i][0], tileCoords[i][1]) == r88.PS.regionId ||
  245.                 $gameMap.terrainTag(tileCoords[i][0], tileCoords[i][1]) == r88.PS.terrainTag)
  246.                 return false;
  247.         }
  248.         return true;
  249.  
  250.     }
  251.  
  252.     function r88_FlipSwitch(args) {
  253.         if (r88_inLineOfSight.call(this)) {
  254.  
  255.             if (args['Self-Switch'] !== '0') {
  256.                 $gameSelfSwitches.setValue([this._mapId, this._eventId,
  257.                 args['Self-Switch']], !$gameSelfSwitches.value([this._mapId, this._eventId,
  258.                 args['Self-Switch']]));
  259.             }
  260.             $gameSwitches.setValue(args['Switch'], !$gameSwitches.value(args['Switch']));
  261.         }
  262.     }
  263.  
  264.     // User-defined plugin commands
  265.     function r88_basicProx(args) {
  266.         if (r88_isProx.call(this, args)) {
  267.             r88_FlipSwitch.call(this, args);
  268.         }
  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.  
  277.     function r88_orthogonalProx(args) {
  278.         if (r88_isProx.call(this, args) && r88_isOrthogonal.call(this)) {
  279.             r88_FlipSwitch.call(this, args);
  280.         }
  281.     }
  282.  
  283.     function r88_facingLineProx(args) {
  284.         if (r88_isProx.call(this, args) && r88_isFacing.call(this) && r88_isOrthogonal.call(this)) {
  285.             r88_FlipSwitch.call(this, args);
  286.         }
  287.     }
  288.  
  289.  
  290.     PluginManager.registerCommand("r88_ProximitySensor", "Basic", r88_basicProx);
  291.     PluginManager.registerCommand("r88_ProximitySensor", "Facing", r88_facingProx);
  292.     PluginManager.registerCommand("r88_ProximitySensor", "Orthogonal", r88_orthogonalProx);
  293.     PluginManager.registerCommand("r88_ProximitySensor", "FacingLine", r88_facingLineProx);
  294.  
  295. })();
回复 支持 1 反对 0

使用道具 举报

Lv2.观梦者

梦石
0
星屑
302
在线时间
53 小时
注册时间
2021-2-27
帖子
15
3
 楼主| 发表于 5 天前 | 只看该作者
Arfies 发表于 2025-7-12 20:18
可以看这个发布页https://reflector88.itch.io/proximity-sensor

这个插件可以让事件设定按面对方向的照格 ...

太好了,谢谢大佬QAQ
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2025-7-17 20:48

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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