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

Project1

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

[原创发布] 自改事件点击触发增强版,添加触发格位

[复制链接]

Lv3.寻梦者

梦石
0
星屑
1120
在线时间
137 小时
注册时间
2020-12-6
帖子
55
跳转到指定楼层
1
发表于 2026-1-21 16:27:41 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

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

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

x
JAVASCRIPT 代码复制
  1. //=============================================================================
  2. // Yanfly Engine Plugins - Event Click Trigger
  3. // YEP_EventClickTrigger.js
  4. //=============================================================================
  5.  
  6. var Imported = Imported || {};
  7. Imported.YEP_EventClickTrigger = true;
  8.  
  9. var Yanfly = Yanfly || {};
  10. Yanfly.EvClTr = Yanfly.EvClTr || {};
  11. Yanfly.EvClTr.version = 1.10;
  12.  
  13. //=============================================================================
  14. /*:
  15.  * @plugindesc v1.10 事件点击触发 - 增强版
  16.  * @author Yanfly Engine Plugins
  17.  * @modified by Your Name
  18.  *
  19.  * @help
  20.  * ============================================================================
  21.  * 导言
  22.  *  ============================================================================
  23.  *
  24.  * 有没有想过可以点击一个事件并立即触发它
  25.  * 而不必让你的玩家角色一直走到它
  26.  * 触发前先检查一下?有了这个插件,你可以。通过设置
  27.  * 特定事件的某些注释标签和/或评论标签,玩家现在可以
  28.  * 只需在远处点击即可触发事件。
  29.  *
  30.  *  ============================================================================
  31.  * 新功能:触发格位
  32.  *  ============================================================================
  33.  *
  34.  * 现在可以设置事件的触发格位,让事件可以在特定的格子位置被触发
  35.  * 而不需要在事件所在的精确格位点击。
  36.  *
  37.  * 用法:
  38.  * <Click Trigger>
  39.  * <trigger area: x,y,width,height>
  40.  *
  41.  * 例如:
  42.  * <trigger area: 0,0,3,2> - 从事件位置开始,向右3格,向下2格的区域
  43.  * <trigger area: -1,-1,3,3> - 以事件为中心的3x3区域
  44.  *
  45.  * 如果不指定,默认只在事件所在格位触发
  46.  *
  47.  *  ============================================================================
  48.  * 注释标签和注释标签
  49.  *  ============================================================================
  50.  *
  51.  * 要从屏幕上的任何位置触发事件,请使用
  52.  * notetags或comment tags,使它们可以单击。如果notetag是
  53.  * 如果已使用,则无论是哪一页,这都将应用于整个事件。如果
  54.  * 仅使用注释标记,它将仅应用于该特定事件页。
  55.  *
  56.  * 事件注释标签和注释标签
  57.  *
  58.  *<Click Trigger>
  59.  *   - 这将导致事件可以从远处点击,而不需要玩家一直走到它前面来触发它。
  60.  *
  61.  *<trigger area: x,y,width,height>
  62.  *   - 设置事件的触发区域。x,y是相对于事件位置的偏移量,width,height是区域大小
  63.  *   - 示例:<trigger area: 0,0,3,2> 表示从事件位置开始3x2的矩形区域
  64.  *   - 示例:<trigger area: -1,-1,3,3> 表示以事件为中心的3x3区域
  65.  *
  66.  * ============================================================================
  67.  * Changelog
  68.  * ============================================================================
  69.  *
  70.  * Version 1.10:
  71.  * - 添加了触发格位功能
  72.  * - 修复了点击检测的问题
  73.  *
  74.  * Version 1.00:
  75.  * - Finished Plugin!
  76.  *
  77.  * ============================================================================
  78.  * End of Helpfile
  79.  * ============================================================================
  80.  *
  81.  */
  82. //=============================================================================
  83.  
  84. //=============================================================================
  85. // Game_Temp
  86. //=============================================================================
  87.  
  88. Yanfly.EvClTr.Game_Temp_setDestination = Game_Temp.prototype.setDestination;
  89. Game_Temp.prototype.setDestination = function(x, y) {
  90.   if (!this.isClickableEventAtXy(x, y)) {
  91.     Yanfly.EvClTr.Game_Temp_setDestination.call(this, x, y);
  92.   }
  93. };
  94.  
  95. Game_Temp.prototype.isClickableEventAtXy = function(x, y) {
  96.   // 先检查是否有事件在点击的精确位置
  97.   var events = $gameMap.eventsXy(x, y);
  98.   for (var i = 0; i < events.length; ++i) {
  99.     var ev = events[i];
  100.     if (ev && ev.isClickTriggered()) {
  101.       ev.onMouseClickTrigger();
  102.       return true;
  103.     }
  104.   }
  105.  
  106.   // 如果没有精确命中,检查触发格位
  107.   var allEvents = $gameMap.events();
  108.   for (var i = 0; i < allEvents.length; ++i) {
  109.     var ev = allEvents[i];
  110.     if (ev && ev.isClickTriggered() && ev.isInTriggerArea(x, y)) {
  111.       ev.onMouseClickTrigger();
  112.       return true;
  113.     }
  114.   }
  115.  
  116.   return false;
  117. };
  118.  
  119. //=============================================================================
  120. // TouchInput
  121. //=============================================================================
  122.  
  123. Yanfly.EvClTr.TouchInput_onMouseMove = TouchInput._onMouseMove;
  124. TouchInput._onMouseMove = function(event) {
  125.     Yanfly.EvClTr.TouchInput_onMouseMove.call(this, event);
  126.     this._mouseOverX = Graphics.pageToCanvasX(event.pageX);
  127.     this._mouseOverY = Graphics.pageToCanvasY(event.pageY);
  128. };
  129.  
  130. //=============================================================================
  131. // Game_Event
  132. //=============================================================================
  133.  
  134. Yanfly.EvClTr.Game_Event_setupPage = Game_Event.prototype.setupPage;
  135. Game_Event.prototype.setupPage = function() {
  136.   Yanfly.EvClTr.Game_Event_setupPage.call(this);
  137.   this.setupEventClickTriggerCommentTags();
  138. };
  139.  
  140. Game_Event.prototype.setupEventClickTriggerCommentTags = function() {
  141.   if (!this.page()) return;
  142.   this._isClickTriggerable = false;
  143.   this._triggerArea = null; // 重置触发区域
  144.  
  145.   // 检查事件备注中的标签
  146.   if (this.event().note.match(/<Click Trigger>/i)) {
  147.     this._isClickTriggerable = true;
  148.     this.setupTriggerAreaFromNote(this.event().note);
  149.   }
  150.  
  151.   // 检查事件页注释中的标签
  152.   var list = this.list();
  153.   if (!list) return;
  154.  
  155.   var length = list.length;
  156.   for (var i = 0; i < length; ++i) {
  157.     var ev = list[i];
  158.     if ([108, 408].contains(ev.code)) {
  159.       var comment = ev.parameters[0];
  160.       if (comment.match(/<Click Trigger>/i)) {
  161.         this._isClickTriggerable = true;
  162.       }
  163.       if (comment.match(/<trigger area:/i)) {
  164.         this.setupTriggerArea(comment);
  165.       }
  166.     }
  167.   }
  168. };
  169.  
  170. Game_Event.prototype.setupTriggerAreaFromNote = function(note) {
  171.   if (note.match(/<trigger area:/i)) {
  172.     this.setupTriggerArea(note);
  173.   }
  174. };
  175.  
  176. Game_Event.prototype.setupTriggerArea = function(comment) {
  177.   var match = comment.match(/<trigger area:\s*([-\d]+),\s*([-\d]+),\s*([-\d]+),\s*([-\d]+)>/i);
  178.   if (match) {
  179.     this._triggerArea = {
  180.       x: parseInt(match[1]),
  181.       y: parseInt(match[2]),
  182.       width: parseInt(match[3]),
  183.       height: parseInt(match[4])
  184.     };
  185.   }
  186. };
  187.  
  188. Game_Event.prototype.isClickTriggered = function() {
  189.   if (this._isClickTriggerable === undefined) {
  190.     this.setupEventClickTriggerCommentTags();
  191.   }
  192.   return this._isClickTriggerable || false;
  193. };
  194.  
  195. Game_Event.prototype.getTriggerArea = function() {
  196.   if (!this._triggerArea) {
  197.     // 默认触发区域:事件所在的一个格子
  198.     return {
  199.       x: 0,
  200.       y: 0,
  201.       width: 1,
  202.       height: 1
  203.     };
  204.   }
  205.   return this._triggerArea;
  206. };
  207.  
  208. Game_Event.prototype.isInTriggerArea = function(mapX, mapY) {
  209.   if (!this.isClickTriggered()) return false;
  210.  
  211.   var evX = this.x;
  212.   var evY = this.y;
  213.   var area = this.getTriggerArea();
  214.  
  215.   // 计算触发区域的边界
  216.   var startX = evX + area.x;
  217.   var startY = evY + area.y;
  218.   var endX = startX + area.width - 1;
  219.   var endY = startY + area.height - 1;
  220.  
  221.   // 检查点击位置是否在区域内
  222.   return mapX >= startX && mapX <= endX &&
  223.          mapY >= startY && mapY <= endY;
  224. };
  225.  
  226. Game_Event.prototype.onMouseClickTrigger = function() {
  227.   if (this.isClickTriggered()) {
  228.     $gameTemp.clearDestination();
  229.     this.start();
  230.   }
  231. };
  232.  
  233. //=============================================================================
  234. // 可视化触发区域(调试用)
  235. //=============================================================================
  236.  
  237. // 如果需要可视化显示触发区域,可以取消注释以下代码
  238. /*
  239. Yanfly.EvClTr.Sprite_Character_update = Sprite_Character.prototype.update;
  240. Sprite_Character.prototype.update = function() {
  241.   Yanfly.EvClTr.Sprite_Character_update.call(this);
  242.  
  243.   // 只在调试模式下显示触发区域
  244.   if (this._character instanceof Game_Event && this._character.isClickTriggered()) {
  245.     if (!this._triggerAreaOverlay) {
  246.       this._triggerAreaOverlay = new Sprite();
  247.       this._triggerAreaOverlay.bitmap = new Bitmap(1, 1);
  248.       this.addChild(this._triggerAreaOverlay);
  249.     }
  250.     
  251.     var area = this._character.getTriggerArea();
  252.     var tileWidth = $gameMap.tileWidth();
  253.     var tileHeight = $gameMap.tileHeight();
  254.     
  255.     // 计算屏幕位置
  256.     var screenX = this.x - tileWidth / 2;
  257.     var screenY = this.y - tileHeight;
  258.     
  259.     // 绘制触发区域
  260.     this._triggerAreaOverlay.x = screenX + area.x * tileWidth;
  261.     this._triggerAreaOverlay.y = screenY + area.y * tileHeight;
  262.     this._triggerAreaOverlay.bitmap.clear();
  263.     this._triggerAreaOverlay.bitmap.resize(
  264.       area.width * tileWidth,
  265.       area.height * tileHeight
  266.     );
  267.     this._triggerAreaOverlay.bitmap.fillRect(
  268.       0, 0,
  269.       area.width * tileWidth,
  270.       area.height * tileHeight,
  271.       'rgba(255, 0, 0, 0.3)'
  272.     );
  273.     this._triggerAreaOverlay.visible = true;
  274.   } else if (this._triggerAreaOverlay) {
  275.     this._triggerAreaOverlay.visible = false;
  276.   }
  277. };
  278. */
  279.  
  280. //=============================================================================
  281. // Scene_Map 更新 - 更好的鼠标悬停检测
  282. //=============================================================================
  283.  
  284. // 添加鼠标悬停检测功能(可选)
  285. /*
  286. Yanfly.EvClTr.Scene_Map_update = Scene_Map.prototype.update;
  287. Scene_Map.prototype.update = function() {
  288.   Yanfly.EvClTr.Scene_Map_update.call(this);
  289.  
  290.   if (TouchInput.isMoved() || TouchInput.isPressed()) {
  291.     var x = $gameMap.canvasToMapX(TouchInput.x);
  292.     var y = $gameMap.canvasToMapY(TouchInput.y);
  293.     
  294.     var allEvents = $gameMap.events();
  295.     for (var i = 0; i < allEvents.length; ++i) {
  296.       var ev = allEvents[i];
  297.       if (ev && ev.isClickTriggered() && ev.isInTriggerArea(x, y)) {
  298.         // 可以在这里添加悬停效果
  299.         document.body.style.cursor = 'pointer';
  300.         return;
  301.       }
  302.     }
  303.     document.body.style.cursor = 'default';
  304.   }
  305. };
  306. */
  307.  
  308. //=============================================================================
  309. // 添加插件命令(可选功能)
  310. //=============================================================================
  311.  
  312. // 如果需要插件命令控制,可以取消注释以下代码
  313. /*
  314. var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  315. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  316.   _Game_Interpreter_pluginCommand.call(this, command, args);
  317.  
  318.   if (command === 'EventClickTrigger') {
  319.     switch(args[0]) {
  320.       case 'enableEvent':
  321.         var eventId = parseInt(args[1]);
  322.         var event = $gameMap.event(eventId);
  323.         if (event) event._isClickTriggerable = true;
  324.         break;
  325.       case 'disableEvent':
  326.         var eventId = parseInt(args[1]);
  327.         var event = $gameMap.event(eventId);
  328.         if (event) event._isClickTriggerable = false;
  329.         break;
  330.       case 'setArea':
  331.         var eventId = parseInt(args[1]);
  332.         var x = parseInt(args[2]);
  333.         var y = parseInt(args[3]);
  334.         var width = parseInt(args[4]);
  335.         var height = parseInt(args[5]);
  336.         var event = $gameMap.event(eventId);
  337.         if (event) {
  338.           event._triggerArea = { x: x, y: y, width: width, height: height };
  339.         }
  340.         break;
  341.     }
  342.   }
  343. };
  344. */
  345.  
  346. //=============================================================================
  347. // End of File
  348. //=============================================================================
我是的大大,一个做辣鸡游戏的。
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

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

GMT+8, 2026-6-4 21:42

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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