Skip to content

Commit

Permalink
Added magic system from Guild Codex
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickBauer committed Mar 17, 2023
1 parent 3631171 commit 7cedc86
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### 1.2.0
* Added support for the Guild Codex magic system (disabled by default)

### 1.1.1
* Automatically updates the prototype token name when an actors name is changed (if both were identical). Can be disabled in the system settings.

Expand Down
2 changes: 2 additions & 0 deletions src/fatex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { FateCombat } from "./module/combat/FateCombat";
import { FateXSettings } from "./module/helper/Settings";
import { ChatActionsFeature } from "./module/features/ChatActionsFeature";
import { PrototypeTokenNameSyncFeature } from "./module/features/PrototypeTokenNameSyncFeature";
import { MagicSystem } from "./module/features/MagicSystem";

/* -------------------------------- */
/* System initialization */
Expand Down Expand Up @@ -128,6 +129,7 @@ TemplateActorsFeature.hooks();
ActorGroupFeature.hooks();
ChatActionsFeature.hooks();
PrototypeTokenNameSyncFeature.hooks();
MagicSystem.hooks();

/* -------------------------------- */
/* Webpack HMR */
Expand Down
68 changes: 67 additions & 1 deletion src/module/chat/FateRoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export class FateRoll extends FateRollDataModel {
static createFromSkill(skill: SkillItemData & ItemDataProperties, { magic = false } = {}) {
const options = { magic };

if (game.settings.get("fatex", "guildCodexMagicSystemEnabled") && magic) {
options["magicCount"] = this.determineMagicCount(skill);

if (options["magicCount"] === false) {
return false;
}
}

return new FateRoll({
_id: foundry.utils.randomID(),
name: skill.name,
Expand All @@ -17,7 +25,13 @@ export class FateRoll extends FateRollDataModel {
}

async roll(userId = "") {
const roll = new Roll(`4dF${this.options.magic && "m"}`).roll({ async: false });
if (game.settings.get("fatex", "guildCodexMagicSystemEnabled")) {
if (this.options.magic && this.options.magicCount > 0) {
return this.rollMagic(userId, this.options.magicCount);
}
}

const roll = new Roll(`4dF`).roll({ async: false });
this.updateSource({ faces: roll.terms[0].results.map((r) => r.count ?? r.result) });

if (game.modules.get("dice-so-nice")?.active) {
Expand All @@ -28,6 +42,32 @@ export class FateRoll extends FateRollDataModel {
return this;
}

async rollMagic(userId = "", magicCount: number) {
const normalCount = 4 - magicCount;

const magicRoll = new Roll(`${this.options.magicCount}dFm`).roll({ async: false });
const normalRoll = new Roll(`${normalCount}dF`).roll({ async: false });

this.updateSource({
faces: [...magicRoll.terms[0].results, ...normalRoll.terms[0].results].map((r) => r.count ?? r.result),
});

magicRoll.terms[0].options.sfx = {
specialEffect: "PlayAnimationParticleSparkles",
};

if (game.modules.get("dice-so-nice")?.active) {
const user = userId ? game.users.get(userId) : game.user;

await Promise.all([
game.dice3d.showForRoll(magicRoll, user, true),
game.dice3d.showForRoll(normalRoll, user, true),
]);
}

return this;
}

async reroll(userId = "") {
const history = this.addHistoryEntry(userId, { type: "reroll", previousRoll: this.faces });
await this.roll(userId);
Expand Down Expand Up @@ -82,4 +122,30 @@ export class FateRoll extends FateRollDataModel {

return await renderTemplate(template, this);
}

static determineMagicCount(skill: SkillItemData & ItemDataProperties) {
const magicSkills = skill.parent.items.filter((i) => i.type === "skill" && i.system.options.isMagicSkill);

if (magicSkills.length === 0) {
ui.notifications.error(game.i18n.localize("FAx.Item.Skill.Roll.NoMagicSkills"));
return false;
}

if (magicSkills.length > 1) {
ui.notifications.warn(
game.i18n.format("FAx.Item.Skill.Roll.MultipleMagicSkills", {
skills: magicSkills.map((s) => s.name).join(", "),
})
);
}

const magicRank = Math.clamped(Number(magicSkills[0].system.rank ?? 0), 0, 4);

if (magicRank < 1) {
ui.notifications.error(game.i18n.localize("FAx.Item.Skill.Roll.MagicSkillTooLow"));
return false;
}

return magicRank;
}
}
17 changes: 17 additions & 0 deletions src/module/features/MagicSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @ts-nocheck

export class MagicSystem {
static hooks() {
Hooks.once("init", () => {
game.settings.register("fatex", "guildCodexMagicSystemEnabled", {
name: game.i18n.localize("FAx.Settings.magicSystemEnabled.Name"),
hint: game.i18n.localize("FAx.Settings.magicSystemEnabled.Hint"),
scope: "world",
config: true,
default: false,
reload: true,
type: Boolean,
});
});
}
}
1 change: 1 addition & 0 deletions src/module/item/ItemSheetFate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class ItemSheetFate extends ItemSheet {
// enforce data to ensure compatability between 0.7 and 0.8
// @ts-ignore
data.data = this.object.system;
data.system = data.data;

// Set owner name if possible
data.isOwnedBy = this.actor ? this.actor.name : false;
Expand Down
15 changes: 11 additions & 4 deletions src/module/item/skill/SkillItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,20 @@ export class SkillItem extends BaseItem {
}
}

static async rollSkill(sheet, item, event) {
const skill = this.prepareItemData(duplicate(item), item);
static async rollSkill(sheet, skill, event) {
const actor = sheet.actor;

const fateRoll = await FateRoll.createFromSkill(skill, { magic: event.shiftKey }).roll();
const fateChatCard = FateChatCard.create(actor, [fateRoll]);
const fateRoll = FateRoll.createFromSkill(skill, {
magic: event.shiftKey,
});

if (!fateRoll) {
return;
}

await fateRoll.roll();

const fateChatCard = FateChatCard.create(actor, [fateRoll]);
await fateChatCard.sendToChat();
}
}
10 changes: 9 additions & 1 deletion src/module/item/skill/SkillSheet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { ItemSheetFate } from "../ItemSheetFate";

export class SkillSheet extends ItemSheetFate {}
export class SkillSheet extends ItemSheetFate {
getData(): any {
const data = super.getData();

data.magicSystemEnabled = game.settings.get("fatex", "guildCodexMagicSystemEnabled");

return data;
}
}
1 change: 1 addition & 0 deletions src/styles/abstract/variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $primary-text-color: #191813;
$light-text-color: #fff;

$caution-color: rgb(242, 22, 40);
$magic-color: rgb(160, 25, 31);
$shadow-color: rgb(160, 160, 160);

// Fonts
Expand Down
4 changes: 4 additions & 0 deletions src/styles/components/chat.scss
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,9 @@
width: 24px;
height: 24px;
text-align: center;

&.fatex-dice__magic_die {
color: $magic-color;
}
}
}
21 changes: 21 additions & 0 deletions src/styles/components/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
min-height: 70px;
}

&__checkbox {
label {
display: flex;
gap: calc($component-padding / 2);

div {
display: flex;
flex-direction: column;
}
}

&__name {
font-weight: bold;
padding: 5px 0;
}

&__help {
font-size: 12px;
}
}

&__editor-wrapper {
height: 250px;
overflow: hidden;
Expand Down
16 changes: 15 additions & 1 deletion system/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@
"EditItem": "Edit Skill",
"Rank": "Rank",
"Description": "Description",
"DescriptionHelp": "Is only displayed when sending to chat"
"DescriptionHelp": "Is only displayed when sending to chat",
"Options": {
"Headline": "Options",
"IsMagicSkill": "Magic Skill",
"IsMagicSkillHelp": "If checked, this skills rank will be used to determine the number of magic dice to roll."
},
"Roll": {
"MultipleMagicSkills": "Multiple magic skills found ({skills}). Using the first one to roll.",
"NoMagicSkills": "No magic skill found. Please add a magic skill to your character sheet and roll again.",
"MagicSkillTooLow": "The magic skill rank is zero or less. Please increase the rank to roll magic dice."
}
},
"Stunt": {
"Name": "Stunt title",
Expand Down Expand Up @@ -217,6 +227,10 @@
"autoUpdateTokenName": {
"Name": "Automatically update prototype token name",
"Hint": "Automatically updates the prototype token name when an actors name is changed (if both were identical)."
},
"magicSystemEnabled": {
"Name": "Enable the magic system from Guild Codex",
"Hint": "This will add a magic skill to the character sheet and allow you to use magic dice."
}
},
"Template": {
Expand Down
2 changes: 1 addition & 1 deletion system/system.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "fatex",
"title": "FateX - Fate Extended",
"description": "An extended implementation of Fate and its derivative systems",
"version": "1.1.1",
"version": "1.2.0",
"authors": [
{
"name": "Patrick Bauer",
Expand Down
5 changes: 4 additions & 1 deletion system/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
},
"skill": {
"templates": ["base"],
"rank": 0
"rank": 0,
"options": {
"isMagicSkill": false
}
},
"stunt": {
"templates": ["base"],
Expand Down
4 changes: 2 additions & 2 deletions system/templates/chat/roll.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<div class="fatex-roll" data-token-id="{{token.id}}">
<div class="fatex-dice">
{{#each symbols as |symbol|}}
<div>{{symbol}}</div>
{{#each symbols as |symbol symbolIndex|}}
<div {{#if (lt symbolIndex ../options.magicCount)}}class="fatex-dice__magic_die"{{/if}}>{{symbol}}</div>
{{/each}}
</div>

Expand Down
18 changes: 18 additions & 0 deletions system/templates/item/skill-sheet.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
radioItems = availableRanks
}}

{{#if magicSystemEnabled}}
<div class="fatex-setting">
<header class="fatex-headline">
<h3 class="fatex-headline__text">{{localize "FAx.Item.Skill.Options.Headline"}}</h3>
</header>

<div class="fatex-setting__checkbox">
<label>
<input type="checkbox" data-dtype="Boolean" name="system.options.isMagicSkill" {{checked system.options.isMagicSkill}}/>
<div>
<span class="fatex-setting__checkbox__name">{{localize "FAx.Item.Skill.Options.IsMagicSkill"}}</span>
<span class="fatex-setting__checkbox__help">{{localize "FAx.Item.Skill.Options.IsMagicSkillHelp"}}</span>
</div>
</label>
</div>
</div>
{{/if}}

{{> "systems/fatex/templates/item/settings/setting-editor.hbs"
headline = "FAx.Item.Skill.Description"
fieldName = "data.description"
Expand Down

0 comments on commit 7cedc86

Please sign in to comment.