UNPKG

jams_mapper

Version:

A RPG Maker MZ plugin that allows you to load maps while walking rather than warping.

208 lines (187 loc) 6.4 kB
/*: * @plugindesc Mapper is a plugin that combines maps together and points to the new maps when the game is ran.// Describe your plugin * @author Michael Stephens */ function JamsMapper() { this.initialize(...arguments); } JamsMapper.prototype.initialize = function () { this._transfer = false; this.map; this.Sectors = {};//Lookup map filename by position. this.maps = {}; //Lookup sector position by map filename. //Unique Id generation this.openID = new Array(); this.generateID = this.eventIDGenerator(); this.loadMetaData = true;//only need to load this once this.isSectorLoading = true; this.loadingSectors = []; //loaded sectors this.sectorAlignment = [ { name: "$leftSector", x: -1, y: 0 }, { name: "$rightSector", x: 1, y: 0 }, { name: "$upperSector", x: 0, y: 1 }, { name: "$lowerSector", x: 0, y: -1 }, { name: "$upperLeftSector", x: -1, y: 1 }, { name: "$upperRightSector", x: 1, y: 1 }, { name: "$lowerLeftSector", x: -1, y: -1 }, { name: "$lowerRightSector", x: 1, y: -1 }, { name: "$center", x: 0, y: 0 } ]; this._mapSectionEvent = new JamsPlayerPosEvent("mapSection"); this._mapSectionEvent.update({ "x": "", "y": "" }); //Creaate Subscriptions Jams.EventBus.subscribe( "$dataMapInfos", object => this.loadDataFiles(object) ); Jams.EventBus.subscribe( "playerPos", object => this.triggerMapSection(object, $dataMap) ); Jams.EventBus.subscribe( "mapSection", object => this.loadSectorAxis(object) ); Jams.EventBus.subscribe( "Character Sprites Loaded", object => this.getCharacterSprites(object) ); this.createDebug(); }; /** * @description create an auto incrementing identity * @yields {number} the identity */ JamsMapper.prototype.eventIDGenerator = function* () { var id = 0; while (true) { yield id++; } }; //load positional data from all maps. JamsMapper.prototype.loadDataFiles = function () { if (this.loadMetaData) { window.$dataMapInfos.forEach(map => { if (map?.id > 0) { const id = parseInt(map.id).toString(); const filename = "Map%1.json".format(id.padZero(3)); DataManager.loadDataFile("$center", filename); } }); this.loadMetaData = false; //this needs to happen only once. } }; /** * @description Add Character sprites and tile map to object for easy reference. */ JamsMapper.prototype.getCharacterSprites = function (object) { this._characterSprites = object._characterSprites; this._tilemap = object._tilemap; }; /** * @description trigger Map Section Event * @param object Map section event */ JamsMapper.prototype.triggerMapSection = function (playerPos, dataMap) { if (dataMap) { this._mapSectionEvent.update(this.getMapSector(playerPos, dataMap)); } }; /** * @description Add debugger metrics; */ JamsMapper.prototype.createDebug = function () { Jams.FPSManager.addMetric("Map Sect: ", "mapSection", 0); }; JamsMapper.prototype.getMapSector = function (playerPos, dataMap) { if (playerPos && dataMap) { const { width, height } = dataMap; const sectionWidth = width / 3; const sectionHeight = height / 3; const { x, y } = playerPos; const xSection = (x - x % sectionWidth) / sectionWidth - 1; const ySection = -(y - y % sectionHeight) / sectionHeight + 1; return { "x": xSection, "y": ySection }; } return { "x": null, "y": null }; } /** * @description verify if event already exists in the data map. * @param uniqueId A unique identifier associated with each event. */ JamsMapper.prototype.eventExist = function (uniqueId) { let bool = false; $dataMap.events.forEach(e => { if (e?.uniqueId == uniqueId) { bool = true; } }); return bool; } /** * @description Load the map notes to get sector position. */ JamsMapper.prototype.getNotes = function (object) { const result = object.note.split(/\r?\n/) || new Array(); result.forEach(str => { if (str.substring(0, 8) === "worldpos") { const arr = str.substring(9).split(" "); this.maps[object.src] = arr; [world, x, y] = arr; this.Sectors[world]; if (!this.Sectors.hasOwnProperty(world)) { this.Sectors[world] = {}; } if (!this.Sectors[world].hasOwnProperty(x)) { this.Sectors[world][x] = {}; } this.Sectors[world][x][y] = object.src; } }); }; JamsMapper.prototype.calcRangePos = function (min, max, number) { const count = max + 1 - min; if (number < 0) { number += count * (Math.floor(-number / count) + 1); //Turn negatives into positives } return (number - min) % count + min; }; JamsMapper.prototype.getSectionClear = function (newX, newY, xChange, yChange) { let x = null; let y = null; if (xChange !== 0) { x = this.calcRangePos(-1, 1, newX + xChange); } else if (yChange !== 0) { y = this.calcRangePos(-1, 1, newY + yChange); } return { "x": x, "y": y }; }; JamsMapper.prototype.getRelativeMapPosition = function (pos, prevPos) { if (Math.abs(pos - prevPos) === 2) { if (pos > prevPos) { return -1; } else { return 1; } } else { return pos - prevPos; } }; JamsMapper.prototype.loadSectorAxis = function (object) { //console.trace("loadSectorAxis"); const x = this.getRelativeMapPosition(object.x, this.map.xPos); const y = this.getRelativeMapPosition(object.y, this.map.yPos); this.updateSection = this.getSectionClear(object.x, object.y, x, y); this.map.xDir = x this.map.yDir = y; this.map.xPos = object.x; this.map.yPos = object.y; }; var Imported = Imported || {}; Imported.Jams = "1.0.0"; var Jams = Jams || {}; Jams.Mapper = Jams.Mapper || new JamsMapper();