Plugin to enable creating arbitrary trigger zones
I've created a plugin, which is compatible with both MV and MZ, that enables you to create trigger zones using a single event rather than having to duplicate a bunch of events in order to achieve the same effect, this is useful for things like map transfers, interact-able counters, and cutscene triggers.I've implemented it using the Event's note field to smuggle JSON into the plugin to be parsed with a very simple structure:
JSON:
{
"extentX": 2,
"extentY": 2,
"triggerOnce":true
}
The extents are defined in tiles defining a rectangle that radiates out from the event's position (the event is the center), the way the trigger's area is calculated favors growing up-left first, so a 2x2 event will end with the event being in the bottom-right, while a 4x4 will have the event in the center, this allows you to create trigger volumes in a very predictable way that is easy to calculate, simply count the tiles to either side of the event you wish to use as the center.
I've released the code under the MIT license so you are free to use it how you wish, even commercially, just make sure to attribute me in some fashion
JavaScript:
/*:
* @target MZ
* @target MV
* @plugindesc Allows events to be defined as a rectangle with extents relative to the event's tile position
* @author Phillip "Antidote" Stephens
*
* @license MIT
* @help AntiTriggerRectEvent.js
*
* This plugin enables the ability to create an arbitrary rectangle from an event's position
* the extents are defined as the total number of tiles surrounding the event.
*
* To define a rectangle simply add this following JSON struct with the extentX/extentY values changed for the desired
* rectangle to the event's notes field
*
* {"extentX":desired_width_in_tiles,"extentY":desired_height_in_tiles,"triggerOnce":desired_behavior}
*
* The following is a working example that defines a 2x2 tile strip starting from the event's x/y position,
* it also only triggers once meaning the event will activate only once
* {"extentX":2,"extentY":2,"triggerOnce":true}
*
* When calculating the rect we favor the building the rect upwards and leftwards,
* so it'll add a tile up then down, and left then right.
*
* As a visual aide this is what the above rectangle would look like:
*
* +----+----+
* |1 |2 |
* +----+----+
* |3 | EV |
* +----+----+
*
*/
(() => {
`use strict`;
const _Game_Event_initialize = Game_Event.prototype.initialize;
Game_Event.prototype.initialize = function(mapId, eventId) {
_Game_Event_initialize.call(this, mapId, eventId);
this._triggerExtentY = 0;
this._triggerExtentY = 0;
this._triggered = false;
this._triggerOnce = false;
const note = $dataMap.events.note;
if (note !== "") {
let rect = JSON.parse($dataMap.events.note) || null;
this._triggerExtentX = rect.extentX || 0;
this._triggerExtentY = rect.extentY || 0;
this._triggerOnce = rect.triggerOnce || false;
}
}
Game_Event.prototype.pos = function(x, y) {
if (this._triggerExtentX === 0 || this._triggerExtentY === 0) {
return Game_CharacterBase.prototype.pos.call(this, x, y);
}
/**
* When calculating the rect we favor the building the rect upwards and leftwards,
* so it'll add a tile up then down, and left then right
*/
const halfExtentX1 = Math.max(0, Math.ceil((this.triggerExtentX() / 2) - .5));
const halfExtentY1 = Math.max(0, Math.ceil((this.triggerExtentY() / 2) - .5));
const halfExtentX2 = Math.max(0, Math.floor((this.triggerExtentX() / 2) - .5));
const halfExtentY2 = Math.max(0, Math.floor((this.triggerExtentY() / 2) - .5));
const startX = Math.max(0, this._x - halfExtentX1);
const startY = Math.max(0, this._y - halfExtentY1);
const endX = Math.min($gameMap.width() - 1, this._x + halfExtentX2);
const endY = Math.min($gameMap.height() - 1, this._y + halfExtentY2);
//console.log("Trigger Rect startX %i, startY %i, endX %i, endY %i, halfX1 %i, halfY1 %i, halfX2 %i, halfY2 %i", startX, startY, endX, endY, halfExtentX1, halfExtentY1, halfExtentX2, halfExtentY2);
if (this._triggered && this._triggerOnce) {
return false;
}
return (x >= startX && y >= startY && x <= endX && y <= endY);
}
const _Game_Event_start = Game_Event.prototype.start;
Game_Event.prototype.start = function() {
_Game_Event_start.call(this);
if (this.isTriggerIn()) {
this._triggered = true;
}
}
Game_Event.prototype.triggerExtentX = function() {
return this._triggerExtentX;
}
Game_Event.prototype.triggerExtentY = function() {
return this._triggerExtentY;
}
})();
Linux Demo
Windows Demo
本贴来自国际rpgmaker官方论坛作者:Antidote174处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/plugin-to-enable-creating-arbitrary-trigger-zones.173783/
页:
[1]