Skip to content

Instantly share code, notes, and snippets.

@drowsepost
Last active September 2, 2020 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drowsepost/1f6409b574c947157f83e51c5280a8d5 to your computer and use it in GitHub Desktop.
Save drowsepost/1f6409b574c947157f83e51c5280a8d5 to your computer and use it in GitHub Desktop.
Tilemap Layer Control Plugin for RPG maker MZ
//=============================================================================
// Drowsepost - Tilemap Layer Control Plugin for RPG maker MZ
//=============================================================================
/*:
* @target MZ
* @version beta2
* @plugindesc Controls the passing state and stacking state of layers
* @author canotun(drowsepost)
* @url https://gist.github.com/drowsepost/1f6409b574c947157f83e51c5280a8d5
* @license MIT License
*
* @param layer1
* @text Settings for layer 1
* @default
* @type struct<layersetting>
*
* @param layer2
* @text Settings for layer 2
* @default
* @type struct<layersetting>
*
* @param layer3
* @text Settings for layer 3
* @default
* @type struct<layersetting>
*
* @param layer4
* @text Settings for layer 4
* @default
* @type struct<layersetting>
*
* @param shadowPriority
* @text Layer to draw shadow
* @default lower
* @type select
* @option Lower layer
* @value lower
* @option Upper Layer
* @value upper
* @desc Change the layer that draws shadows
*
* @param fixTableLeg
* @text Patch table feet
* @type boolean
* @default true
* @desc Patch for drawing table feet
*
* @help DPMZ_LayerControl.js
* The function to set the behavior for each layer is expanded.
* Objects that overlap the upper and lower layers are
* drawn in the layer order regardless of the settings.
*
* Currently you can make the following settings:
*
* - Disable reflection of passability judgment
* - Forced display as upper or lower tiles
* - Change the shadow layer
*
* MIT License
*/
/*~struct~layersetting:
*
* @param permission
* @text use tile passability
* @type boolean
* @default true
* @desc reflection of passability judgment
*
* @param priority
* @text visibility settings
* @type select
* @default none
* @option use tile flag
* @value none
* @option force upper layer
* @value upper
* @option force lower layer
* @value lower
* @desc Forced display as upper or lower tiles
* none: use tile flag(default)、upper:force upper layer、lower: force lower layer
*
*/
/*:ja
* @target MZ
* @version beta2
* @plugindesc レイヤーの通行状態や重ね状態をコントロールします。
* @author canotun(drowsepost)
* @url https://gist.github.com/drowsepost/1f6409b574c947157f83e51c5280a8d5
* @license MIT License
*
* @param layer1
* @text レイヤー1の設定
* @default
* @type struct<layersetting>
*
* @param layer2
* @text レイヤー2の設定
* @default
* @type struct<layersetting>
*
* @param layer3
* @text レイヤー3の設定
* @default
* @type struct<layersetting>
*
* @param layer4
* @text レイヤー4の設定
* @default
* @type struct<layersetting>
*
* @param shadowPriority
* @text 影を描画するレイヤー
* @type select
* @default lower
* @option 下層
* @value lower
* @option 上層
* @value upper
* @desc 影を描画するレイヤーを変更します
*
* @param fixTableLeg
* @text テーブルの足描画パッチ
* @type boolean
* @default true
* @desc レイヤー1以外でもテーブルの足を描画
*
* @help DPMZ_LayerControl.js
* レイヤーごとのふるまいを設定する機能を拡張します。
* 上層下層が重なるオブジェクトについては
* 設定にかかわらずレイヤー順に描画されます。
*
* 現在以下の設定を行えます。
*
* ・通行判定の反映を無効化
* ・強制的に上層ないし下層タイルとして表示
* ・影をつけるレイヤーの変更
*
* MIT License
*/
/*~struct~layersetting:ja
*
* @param permission
* @text 通行設定の反映
* @type boolean
* @default true
* @desc 通行設定の反映を行うか否か
*
* @param priority
* @text 表示設定
* @type select
* @default none
* @option 標準
* @value none
* @option 上層強制
* @value upper
* @option 下層強制
* @value lower
* @desc レイヤーの表示設定
* none: タイル設定ごと(標準)、upper:上層強制、lower: 下層強制
*
*/
(() => {
'use strict';
const pluginFilename = 'DPMZ_LayerControl';
const pluginOptions = PluginManager.parameters(pluginFilename);
const parseMzType = (json) => {
let r = {"permission": true, "priority": "none"};
let obj = (!!json)? JSON.parse(json) : {};
Object.keys(obj).forEach(function (key) {
if(!isNaN(obj[key])) {
r[key] = Number(obj[key]);
return;
}
if(obj[key] === 'true') {
r[key] = true;
return;
}
if(obj[key] === 'false') {
r[key] = false;
return;
}
r[key] = obj[key];
});
return r;
};
const layerParams = ((param)=>{
let r = [];
r.push(parseMzType(param.layer1));
r.push(parseMzType(param.layer2));
r.push(parseMzType(param.layer3));
r.push(parseMzType(param.layer4));
return r;
})(pluginOptions);
const shadowUpper = (pluginOptions.shadowPriority == 'upper');
const fixTableLeg = (pluginOptions.fixTableLeg == 'true');
const _Game_Map_allTiles = Game_Map.prototype.allTiles;
Game_Map.prototype.allTiles = function(x, y) {
let r = _Game_Map_allTiles.apply(this, arguments);
for(let i = 0;i < 4;i++) {
if(!layerParams[3 - i].permission) {
r[i] = 0;
}
}
return r;
};
Tilemap.prototype._addSpot_Edge = function(mx, my, dx, dy, layer) {
const upperTileId = this._readMapData(mx, my - 1, layer);
const activeTile = this._readMapData(mx, my, layer);
const lowerTile = (layer > 0)?
this._readMapData(mx, my, layer - 1):
this._readMapData(mx, my, layer);
if (this._isTableTile(upperTileId) && !this._isTableTile(activeTile)) {
if (!Tilemap.isShadowingTile(lowerTile)) {
if(layerParams[layer].priority === 'upper') {
this._addTableEdge(this._upperLayer, upperTileId, dx, dy);
} else {
this._addTableEdge(this._lowerLayer, upperTileId, dx, dy);
}
}
}
};
const _Tilemap_addSpot = Tilemap.prototype._addSpot;
Tilemap.prototype._addSpot = function(startX, startY, x, y) {
const mx = startX + x;
const my = startY + y;
const dx = x * this._tileWidth;
const dy = y * this._tileHeight;
const tileId0 = this._readMapData(mx, my, 0);
const tileId1 = this._readMapData(mx, my, 1);
const tileId2 = this._readMapData(mx, my, 2);
const tileId3 = this._readMapData(mx, my, 3);
const shadowBits = this._readMapData(mx, my, 4);
this._addSpotTile(tileId0, dx, dy, 0);
if(fixTableLeg) this._addSpot_Edge(mx, my, dx, dy, 0);
this._addSpotTile(tileId1, dx, dy, 1);
this._addSpot_Edge(mx, my, dx, dy, 1);
if (this._isOverpassPosition(mx, my)) {
this._addTile(this._upperLayer, tileId2, dx, dy);
} else {
this._addSpotTile(tileId2, dx, dy, 2);
}
if(fixTableLeg) this._addSpot_Edge(mx, my, dx, dy, 2);
if (this._isOverpassPosition(mx, my)) {
this._addTile(this._upperLayer, tileId3, dx, dy);
} else {
this._addSpotTile(tileId3, dx, dy, 3);
}
if(fixTableLeg) this._addSpot_Edge(mx, my, dx, dy, 3);
if(shadowUpper) {
this._addShadow(this._upperLayer, shadowBits, dx, dy);
} else {
this._addShadow(this._lowerLayer, shadowBits, dx, dy);
}
};
const _Tilemap_addSpotTile = Tilemap.prototype._addSpotTile;
Tilemap.prototype._addSpotTile = function(tileId, dx, dy, layer) {
if (layerParams[layer].priority === 'upper' ) {
this._addTile(this._upperLayer, tileId, dx, dy);
return;
} else if(layerParams[layer].priority === 'lower') {
this._addTile(this._lowerLayer, tileId, dx, dy);
return;
}
_Tilemap_addSpotTile.apply(this, arguments);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment