-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokeymanz.mjs
492 lines (474 loc) · 14.3 KB
/
pokeymanz.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
const POKEYMANZ = {
attributes: {
heart: {
label: "POKEYMANZ.Heart",
},
fitness: {
label: "POKEYMANZ.Fitness",
},
research: {
label: "POKEYMANZ.Research",
},
tatics: {
label: "POKEYMANZ.Tatics",
},
},
pokemonTypes: {
none: "POKEYMANZ.None",
normal: "POKEYMANZ.Normal",
fire: "POKEYMANZ.Fire",
water: "POKEYMANZ.Water",
grass: "POKEYMANZ.Grass",
flying: "POKEYMANZ.Flying",
fighting: "POKEYMANZ.Fighting",
poison: "POKEYMANZ.Poison",
electric: "POKEYMANZ.Electric",
ground: "POKEYMANZ.Ground",
rock: "POKEYMANZ.Rock",
psychic: "POKEYMANZ.Psychic",
ice: "POKEYMANZ.Ice",
bug: "POKEYMANZ.Bug",
ghost: "POKEYMANZ.Ghost",
steel: "POKEYMANZ.Steel",
dragon: "POKEYMANZ.Dragon",
dark: "POKEYMANZ.Dark",
fairy: "POKEYMANZ.Fairy",
},
itemCategories: {
edge: {
battle: "POKEYMANZ.Battle",
social: "POKEYMANZ.Social",
utility: "POKEYMANZ.Utility",
},
},
itemTypes: {
edge: "POKEYMANZ.Edge",
},
};
/*Restering Handlebars Helpers*/
Handlebars.registerHelper("toLowerCase", function (str) {
return str.toLowerCase();
});
/* Defining CharacterData*/
function attributeDiceFields() {
const fields = foundry.data.fields;
return {
die: new fields.SchemaField({
sides: new fields.NumberField({
initial: 4,
min: 0,
integer: true,
positive: true,
}),
modifier: new fields.NumberField({
initial: 0,
integer: true,
required: false,
}),
}),
};
}
function ensureCurrencyIsNumeric(source) {
if (!source.details || !Object.hasOwn(source.details, "currency")) return; // return early in case of update
if (
source.details.currency === null ||
typeof source.details.currency === "number"
)
return;
if (typeof source.details.currency === "string") {
// remove all symbols that aren't numeric or a decimal point
source.details.currency = Number(
source.details.currency.replaceAll(/[^0-9.]/g, "")
);
}
}
function boundTraitDie(die) {
const sides = die.sides;
if (sides < 4 && sides !== 1) {
die.sides = 4;
} else if (sides > 12) {
const difference = sides - 12;
die.sides = 12;
die.modifier += difference / 2;
}
return die;
}
class CharacterData extends foundry.abstract.TypeDataModel {
static defineSchema() {
const fields = foundry.data.fields;
return {
attributes: new fields.SchemaField({
heart: new fields.SchemaField(attributeDiceFields()),
fitness: new fields.SchemaField(attributeDiceFields()),
research: new fields.SchemaField(attributeDiceFields()),
tatics: new fields.SchemaField(attributeDiceFields()),
}),
stats: new fields.SchemaField({
toughness: new fields.SchemaField({
value: new fields.NumberField({ initial: 0, integer: true }),
modifier: new fields.NumberField({
initial: 0,
integer: true,
required: false,
}),
sum: new fields.NumberField({ integer: true }),
}),
types: new fields.SchemaField({
primary: new fields.StringField({ initial: "none" }),
secondary: new fields.StringField({ initial: "none" }),
}),
wounds: new fields.SchemaField({
value: new fields.NumberField({ initial: 0 }),
max: new fields.NumberField({ initial: 3 }),
}),
}),
details: new fields.SchemaField({
calling: new fields.StringField({ initial: "" }),
currency: new fields.NumberField({ initial: 0, integer: true }),
pronouns: new fields.StringField({ initial: "" }),
age: new fields.NumberField({ integer: true }),
}),
setting: new fields.SchemaField({
autoCalcToughness: new fields.BooleanField({ initial: true }),
}),
};
}
static migrateData(source) {
ensureCurrencyIsNumeric(source);
return super.migrateData(source);
}
prepareBaseData() {
for (const key in this.attributes) {
const attribute = this.attributes[key];
attribute.effects = new Array();
}
if (this.setting.autoCalcToughness) {
this.stats.toughness.value = 0;
}
this.stats.globalModifiers = {
heart: new Array(),
fitness: new Array(),
research: new Array(),
tatics: new Array(),
toughness: new Array(),
attack: new Array(),
damage: new Array(),
};
}
prepareDerivedData() {
Object.entries(this.attributes).forEach(([key, attribute]) => {
attribute.die = boundTraitDie(attribute.die);
});
}
}
class ItemData extends foundry.abstract.TypeDataModel {
static defineSchema() {
const fields = foundry.data.fields;
return {
category: new fields.StringField({ initial: "", textSearch: true }),
source: new fields.StringField({initial: "", textSearch: true}),
enrichedTexts:new fields.SchemaField({
description: new fields.HTMLField({initial: "" }),
requirement: new fields.HTMLField({ initial: "" }),
notes: new fields.HTMLField({ initial: "" }),
})
};
}
}
/*Defining ActorDocumments*/
class PokeymanzActor extends Actor {
prepareData() {
super.prepareData();
}
prepareDerivedData() {
const actorData = this;
const systemData = actorData.system;
const flags = actorData.flags.pokeymanz || {};
if (systemData.setting.autoCalcToughness) {
systemData.stats.toughness.value =
systemData.attributes.fitness.die.sides / 2;
}
systemData.stats.toughness.sum =
systemData.stats.toughness.value + systemData.stats.toughness.modifier;
}
getRollData() {
const data = super.getRollData();
const attributes = foundry.utils.deepClone(this.system.attributes);
for (const [key, attribute] of Object.entries(attributes)) {
data.attributes[key].name = game.i18n.localize(
POKEYMANZ.attributes[key].label
);
}
return data;
}
rollAttribute(event, option = {}) {
event.preventDefault();
const data = this.actor.getRollData();
const attribute = event.currentTarget.dataset.attribute;
const label = data.attributes[attribute].name;
const die = data.attributes[attribute].die.sides;
const mod = data.attributes[attribute].die.modifier;
const Mod = mod >= 0 ? `+${mod}` : `${mod}`;
const wounds = `${this.actor.calcWoundPenalties() || ""}`;
const formula = `1d${die}x${Mod}${wounds}[${label}]`;
let roll = new Roll(formula, data);
roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
flavor: game.i18n.format("POKEYMANZ.AttributePromptTitle", {
attr: label,
}),
rollMode: null,
});
return roll;
}
async attributeMenu(event) {
const attributesPath = "system.attributes";
const attributes = deepClone(this.actor.getRollData().attributes);
const content = await renderTemplate(
"systems/pokeymanz/templates/apps/attribute-menu.hbs",
{
actor: this.actor,
attributes: attributes,
}
);
let d = new Dialog(
{
title: game.i18n.format("POKEYMANZ.AttributeMenuTitle"),
content: content,
buttons: {
cancel: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.format("POKEYMANZ.CancelButton"),
},
accept: {
icon: '<i class="fas fa-check"></i>',
label: game.i18n.format("POKEYMANZ.ApplyButton"),
callback: (html) => {
const newData = Object.keys(attributes).reduce((data, att) => {
data[`${attributesPath}.${att}.die.modifier`] = parseInt(
html.find(`input[name="${att}.modifier"]`).val() || 0
);
data[`${attributesPath}.${att}.die.sides`] = html
.find(`select[name="${att}.sides"]`)
.val();
return data;
}, {});
this.actor.update(newData);
},
},
},
default: "cancel",
},
{
classes: ["pokeymanz", "dialog"],
height: 255,
}
);
d.render(true);
}
async statMenu(event) {
const toughnessBolean = this.actor.system.setting.autoCalcToughness;
const toughness = this.actor.system.stats.toughness;
const primary = this.actor.system.stats.types.primary;
const secondary = this.actor.system.stats.types.secondary;
const choices = POKEYMANZ.pokemonTypes;
const content = await renderTemplate(
"systems/pokeymanz/templates/apps/stat-menu.hbs",
{
actor: this.actor,
toughness: toughness,
toughnessBolean: toughnessBolean,
primary: primary,
secondary: secondary,
choices: choices,
}
);
let d = new Dialog(
{
title: game.i18n.format("POKEYMANZ.StatMenuTitle"),
content: content,
buttons: {
cancel: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.format("POKEYMANZ.CancelButton"),
},
accept: {
icon: "<i class fas fa-check></i> ",
label: game.i18n.format("POKEYMANZ.ApplyButton"),
callback: (html) => {
const newData = {};
newData["system.setting.autoCalcToughness"] = html.find(
`input[name="toughnessBolean"]`
)[0].checked;
newData["system.stats.toughness"] = {
value: parseInt(
html.find('input[name="toughness.value"]').val() || 0
),
modifier: parseInt(
html.find('input[name="toughness.modifier"]').val() || 0
),
};
newData["system.stats.types"] = {
primary: html.find(`select[name="primaryType"]`).val(),
secondary: html.find(`select[name="secondaryType"]`).val(),
};
this.actor.update(newData);
},
},
},
default: "cancel",
},
{
classes: ["pokeymanz", "dialog"],
}
);
d.render(true);
}
calcWoundPenalties() {
const wounds = getProperty(this, "system.stats.wounds");
return Math.clamped(wounds.value, 0, wounds.max) * -1;
}
}
class PokeymanzItem extends Item {
prepareData() {
super.prepareData();
}
getRollData() {
if (!this.actor) return null;
const rollData = this.actor.getRollData();
rollData.item = foundry.utils.deepClone(this.system);
return rollData;
}
async _preCreate(data, options, user){
await super._preCreate(data, options, user);
if (!data.img) {
this.updateSource({
img: `systems/pokeymanz/assets/item-img/${data.type}.svg`,
});
}
}
}
/*Defining ActorSheets*/
class CharacterSheet extends ActorSheet {
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
classes: ["pokeymanz", "sheet", "actor"],
width: 600,
height: 600,
resizable: true,
tabs: [
{
group: "primary",
navSelector: ".tabs",
contentSelector: ".sheet-body",
initial: "features",
},
],
});
}
get template() {
const base = "systems/pokeymanz/templates/";
//if(this.actor.limited)return base+'limited.hbs'; //Create limited template later.
return base + "character-sheet.hbs";
}
async getData(options) {
const context = super.getData();
const actorData = this.actor.toObject(false);
context.system = actorData.system;
context.flags = actorData.flags;
return context;
}
activateListeners(html) {
super.activateListeners(html);
/*ATTRIBUTES BUTTON*/
html
.find(".attributes .config-button")
.click(this.actor.attributeMenu.bind(this));
html
.find(".rollable[data-attribute]")
.click(this.actor.rollAttribute.bind(this));
/*STATS BUTTONS*/
html.find(".stats .config-button").click(this.actor.statMenu.bind(this));
/*COUNTERS BUTTONS*/
html.find(".wounds .wounds-button").on("click", (event) => {
const wounds = this.actor.system.stats.wounds;
switch (event.currentTarget.dataset.action) {
case "wounds-plus":
this.actor.update({
"system.stats.wounds.value": Math.min(wounds.max, wounds.value + 1),
});
break;
case "wounds-minus":
this.actor.update({
"system.stats.wounds.value": Math.max(0, wounds.value - 1),
});
break;
}
});
}
}
class PokeymanzItemSheet extends ItemSheet {
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
classes: ["pokeymanz", "sheet", "item"],
width: 605,
height: 480,
});
}
get template() {
return `systems/pokeymanz/templates/items/${this.item.type}-sheet.hbs`;
}
async getData(options) {
const context = await super.getData(options);
const itemData = context.item;
let actor = this.object?.parent;
return foundry.utils.mergeObject(context, {
rollData: actor?.getRollData() ?? null,
system: itemData.system,
flags: itemData.flags,
itemCategory: POKEYMANZ.itemCategories[this.item.type],
itemType: game.i18n.localize(POKEYMANZ.itemTypes[this.item.type]),
enrichedHTML: Object.fromEntries(
await Promise.all(
Object.entries(this.object.system.enrichedTexts).map(
async ([key, val]) => [
key + "HTML",
await TextEditor.enrichHTML(val, { async: true }),
]
)
)
),
});
}
activateListeners(html) {
super.activateListeners(html);
}
}
Hooks.once("init", () => {
/*Registering dataModels*/
Object.assign(CONFIG.Actor.dataModels, {
character: CharacterData,
});
Object.assign(CONFIG.Item.dataModels, {
edge: ItemData,
});
game.pokeymanz = {
PokeymanzActor,
PokeymanzItem,
};
/*Registering ActorDocumments*/
CONFIG.Actor.documentClass = PokeymanzActor;
CONFIG.Item.documentClass = PokeymanzItem;
/*Registering ActorSheets*/
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("Character Sheet", CharacterSheet, {
types: ["character"],
makeDefault: true,
label: "Pokeymanz.CharacterSheet",
});
Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("Item Sheet", PokeymanzItemSheet, {
makeDefault: true,
label: "Pokeymanz.ItemSheet",
});
});