Losing health in battles

In this section, you calculate the player’s health, which will be lost after each battle. When the player loses all their health, the game is over.

In this section, you will calculate the player’s health, which will be lost after each battle. If the player loses all their health, then the game is over!

Storing the player’s HP

  1. Create a pool to the right of the “Max health” register and label it “Current player health”.
  1. We need to change the state of the “Current player health” whenever the Max health changes. Whenever you need to get the state from a component and “pass” it to another component, you should use State Connections. Create a State Connection and click first in the “Max health” register and last in the “Current player health” pool.
The player health part of the model before a Play: Max health feeds Current player health through an Overwrite state connection, the Hurt connection takes 1 to 51 Health into Take damage, and an Enable while = 0 state connection leads to the Player dies end conditionThe player health part of the model before a Play: Max health feeds Current player health through an Overwrite state connection, the Hurt connection takes 1 to 51 Health into Take damage, and an Enable while = 0 state connection leads to the Player dies end condition
  1. Press “Play” and observe how the “Current player health” pool is updating each time the “Max health” register changes. In this example, Max health reaches 200 after two skill points go to Constitution, and Current player health follows it.
After a Play: Max health reads 200 and Current player health holds 173, after the Hurt connection took 27 Health in the last stepAfter a Play: Max health reads 200 and Current player health holds 173, after the Hurt connection took 27 Health in the last step

Taking Damage

After calculating the player’s health on each step, we need to model the player losing up to 50 HP each minute. We must remove HP from the “Current player health” pool whenever they take damage. When we have to take away or remove resources from a pool, we need to use Sinks.

  1. Click the Sink icon in the toolbar or use the keyboard shortcut “k”.
The toolbar above the canvas with the Sink tool outlined in red.
  1. Click on the canvas below the “Current player health” pool to place the sink in the canvas.
  1. Select the sink, and type “Take damage” into the label at the top of the Properties panel (it reads “Add a label” on a new component).
The Properties panel of a Sink with its name “Take damage” outlined in red.
  1. As the player will take damage every minute, the “Take damage” sink must also be activated automatically every minute. Select the sink, and in the “Properties” panel, under the “Trigger” section, check the Trigger is “Auto” (a new Sink already is). You will see the sink now has a lightning bolt on top.
The Properties panel of the Sink with the five Trigger buttons outlined in red and Auto selected.
  1. It is necessary to transfer the HP from the “Current player health” pool to the “Take damage” sink. Create a new Resource Connection, clicking first on the “Current player health” pool and last on the “Take damage” sink. (You can left-click on the pool’s name and drag it on top of the pool so that it does not get in the way of the connection).
  1. The player has to lose a random number of HP up to 50 points every minute. Select the Resource Connection and set Type to “Dice roll” or “Formula” and use one of the following options:

6a. To throw a 50-sided dice, choose “Dice roll” and write 50 after the D.

The Properties panel of a Resource Connection with Type “dice roll” and 50 after the D, outlined in red.

6b. To get a random integer number between 1 and 50, write “randomInt(1,51)” into the box under Type “Formula”. Functions can also be used in resource connections, and we will use this last method to move forward.

The Properties panel of a Resource Connection with Type “formula” and randomInt(1,51) outlined in red.
  1. Press “Play” and observe how the player loses HP every minute, although it can be increased if they allocate skill points to Constitution.

Detecting when the player dies

To finish this stage of the Level Progression system, we will detect if the player has died, and if they die, end the Play.

  1. Create an End Condition and place it in the canvas to the right of the “Current player health” pool.
  1. With the End Condition selected, type “Player dies” into the label at the top of the Properties panel (it reads “Add a label” on a new component).
The Properties panel of an End Condition with its name “Player dies” outlined in red.
  1. You need to check the state of the player’s health. Create a State Connection by first clicking on the “Current player health” pool and then on the “Player dies” end condition.
  1. We want the Play to finish when the player’s health is equal to 0, and the State Connection is responsible for checking this condition. Select the State Connection and leave Type on “Enable while”, set Formula to “Equal” and type 0 into the box under it. “Less or equal” with 0 would work as well.
The Properties panel of the State Connection: Type “condition”, Formula “equal” and the value 0, outlined in red.
  1. Press “Play” to start the Play again. How many minutes (or steps) went by before the player dies? In this Play the player survived all 20 steps and ended with 169 HP, because two skill points went to Constitution and raised Max health to 200.
The Play bar and Play summary after 20 steps: Constitution points 2, Max health 200, Current player health 169The Play bar and Play summary after 20 steps: Constitution points 2, Max health 200, Current player health 169

If no skill points go to Constitution, Max health stays at 100 and the player can die within a few steps.