// ==================================================
// Cae_AlignBattlers.js
// ==================================================
/*:
* @plugindesc v1.0 - Specify troop/party battler positioning based on unit size. Includes basic perspective scaling (1 vanishing point).
* @author Caethyril
* @help Help:
* In the following, values written in "Quotes" are plugin parameters.
*
* Perspective is applied using a vanishing point at:
* X = (centre screen) = (screen width) * 0.5
* Y = "Horizon"
*
* "Positions" is a set of values for deploying to a square of size 1.
* These values are scaled to fit the true deployment area.
* The X values are then adjusted based on the corresponding Y value.
* (X values become closer together when nearer the horizon.)
*
* The deployment area is a scalene triangle with corners at:
* 1) Vanishing point
* 2) X = (centre screen) + "Skew Offset"
* Y = "Lower Y"
* 3) X = (centre screen) + "Skew Offset" + "Deploy Width"
* Y = "Lower Y"
*
* "Inner X Margin" = distance between screen centre and "Position" X = 0.
* "Top Margin" = distance between the horizon and "Position" Y = 0.
*
* Terms of use:
* Free to use and modify.
*
* @param Positions
* @text Positions
* @type struct<CaeScalePointsType>[]
* @desc Values used for positioning (mirrored for enemies). Units of size 1 use the first array; size 2, second array; etc.
* @default ["{\"X\":\"[\\\"0\\\"]\",\"Y\":\"[\\\"0.5\\\"]\"}","{\"X\":\"[\\\"0\\\",\\\"0\\\"]\",\"Y\":\"[\\\"0\\\",\\\"1\\\"]\"}","{\"X\":\"[\\\"0\\\",\\\"0\\\",\\\"0\\\"]\",\"Y\":\"[\\\"0\\\",\\\"0.5\\\",\\\"1\\\"]\"}","{\"X\":\"[\\\"0\\\",\\\"0\\\",\\\"1\\\",\\\"1\\\"]\",\"Y\":\"[\\\"0\\\",\\\"1\\\",\\\"0\\\",\\\"1\\\"]\"}","{\"X\":\"[\\\"0\\\",\\\"0\\\",\\\"1\\\",\\\"1\\\",\\\"1\\\"]\",\"Y\":\"[\\\"0\\\",\\\"1\\\",\\\"0\\\",\\\"0.5\\\",\\\"1\\\"]\"}","{\"X\":\"[\\\"0\\\",\\\"0\\\",\\\"0\\\",\\\"1\\\",\\\"1\\\",\\\"1\\\"]\",\"Y\":\"[\\\"0\\\",\\\"0.5\\\",\\\"1\\\",\\\"0\\\",\\\"0.5\\\",\\\"1\\\"]\"}"]
*
* @param --- X ---
* @text --- X ---
* @type select
* @desc Scaling settings for X coordinates!
*
* @param Deploy Width
* @text Deploy Width
* @parent --- X ---
* @type number
* @min 1
* @desc Full width of the areas in which enemies/actors are positioned. Default = 180 px.
* @default 180
*
* @param Skew Offset
* @text Skew Offset
* @parent --- X ---
* @type number
* @desc Distance between centre of screen and front edge of each deploy area. Default = 100 px.
* @default 100
*
* @param Inner X Margin
* @text Inner X Margin
* @parent --- X ---
* @type number
* @desc Distance (px) from the centre edge of the deployment area left empty. Default: 50 px.
* @default 50
*
* @param --- Y ---
* @text --- Y ---
* @type select
* @desc Scaling settings for Y coordinates!
*
* @param Horizon
* @text Horizon
* @parent --- Y ---
* @type number
* @desc Vertical distance between the top of the screen and the horizon. Default: 100 px.
* @default 100
*
* @param Lower Y
* @text Lower Y
* @parent --- Y ---
* @type number
* @desc Distance (px) between the bottom of the deployable area and the bottom of the screen. Default = 200 px.
* @default 200
*
* @param Top Margin
* @text Top Margin
* @parent --- Y ---
* @type number
* @desc Distance (px) from the top edge of the deployment area left empty. Default: 175 px.
* @default 175
*/
// =========================
/*~struct~CaeScalePointsType:
* @param X
* @text X
* @type number[]
* @max 1
* @decimals 3
* @desc A list of values in [0, 1] representing X positions within the deployable area.
* @default []
*
* @param Y
* @text Y
* @type number[]
* @max 1
* @decimals 3
* @desc A list of 0 to 1 values specifying Y positions within the deployable area.
* @default []
*/
var Imported = Imported || {};
Imported.Cae_AlignBattlers = 1.0;
var CAE = CAE || {};
CAE.AlignBattlers = CAE.AlignBattlers || {};
(function(_) {
'use strict';
const PLUGIN_NAME = 'Cae_AlignBattlers';
const ERR_PRE = PLUGIN_NAME + '.js ';
const ERR_NOPARAM = ERR_PRE + 'could not find its parameters!\nCheck the plugin file is named correctly and try again.';
const ERR_BADPAR = ERR_PRE + 'could not parse parameter "%1".\nCheck the value in the Plugin Manager and try again.';
// ======= Parameter stuff ======= //
_.params = PluginManager.parameters(PLUGIN_NAME);
if (!_.params) throw new Error(ERR_NOPARAM);
try {
// String > array > object > array > number => 3 object parses, 1 Number parse
_.positions = JSON.parse(_.params['Positions']).map(JSON.parse).map(function(o) {
return { x: JSON.parse(o.X).map(Number), y: JSON.parse(o.Y).map(Number) };
});
} catch (ex) {
throw new Error(ERR_BADPAR.format('Positions') + '\n\n' + ex);
}
_.deX = parseInt(_.params['Deploy Width'], 10) || 0;
_.skX = parseInt(_.params['Skew Offset'], 10) || 0;
_.loY = parseInt(_.params['Lower Y'], 10) || 0;
_.hoz = parseInt(_.params['Horizon'], 10) || 0;
_.mgX = parseInt(_.params['Inner X Margin'], 10) || 0;
_.mgY = parseInt(_.params['Top Margin'], 10) || 0;
// =========== Utility =========== //
// Fetch/scale functions: convert unit size/index into X/Y positions
_.get = {
arr: function(size, index) {
// Values in range [0, 1], representing relative positioning
let pos = _.positions[size-1];
return { x: pos.x[index] || 0, y: pos.y[index] || 0 };
},
margin: function(p, margin, max) {
// Scale input to current range - margin, then offset by margin
if (!margin || !max) return p;
return p * (max - margin) / max + margin;
},
height: function() {
// Deployment area height
return Math.max(Graphics.boxHeight - _.loY - _.hoz, 0);
},
scaleX: function(y) {
// Distance from current Y to horizon, divided by height of deployable area; min 0, max 1
return ((y - _.hoz) / this.height()).clamp(0, 1);
},
x: function(rawX, y) {
// ((input * area width * perspective scale) adjusted for margin + offset * perspective scale
let scale = this.scaleX(y);
return this.margin(rawX * scale * _.deX, _.mgX, _.deX) + _.skX * scale;
},
y: function(rawY) {
// input * area height + minimum
return this.margin(rawY * this.height(), _.mgY, this.height()) + _.hoz;
},
p: function(size, index) {
// Get X and Y coordinates from unit size and member index
let arr = this.arr(size, index);
let res = {};
res.y = this.y(arr.y);
res.x = this.x(arr.x, res.y);
return res;
}
};
// ========= Alterations ========= //
// Override actor home position with new values~
Sprite_Actor.prototype.setActorHome = function(index) { // Override!
let size = this._battler.friendsUnit().members().length;
let p = _.get.p(size, index);
this.setHome(Graphics.boxWidth / 2 + p.x, p.y); // X is relative to screen centre
};
// Reapply enemy home position with new values~
_.Sprite_Enemy_setBattler = Sprite_Enemy.prototype.setBattler; // Alias
Sprite_Enemy.prototype.setBattler = function(battler) {
_.Sprite_Enemy_setBattler.apply(this, arguments);
let size = battler.friendsUnit().members().length;
let index = battler.index();
let p = _.get.p(size, index);
this.setHome(Graphics.boxWidth / 2 - p.x, p.y); // X is relative to screen centre
this._stateIconSprite.setup(battler);
};
})(CAE.AlignBattlers);