For something like this, it’s best to either use a generic object or an array of generic objects to represent each creature. I’ll show you how to do it using a generic object.
First, you’ll want to create a variable to keep track of everyone in your StoryInit passage like this:
<<set $characters = {}>>
That makes the $characters
variable an empty object.
You can then add characters and all of their properties to it like this:
<<set $characters.player = { name: "Steve", hp: 10, mp: 20, dmg: 2 }>>
You can add other properties to a character later on like this:
<<set $characters.player.poisoned = 2>>
So that could indicate that the player takes poison damage for the next two rounds.
If the player was healed, then you can remove that status effect by doing this:
<<set delete $characters.player.poisoned>>
and that will delete the “poisoned” property from the player.
Note that each character and property needs to be a unique identifier (such as “player”). You can’t have two creatures that both have the identifier of “orc”, so you’d need to call them “orc1” and “orc2”, or something similar. You’ll also probably want to add a property for enemies, so that your code can tell them apart. Something like:
<<set $characters.orc1 = { name: "Fat Orc", hp: 5, mp: 0, dmg: 2, enemy: true }>>
With that, in your code you could check to see if a character has a .enemy
property set to true
to determine if it’s an enemy or not.
Now, within your game loop, when a turn ends, you could check everyone for poison damage like this:
<<nobr>>
<<for _char range $characters>>
/* Check for poison damage. */
<<if def _char.poisoned>>
_char.name takes 1 point of poison damage!<br>
<<set _char.hp -= 1>>
<<set _char.poisoned -= 1>>
<<if _char.poisoned = 0>>
<<set delete _char.poisoned>>
_char.name is no longer poisoned.<br>
<</if>>
<</if>>
<</for>>
<</nobr>>
That checks each character, and if the .poisoned
property is defined on that character, then it applies the poison effect, reduces the poison counter, and deletes the .poisoned
property when it reaches 0
.
You can do other effects in similar ways.
Please let me know if you have any questions on the above.
Hope that helps!