Time to fight – albeit one sided.
Today I’ve completed the hero’s damage giving behaviour. The hero can now kill the enemy. I also setup spawning to test multiple battles in one playtest.
First we need getters and setters to communicate the variable changes between the two objects. This is safer and more controlled than using public (global) variables.
So to the wait state I’ve added two ‘Get FSM Float’ actions that will get the gameobject that was collided with and seek out the FSM that contains the EnemyHealth variable, and the EnemyDefence variable. Both of these variables are stored locally on this FSM using the same variable names. That is the getters setup, after fighting we can use some setters.
Diving back over to where we left off yesterday, lets calculate the damage on our ‘NormalDamage’ state. Here we use a ‘Float Operator’ action so we can store the result (PlayerAttack – EnemyDefence) as a new variable called ‘DamageGiven’ – in code it would be:
var DamageGiven:float;
DamageGiven = (PlayerAttack – EnemyDefence);
Next it uses a ‘Float Subtract’ action (EnemyHealth – DamageGiven) which uses itself to do the calculation – in code it would be :
EnemyHealth -= (EnemyHealth – DamageGiven);
Now we can add the final piece of the puzzle; the setter. For this we use ‘Set FSM Float’ action. Like the getter but in reverse we use the gameObject that was collided with and stored as a variable. Choose its EnemyVitality FSM and drill down to the EnemyHealth variable.
This is our damage sequence set. For our ‘CriticalDamage’ state we just have an added ‘Float Multiply’ action which double the damage before affecting the enemy health.
In the same way that the enemies FSM is constantly checking if it has zero health (and dies) we now need to complete the hero loop by checking if the enemy is still alive. A quick ‘Float Compare’ action which checks against zero health on the EnemyHealth variable is enough. If it is higher is loops back to the ‘Wait’ state and the hero will strike again, if not we can loop back to the ‘WalkingRight’ state.
We are all good to go – playtesting the hero cube will wander over, batter the enemy repeatedly until he has no health and gets destroyed, then he’ll walk on.

As a final piece I’ve setup some spawning which is super easy with PlayMaker. On the camera I’ve created a a Spawn FSM with ‘Get Mouse Button Up’ actions for left and right click – these transition to one state for ‘SpawningHero’ and one for ‘SpawningEnemy’. Both of these have a ‘CreateObject’ action which uses a prefab for the instance gameobject and a reference to an empty gameobject in the scene as the spawn location.
Now clicking will create more heroes or enemies if I need them.
So next I need the enemy to fight back, although I’m tempted to work on some terrible art assets in MS Paint – I’ll see how the next day takes me!