want character to move away from playable character if too close (SOLVED)

Started by jfarwyke, Wed 15/04/2009 17:53:24

Previous topic - Next topic

jfarwyke

Thought I could use FolllowCharacter with special values to have character move away instead of follow. Basically, as my playable character approaches the other character, I want the other character to move away. I didn't want to set coordinates though, but instead have him move a few steps away if my playable character gets too close. Is this possible? I've searched the manual and forum to no end. thanks.

Matti

I'd suggest simple checks to determine whether the player character is too close or not. Something like the following:

Code: ags

if (player.x < cBla.x && player.x > cBla.x - 50) cBla.walk (cBla.x+50, cBla.y);
if (player.x > cBla.x && player.x < cBla.x + 50) cBla.walk (cBla.x-50, cBla.y);
if (player.y < cBla.y && player.y > cBla.y - 50) cBla.walk (cBla.x, cBla.y+50);
if (player.y > cBla.y && player.y < cBla.y + 50) cBla.walk (cBla.x, cBla.y-50);


I don't know the situation in your game but I guess you would have to check too if the character is next to a wall / the border of the walking area. In that case he would have to walk around the player character. I'm doing that in my current game, it's an annoying list of coordinate checks, but it's possible.

I don't know if there's an easier way...

Khris

That code won't work as intended (since x & y are handled separately, the y walk will always cancel the x walk).

I'd put this in rep_ex:

Code: ags
  Character*c = cOtherChar;  // replace cOtherChar
  int x = c.x - player.x;
  int y = c.y - player.y;
  int dist = 30;   // walk away if distance is below 30
  if (!c.Moving && x*x + y*y < dist*dist) {
    x = (x*5)/3; y = (y*5)/3;    // move away to a distance of ~50 
    c.Walk(c.x + x, c.y +y);    // walk away directly from player
  }


Tested and more or less working. If the other character is trapped, it will often start moving and turning on the spot.

jfarwyke

Thanks guys! That code does the trick Khris, it looks great, just what I wanted. But let's say I wanted to apply this to the other characters in the room too. I tried using the same code with the other character names, but it wants me to change the variables, x, y, dist....If I change those variables, the code won't work.

Trent R

Copy the code again: (I think I changed all the vars)
Code: ags
  Character*c1 = cOtherChar;  // replace cOtherChar
  int x1 = c1.x - player.x;
  int y1 = c1.y - player.y;
  int dist1 = 30;   // walk away if distance is below 30
  if (!c1.Moving && x1*x1 + y1*y1 < dist1*dist1) {
    x1 = (x1*5)/3; y = (y1*5)/3;    // move away to a distance of ~50 
    c1.Walk(c1.x + x1, c1.y +y1);    // walk away directly from player
  }

  Character*c2 = cAnotherChar;  // replace
  int x2 = c2.x - player.x;
  int y2 = c2.y - player.y;
  int dist2 = 30;   // walk away if distance is below 30
  if (!c2.Moving && x2*x2 + y2*y2 < dist2*dist2) {
    x2 = (x2*5)/3; y = (y2*5)/3;    // move away to a distance of ~50 
    c2.Walk(c2.x + x2, c2.y +y2);    // walk away directly from player
  }



~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

jfarwyke

Wow, thanks man. I should of thought of that. You guys are smart.

Khris

Put this above rep_ex in the global script:
Code: ags
function Flee(this Character*, int dist) {
  if (this.Room != player.Room) return;
  int x = this.x - player.x;
  int y = this.y - player.y;
  if (!this.Moving && x*x + y*y < dist*dist) {
    x = (x*5)/3; y = (y*5)/3;    // move away to a distance of ~50                          Edit: REPLACED dist BY 3 
    this.Walk(this.x + x, this.y + y);    // walk away directly from player
  }
}


Call this inside rep_ex:
Code: ags
  cShyguy1.Flee(30);
  cShyguy2.Flee(30);
  ...


Edit: corrected line

jfarwyke

I can't thank you enough. One day I hope to be able to understand all that code and why it works. I tried the scripts tutorial, but for some reason I can't make sense of variables. But I'll get it sooner or later. Thanks again.

Khris

I can break it down for you:

I have used an extender function, so 'this' represents the current character that's supposed to move away from the player

  if (this.Room != player.Room) return;
If the character isn't in the current (player's) room, exit the function.

  int x = this.x - player.x;
  int y = this.y - player.y;

Create two variables, type int (= integer = whole number), and store the x and y distance to the player in them.

  if (!this.Moving && x*x + y*y < dist*dist) {
If the character currently isn't moving AND the distance to the player is below the submitted distance (30)...

    x = (x*5)/3; y = (y*5)/3;    // move away to a distance of ~50
...increase the length of the vector from player to character by 40%...

    this.Walk(this.x + x, this.y + y);    // walk away directly from player
...and send them on their way



SMF spam blocked by CleanTalk