Skip to content

Commit

Permalink
link weapon to ammo
Browse files Browse the repository at this point in the history
  • Loading branch information
Stexinator committed Aug 4, 2024
1 parent 4a0cb38 commit 9cf69db
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
52 changes: 52 additions & 0 deletions script/data/item/ammunitionData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import EquipmentItemData from "./equipmentItemData.js";

const fields = foundry.data.fields;

export default class AmmunitionData extends EquipmentItemData {

static defineSchema() {

const equipmentItemData = super.defineSchema();
return {
// Using destructuring to effectively append our additional data here
...equipmentItemData,
quantity: new fields.NumberField({ initial: 0 }),
effect: new fields.SchemaField({
damage: new fields.SchemaField({
modifier: new fields.NumberField({ initial: 0 }),
type: new fields.StringField({ initial: "impact" })
}),
special: new fields.StringField({ initial: "" }),
penetration: new fields.StringField({ initial: "0" }),
attack: new fields.SchemaField({
modifier: new fields.NumberField({ initial: 0 })
})
}),
weapon: new fields.StringField({ initial: "" }),
weaponId: new fields.StringField({ initial: "" })
};

}

prepareDerivedData() {
super.prepareDerivedData();

this.prepareWeaponFetch();

}

prepareWeaponFetch() {
// We only store a reference to the weapon, here we get the whole item and store it in memory only
// Weapons can only be connected to ammo for actor owned ammo
if (this.parent.actor && this.weaponId !== "") {
this.weaponItem = this.parent.actor.items.get(this.weaponId);
this.weapon = this.weaponItem.name;
}

if (this.parent.actor && this.weaponId === "") {
this.weaponItem = null;
this.weapon = "";
}
}

}
6 changes: 4 additions & 2 deletions script/sheet/weapon.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ export class WeaponSheet extends DarkHeresyItemSheet {

// It has to be ammunition from the same actor
if (item?.type === "ammunition" && item?.actor.uuid === this.item.actor.uuid) {
if (this.item.ammo !== "") {
// remove old ammo
if (this.item.system.ammo !== "") {
let oldAmmo = this.item.actor.items.get(this.item.system.ammo);
oldAmmo.update({ "system.weaponId": "" });
}

item.update({ "system.weaponId": this.item.id });
this.item.update({ "system.ammo": item.id });
}
}
Expand Down
4 changes: 2 additions & 2 deletions template/sheet/actor/tab/gear.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
</div>
<div class="items">
{{#each items.weapons as |item|}}
<div class="gear item">
<div class="flex row" data-item-id="{{item.id}}">
<div class="gear item" data-item-id="{{item.id}}">
<div class="flex row">
<div class="flex name item-edit">
<div class="image-container">
<div class="image" style="background-image: url('{{item.img}}')"></div>
Expand Down

0 comments on commit 9cf69db

Please sign in to comment.