jams_mapper
Version:
A RPG Maker MZ plugin that allows you to load maps while walking rather than warping.
247 lines (216 loc) • 7.71 kB
JavaScript
/**
* @description Standard structure for the event bus and subscribers to use.
* @param {string} name the name of the event.
*/
function JamsEvent() {
this.initialize(...arguments);
};
JamsEvent.prototype.initialize = function(name) {
this.name = name;
this.debugMsg = "";
};
/**
* @description publishes the event when the new attributes values don't match the current attribute values.
* @param {string} name the name of the event.
*/
JamsEvent.prototype.update = function(obj) {
//only publish if different
let isDiff = false;
Object.keys(obj).forEach(function(key) {
if(this[key] !== obj[key]){
isDiff = true;
this[key] = obj[key];
}
}.bind(this));
if(isDiff){
Jams.EventBus.publish(this.name, this);
}
};
/**
* @description Returns the x and y position of the player in the following format (xxx,yyy).
* @param {string} name the name of the event.
*/
JamsEvent.prototype.toString = function() {
return this.debugMsg;
};
/**
* @description the Player position event with a unique toString value.
* @param {string} name the name of the event.
*/
function JamsPlayerPosEvent() {
this.initialize(...arguments);
};
JamsPlayerPosEvent.prototype = Object.create(JamsEvent.prototype);
JamsPlayerPosEvent.prototype.constructor = JamsEvent;
JamsPlayerPosEvent.prototype.initialize = function(name) {
JamsEvent.prototype.initialize.call(this, name);
this.x = 0;
this.y = 0;
};
/**
* @description Returns the x and y position of the player in the following format (xxx,yyy).
* @param {string} name the name of the event.
*/
JamsPlayerPosEvent.prototype.toString = function() {
let x = Math.abs(this.x);
let y = Math.abs(this.y);
if(this.x >= 0){
x = ` ${x.padZero(3)}`;
}else{
x = `-${x.padZero(3)}`;
}
if(this.y >= 0){
y = ` ${y.padZero(3)}`;
}else{
y = `-${y.padZero(3)}`;
}
return this.x !== null ? `(${x},${y})` : "( xxx, yyy)";
};
/**
* @description An event bus
*/
function JamsEventBus() {
this.initialize(...arguments);
};
JamsEventBus.prototype.initialize = function() {
this.subscriptions = {};
this.generateID = this.eventIDGenerator();
this.backlog = [];
};
/**
* @description Adds an event to the event bus.
* @param {string} eventType name of the event
* @param {function} callback call back function when event is triggered
* @Assumption If an event doesn't exist then it is registered as a blank object.
*/
JamsEventBus.prototype.subscribe = function(eventType, callback) {
const id = this.generateID.next().value;
if (!this.subscriptions[eventType])
this.subscriptions[eventType] = {};
this.subscriptions[eventType][id] = callback;
return {
unsubscribe: () => {
delete this.subscriptions[eventType][id];
if (Object.keys(this.subscriptions[eventType]).length === 0)
delete this.subscriptions[eventType];
}
};
};
/**
* @description triggers an event
* @param {string} eventType name of the event
* @param {function} arg event to pass to the subscribers
*/
JamsEventBus.prototype.publish = function(eventType, arg) {
if (!this.subscriptions[eventType])
return;
Object.keys(this.subscriptions[eventType])
.forEach(id => this.subscriptions[eventType][id](arg));
//.forEach(id => this.backlog.push({id:id, eventType:eventType,arg:arg}));
};
JamsEventBus.prototype.execution = function (eventType, arg) {
if (!this.subscriptions[eventType])
return;
this.subscriptions[eventType][id](arg)
var max = 500;// Math.ceil(this.backlog.length * .20);
if (max < 2) {
max = 2;
}
for (let i = 0; i <= max; i++) {
this.subscriptions[this.backlog[0].eventType][this.backlog[0].id](this.backlog[0].arg);
this.subscriptiosn.shift();
}
};
/**
* @description create an auto incrementing identity
* @yields {number} the identity
*/
JamsEventBus.prototype.eventIDGenerator = function* () {
var id = 0;
while(true){
yield id++;
}
};
//=============================================================================
// Hooks
//=============================================================================
/**
* @description Get Payer Position
*/
let _updateFrameCount = SceneManager.updateFrameCount;
SceneManager.updateFrameCount = function() {
if(this._PosEvent == undefined){this._PosEvent = new JamsPlayerPosEvent("playerPos")};
this._PosEvent.update({ "x": $gamePlayer?.x, "y": $gamePlayer?.y });
if (this._MochaTestEvent === undefined) { this._MochaTestEvent = new JamsEvent("MochaReady") };
if (window.MochaTestComplete) {
this._MochaTestEvent.update({ "isReady": window.MochaTestComplete });
}
_updateFrameCount();
};
/**
* @description $dataMapInfos has been loaded and one for $DataMap
*/
DataManager._Jams_onXhrLoad = DataManager.onXhrLoad;
DataManager.onXhrLoad = function(xhr, name, src, url) {
this._Jams_onXhrLoad(xhr, name, src, url);
this.Jams_EventList = this.Jams_EventList || {};
if (this.Jams_Loads === undefined) {
this.Jams_Loads = ["$dataMapInfos", "$dataMap", "$leftSector", "$rightSector", "$upperSector", "$lowerSector"
, "$upperLeftSector", "$upperRightSector", "$lowerLeftSector"
, "$lowerRightSector", "$center"];
}
for (i in this.Jams_Loads) {
loadName = this.Jams_Loads[i];
if (name === loadName) {
this.Jams_EventList[name] = this.Jams_EventList[name] || new JamsEvent(name)
this.Jams_EventList[name].update(window[name]);
}
}
};
/**
* @description Verify when character sprites are loaded.
*/
var _createCharacters = Spriteset_Map.prototype.createCharacters;
Spriteset_Map.prototype.createCharacters = function() {
this._characterSprites = [];
for (const event of $gameMap.events()) {
// ReSharper disable once InconsistentNaming
this._characterSprites.push(new Sprite_Character(event));
}
for (const vehicle of $gameMap.vehicles()) {
// ReSharper disable once InconsistentNaming
this._characterSprites.push(new Sprite_Character(vehicle));
}
for (const follower of $gamePlayer.followers().reverseData()) {
// ReSharper disable once InconsistentNaming
this._characterSprites.push(new Sprite_Character(follower));
}
// ReSharper disable once InconsistentNaming
this._characterSprites.push(new Sprite_Character($gamePlayer));
for (const sprite of this._characterSprites) {
this._tilemap.addChild(sprite);
}
if(this._jamsEvent === undefined){this._jamsEvent = new JamsEvent("Character Sprites Loaded")};
this._jamsEvent._characterSprites = this._characterSprites;
this._jamsEvent._tilemap = this._tilemap;
this._jamsEvent.update({"debugMsg": new Date()});
};
//Game_Map.prototype.update = function (sceneActive) {
// Jams?.Eventbus?.execution();
// this.refreshIfNeeded();
// if (sceneActive) {
// this.updateInterpreter();
// }
// this.updateScroll();
// this.updateEvents();
// this.updateVehicles();
// this.updateParallax();
//};
var Imported = Imported || {};
Imported.Jams = "1.0.0";
var Jams = Jams || {};
Jams.EventBus = Jams.EventBus || new JamsEventBus();
/*:
* @plugindesc EventBus for Jams plugins
* @author Michael Stephens
*/