things+stuff

This commit is contained in:
Kim Ravn Hansen
2025-09-10 12:36:42 +02:00
parent 5d0cc61cf9
commit ba293d08b3
39 changed files with 3425 additions and 2713 deletions

View File

@@ -1,12 +1,10 @@
/**
* Item templates are the built-in basic items of the game.
* A character cannot directly own one of these items,
* they can only own CharacterItems, and ItemTemplates can be used to
* generate these CharacterItems.
* Abstract class for documentation purposes.
* @abstract
*/
export class ItemTemplate {
/** @constant @readonly @type {string} Item's machine-friendly name */
id;
export class ItemAttributes {
/** @constant @readonly @type {string} Machine-friendly name for the blueprint */
blueprintId;
/** @constant @readonly @type {string} Item's human-friendly name */
name;
@@ -18,9 +16,9 @@ export class ItemTemplate {
itemSlots;
/** @constant @readonly @type {number?} How much damage (if any) does this item deal */
damage;
baseDamage;
/** @constant @readonly @type {string?} Which special effect is triggered when successfull attacking with this item? */
/** @constant @readonly @type {string?} Which special effect is triggered when successful attacking with this item? */
specialEffect;
/** @constant @readonly @type {boolean?} Can this item be used as a melee weapon? */
@@ -29,84 +27,83 @@ export class ItemTemplate {
/** @constant @readonly @type {boolean?} Can this item be used as a ranged weapon? */
ranged;
/** @readonly @type {number} How many extra HP do you have when oyu wear this armor. */
armorHitPoints;
/** @constant @readonly @type {string?} Type of ammo that this item is, or that this item uses */
ammoType;
/** @readonly @type {number} how much is left in this item. (Potions can have many doses and quivers many arrows) */
count;
/** @readonly @type {number} Some items (quivers) can be replenished, so how much can this quiver/potion/ration pack hold */
maxCount;
/** @constant @readonly @type {string[]} Type of ammo that this item is, or that this item uses */
skills = [];
}
/**
* Item blueprints are the built-in basic items of the game.
* A character cannot directly own one of these items,
* they can only own Items, and ItemBlueprints can be used to
* generate these Items.
*/
export class ItemBlueprint extends ItemAttributes {
/**
* Constructor
*
* @param {string=null} id Item's machine-friendly name.
* @param {string} name. The Item's Name.
* @param {number} itemSlots number of item slots the item takes up in a character's inventory.
* @param {object} o Object whose attributes we copy
*/
constructor(id, name, itemSlots) {
constructor(o) {
super();
if (typeof id !== "string" || id.length < 1) {
throw new Error("id must be a string!");
if (typeof o.blueprintId !== "string" || o.name.length < 1) {
throw new Error("blueprintId must be a string, but " + typeof o.blueprintId + " given.");
}
if (typeof name !== "string" || name.length < 1) {
throw new Error("Name must be a string, but " + typeof name + " given.");
if (typeof o.name !== "string" || o.name.length < 1) {
throw new Error("Name must be a string, but " + typeof o.name + " given.");
}
if (!Number.isFinite(itemSlots)) {
if (!Number.isFinite(o.itemSlots)) {
throw new Error("itemSlots must be a finite number!");
}
this.name = name;
this.id = id;
this.itemSlots = Number(itemSlots);
o.itemSlots = Number(o.itemSlots);
for (const [key, _] of Object.entries(this)) {
if (o[key] !== "undefied") {
this[key] = o[key];
}
}
}
//
// Spawn a new item!
// Spawn a new non-unique item!
/** @returns {Item} */
createItem() {
return new ChracterItem(
this.id,
this.name,
this.description,
this.itemSlots,
);
const item = new Item();
for (const [key, value] of Object.entries(this)) {
item[key] = value;
}
item.blueprintId = this.blueprintId;
return item;
}
}
/**
* Characters can only own CharacterItems.
* An object of this class represents a single instance
* of a given item in the game. It can be a shortsword, or a potion,
* or another, different shortsword that belongs to another character, etc.
*
* If two characters have a short sword, each character has a CharacterItem
* with the name of Shortsword and with the same properties as the orignial Shortsword ItemTemplate.
*
* If a character picks up a Pickaxe in the dungeon, a new CharacterItem is spawned and injected into
* the character's Equipment Map. If the item is dropped/destroyed/sold, the CharacterItem is removed from
* the character's Equipment Map, and then deleted from memory.
*
* If a ChracterItem is traded away to another character, The other character inserts a clone of this item
* into their equipment map, and the item is then deleted from the previous owner's equipment list.
* This is done so we do not have mulltiple characters with pointers to the same item - we would rather risk
* dupes than wonky references.
*
* An added bonus is that the character can alter the name and description of the item.
*
* Another bonus is, that the game can spawn custom items that arent even in the ItemTemplate Set.
* If a character has two identical potions of healing, they are each represented
* by an object of this class.
* The only notable tweak to this rule is collective items like quivers that have
* arrows that are consumed. In this case, each individual arrow is not tracked
* as its own entity, only the quiver is tracked.
*/
export class CharacterItem {
/** @type {ItemTemplate|null} The template that created this item. Null if no such template exists [anymore]. */
itemTemplate; // We use the id instead of a pointer, could make garbage collection better.
/** @type {string} The player's name for this item. */
name;
/** @type {string} The player's description for this item. */
description;
/** @type {number} Number of item slots taken up by this item. */
itemSlots;
constructor(templateItemId, name, description, itemSlots) {
this.templateItemId = templateItemId;
this.name = name;
this.description = description;
this.itemSlots = itemSlots;
}
}
export class Item extends ItemAttributes {}