Pulp how-to: chaser enemies

EDIT: When I posted this I thought there wasn't greater-than/less-than comparisons in PulpScript, but THERE ARE. Thanks to BoldBigflank in Discord who made me aware of this. This is the corrected file using them: Chaser Enemies 2.json.zip (7.9 KB)


Hi! I want to share with you a way to make enemies that move towards the player.

Here is the commented .json file.
Chaser Enemies.json.zip (8.1 KB)

I think everything is explained in there (look at the "chaserCommented" sprite code) so I'll make some notes in this post. These enemies are done for a room with "painted" floor (not only using white tiles). That's why I did two things:

The first thing is to have a "fakeWhite" tile that surrounds the room for preventing the enemies to move to those tiles and that way also prevent them to go out of screen.

The second, and most important, thing is to store the previous tile there was before the enemy moved to that position in a variable called "prevTile". That's why if you want to put a lot of enemies with this code, you can't just use the same sprite (they will be swapping tiles randomly between them). You have to duplicate it and change this variable. For not having to deal with other possible issues I just put a prefix like "ch1_", "ch2_", etc. to all the variables. In the game example there is 5 different enemies so you can just look how is the "copy/pasting" made.

To make an enemy move towards the player we just need to compare the enemy's position and the players position. It would be very easy with greater-than/less-than comparators, but we don't have that in Pulp. But we can replicate it :partying_face:. This is how it's done in the example game for the X coordinates:

compNum = event.x
compNum /= event.px
compNum = floor compNum
		
if compNum==0 then
	destX++
else
	destX--
end

As you can see we just need to make a division of the positions. We use the function "floor" and if it's equal to 0 we know that event.x (enemy position) is smaller than event.px, so we move to the right. Otherwise, if is not 0 we know that the event.x is the same or bigger. I'm not making a triple condition (like adding a special case if the position is the same) because I liked what happened if the enemy is close to the player:
enemyMoving

But anyway it should be something like this:

compNum = event.x
compNum /= event.px
compNum = floor compNum
		
if compNum==0 then
	//code if event.x is smaller
else
	compNum = event.x
	compNum /= event.px
	compNum = ceil compNum
	
	if compNum==1 then
		// code if they are the same
	else
		//code if event.x is bigger
	end
end

And that's the most remarkable things I can think of right now. There is room for a lot of improvement in this example, so if anyone wants to take it and change things and share them here, that would be awesome :smiley:

PS, hope you enjoy the song :stuck_out_tongue:

13 Likes

Before I saw this, I wasn't sure what kind of action games would be possible in pulp.
Fantastic work! Thank you for sharing!

1 Like

I don't know why I thought you couldn't do < > <= => comparisons in PulpScript :sweat_smile: I have edited the beginning of the post to avoid confusing people, but I learned some things from that mistake so I'll leave the rest of the post as it was.

This is how to calculate the enemy's destination for the X coordinate in a normal way:

destX = event.x

if destX < event.px then
	destX++
elseif destX > event.px then
	destX--
end

Much cleaner and obvious :sweat_smile:

3 Likes

Gosh, I know this is a bit old, but you did an amazing job! :sparkles:
I was also wondering if other people had permission to use the song? :playdate_music:
If not, that's perfectly fine you still did an incredible job! :playdate_happy:

Where can I open this file? I'm very new to all of this.

Unzip the json file and you can import it into the Pulp editor (be aware it will overwrite the currently open pulp project)

Got it to work! holy moly, I have no idea how to explain this code. I need more practice.

one other thing, for the player to take damage i had to add ON Interact do command to do so. is that correct?

I just wanted to say thanks. I was able to implement this into my game and it works great!