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 . 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:
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
PS, hope you enjoy the song