//=============================================================================
// MULI_ReplaceTile.js
//=============================================================================
/*:
* @plugindesc 替換地圖上ID為n的圖塊
* @author moonyoulove
*
* @help
* 有一個新的全域變量:$gameReplacedTiles,遊戲存檔裡會保存替換的狀態
*
* 插件命令:
* ReplaceTile Replace z tileId1 tileId2 //將z層的圖塊1都換成圖塊2
* ReplaceTile Restore z tileId //將z層的圖塊1還原成原本的數據
* ReplaceTile Clear All //將所有地圖的替換都還原
* ReplaceTile Clear Map //將當前地圖的替換都還原
* ReplaceTile Refresh //立即刷新圖塊更變
*
* Z coordinate:
* 0 : Lower tiles
* 1 : Lower characters
* 3 : Normal characters
* 4 : Upper tiles
* 5 : Upper characters
* 6 : Airship shadow
* 7 : Balloon
* 8 : Animation
* 9 : Destination
*/
function RpcTile() {
throw new Error('This is a static class');
}
RpcTile.parameters = PluginManager.parameters("MULI_ReplaceTile");
RpcTile.hasPlugin = {
// "SomePlugin": PluginManager._scripts.includes("SomePlugin")
};
RpcTile.globalThis = self || window || global;
function Game_ReplacedTiles() {
this.initialize.apply(this, arguments);
}
Game_ReplacedTiles.prototype.initialize = function() {
this.clear();
this._needRepaint = false;
};
Game_ReplacedTiles.prototype.clear = function(mapId) {
if (mapId > 0) {
delete this._data[mapId];
} else {
this._data = {};
}
};
Game_ReplacedTiles.prototype.replace = function(mapId, z, tileId1, tileId2) {
this._data[mapId] = this._data[mapId] || {};
this._data[mapId][z] = this._data[mapId][z] || {};
this._data[mapId][z][tileId1] = tileId2;
};
Game_ReplacedTiles.prototype.restore = function(mapId, z, tileId) {
if (this._data[mapId] && this._data[mapId][z]) {
delete this._data[mapId][z][tileId];
if (Object.keys(this._data[mapId][z]).length === 0) {
delete this._data[mapId][z];
if (Object.keys(this._data[mapId]).length === 0) {
delete this._data[mapId];
}
}
}
};
Game_ReplacedTiles.prototype.value = function(mapId, z, tileId) {
if (this._data[mapId] && this._data[mapId][z] && this._data[mapId][z][tileId]) {
return this._data[mapId][z][tileId];
}
return 0;
};
Game_ReplacedTiles.prototype.isNeedRepaint = function() {
return this._needRepaint;
};
Game_ReplacedTiles.prototype.needRepaint = function() {
this._needRepaint = true;
};
Game_ReplacedTiles.prototype.repainted = function() {
this._needRepaint = false;
};
(function() {
let _DataManager_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
_DataManager_createGameObjects.call(this);
$gameReplacedTiles = new Game_ReplacedTiles();
};
let _DataManager_makeSaveContents = DataManager.makeSaveContents;
DataManager.makeSaveContents = function() {
let contents = _DataManager_makeSaveContents.call(this);
contents.replacedTiles = $gameReplacedTiles;
return contents;
};
let _DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function(contents) {
_DataManager_extractSaveContents.call(this, contents);
$gameReplacedTiles = contents.replacedTiles || $gameReplacedTiles;
};
_Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === "ReplaceTile") {
switch (args[0]) {
case "Replace": {
let z = Number(args[1]);
let tileId1 = Number(args[2]);
let tileId2 = Number(args[3]);
if (z >= 0 && tileId1 > 0 && tileId2 > 0) {
$gameReplacedTiles.replace(this._mapId, z, tileId1, tileId2);
}
break;
}
case "Restore": {
let z = Number(args[1]);
let tileId = Number(args[2]);
if (z >= 0 && tileId > 0) {
$gameReplacedTiles.restore(this._mapId, z, tileId);
}
break;
}
case "Clear": {
if (args[1] === "All") {
$gameReplacedTiles.clear();
} else if (args[1] === "Map") {
$gameReplacedTiles.clear(this._mapId);
}
break;
}
case "Refresh": {
$gameReplacedTiles.needRepaint();
break;
}
}
}
};
let _Game_Map_tileId = Game_Map.prototype.tileId;
Game_Map.prototype.tileId = function(x, y, z) {
let tileId = _Game_Map_tileId.call(this, x, y, z);
if (tileId > 0) {
let replacedId = $gameReplacedTiles.value(this._mapId, z, tileId);
if (replacedId > 0) {
return replacedId;
}
}
return tileId;
};
let _Tilemap__readMapData = Tilemap.prototype._readMapData;
Tilemap.prototype._readMapData = function(x, y, z) {
let tileId = _Tilemap__readMapData.call(this, x, y, z);
if (tileId > 0) {
let replacedId = $gameReplacedTiles.value($gameMap.mapId(), z, tileId);
if (replacedId > 0) {
return replacedId;
}
}
return tileId;
};
let _Spriteset_Map_updateTilemap = Spriteset_Map.prototype.updateTilemap;
Spriteset_Map.prototype.updateTilemap = function() {
_Spriteset_Map_updateTilemap.call(this);
if ($gameReplacedTiles.isNeedRepaint()) {
this._tilemap.refresh();
$gameReplacedTiles.repainted();
}
};
})();