Original Source
- Original title: Plugin to enable creating arbitrary trigger zones
- Original author: Antidote174
- Original date: December 9, 2024
- Source thread: https://forums.rpgmakerweb.com/threads/plugin-to-enable-creating-arbitrary-trigger-zones.173783/
- Source forum path: Game Development Engines > RPG Maker Javascript Plugins > JS Plugin Releases (RMMZ)
Summary
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: {
Archived First Post
I've implemented it using the Event's note field to smuggle JSON into the plugin to be parsed with a very simple structure:
{
"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
/*:
* @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[eventId].note;
if (note !== "") {
let rect = JSON.parse($dataMap.events[this._eventId].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([0, 1, 2])) {
this._triggered = true;
}
}
Game_Event.prototype.triggerExtentX = function() {
return this._triggerExtentX;
}
Game_Event.prototype.triggerExtentY = function() {
return this._triggerExtentY;
}
})();
Windows Demo
Downloads / Referenced Files
Log in, then follow the RPG Maker Developers Group to see these download links.
Log in to downloadLicense / Terms Note
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
Creator Claims / Removal
If you are the original creator and want this listing reassigned, edited, or removed, join BMMPlay and contact the moderators with proof that matches the original RPG Maker Web profile, linked GitHub, itch.io page, or another public creator identity.
Replies (0)
No replies yet.
Topic Summary
Loading summary...