Add skills as items

This commit is contained in:
mstein
2022-06-07 00:58:21 +02:00
parent ab59b19e56
commit c7f6a38acf
13 changed files with 399 additions and 136 deletions
+19
View File
@@ -0,0 +1,19 @@
export interface M5Skill {
fw: number
attribute: string
}
export interface M5SkillUnlearned extends M5Skill {
initial: number
}
export interface M5SkillLearned extends M5Skill {
skill: string
type: string
}
export interface M5Attribute {
value: number
bonus: number
}
+26 -56
View File
@@ -1,16 +1,4 @@
export interface M5Skill {
label: string
skill: string
attribute: string
fw: number
ew: number
}
export interface M5Attribute {
value: number
bonus: number
}
import { M5Attribute, M5Skill, M5SkillLearned } from "../M5Base"
export interface M5CharacterCalculatedData {
level: number,
@@ -29,10 +17,11 @@ export interface M5CharacterCalculatedData {
enduranceBonus: number
},
skills: {
general: {}
}
}
export default class M5Character extends Actor {
export class M5Character extends Actor {
// constructor(
// data: ConstructorParameters<typeof foundry.documents.BaseActor>[0],
@@ -61,7 +50,6 @@ export default class M5Character extends Actor {
prepareDerivedData() {
const context = (this as any).data;
//console.log(context)
context.data.calc = {
level: 0,
@@ -79,7 +67,9 @@ export default class M5Character extends Actor {
poisonResistance: 0,
enduranceBonus: 0
},
skills: {}
skills: {
general: {}
}
} as M5CharacterCalculatedData
const data = context.data
@@ -98,10 +88,19 @@ export default class M5Character extends Actor {
calc.stats.movementBonus = 0
calc.stats.resistanceMind = calc.stats.defense
calc.stats.resistanceBody = calc.stats.defense + 1
calc.stats.spellCasting = M5Character.spellCastingFromLevel(calc.level) + M5Character.attributeBonus(data.attributes.zt)
calc.stats.spellCasting = (data.info.magicUsing ? M5Character.spellCastingFromLevel(calc.level) : 3) + M5Character.attributeBonus(data.attributes.zt)
calc.stats.brawl = Math.floor((st + gw) / 20)
calc.stats.poisonResistance = 30 + Math.floor(ko / 2)
calc.stats.enduranceBonus = Math.floor(ko/10) + Math.floor(st/20)
//console.log("prepareDerivedData", context)
context.items?.filter(item => item.data.type === "skill").forEach(item => {
calc.skills.general[item.data.name] = {
fw: item.data.data.fw,
attribute: item.data.data.attribute,
id: item.data._id
}
})
}
static readonly levelThreshold: Array<number> = [0, 100, 250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 6000, 7000, 8000, 9000, 10000, 12500, 15000, 17500, 20000, 22500, 25000, 30000, 35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000, 75000, 80000, 85000, 90000, 95000, 100000, 105000, 110000, 115000, 120000, 125000, 130000, 135000, 140000, 145000, 150000, 155000, 160000, 165000, 170000, 175000, 180000, 185000, 190000, 195000, 200000, 205000, 210000, 215000, 220000, 225000, 230000, 235000, 240000, 245000, 250000, 255000, 260000, 265000, 270000, 275000, 280000]
@@ -140,49 +139,20 @@ export default class M5Character extends Actor {
return ret ? ret[1] : M5Character.spellCastingThreshold[M5Character.spellCastingThreshold.length - 1][1]
}
attributeStrength() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.st)
skillBonus(skill: M5Skill, skillName?: string) {
const attribute = this.attribute(skill.attribute)
let ret = attribute ? M5Character.attributeBonus(attribute) : 0
return ret
}
attributeDexterity() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.gs)
skillEw(skill: M5Skill, skillName?: string) {
const bonus = this.skillBonus(skill, skillName)
return skill.fw + bonus
}
attributeAgility() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.gw)
}
attributeConstitution() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.ko)
}
attributeIntelligence() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.in)
}
attributeMagic() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.zt)
}
attributeBeauty() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.au)
}
attributeCharisma() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.pa)
}
attributeWillpower() {
const data = (this as any).data.data
return M5Character.attributeMinMax(data.data.attributes.wk)
attribute(name: string): M5Attribute {
const context = (this as any).data
return context.data.attributes[name]
}
}
+65
View File
@@ -0,0 +1,65 @@
import { M5Character } from "../actors/M5Character"
import { M5Skill } from "../M5Base"
export class M5Item extends Item {
static readonly SKILL = "skill"
getRollData() {
if (!this.actor)
return null
const actor = this.actor as any
const context = (this as any).data
let ret = null
if (context.type === "skill") {
const character = actor as M5Character
const actorData = actor.data.data
const itemData = context.data as M5Skill
ret = {
fw: itemData.fw,
bonus: M5Character.attributeBonus(character.attribute(itemData.attribute))
}
} else {
ret = actor.getRollData()
ret.item = foundry.utils.deepClone(context.data)
}
return ret
}
async roll() {
const item = (this as any).data
// Initialize chat data.
const speaker = ChatMessage.getSpeaker({ actor: this.actor })
const rollMode = (game as Game).settings.get('core', 'rollMode')
const label = `[${item.type}] ${item.name}`
// If there's no roll data, send a chat message.
if (!item.data.formula) {
ChatMessage.create({
speaker: speaker,
rollMode: rollMode,
flavor: label,
content: item.data.description ?? ''
})
} else { // Otherwise, create a roll and send a chat message from it.
// Retrieve roll data.
const rollData = this.getRollData()
// Invoke the roll and submit it to chat.
const roll = new Roll(item.data.formula, rollData)
// If you need to store the value first, uncomment the next line.
// let result = await roll.roll({async: true})
roll.toMessage({
speaker: speaker,
rollMode: rollMode,
flavor: label,
})
return roll
}
}
}
+61 -5
View File
@@ -1,5 +1,7 @@
import Logger from "../../utils/Logger"
import M5Character, { M5Attribute } from "../actors/M5Character"
import { M5Character } from "../actors/M5Character"
import { M5Item } from "../items/M5Item"
import { M5SkillLearned, M5SkillUnlearned } from "../M5Base"
export default class M5CharacterSheet extends ActorSheet {
@@ -27,7 +29,7 @@ export default class M5CharacterSheet extends ActorSheet {
context.actor = actorData;
context.data = actorData.data;
//console.log("Sheet Promise", context)
//console.log("Sheet Promise", context.actor, context.data)
return context
})
}
@@ -35,8 +37,29 @@ export default class M5CharacterSheet extends ActorSheet {
override activateListeners(html: JQuery) {
super.activateListeners(html)
html.find(".roll-button").on("click", async (event) => {
html.find(".edit-skill").on("click", async (event) => {
const row = event.target.parentElement
let skillId = row.dataset["skill"]
const context = this.actor.data
const item = context.items.get(skillId)
item.sheet.render(true)
})
html.find(".roll-learned-button").on("click", async (event) => {
const row = event.target.parentElement.parentElement
let skillId = row.dataset["skill"]
const actor = this.actor as any
const context = this.actor.data
const data = context.data
const item = context.items.get(skillId) as M5Item
await item.roll()
})
html.find(".roll-general-button").on("click", async (event) => {
const row = event.target.parentElement.parentElement
let skillName = row.dataset["skill"]
const actor = this.actor as M5Character
@@ -45,10 +68,10 @@ export default class M5CharacterSheet extends ActorSheet {
const skill = data.skills.general[skillName]
const attribute = data.attributes[skill.attribute]
console.log(skill, attribute)
//console.log(skill, attribute)
const r = new Roll("1d20 + @fw + @bonus", {
fw: skill.fw + 12,
fw: skill.fw,
bonus: M5Character.attributeBonus(attribute)
})
await r.evaluate().then(value => {
@@ -62,5 +85,38 @@ export default class M5CharacterSheet extends ActorSheet {
return ChatMessage.create(chatData)
})
})
html.find(".learn-button").on("click", async (event) => {
const row = event.target.parentElement.parentElement
let skillName = row.dataset["skill"]
const data = this.actor.data.data
const unlearnedSkill = data.skills.general[skillName] as M5SkillUnlearned
const itemData = {
name: (game as Game).i18n.localize("midgard5." + skillName),
type: "skill",
data: {
fw: unlearnedSkill.initial,
attribute: unlearnedSkill.attribute,
skill: skillName,
type: "general"
} as M5SkillLearned
}
this.actor.createEmbeddedDocuments("Item", [itemData]).then(docs => {
const item = docs[0]
//console.log("createEmbeddedDocuments", item.data)
})
})
// Drag & Drop
// const dragDropSkill = new DragDrop({
// dragSelector: ".items-list .item",
// dropSelector: ".sheet-body",
// permissions: { dragstart: this._canDragStart.bind(this), drop: this._canDragDrop.bind(this) },
// callbacks: { dragstart: this._onTransferItemDragStart.bind(this), drop: this._onTransferItemDrop.bind(this) },
// })
// dragDrop.bind(html[0])
}
}
+29 -3
View File
@@ -1,14 +1,40 @@
export default class M5ItemSheet extends ItemSheet {
export class M5ItemSheet extends ItemSheet {
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
width: 530,
height: 340,
classes: ["midgard5", "sheet", "item"]
});
})
}
get template() {
return 'systems/midgard5/templates/sheets/m5Item-Sheet.hbs';
//console.log("M5ItemSheet", this.item.data.type)
const path = "systems/midgard5/templates/sheets/item"
return `${path}/${this.item.data.type}.hbs`
}
override getData(options?: Partial<ItemSheet.Options>): ItemSheet.Data<ItemSheet.Options> | Promise<ItemSheet.Data<ItemSheet.Options>> {
return Promise.resolve(super.getData()).then(value => {
const context = value as any
// Use a safe clone of the item data for further operations.
const itemData = context.item.data
// Retrieve the roll data for TinyMCE editors.
context.rollData = {}
let actor = this.object?.parent ?? null
if (actor) {
context.rollData = actor.getRollData()
}
// Add the actor's data to context.data for easier access, as well as flags.
context.data = itemData.data
context.flags = itemData.flags
return context
})
}
}