Welcome to part 3 of the series where I port a popular Godot tutorial to Defold
Last time we ended on a low note because I was not quite able to make Defolds standard Tilemaps work so we start this time by "fixing" this and by fixing I mean using LDtk instead
Since neither Defold nor LDtk support each other I simply exported the images in the "Super simple export format" which essentially just renders the map as an image (plus some JSON files), so we took this and just add some sprites in Defold
and now we have a nice tilemap :).
Next step is cleaning up the animation code which sadly does not work quite as nice as in the Godot version just yet, see for instance:
This is partly because Godot is using an animation tree which is a vastly more sophisticated solution than what I currently have set up (press button use animation, reset when no inputs are pressed) so we just make our own!
next we just initialize it in the player controller:
and then just call the update function in update...
aaand we have a somewhat nice way to animate sprites :)
Video 8 - Collisions with the world!
Since we are using an external tilemap this is theoretically a bit harder than just using the internal tilemap which would have supported collisions, I opted to do one fairly quick and dirty solution which is just adding collision shapes manually to where I want them
Although if we would want to do this programmatically we have the locations of the cliffs available via a CSV file and/or some other ways.
I also refrained from investing too much time into setting up the auto tile feature here as LDtk does not seem to play along nicely with tilesets of different tile sizes (Heartbeast uses 32x32 for the cliffs)
Video 9 - Attacking!
First we need to define our states:
and set the initial state to be MOVE in our init function
while we are not in move we do not quite need the current input handling code so we just return early when not moving
Next lets extract the current code inside update into its own function as this is basically already what the move state is supposed to be
now lets also create functions for the other states and also to differentiate between them in update
inside on_input we now want to check if the attack action has been pressed and switch to the attack state if that is true:
If we just play the animation in our attack state we will get hit by our old friend: Calling sprite.play_book multiple times means resetting the animation in Defold which in Godot would do nothing until the animation is finished, to solve this we add a guard called "animation_playing" which we set to true and then after the animation is finished for which Defold offers a nice callback, we unset it and return the state to STATE_MOVE
next to make this work with all directions and our animation tree we had to implement a small hack
we misuse our animation tree a bit here, by changing the state and calling update the animation will change to whatever attack animation fits the current input vector and therefore we get the correct direction.
And thats it for video 9! We can now run around and attack in all directions :)!
Video 10 - Sending messages and instancing things
First lets make some grass!
Next we need to add the grass effect which is a simple new game object with a sprite and a new script which simply plays the animation and then deletes itself again
lastly all we need is a way to spawn and trigger the animation for that we set up a game object with a factory in the level:
and add the following script to the grass game object:
aka for now we destroy all grass when the player hits the attack action!