'...but it's dead!' and my people freeze!

It seems like a lot of extra, but I check both if the attack is ready AND if the target is alive before attacking on each of those if statements.

You have to remember, any given attack can kill the enemy, bringing their health <0, making the next attack a waste of time at best, an attack against a null target at worst. If attacking a null target, you bring back the issue of not being able to perform the function.

You can also break down each step into a function that you can save for later levels.

function castChain(enemy) {
Validate
attack
}

It will increase your overall code size a little, but you’ll be able to create a library of functions that you can use in all of your missions, and it should run a little faster/better because functions should be pre-loaded before the main loop begins, but that can be tested and verified in console.

Then instead of "if enemy: if hero… elif hero… else: … you could simply call the functions in order, knowing that they will self check/validate and if good, attack.

So your main() gets simplified.

2 Likes