Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vaesen conditions on tokens #86

Merged
merged 5 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions model/tab/vaesen-main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
{{#if item.system.active}}
checked
{{/if}}
{{#if ../actor.flags.vaesen.isActorSheet}}
onclick="ui.notifications.warn(`Vaesen's conditions should be applied in tokens only.`); return false;"
{{/if}}
/>
<span class="checkbox-custom rectangular"></span>
</label>
Expand Down
7 changes: 5 additions & 2 deletions script/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { YearZeroRollManager } from "./lib/yzur.js";
import * as Chat from "./util/chat.js";
import { vaesenItemSheet } from "./sheet/itemSheet.js";
import { migrate } from "./util/migrator.js";
import { VaesenTokenHUD } from "./util/token.js";

Hooks.on("renderChatMessage", (app, html, data) => {
Chat.hideChatActionButtons(app, html, data);
Expand Down Expand Up @@ -96,8 +97,6 @@ Hooks.once("init", () => {
"ROLL.tooltipTemplate": "systems/vaesen/model/templates/dice/tooltip.hbs",
"ROLL.infosTemplate": "systems/vaesen/model/templates/dice/infos.hbs",
});


});

Hooks.once("ready", async function () {
Expand All @@ -109,6 +108,10 @@ Hooks.once("ready", async function () {

});

Hooks.on('canvasReady', () => {
canvas.hud.token = new VaesenTokenHUD();
});

Hooks.on("updateActor", (actor,changes,diff,userId) => {
game.scenes.current.tokens.forEach(x => {
if (x.actorId !== actor._id)
Expand Down
10 changes: 6 additions & 4 deletions script/sheet/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,15 @@ export class PlayerCharacterSheet extends VaesenActorSheet {
updateCondition(conditionName) {
let actor = this.actor;

const currentEffect = Array.from(actor.effects?.values()).find(it => it.statuses.has(conditionName));
if (currentEffect)
const statusEffect = CONFIG.statusEffects.find(it => it.id === conditionName);

const currentEffect = Array.from(actor.effects?.values()).find(it => it.icon === statusEffect.icon);
if (currentEffect) {
actor.deleteEmbeddedDocuments('ActiveEffect', [currentEffect.id]);
}
else {
const statusEffect = CONFIG.statusEffects.find(it => it.id === conditionName);
actor.createEmbeddedDocuments("ActiveEffect", [{
label: statusEffect.label,
label: game.i18n.localize(statusEffect.label),
icon: statusEffect.icon,
changes: statusEffect.changes,
id: this.uuid,
Expand Down
16 changes: 6 additions & 10 deletions script/sheet/vaesen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prepareRollNewDialog } from "../util/roll.js";
import { VaesenActorSheet } from "../actor/vaesen-actor-sheet.js";
import { conditions } from "../util/conditions.js";

export class VaesenCharacterSheet extends VaesenActorSheet {

Expand All @@ -19,6 +20,10 @@ export class VaesenCharacterSheet extends VaesenActorSheet {
});
}

async getData() {
this.actor.setFlag("vaesen", "isActorSheet", !this.actor.isToken);
return super.getData();
}

activateListeners(html) {
super.activateListeners(html);
Expand Down Expand Up @@ -71,16 +76,7 @@ export class VaesenCharacterSheet extends VaesenActorSheet {
async onToggleActive(event) {
let element = event.currentTarget;
let itemID = element.closest(".item").dataset.itemId;
let item = this.actor.items.get(itemID);
if (item.system.active) {
await this.actor.updateEmbeddedDocuments("Item", [
{ _id: itemID, "data.active": false },
]);
} else {
await this.actor.updateEmbeddedDocuments("Item", [
{ _id: itemID, "data.active": true },
]);
}
await conditions.onVaesenCondition(this.actor, itemID);
}

onWeaponRoll(event) {
Expand Down
26 changes: 26 additions & 0 deletions script/util/conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ export class conditions{
return this.allStatusEffects.find(effect => effect.id === id);
}

static async onVaesenCondition(actor, conditionId) {
let condition = Array.from(actor.items?.values()).find(x => x.type == "condition" && x.id == conditionId);
console.log(condition);

await actor.updateEmbeddedDocuments("Item", [
{ _id: condition.id, "data.active": !condition.system.active },
]);

const statusEffect = {
label: condition.name,
icon: condition.img,
id: condition.name,
statuses: [condition.name]
};

const currentEffect = Array.from(actor.effects?.values()).find(it => it.icon === statusEffect.icon);
if (currentEffect) {
await actor.deleteEmbeddedDocuments('ActiveEffect', [currentEffect.id]);
}
else {
await actor.createEmbeddedDocuments("ActiveEffect", [statusEffect]);
}

return condition.system.active;
}

}

conditions.eventsProcessing = {
Expand Down
75 changes: 75 additions & 0 deletions script/util/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { conditions } from "../util/conditions.js";

export class VaesenTokenHUD extends TokenHUD {
constructor(...args) {
super(...args);
}

_getStatusEffectChoices(params) {
var actor = this.object.document.actor;

if (actor.type === "player") {
let ret = super._getStatusEffectChoices();
console.log(ret);
return ret;
}

let yzurActions = {
"modules/yze-combat/assets/icons/fast-action.svg" : {
cssClass: "",
id: "fastAction",
isActive: false,
isOverlay: false,
src: "modules/yze-combat/assets/icons/fast-action.svg",
title: "Fast Action"
},
"modules/yze-combat/assets/icons/slow-action.svg" : {
cssClass: "",
id: "slowAction",
isActive: false,
isOverlay: false,
src: "modules/yze-combat/assets/icons/slow-action.svg",
title: "Slow Action"
}
};

if (actor.type === "npc") {
return yzurActions;
}

if (actor.type === "vaesen") {
let vaesenActions = {};
for (let item of Object.values(actor.items.contents)) {
if (item.type !== "condition") {
continue;
}
console.log(item);
vaesenActions[item.img] = {
cssClass: item.system.active ? "active" : "",
id: item.id,
src: item.img,
title: `${item.name} (${item.system.bonus})`,
isActive: item.system.active
};
}
vaesenActions["modules/yze-combat/assets/icons/fast-action.svg"] = yzurActions["modules/yze-combat/assets/icons/fast-action.svg"];
vaesenActions["modules/yze-combat/assets/icons/slow-action.svg"] = yzurActions["modules/yze-combat/assets/icons/slow-action.svg"];
return vaesenActions;
}
}

async _onToggleEffect(effect, {active, overlay=false}={}) {
var actor = this.object.document.actor;
if (actor.type !== "vaesen") {
return super._onToggleEffect(effect, {active, overlay});
}

effect.preventDefault();
effect.stopPropagation();
let img = effect.currentTarget;
const result = await conditions.onVaesenCondition(actor, img.dataset.statusId);

if ( this.hasActiveHUD ) canvas.tokens.hud.refreshStatusIcons();
return result;
}
}