Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: geork on Sat 13/06/2009 10:42:21

Title: 2 characters moving at same time
Post by: geork on Sat 13/06/2009 10:42:21
 Hey All!
  I've searched the manual and index, but I can't find something on this:
      How does one get 2 characters to move at the same time according to each others movements? I'm trying to achieve a sort of minigame where the NPC is walking in the opposite direction. The eNoBlock doesn't work because my character is constantely moving. Is there a solution?
     Thanks
Title: Re: 2 characters moving at same time
Post by: Akatosh on Sat 13/06/2009 12:36:50
AFAIK, there's no truly easy way to do this. One thing you can do is this:



//on_mouse_click

//stuff

else if (button == eMouseLeft)
{
if ((mouse.Mode==eModeWalkto)&&(player.Room==10)) { //replace 10 with the room the minigame takes place in
cCharacter2.StopMoving(); //Character 2 is the NPC
cCharacter2.Walk(cCharacter2.x - (player.x-mouse.x), cCharacter2.y + (player.y - mouse.y), eNoBlock, eWalkableAreas);}

ProcessClick(mouse.x,mouse.y, mouse.Mode);
}



It's untested, but should get the job done. If it doesn't work, try replacing "mouse.x" with "mouse.x + GetViewportX()" and mouse.y with "mouse.y + GetViewportY()" (I can't test it right now, sorry).
Title: Re: 2 characters moving at same time
Post by: GuyAwesome on Sat 13/06/2009 12:59:36
OK, I spent so long typing this up, Akatosh WAY beat me to it. But anyway:
What exactly are you trying to do?
You should be able to use the room_RepExec function (I'm assuming this is limited to one room - use the global repeatedly_execute and a variable to turn it on/off as needed otherwise) to check the player's loop (for direction) and move the NPC appropriately. Something like

int oldLoop; // top of room script (or global, if needed)

//rest in RepExec
if (cNpc.Moving == false || player.Loop != oldLoop) { // NPC stops, or player changes direction
 oldLoop == player.Loop;
 if (oldLoop == 0) { // player walking downwards
   cNpc.Walk(cNpc.x, cNpc.y-100); // NPC walks upward
 }
 // etc for other loops
}



But becuase your player won't always be walking, e.g., directly left in loop 1, it might be more accurate to capture the player's input (mouse click in Walk mode, or key press, whichever you're using) to get their exact 'direction' and pass in to the NPC, e.g.

//on_mouse_click
if (mouse.Mode == eModeWalkto) {
 int xDiff = (player.x - GetViewportX()) - mouse.x;
 int yDiff = (player.y - GetViewportY()) - mouse.y;
 // GetViewportX/Y compensates for scrolling rooms
 cNpc.Walk(cNpc.x + xDiff, cNpc.y + yDiff);
}

Basically what Akatosh said but with the GetViewportX/Y bits added, and tested :) Does just what I expected - not sure it that's what you actually WANT, though...
The NPC will always move in the exact opposite direction to the player - but if the player moves towards the NPC, the NPC will move towards the player (the opposite of up-left, for example, is down-right; so if the NPC is up-left of the player and they move in that direction, they NPC will move down-right to 'meet' them). If you want the NPC to always walk away for the player, you'd need different code. (That I'm not going to write unless you actually need it :))
Title: Re: 2 characters moving at same time
Post by: geork on Sun 14/06/2009 18:17:57
Yep, it worked.
  Thankyou again