Skip to content

Commit

Permalink
Refactor of Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ChasarooniZ committed Aug 14, 2024
1 parent cd28f08 commit c611617
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 127 deletions.
1 change: 1 addition & 0 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"choices": {
"default": "Default",
"custom": "Custom",
"unchanged": "Unchanged",
"health": "Health-Based",
"disposition": "Disposition",
Expand Down
210 changes: 83 additions & 127 deletions scripts/autoColorRing.js
Original file line number Diff line number Diff line change
@@ -1,141 +1,76 @@
import { COLORS, MODULE_ID } from "./misc.js";
import { getHealthLevel } from "./systemCompatability.js";

/**
* Check if the token is a valid disposition for our health target setting.
* @param {Object} Token instance
* @returns {boolean} If the token should be colored
*/
function getValidHealthTarget(token) {
const healthTargetsSetting = game.settings.get(
MODULE_ID,
"auto-coloring.health-targets"
);
if (
token.document.disposition == CONST.TOKEN_DISPOSITIONS.FRIENDLY &&
(healthTargetsSetting === "non-friendly" ||
healthTargetsSetting === "hostile")
)
return false;
if (
token.document.disposition == CONST.TOKEN_DISPOSITIONS.HOSTILE &&
(healthTargetsSetting === "non-hostile" ||
healthTargetsSetting === "friendly")
)
return false;
if (
token.document.disposition == CONST.TOKEN_DISPOSITIONS.NEUTRAL &&
(healthTargetsSetting === "hostile" || healthTargetsSetting === "friendly")
)
return false;
return !(
token.document.disposition == CONST.TOKEN_DISPOSITIONS.SECRET &&
healthTargetsSetting !== "all"
);
}

export function autoColorRing() {
let ringColor = this.document.ring.colors.ring;
let backgroundColor = this.document.ring.colors.background;
const overideColor = game.settings.get(
MODULE_ID,
"auto-coloring.override-color"
);
const ringSetting = game.settings.get(MODULE_ID, "auto-coloring.ring");
const backgroundSetting = game.settings.get(
MODULE_ID,
"auto-coloring.background"
);
const { ring, background } = this.document.ring.colors;
const ringSetting = getSetting(this.actor, "type", "ring");
const backgroundSetting = getSetting(this.actor, "type", "background");

if (ringSetting === "health" || backgroundSetting === "health") {
const level =
this.document.getFlag(MODULE_ID, "tokenHealthLevel") ??
getHealthLevel(this.actor);
if (!isNaN(level)) {
const healthColor = getColorForHealthLevel(level);
const valid = getValidHealthTarget(this);
if (ringSetting === "health" && valid) ringColor = healthColor;
if (backgroundSetting === "health" && valid)
backgroundColor = healthColor;
}
}

if (ringSetting === "disposition" || backgroundSetting === "disposition") {
let dispositionColor = undefined;
switch (this.document.disposition) {
case CONST.TOKEN_DISPOSITIONS.FRIENDLY:
dispositionColor = Color.fromString(COLORS.GREEN);
break;
case CONST.TOKEN_DISPOSITIONS.NEUTRAL:
dispositionColor = Color.fromString(COLORS.YELLOW);
break;
case CONST.TOKEN_DISPOSITIONS.HOSTILE:
dispositionColor = Color.fromString(COLORS.RED);
break;
}
if (ringSetting === "disposition") ringColor = dispositionColor;
if (backgroundSetting === "disposition") backgroundColor = dispositionColor;
}
const colorMap = {
health: () =>
getColorForHealthLevel(
this.document.getFlag(MODULE_ID, "tokenHealthLevel") ??
getHealthLevel(this.actor)
),
disposition: () =>
({
[CONST.TOKEN_DISPOSITIONS.FRIENDLY]: Color.fromString(COLORS.GREEN),
[CONST.TOKEN_DISPOSITIONS.NEUTRAL]: Color.fromString(COLORS.YELLOW),
[CONST.TOKEN_DISPOSITIONS.HOSTILE]: Color.fromString(COLORS.RED),
}[this.document.disposition]),
levelDiff: () => {
const partyLevel =
game.actors.party.members.reduce((tot, char) => tot + char.level, 0) /
game.actors.party.members.length || 1;
const levelDiff = this.actor.level - partyLevel;
return Color.fromString(
COLORS.PF2E.LEVELDIFF.DEFAULT[
levelDiff <= -4
? "-4"
: levelDiff <= -3
? "-3"
: levelDiff <= -2
? "-2"
: levelDiff <= -1
? "-1"
: levelDiff <= 0
? "0"
: levelDiff <= 1
? "+1"
: levelDiff <= 2
? "+2"
: levelDiff <= 3
? "+3"
: "+4"
]
);
},
custom: (type) => getSetting(this.actor, "color", type),
};

if (ringSetting === "levelDiff" || backgroundSetting === "levelDiff") {
const partyLevel =
game.actors.party.members.reduce((tot, char) => (tot += char.level), 0) /
game.actors.party.members.length ?? 1;
const charLevel = this.actor.level;
const levelDiff = charLevel - partyLevel;
let levelDiffColor = undefined;
if (levelDiff <= -4) {
// PL -4
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["-4"]);
} else if (levelDiff <= -3) {
// PL -3
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["-3"]);
} else if (levelDiff <= -2) {
// PL -2
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["-2"]);
} else if (levelDiff <= -1) {
// PL -1
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["-1"]);
} else if (levelDiff <= 0) {
// PL
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["0"]);
} else if (levelDiff <= 1) {
// PL +1
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["+1"]);
} else if (levelDiff <= 2) {
// PL +2
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["+2"]);
} else if (levelDiff <= 3) {
// PL +3
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["+3"]);
} else {
// PL +4
levelDiffColor = Color.fromString(COLORS.PF2E.LEVELDIFF.DEFAULT["+4"]);
}
if (ringSetting === "levelDiff") ringColor = levelDiffColor;
if (backgroundSetting === "levelDiff") backgroundColor = levelDiffColor;
}
//Ring Color Set
let ringColor = colorMap[ringSetting]?.("ring") ?? ring;
// Background Color Set
let backgroundColor =
colorMap[backgroundSetting]?.("background") ?? background;

const percentColor = game.settings.get(
MODULE_ID,
"auto-coloring.percent-color"
);
if (ringSetting !== "unchanged" && ringColor) {
ringColor = Color.multiplyScalar(ringColor, percentColor);
}
if (backgroundSetting !== "unchanged" && backgroundColor) {
backgroundColor = Color.multiplyScalar(backgroundColor, percentColor);
}
if (!overideColor) {
if (this.document.ring.colors.ring !== null)
ringColor = this.document.ring.colors.ring;
if (this.document.ring.colors.background !== null)
backgroundColor = this.document.ring.colors.background;
}
return {
ring: ringColor,
background: backgroundColor,
};

//Ring Color Scaling
ringColor =
ringSetting !== "unchanged"
? Color.multiplyScalar(ringColor, percentColor)
: ringColor;
//Background Color Scaling
backgroundColor =
backgroundSetting !== "unchanged"
? Color.multiplyScalar(backgroundColor, percentColor)
: backgroundColor;

return { ring: ringColor, background: backgroundColor };
}

/**
Expand All @@ -147,3 +82,24 @@ export function autoColorRing() {
function getColorForHealthLevel(level) {
return Color.fromHSV([level / 3.0, 1.0, 1.0]);
}

/**
*
* @param {*} actor Actor to getSettingFor
* @param {"type" | "color"} typeOrColor "type" or "color"
* @param {"ring" | "background"} ringOrBackground "ring" or "background"
*/
function getSetting(actor, typeOrColor, ringOrBackground) {
const isParty = game.actors.party.members.some((a) => a.id === actor.id);
const type = isParty
? "party"
: {
[CONST.TOKEN_DISPOSITIONS.FRIENDLY]: "friendly",
[CONST.TOKEN_DISPOSITIONS.NEUTRAL]: "neutral",
[CONST.TOKEN_DISPOSITIONS.HOSTILE]: "hostile",
}[this.document.disposition] || "";

return resolvePlayerWorldSetting(
`auto-coloring.${ringOrBackground}.${typeOrColor}.${type}`
);
}
1 change: 1 addition & 0 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export function registerSettings() {

const autoColoringChoices = {
unchanged: localize("auto-coloring.choices.unchanged"),
custom: localize("auto-coloring.choices.custom"),
health: localize("auto-coloring.choices.health"),
disposition: localize("auto-coloring.choices.disposition"),
...(game.system.id === "pf2e" && {
Expand Down

0 comments on commit c611617

Please sign in to comment.