Welcome to part 4 of the series where I port a popular Godot tutorial to Defold
Video 11 - Attacking things with Hitboxes
First we create a hitbox for the grass and set the group to "hitable" and the mask to "player_hit" and "enemy_hit" to make both players and enemies being able to hit the grass
and also do the same for the player, a hitbox with the group "hitable" and the mask "enemy_hit".
In the video Heartbeast attaches the swords hitbox to the swing of the sword within the animation which is not something we can do in Defold so instead we spawn a small invisible game object while our attack animation is playing which acts as our hit proxy. Like a bullet just that it does not move.
First we add a factory component to the player and call it "sword_hit_factory" which will spawn the new game object
Also need to add it to attack_state:
I created the GO and added a hitbox and a test sprite to see if I actually place it correctly:
Next we attach a script to the sword hit effect that deletes the object again a few frames later that could look like this:
aka we delete the object again within one frame which might be a bit too short but we'll see!
Now its time to add this functionality to the grass, first we remove the old code which handled input on the grass (dont really need that lol) and replace it with an on_message that handles the grass cutting:
The only problem now is that we spawn the object only correctly when facing upwards which we can solve by rotating the image when facing sideways. The final sword hit spawn function should look something like this:
and lastly we remove the red box again :)
Video 12 - They see me rollin'
In this part we are going to finally add the roll state. First we define the roll animations in our animation tree:
and then add a transition to it:
and lastly adding the code into the roll state function which is mostly the same as attack_state with the important difference that we actually set a velocity and update (move) stuff while the animation is playing.
and thats the roll animation
Video 13 - Knockback and the Enemy Bat
First we set up the bat game object:
next we write some code, essentially when the player hits the target it gets knocked back away from the player. In the video Heartbeast chose to use the player direction for the knockback vector we will however calculate the direction from where the player was hit:
and with this we have a bat that can be knocked around!
Video 14 - Enemy Stats
In this part we are going to add HP and damage to the game, lets first add the damage property to the player controller
next we modify the sword hit controller to apply a take_damage message to the hit object via:
Note: We read the damage property here directly from the player controller component
Now we need something that can actually handle the damage for this lets write a health component
If we receive the "take_damage" message we subtract our health by 1 (or the amount given) and enable a 30 frame iframe duration to prevent the game from triggering one damage source multiple times instantly. If no health is remaining we emit a "on_death" message to the current game object.
Now we can add this component to the bat and set the health value to 3
Lastly in the bat controller we handle said on death message by deleting the object:
and with that we conclude video 14.
Video 15 - Enemy Death Effect
Similar to the grass death effect we add a enemy effect spawner game object to the level with a factory referencing to a new game object which will again contain a sprite and a script. Since the code for the script is exactly the same as for grass effect might as well reuse that (and rename it to effect controller I guess).
And lastly we create the animation just before our little friend dies:
Video 16 - Smarter bats
In this part we will start adding AI to the bats.
First off like the last part we will add a new effect (the hit effect) lets start by creating a game object with a factory again and then referencing our hit effect (that we also just created like in the Video 15 part)
First we add a new collision object called detection zone that is a big circle:
and then we add a new component called "can see player" that will utilize this detection zone to help us find the player
and now we extend the bat controller to include the states idle, wander and chase. If we find the player we start chasing him and if we lost him we go back to idle for now:
This means the bat is now chasing us as you can see here:
And with that we have a chasing bat and this also concludes part 4! See you again for part 5 which will probably also be the last part :)