9. Items
Going out there without decent gear will cut any adventuring career short. Let's add some basic items for our hero to pick up when he starts his adventure, using the npm run sci command. We'll add a Sword and some LeatherBoots. In tutorial 7, we worked on the hero's equipment slots. Now, we need to indicate which item can be equipped to which slot, if any:
export function Sword() {
return Item({
name: 'Sword',
description: description,
attack: '1d6',
equipmentType: EquipmentType.RightHand,
value: 5,
targetType: TargetType.Enemy
});
}
export function LeatherBoots() {
return Item({
name: 'Leather boots',
defense: 1,
equipmentType: EquipmentType.Feet,
value: 2
});
}
Items have an equipmentType property that takes an enum which has all values aligning with the equipment slots available, or a string value when you're using custom slots. There's also the special EquipmentType.Miscellaneous value that you use to indicate that an item cannot be equipped. An item can have a targetType, which tells StoryScript how this item can be used in combat (a.o. attack an enemy or aid a friend). The value property indicates how much money (whatever currency you use in your game) the item is worth (more on that in the tutorial on trading).
There are also two properties unique to our game: attack and defense. TypeScript will complain that these are not valid properties of an item. We need to let StoryScript know that we want to work with these properties on our game's items. To do that, open the item.ts file in your game's interfaces folder and add the properties:
export interface IItem extends IFeature, StoryScriptIItem {
// Add game-specific item properties here
attack?: string;
damage?: number;
defense?: number;
}
Now that we have our items, we can add them to our starting location. Change your Start.ts like this:
items: [
Sword(),
LeatherBoots()
]
You can pick up these items by clicking them, and you can then equip them or drop them again. If you add a description to the items HTML file, you'll also see a button to view that description.
As you can see in your Item interface definiton, an Item is actually a special kind of Feature. Hence, it can be used in combinations as you saw in tutorial 4. Items are also used by Enemies, and can be traded. Let's look at enemies next.
