Post new topic    
Liquid Metal Slime
Send private message
Problems with a "knockback effect" 
 PostTue Jan 05, 2010 6:30 pm
Send private message Reply with quote
I have something set up that when you press a key and are in range of an enemy, it knocks them back a few steps. It seems to be working properly except for two things:

#1 - If the npc is walking in a direction other than right at you, it can cause them to misalign from tiles, causing their movement to be out of whack (and sometimes cause them to be stuck) RESOLVED

#2 - This script is written intended to only knock back a single npc (and only when it is in range). For some reason though, it will occasionally knock back any npc that is behind and in line with the target npc being knocked back.
RESOLVED
Any suggestions would be much appreciated. Here's some code:

Code:
                if (key is pressed(45)) then (
                        play sound (7,false,false)
                   if (hero direction (0) == up ,and, hero y (0) >= npc y (1) + 1 ,and, hero x (0) == npc x (1)) then (
              walk npc (1,up,2)
           )
           if ( hero direction (0) == down ,and, hero y (0) >= npc y (1) -- 1 ,and, hero x (0) == npc x (1)) then (
              walk npc (1,down,2)
         )
               if ( hero direction (0) == right ,and, hero x (0) >= npc x (1) -- 1 ,and, hero y (0) == npc y (1)) then (
              walk npc (1,right,2)
         )
           if ( hero direction (0) == left ,and, hero x (0) >= npc x (1) + 1 ,and, hero y (0) == npc y (1)) then (
              walk npc (1,left,2)
           )
                       if ( hero direction (0) == up ,and, hero y (0) >= npc y (2) + 1 ,and, hero x (0) == npc x (2)) then (
            walk npc (2,up,2)
         )
           if ( hero direction (0) == down ,and, hero y (0) >= npc y (2) -- 1 ,and, hero x (0) == npc x (2)) then (
            walk npc (2,down,2)
         )
           if ( hero direction (0) == right ,and, hero x (0) >= npc x (2) -- 1 ,and, hero y (0) == npc y (2)) then (
            walk npc (2,right,2)
         )
           if ( hero direction (0) == left ,and, hero x (0) >= npc x (2) + 1 ,and, hero y (0) == npc y (2)) then (
            walk npc (2,left,2)
         )
                       if ( hero direction (0) == up ,and, hero y (0) >= npc y (3) + 1 ,and, hero x (0) == npc x (3)) then (
            walk npc (3,up,2)
         )
           if ( hero direction (0) == down ,and, hero y (0) >= npc y (3) -- 1 ,and, hero x (0) == npc x (3)) then (
              walk npc (3,down,2)
         )
         if ( hero direction (0) == right ,and, hero x (0) >= npc x (3) -- 1 ,and, hero y (0) == npc y (3)) then (
            walk npc (3,right,2)
         )
               if ( hero direction (0) == left ,and, hero x (0) >= npc x (3) + 1 ,and, hero y (0) == npc y (3)) then (
              walk npc (3,left,2)
           )
      )
[/b]
Cornbread Chemist
Liquid Metal Slime
Send private message
 
 PostTue Jan 05, 2010 8:54 pm
Send private message Reply with quote
I don't have any ideas about #2, but I can help with #1, as I had this problem more than once in the past.

James and TMC both informed me once of a great operater called "modulus," which boils numbers down to their rawest denominator. This modulus is what you need to set the NPC back on its proper track.

The trick here is to set the NPC back onto a pixel coordinate that's divisible by 20 (the tile's size, and thus, topleft pixel).

To do this, first figure out which pixel coordinate the npc is standing on (you can convert this to updated syntax, but I'm still more comfortable with old syntax):

Code:
set variable (px,npc pixel x (ref))
set variable (py,npc pixel y (ref))


Then set aside another set of variables to figure out how many pixels the npc is away from the topleft corner. This is where that fancy modulus comes into play:

Code:
set variable (mx,npc pixel x (ref),mod,20)
set variable (my,npc pixel y (ref),mod,20)


And finally, you want to subtract the modulus (leftover) number from the misaligned coordinate and put the npc on the aligned coordinate:

Code:
set variable (px,px--mx)
set variable (py,py--my)
put npc (ref,px,py)


From there, you should have an aligned, functional npc.

Someone else will have to help you with your second issue. Let us know if it works.
Place Obligatory Signature Here
Liquid Metal Slime
Send private message
 
 PostTue Jan 05, 2010 9:19 pm
Send private message Reply with quote
Nice, that solution seems to work great so far V
Cornbread Chemist
Liquid Metal Slime
Send private message
 
 PostWed Jan 13, 2010 7:46 pm
Send private message Reply with quote
It looks like the npcs still get misaligned sometimes. Not sure what the problem would be though. Here's my current script for that:

Code:
if (hero direction (0) == up ,and, hero y (0) >= npc y (20) + 1 ,and, hero x (0) == npc x (20)) then (
           walk npc (20,up,2)
           wait for npc (20)
           px := npc pixel x (20)
           py := npc pixel y (20)
           mx := npc pixel x (20) ,mod, 20
           my := npc pixel y (20) ,mod, 20
           px := px--mx
           py := py--my
           put npc (20,px,py)
           wait for npc (20)
        )
        if ( hero direction (0) == down ,and, hero y (0) >= npc y (20) -- 1 ,and, hero x (0) == npc x (20)) then (
           walk npc (20,down,2)
           wait for npc (20)
           px := npc pixel x (20)
           py := npc pixel y (20)
           mx := npc pixel x (20) ,mod, 20
           my := npc pixel y (20) ,mod, 20
           px := px--mx
           py := py--my
           put npc (20,px,py)
           wait for npc (20)
      )
            if ( hero direction (0) == right ,and, hero x (0) >= npc x (20) -- 1 ,and, hero y (0) == npc y (20)) then (
           walk npc (20,right,2)
           wait for npc (20)
           px := npc pixel x (20)
           py := npc pixel y (20)
           mx := npc pixel x (20) ,mod, 20
           my := npc pixel y (20) ,mod, 20
           px := px--mx
           py := py--my
           put npc (20,px,py)
           wait for npc (20)
      )
        if ( hero direction (0) == left ,and, hero x (0) >= npc x (20) + 1 ,and, hero y (0) == npc y (20)) then (
           walk npc (20,left,2)
           wait for npc (20)
           px := npc pixel x (20)
           py := npc pixel y (20)
           mx := npc pixel x (20) ,mod, 20
           my := npc pixel y (20) ,mod, 20
           px := px--mx
           py := py--my
           put npc (20,px,py)
           wait for npc (20)
        )


Also, it seems to be pushing npcs through walls, and I don't have obstruction or npc walls suspended. Confused
Cornbread Chemist
Super Slime
Send private message
 
 PostWed Jan 13, 2010 7:48 pm
Send private message Reply with quote
Meatballsub wrote:
Also, it seems to be pushing npcs through walls, and I don't have obstruction or npc walls suspended. Confused


You're doing pixel movement and you're not checking walls. Pixel movement cares nothing for obstruction.
Mega Tact v1.1
Super Penguin Chef
Wizard Blocks
Liquid Metal Slime
Send private message
 
 PostWed Jan 13, 2010 8:01 pm
Send private message Reply with quote
Wow, sad that I didn't realize that. I'll add some wall checking in there and see how it goes. The misalignment still remains though (on occasion, that is)
Cornbread Chemist
Display posts from previous: