Completing another Pulp tutorial
Tags: gamedev, beginners, playdate, pulp
Published: 2023-01-27 00:15:48

## 3/3
The last article featured my write-ups as I followed along to two of SquidGod's Playdate tutorials listed as resources on Playdate's official website.

This article is a write-up of the third tutorial, where I'll be shown how to make:
1. Directional player sprites
2. A short melee attack
3. A simple enemy pathfinding AI
4. Lock a room until all enemies are defeated

## Directional player sprites
This seems simple enough!

- Create four Player tiles, one for each facing - really, it's just the eye sockets that move or disappear
- Write an `on update do` event listener
- Leverage the `event.dx` and `event.dy` variable members to determine which direction the player is facing
- Swap for the appropriate tile in each condition

## A short melee attack
- Create four Item tiles, one for each sword facing
- Write an `on confirm do` event listener, which triggers when the `A` button is pressed
- Create two variables that store the eventual position of the sword tile
- Leverage the same `dx` and `dy` variable members to store the appropriate tile name to be used
- Swap the tile, wait a bit, then swap it back to white

### Fix bug: swinging at walls
- Move the sword-swinging code into a custom function
- Store the name of the tile being swung at
- If it is `white` or `enemy`, swing the sword

### Fix bug: don't move during sword swing
- Leverage `ignore` and `listen` functions to essentially disable and enable player input before and after sword swings

## A simple enemy pathfinding AI
In essence:
- At a set interval...
- Determine the position of the target tile...
- Ensure it is available...
- Swap tiles only when available

It takes:
- 3 variables
- 4 conditionals
- 4 event members
- 2 tile swaps

### Unexpected bug
- I made `enemy` a sprite, as the author did
- When playing the game, all enemies instantly moved toward the player
- I checked everything to ensure my code matched the author's
- After several minutes, I tried one last thing: change the tile type from Sprite to Item
- That worked!
- I'm not sure why Sprites and Items behave differently with respect to intervals and the game's `loop` event
- But now I know it's something to be mindful of

### Enemy damage
- Time to duplicate a bunch of code from the previous tutorial
- It was fairly easy to toggle between games and copy code snippets
- It was wonderfully simple to export and import layer tiles
- Just a bummer that the names didn't transfer
- After play testing, my game feels nearly identical
- The only difference seems to be a delay in shake after colliding with an enemy: it happens immediately in the video, whereas in mine it seems delayed

## Lock a room until all enemies are defeated
- I imported the gate sprite from the other game, placed it in the room, and added the custom event to make the gate disappear
- I added `on enter do` event listeners to the `game` and `enemy` scripts
- I set or updated the same variable appropriately in each function
- I updated the `player` script to adjust that variable and trigger the custom event on the `gate` when the variable is 0
- Play testing confirms everything works as expected!

## Conclusion
- That was another really fun tutorial!
- When broken down, all of the code is fairly simple: checking tile type, checking positions, incrementing or decrementing values, calling functions from other functions inside conditionals
- I'm glad I took the time to re-create all the code and play test along the way

Next up - you guessed it - another tutorial!