```lua -- Another chest in the field for the hero to break open! -- Attack the chest to break it open. -- Some munchkins wont stand idly by while you attack it! -- Defend yourself when a munchkin gets too close.
while true do
local enemy = hero:findNearestEnemy()
local distance = hero:distanceTo(enemy)
if hero:isReady(“cleave”) then
– First priority is to cleave if its ready:
elseif distance < 5 then -- Attack the nearest munchkin that gets too close: else -- Otherwise, try to break open the chest: end
<hr> Introduction: Break open a chest while being attacked by groups of munchkins and certain, particularly angry, munchkins. ```lua if condition1 then -- This only happens if condition1 is true. elseif condition2 then -- This only happens if condition1 is false and condition2 is true. else -- This only happens if both condition1 and condition2 are false. end
In this level, the munchkins will periodically attack without the support of their comrades!
cleave to defeat any groups of munchkins that get close, but only use it when it’s off cooldown!
isReady will help with that.
if hero:isReady("cleave") then -- This will only happen when the heros "cleave" is ready to be used. end
Check if munchkins get to close using the
distanceTo method. Remember that
distanceTo finds a
number between the hero and the argument.
Note that the
less-than sign, or
< is only useful at comparing two
hero.findNearestEnemy() returns an enemy, not a number!
hero.isReady() returns a
false value, not a number! Be sure to only use
< when comparing 2 numbers, like
local distance = hero:distanceTo(enemy) if hero:isReady("cleave") then -- elseif is a special term! It tells the hero to not do the next part if the first part was True, or tells them to do the second part if the first part wasnt True. elseif distance < 5 then -- This will only happen when the enemy is closer than 5 meters and cleave isnt ready. end
Finally, if cleave isn’t ready and the nearest munchkin is more than 5 meters away, you’re free to attack the chest!
-- Remember that else only happens when the other if-statements were False. else -- This will only happen when there isnt an enemy closer than 5 meters and cleave isnt ready. end