/*:
* @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;
}
})();