I wrote a Libra/Scan plugin (my first foray into JavaScript). I have Skill which calls a Common Event which runs the Script Event Command:
I'm pretty happy with the outcome. (Sidenote: the padEnds don't seem to account for icon widths, but I just played with the numbers until they aligned.)
[Screenshot]
[/Screenshot]
My question is, should I bother passing targetEnemy as an argument, or just put it directly into the plugin code? I'm used to considering coupling and cohesion. If I move code to determine the target into the plugin it becomes self-contained, but I'm used to passing arguments so I can get reuse out of code in other contexts. Your thoughts?
Code:
// Get the target enemy
var targetIndex = BattleManager._action._targetIndex;
var targetEnemy = $gameTroop.members()[targetIndex];
// Call Libra
JSH_Libra.displayEnemyStatsAndWeaknesses(targetEnemy);
I'm pretty happy with the outcome. (Sidenote: the padEnds don't seem to account for icon widths, but I just played with the numbers until they aligned.)
[Screenshot]
[/Screenshot]
My question is, should I bother passing targetEnemy as an argument, or just put it directly into the plugin code? I'm used to considering coupling and cohesion. If I move code to determine the target into the plugin it becomes self-contained, but I'm used to passing arguments so I can get reuse out of code in other contexts. Your thoughts?
/*:
* @target MZ
* @plugindesc Scans enemy and returns their stats and elemental weaknesses
* @Author John Hunter
*
* @Help
* This plugin scans an enemy and displays formatted results.
*/
var JSH_Libra = JSH_Libra || {};
// Function to display the target enemy's name, stats, and weaknesses
JSH_Libra.displayEnemyStatsAndWeaknesses = function(targetEnemy) {
if (targetEnemy) {
// Get enemy stats
const targetName = targetEnemy.name();
const targetHP = targetEnemy.hp;
const targetMHP = targetEnemy.mhp;
const targetMP = targetEnemy.mp;
const targetMMP = targetEnemy.mmp;
const targetTP = targetEnemy.tp;
const targetMTP = 255;
const targetSTR = targetEnemy.str;
const targetDEF = targetEnemy.def;
const targetMAT = targetEnemy.mat;
const targetMDF = targetEnemy.mdf;
const targetAGI = targetEnemy.agi;
const targetLUK = targetEnemy.luk;
// Format enemy stats
const formatTargetName = `\\C[14]${targetName}\\C[0]`;
const formatHPX = `\\C[4]HP:\\C[0] ${targetHP}/${targetMHP}`.padEnd(27);
const formatMPX = `\\C[4]MP:\\C[0] ${targetMP}/${targetMMP}`.padEnd(27);
const formatTPX = `\\C[4]TP:\\C[0] ${targetTP}/${targetMTP}`.padEnd(27);
const formatSTR = `\\C[4]STR:\\C[0] ${targetSTR}`.padEnd(23);
const formatDEF = `\\C[4]DEF:\\C[0] ${targetDEF}`.padEnd(23);
const formatMAT = `\\C[4]MAT:\\C[0] ${targetMAT}`.padEnd(23);
const formatMDF = `\\C[4]MDF:\\C[0] ${targetMDF}`.padEnd(23);
const formatAGI = `\\C[4]AGI:\\C[0] ${targetAGI}`.padEnd(23);
const formatLUK = `\\C[4]LUK:\\C[0] ${targetLUK}`.padEnd(23);
// Get enemy weaknesses
// Custom mapping of element IDs to their icons
const elementDetails = {
1: { name: "Physical", icon: "\\I[76]" },
2: { name: "Fire", icon: "\\I[64]" },
3: { name: "Ice", icon: "\\I[65]" },
4: { name: "Thunder", icon: "\\I[66]" },
5: { name: "Water", icon: "\\I[67]" },
6: { name: "Earth", icon: "\\I[68]" },
7: { name: "Wind", icon: "\\I[69]" },
8: { name: "Light", icon: "\\I[70]" },
9: { name: "Darkness", icon: "\\I[71]" }
};
// Get enemy weaknesses
const elementRates = {};
const noteTag = targetEnemy.enemy().note;
for (const [id, details] of Object.entries(elementDetails)) {
let rate = (targetEnemy.elementRate(Number(id)) * 100) + "%";
if (rate === "0%") rate = "Immune";
if (noteTag.includes(`<Element Absorb: ${details.name}>`)) rate = "Absorb";
if (noteTag.includes(`<Element Reflect: ${details.name}>`)) rate = "Reflect";
//Format enemy weaknesses
elementRates[id] = `${details.icon}: ${rate}`.padEnd(17);
}
// Display enemy stats and weaknesses
$gameMessage.add(formatTargetName);
$gameMessage.add("\\>" + formatHPX + formatSTR + formatMAT + elementRates[2] + elementRates[5] + elementRates[8]);
$gameMessage.add("\\>" + formatMPX + formatDEF + formatMDF + elementRates[3] + elementRates[6] + elementRates[9]);
$gameMessage.add("\\>" + formatTPX + formatAGI + formatLUK + elementRates[4] + elementRates[7] + elementRates[1]);
}
}
* @target MZ
* @plugindesc Scans enemy and returns their stats and elemental weaknesses
* @Author John Hunter
*
* @Help
* This plugin scans an enemy and displays formatted results.
*/
var JSH_Libra = JSH_Libra || {};
// Function to display the target enemy's name, stats, and weaknesses
JSH_Libra.displayEnemyStatsAndWeaknesses = function(targetEnemy) {
if (targetEnemy) {
// Get enemy stats
const targetName = targetEnemy.name();
const targetHP = targetEnemy.hp;
const targetMHP = targetEnemy.mhp;
const targetMP = targetEnemy.mp;
const targetMMP = targetEnemy.mmp;
const targetTP = targetEnemy.tp;
const targetMTP = 255;
const targetSTR = targetEnemy.str;
const targetDEF = targetEnemy.def;
const targetMAT = targetEnemy.mat;
const targetMDF = targetEnemy.mdf;
const targetAGI = targetEnemy.agi;
const targetLUK = targetEnemy.luk;
// Format enemy stats
const formatTargetName = `\\C[14]${targetName}\\C[0]`;
const formatHPX = `\\C[4]HP:\\C[0] ${targetHP}/${targetMHP}`.padEnd(27);
const formatMPX = `\\C[4]MP:\\C[0] ${targetMP}/${targetMMP}`.padEnd(27);
const formatTPX = `\\C[4]TP:\\C[0] ${targetTP}/${targetMTP}`.padEnd(27);
const formatSTR = `\\C[4]STR:\\C[0] ${targetSTR}`.padEnd(23);
const formatDEF = `\\C[4]DEF:\\C[0] ${targetDEF}`.padEnd(23);
const formatMAT = `\\C[4]MAT:\\C[0] ${targetMAT}`.padEnd(23);
const formatMDF = `\\C[4]MDF:\\C[0] ${targetMDF}`.padEnd(23);
const formatAGI = `\\C[4]AGI:\\C[0] ${targetAGI}`.padEnd(23);
const formatLUK = `\\C[4]LUK:\\C[0] ${targetLUK}`.padEnd(23);
// Get enemy weaknesses
// Custom mapping of element IDs to their icons
const elementDetails = {
1: { name: "Physical", icon: "\\I[76]" },
2: { name: "Fire", icon: "\\I[64]" },
3: { name: "Ice", icon: "\\I[65]" },
4: { name: "Thunder", icon: "\\I[66]" },
5: { name: "Water", icon: "\\I[67]" },
6: { name: "Earth", icon: "\\I[68]" },
7: { name: "Wind", icon: "\\I[69]" },
8: { name: "Light", icon: "\\I[70]" },
9: { name: "Darkness", icon: "\\I[71]" }
};
// Get enemy weaknesses
const elementRates = {};
const noteTag = targetEnemy.enemy().note;
for (const [id, details] of Object.entries(elementDetails)) {
let rate = (targetEnemy.elementRate(Number(id)) * 100) + "%";
if (rate === "0%") rate = "Immune";
if (noteTag.includes(`<Element Absorb: ${details.name}>`)) rate = "Absorb";
if (noteTag.includes(`<Element Reflect: ${details.name}>`)) rate = "Reflect";
//Format enemy weaknesses
elementRates[id] = `${details.icon}: ${rate}`.padEnd(17);
}
// Display enemy stats and weaknesses
$gameMessage.add(formatTargetName);
$gameMessage.add("\\>" + formatHPX + formatSTR + formatMAT + elementRates[2] + elementRates[5] + elementRates[8]);
$gameMessage.add("\\>" + formatMPX + formatDEF + formatMDF + elementRates[3] + elementRates[6] + elementRates[9]);
$gameMessage.add("\\>" + formatTPX + formatAGI + formatLUK + elementRates[4] + elementRates[7] + elementRates[1]);
}
}