Damage knockback woes, losing tile alignment

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

User avatar
Spoonweaver
Liquid Metal King Slime
Posts: 6461
Joined: Mon Dec 08, 2008 7:07 am
Contact:

Post by Spoonweaver »

Why no pixel based movement in this?
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

At this point it's pretty much because I want to use built-in NPC/enemy behavior and not have to code them myself, in order to reduce development time. I also kind of prefer the tile-based movement.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Ohhhh my god it wooooorks! You are the man TMC. I'm too tired to read into the code yet but I just slapped it into the game and it seems to be working just fine. Feeling pretty excited now.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

So I noticed a glitch that happens in a very specific circumstance with the knockback scripts. If an enemy is right next to a solid wall and is facing it, and you walk into the enemy facing the same direction, you'll get launched right through the wall one tile.

Image
(the new GIF recording feature, so damn good)

In my original scripts I made it so that the knockback direction is set to the direction of the attacker. I'm not really sure why this is happening, but thankfully there won't be a lot of instances of this happening in actual in-game scenarios.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Hmm, I took special care to make sure you get knocked back to the left in that case. Maybe you forgot to remove the "player hurtdir := npc direction(ref)" line from "damage player"?
However, that doesn't explain why knockback is pushing the player through a wall. That's pretty concerning.
Ah... I think I can guess why that's happening. The script only checks that the knockback doesn't push an NPC/player through a wall, but allows normal npc/hero movement to continue. The builtin movement only does collision checking at the beginning of every step. So I need to make it check whether the hero/npc is walking, and if so, if it's about to walk through a wall.
Last edited by TMC on Sun Jan 29, 2017 8:23 am, edited 1 time in total.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Oh god, whoops. I totally overlooked that script block with calculate player hurtdir. I just implemented it and it works well, better than before because going through an enemy when you run into them and they're facing away from you was a little wonky anyways.

Thanks again TMC, this is ridiculously awesome.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Alright, self-necroing this thread.

Thanks to Morpheus Kitami I've found out how the whole getting knocked into walls thing is happening, here's a GIF I recorded with infinite health:
Image

As you can see, there are 3 instances where a ridiculously fast knockback 'jump' is occurring. The third one is what knocks the hero into the tree, and the automatic game over feature occurs shortly thereafter.

I'm guessing that the knockback is getting multiplied somehow when the hero is inside of the NPC's space. Here's the script I'm using for player knockback, the first is the "inflict damage" one and the second is the one that gets checked every frame.

Code: Select all

script, damage player, damage, ref, begin
	play sound (2)
	suspend player
	if (get npc id (ref) >= 0) then (calculate player hurtdir npc(ref))	#Checks that the "ref" arg is an NPC
	elseif (slice is valid(ref)) then (calculate player hurtdir slice(ref))	#...or if it's a slice
	if (damage >= 2 && check tag (tag:have best armor)) then (player health := player health -- damage +1)
	else (player health := player health -- damage)
	update player health
	if (player health > 0) then (
		player hurtframe := 8
		player iframes := 30
	)
end

Code: Select all

if (player hurtframe > 0) then (
      switch (player hurtdir) do (
         case (north) knockback player (north, 0, -5)
         case (east) knockback player (east,  5, 0)
         case (south) knockback player (south, 0, 5)
         case (west) knockback player (west, -5, 0)
      )
      decrement (player hurtframe)
      if &#40;player hurtframe <= 0&#41; then &#40;
         resume player
      &#41;
   &#41;
Now, the only thing I changed with TMC's original scripts was adding the separate "calculate player hurtdir" scripts, so that I could use the slice X/Y of a projectile - which is just a slice, it isn't an NPC nor does it have a NPC parent - to calculate knockback. It just replaces "npc pixel x/y" with "slice x/y", that's the only difference.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

The large knockback happens when the player's 20px forward movement is added to the 20px knockback when they're in the same direction. Ignoring the wall-checking problem, are you saying that isn't desirable? The only alternative I can see is not doing knockback in that case. I guess that also potentially could avoid the wall checking problem without actually having to fix it.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Oh, my point was that the huge jumps are the obvious cause of the player getting shoved into walls, so it should be fixed. The normal knockback behavior is just fine. I don't know if there's any way to fix it or not though, the scripts you wrote to make it work didn't make any sense to me but they did work so I went ahead and just implemented them.

Getting rid of knockback this late into development would kind of suck, especially if it can be fixed somehow.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

(Oh, I was wrong, the knockback is 40 pixels, not 20)

I tried doing the same thing as you, giving myself infinite hp and running back and forth on top of monsters. I found that it takes a while for the bug to appear, it's quite rare. Thank goodness for --recordinput! By recording and replaying input I can be sure I fixed the problem (or at least the bug I saw).

BTW, you don't need to separate 'calculate player hurtdir' scripts, just pass the npc slice to the slice-based one.
I would also write "player health -= damage -- 1"

The fixed scripts. Pretty dang verbose: https://hastebin.com/zaloqoruja.hss
Last edited by TMC on Sun Mar 19, 2017 4:51 am, edited 3 times in total.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Woohoo! I enabled infinite health and ran into the purple ghosty monsters for a good 5 minutes straight and nothing bad happened. I do believe you've fixed it.

Also improved my damage player script, per your suggestion:

Code: Select all

script, damage player, damage, sl, begin
	play sound &#40;2&#41;
	suspend player
	calculate player hurtdir &#40;sl&#41;
	##Armor reduces damage by 1 if greater than 1 damage is being dealt
	if &#40;damage >= 2 && check tag &#40;tag&#58;have best armor&#41;&#41; then &#40;decrement &#40;damage&#41;&#41;
	player health -= damage
	update player health
	if &#40;player health > 0&#41; then &#40;
		player hurtframe &#58;= 8
		player iframes &#58;= 30
	&#41;
end
Can't thank you enough for helping with this part of the game coding, I had no idea it would be this troublesome when I first started the project.
Last edited by Foxley on Sun Mar 19, 2017 5:09 pm, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Neither did I :)
Post Reply