UNPKG

jams_mapper

Version:

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

247 lines (213 loc) 9.73 kB
class Jams_Sector { constructor(...args) { this.initialize(...args); } initialize(...args) { Jams?.Logger?.log("Jams_Sector.initialize("+this.name+")", { args: args }); this.name = args[0]; Jams.EventBus. subscribe( this.name, object => this.refresh() ); Jams.EventBus.subscribe( "SectorChange", object => this.test() ); this.active = Jams_Sector.ActiveSector; this.filename = args[1]; this.x = parseInt(args[2]); this.y = parseInt(args[3]); this.loadDataFile(); Jams.EventBus.subscribe( "mapSection", object => this.checkPrimary(object) ); } checkPrimary = function (object) { Jams?.Logger?.log("Jams_Sector.checkPrimary(" + this.name + ")", { object: object }); if (object.x == this.x && object.y == this.y) { if (!this.filename) { console.error(this); } Jams_Sector.ActiveSector = this; this.pushMapMeta(); Jams.EventBus.Jams_Event = Jams.EventBus.Jams_Event || new JamsEvent("SectorChange"); Jams.EventBus.publish("SectorChange", this); } } test = function (){ const { x, y } = Jams.Mapper.updateSection; if (x == this.x || y == this.y) { if (!Jams_Sector.ActiveSector) { console.error("ActiveSector is null"); } console.log(Jams_Sector.ActiveSector); console.log(Jams.Mapper.maps); const { x, y, filename } = Jams_Sector.ActiveSector; const [world, activeX, activeY] = Jams.Mapper.maps[filename]; var xOffset = this.x - x; if (Math.abs(xOffset) == 2) { xOffset = xOffset * -0.5; } var yOffset = this.y - y; if (Math.abs(yOffset) == 2) { yOffset = yOffset * -0.5; } var destX = parseInt(activeX) + xOffset; var destY = parseInt(activeY) + yOffset; if (Jams.Mapper.map.xDir != 0) { destX = parseInt(activeX) + Jams.Mapper.map.xDir; } if (Jams.Mapper.map.yDir != 0) { destY = parseInt(activeY) + Jams.Mapper.map.yDir; } this.filename = null; if (Jams.Mapper.Sectors.hasOwnProperty(world) && Jams.Mapper.Sectors[world].hasOwnProperty(destX) && Jams.Mapper.Sectors[world][destX].hasOwnProperty(destY)) { this.filename = Jams.Mapper.Sectors[world][destX][destY];//need to calculate which map to load. } this.loadDataFile(); Jams?.Logger?.log("Jams_Sector.test(" + this.name + ")", {xOffset:xOffset,yOffset:yOffset, "Jams.Mapper.map.xDir": Jams.Mapper.map.xDir, "Jams.Mapper.map.yDir": Jams.Mapper.map.yDir, world:world,activeX: activeX, activeY: activeY, destX:destX,destY:destY, x: x, y: y }, { this: { x: this.x, y: this.y } }); } } loadDataFile = function () { if (this.filename) { window[this.name] = null; DataManager.loadDataFile(this.name, this.filename); } else { var emptySector = {}; emptySector.data = []; for (let k = 0; k < $dataMap.width * 3 * $dataMap.height * 3 * 6; k++) { //Zero out the original map emptySector.data[k] = 0; } emptySector.events = []; emptySector.width = $dataMap.width/3; emptySector.height =$dataMap.height/3; emptySector.scrollType = 3; window[this.name] = emptySector; this.refresh(); } Jams?.Logger?.log("Jams_Sector.loadDataFile(" + this.name + ")", { Jams_Sector: this }, {object:window[this.name]}); } isLoaded = function () { var b = false; if (window[this.name]) { b = true; } Jams?.Logger?.log("Jams_Sector.isLoaded(" + this.name + ")",b, { Jams_Sector: window[this.name] }); return b; } refresh = function () { //this.clear(); //this.clearEvents(); this.clearEvents(); Jams?.Logger?.log("Jams_Sector.refresh(" + this.name + ")", { Jams_Sector: this }); var width = $dataMap.width; var SectorWidth = width / 3; var SectorVolume = SectorWidth * SectorWidth; //technically width*height, but we always assume a square. var yVolume = SectorVolume * 3; var mapVolume = SectorVolume * 9; var m = window[this.name].data; var xOffset = this.x + 1; var yOffset = -this.y + 1; let i = 0; while (i < m.length) { var mod = i % SectorWidth; var layerMod = i % SectorVolume; var Layer = (i - layerMod) / SectorVolume; var Sector = (i - mod) / SectorWidth - SectorWidth * Layer; var DestinationPosition = i + SectorWidth * Sector * 2 + xOffset * SectorWidth + yVolume * yOffset - Layer * SectorVolume + Layer * mapVolume; $dataMap.data[DestinationPosition] = m[i]; i++; } this.createEvents(); } createEvents = function () { Jams?.Logger?.log("Jams_Sector.createEvents(" + this.name + ")", { Jams_Sector: this }); var m = JSON.parse(JSON.stringify(window[this.name])); //I need the original data in place for the other maps. m.events.forEach(e => { var id = m.src.replace(".json", "") id = parseInt(id.substr(id.length - 3)); if (e && !Jams.Mapper.eventExist(id + e.id.padZero(5))) { e.uniqueId = id + e.id.padZero(5); if (Jams.Mapper.openID.length > 0) { e.id = Jams.Mapper.openID.shift(); } else { e.id = Jams.Mapper.generateID.next().value; } e.x += (this.x + 1) * m.width; e.y += (this.y + 1) * m.width; $dataMap.events[e.id] = e; $gameMap._events[e.id] = new Game_Event(id, e.id); if (Jams.Mapper._characterSprites) { const sprite = new Sprite_Character($gameMap._events[e.id]); Jams.Mapper._characterSprites.push(sprite); Jams.Mapper._tilemap.addChild(sprite); } } }); }; clearEvents = function () { Jams?.Logger?.log("Jams_Sector.clearEvents(" + this.name + ")", { Jams_Sector: this }); $gameMap._events.forEach(e => { if (e) { const pos = this.getMapSector(e); if (pos.x == this.x && pos.y == this.y) { console.log(pos, e, window[this.name]); $dataMap.events[e._eventId] = null; e.erase(); $gameMap._events[e._eventId] = null; Jams.Mapper.openID.push(e._eventId); } } }); }; getMapSector = function (playerPos) { //Jams?.Logger?.log("Jams_Sector.getMapSector", { playerPos: playerPos }); if (playerPos && window[this.name]) { const { width, height } = window[this.name]; const { x, y } = playerPos; const xSector = (x - x % width) / width - 1; const ySector = -(y - y % height) / height + 1; return { "x": xSector, "y": ySector }; } return { "x": null, "y": null }; } getRelativePosition = function () { Jams?.Logger?.log("Jams_Sector.getRelativePosition(" + this.name + ")", { Jams_Sector: this }); const { x, y } = this.getActiveSector(); return { x: this.getRelativeMapPosition(this.x, x), y: this.getRelativeMapPosition(this.y, y) }; } getActiveSector = function () { Jams?.Logger?.log("Jams_Sector.getActiveSector(" + this.name + ")", { Jams_Sector: this }); if ($gamePlayer) { const { width, height } = window[this.name]; const { x, y } = $gamePlayer; const xSector = (x - x % width) / width - 1; const ySector = -(y - y % height) / height + 1; return { "x": xSector, "y": ySector }; } return { "x": null, "y": null }; } getSector = function () { Jams?.Logger?.log("Jams_Sector.getSector(" + this.name + ")", { Jams_Sector: this }); width = $dataMap.width / 3; x = ($gamePlayer.x - $gamePlayer.x % width) / width; y = ($gamePlayer.y - $gamePlayer.y % width) / width; for (i in this.sectorAlignment) { if (x == this.sectorAlignment[i].x && y == this.sectorAlignment[i].y) { return this.sectorAlignment[i].name; } } }; pushMapMeta = function () { Jams?.Logger?.log("Jams_Sector.pushMapMeta(" + this.name + ")", { Jams_Sector: this }); var map = window[this.name]; Object.keys(map).forEach(function (key) { if ($dataMap[key] !== map[key] && key !== "data" && key !== "events" && key !== "width" && key !== "height" && key !== "scrollType") { $dataMap[key] = map[key]; } }); }; } Jams_Sector.ActiveSector = null;