Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: JimmyD on Tue 19/02/2008 22:07:22

Title: Returning how far someone has moved...
Post by: JimmyD on Tue 19/02/2008 22:07:22
I've been digging through the help and am thus far unable to find information on it. Is there any way to get AGS to measure the distance someone moves as they do?

I'm trying to make a room where the floor is hot, and they can only take so much movement across it before it becomes unbearable and they leave the room. So, basically, counting down an arbitrary figure, which decreases the more they move.

Thanks for the help!
Title: Re: Returning how far someone has moved...
Post by: Pumaman on Tue 19/02/2008 22:10:30
One approach would be to create a Region covering the hot floor, and use the "While standing on region" event to notch up a counter to determine how long they've been on there.
Title: Re: Returning how far someone has moved...
Post by: Radiant on Tue 19/02/2008 23:56:49
Another would be to store the position of the character when he starts moving, and in repeatedly_execute calculate the distance from there to his current position.
Title: Re: Returning how far someone has moved...
Post by: GarageGothic on Wed 20/02/2008 00:00:32
The game context taken into consideration, I think Pumaman's solution (time of exposure) makes more sense than measuring the distance in whatever way. Why shouldn't it also get unbearable for a character standing still on the hot floor?
Title: Re: Returning how far someone has moved...
Post by: JimmyD on Wed 20/02/2008 12:14:05
The region suggestion is good, but in this context Radiant's is more along the lines of what I'm looking for. Can you advise me as to how I'd get it to store the original position and work out how far they've gone? Do excuse my amateurism, I'm fairly new to this coding nonsense.
Title: Re: Returning how far someone has moved...
Post by: Radiant on Wed 20/02/2008 13:31:01
You store the original position in the on_mouse_click () function,


int orig_x, orig_y;
function on_mouse_click (MouseButton button) {
  if (GetCursorMode () == eModeWalkto) {
    orig_x = character[EGO].x;
    orig_y = character[EGO].y;
  }
}


And you calculate the difference in here:

function repeatedly_execute () {
  int dx = character[EGO].x - orig_x;
  int dy = character[EGO].y - orig_y;
  int distance = Maths.Sqrt (dx * dx + dy * dy);
  if (distance > 40) {
    Display ("ouch!");
  }
}
Title: Re: Returning how far someone has moved...
Post by: SSH on Wed 20/02/2008 15:01:15
Quote from: Radiant on Wed 20/02/2008 13:31:01
You store the original position in the on_mouse_click () function,


int orig_x, orig_y;
function on_mouse_click (MouseButton button) {
  if (GetCursorMode () == eModeWalkto) {
    orig_x = character[EGO].x;
    orig_y = character[EGO].y;
  }
}


And you calculate the difference in here:

function repeatedly_execute () {
  int dx = character[EGO].x - orig_x;
  int dy = character[EGO].y - orig_y;
  int distance = Maths.Sqrt (dx * dx + dy * dy);
  if (distance > 40) {
    Display ("ouch!");
  }
}


Or slightly faster:


function repeatedly_execute () {
  int dx = character[EGO].x - orig_x;
  int dy = character[EGO].y - orig_y;
  int distance = (dx * dx + dy * dy);
  if (distance > 1600) {
    Display ("ouch!");
  }
}
Title: Re: Returning how far someone has moved...
Post by: Khris on Wed 20/02/2008 15:45:30
And: "GetCursorMode()" is old code, "mouse.Mode" is the new (current) style. :=
Title: Re: Returning how far someone has moved...
Post by: JimmyD on Thu 21/02/2008 13:55:24
Thanks, that's surprisingly straightforward - it's just the maths that sometimes does my head in. Since I have multiple characters (who may be in this same room/situation), am I correct in thinking that I can substitute character[EGO] for "player"?
Title: Re: Returning how far someone has moved...
Post by: Radiant on Thu 21/02/2008 15:57:42
Quote from: JimmyD on Thu 21/02/2008 13:55:24
am I correct in thinking that I can substitute character[EGO] for "player"?

Yes.