Treasure and Experience
- seanmcgarry65
- Mar 10, 2023
- 2 min read
This week, I added a boomerang weapon for the player, treasure chests, and a leveling system to the adventure game.

The first item added is the boomerang. When thrown, it strikes an enemy at medium range before returning to the player.

When the player uses the boomerang while the variable indicating that the boomerang is in use is set to false, the game sets that variable to true, spawns a boomerang bullet, and plays a sound.
The angle of motion of the boomerang is then set to match the direction that the player is facing.

Once the boomerang has hit an enemy or reached its maximum range, it continually reorients itself towards the player. Once it is actually overlapping the player, the boomerang is destroyed and the variable indicating that it is in use is set to false again, allowing it to be thrown once more.
Next is the treasure chest. When the player opens one, items are added to their inventory.

When the player attacks the chest while the chest is closed, the animation of the chest is set to open and the variable that determines if it's closed or not is set to true.
At this point, the game will add items to the player's inventory based on a variable that determines what was in the chest. These items are added exactly once, so the player can't get multiple rewards from the same chest. At the moment, there are three potential rewards; rupees, bombs, and potions.

When the player opens a chest, a text box pops up telling the player what they got.

(This is an updated version of the previous snippet of code.)
The text box is activated with the same code that opens the chest. In fact, the text box is always present, but opening the chest makes the box and the text visible, while also pausing the game. The words in the text box are changed based on the contents of the chest.

While the text box is on screen, pressing Z makes it invisible again, and unpauses the game.
Finally, there is now a leveling system. When the player kills enemies, they gain experience, and once they have enough experience they level up, which allows them to deal and take more damage.

When the game starts, an AJAX object requests the LevelUp JSON file. When the system detects that a 'LevelUp' is completed, it loads the next string of data from the JSON file. When the player's experience is greater than the amount needed to level up, their level counter goes up by one, their max health is increased by 10, 10 points are added to their HP, and their sword does 10 additional damage. There isn't really a point to leveling up more than once, as no enemy currently implemented has more than 20 hp, but the system as implemented goes as high as Level 10.

The code that controls when an enemy dies has been tweaked so that once they die, the player gains experience.
If you would like to play the game in its current state, you can do so by following the link below:
Comments