nyacronomicon/js/plugins/KN_Camera.js
2026-03-12 22:39:10 -05:00

168 lines
No EOL
6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//=============================================================================
// RPG Maker MZ - KN_Camera.js
//=============================================================================
/*:ja
* @target MZ
* @author kurogoma knights
* @base PluginCommonBase
* @plugindesc カメラ機能の提供
* @help
* @param adjustX
* @default 0 @type number @min -10 @max 10
* @desc 指定したタイル数だけ中心がX方向にずれて表示されます。
*
* @param adjustY
* @default 0 @type number @min -10 @max 10
* @desc 指定したタイル数だけ中心がY方向にずれて表示されます。
*
* @param defaultZoomScale
* @type number @decimals 1 @min 1 @max 4 @default 1
* @desc ゲーム全体に適用されるズーム倍率2で2倍
*
* @command ZOOM
* @arg eventId @default 0
* @desc イベントID(もしくは名称) 0はこのイベント -1はプレイヤー
* @arg scale @type number @default 4
*
* @command ZOOM_RESET
*
*/
(() => {
'use strict';
const script = document.currentScript;
const param = PluginManagerEx.createParameter(script);
const searchDataItem = function(dataArray, columnName, columnValue) {
let result = 0;
dataArray.some(dataItem => {
if (dataItem && dataItem[columnName] === columnValue) {
result = dataItem;
return true;
}
return false;
});
return result;
};
// Spriteset_Map をデフォルトで指定倍数に拡大する
const _Game_Screen_prototype_clearZoom = Game_Screen.prototype.clearZoom;
Game_Screen.prototype.clearZoom = function() {
_Game_Screen_prototype_clearZoom.call(this);
this._zoomScale = Number(param.defaultZoomScale || 1);
};
// ズーム&リセット
PluginManagerEx.registerCommand(script, 'ZOOM', function(args) {
const eventId = args.eventId;
let character = null;
if ($gameMap.event(eventId)) {
character = $gameMap.event(eventId);
} else if (eventId < 0) {
character = $gamePlayer;
} else if (eventId !== 0) {
let item = searchDataItem($dataMap.events, 'name', eventId);
character = $gameMap.event(item.id);
} else {
character = $gameMap.event(this.eventId());
}
camera.zoom(args.scale, character);
});
PluginManagerEx.registerCommand(script, 'ZOOM_RESET', args => {
camera.reset();
});
var camera = {};
camera.zoom = function(scale, target) {
$gameScreen.setZoom(0, 0, scale);
target.center(target._realX, target._realY);
};
camera.reset = function() {
this.zoom(1, $gamePlayer);
};
// Game_Map
(() => {
// @override
Game_Map.prototype.screenTileX = function() {
// 画面表示タイル数X 基準25マス zoom2なら12.5マス
return Graphics.width / (this.tileWidth() * $gameScreen.zoomScale());
};
// @override
Game_Map.prototype.screenTileY = function() {
// 画面表示タイル数Y 基準15マス zoom2なら7.5マス
return Graphics.height / (this.tileHeight() * $gameScreen.zoomScale());
};
// マウス座標からマップ座標への変換をズーム対応にする
const _Game_Map_canvasToMapX = Game_Map.prototype.canvasToMapX;
Game_Map.prototype.canvasToMapX = function(x) {
const scale = $gameScreen.zoomScale();
const adjustedX = x / scale;
return _Game_Map_canvasToMapX.call(this, adjustedX);
};
const _Game_Map_canvasToMapY = Game_Map.prototype.canvasToMapY;
Game_Map.prototype.canvasToMapY = function(y) {
const scale = $gameScreen.zoomScale();
const adjustedY = y / scale;
return _Game_Map_canvasToMapY.call(this, adjustedY);
};
})();
// Game_Character
// プレイヤーフォーカスならこれは不要そう
// プレイヤー以外にカメラをあてるならadjust補正いる気がする
(() => {
Game_Character.prototype.centerX = function() {
return ($gameMap.screenTileX() - 1) / 2;
};
Game_Character.prototype.centerY = function() {
return ($gameMap.screenTileY() - 1) / 2;
};
Game_Character.prototype.center = function(x, y) {
return $gameMap.setDisplayPos(x - this.centerX(), y - this.centerY());
};
})();
// Game_Player
(() => {
// @override
Game_Player.prototype.centerX = function() {
// 目的:
// 画面の論理中央を求める。
// ただし、右側にUI領域があるためその分だけ左にカメラをずらす。
// また、ズーム倍率が変化しても見た目の中心がブレないように補正する。
//
// 手順:
// 1. 画面に表示されるタイル数を取得 ($gameMap.screenTileX())
// 2. 0始まりインデックスのため -1 をして中央タイルを求める
// 3. プラグインパラメータの AdjustX1倍時のズレ量を取得
// 4. ズーム倍率で割り、スケール変化に対応した実効値を算出
// 5. 中央値に AdjustX を加算して、最終的な中心座標とする
//
const adjustX = param.adjustX / $gameScreen.zoomScale();
const centerX = (($gameMap.screenTileX() - 1) / 2) + adjustX;
return centerX;
};
// @override
Game_Player.prototype.centerY = function() {
// 同様
const adjustY = param.adjustY / $gameScreen.zoomScale();
const centerY = (($gameMap.screenTileY() - 1) / 2) + adjustY;
return centerY;
}
})();
})();