Catch me if you can!

Started by Digital Mosaic Games, Tue 19/04/2011 14:12:02

Previous topic - Next topic

Khris

Inside the distance function, add "*2.0":

Code: ags
  float dy = IntToFloat(c.y - this.y)*2.0;


In rep_ex, get rid of the first two lines, you don't need them.
Then change the 10.0 to 10. GetDistanceFrom() is an int, thus you can't compare it to a float.

Digital Mosaic Games

#21
Quote from: Khris on Tue 26/04/2011 11:13:40
Inside the distance function, add "*2.0":

Code: ags
  float dy = IntToFloat(c.y - this.y)*2.0;


In rep_ex, get rid of the first two lines, you don't need them.
Then change the 10.0 to 10. GetDistanceFrom() is an int, thus you can't compare it to a float.

Thank you very much! There are no more errors. But one big fault! :( Like in my own tries before everytime when the enemy catches my character(from every direction/angle) there just appears the same animation(my player faces right and is attacked from the enemy who comes from the right). I thought maybe it's because the view of the enemy. So I changed his view to the same like the players. But still the same fault! I set the distance from 10 to 5. I also tried this: player.Solid=false. But no changes.  :-\
I'm not really sure if it is a scripting or maybe a "graphical" fault.  ???

Now this is my Attack script:

Code: ags

if( (angle > 45) && (angle <= 135) ){
    cJohn.FaceLocation(cJohn.x, cJohn.y-100);//UP
    Wait(30);
    cJohn.LockView(36);
    cJohn.ManualScaling=true;
    cJohn.Scaling=100;
    cDog.ChangeRoom(0);
    cJohn.Animate(107, 3, eOnce, eBlock);
 // ( I believe this would be upwards, and anticlockwise from there,)
}
else if( (angle > 135) && (angle <= 225) ){
 cJohn.FaceLocation(cJohn.x-100, cJohn.y);//LEFT
 Wait(30);
 cJohn.LockView(36);
 cJohn.ManualScaling=true;
    cJohn.Scaling=100;
 cDog.ChangeRoom(0);
 cJohn.Animate(105, 3, eOnce, eBlock);
}
else if( (angle > 225) && (angle <= 315) )
 cJohn.FaceLocation(cJohn.x, cJohn.y+100);//DOWN
 Wait(30);
 cJohn.LockView(36);
 cJohn.ManualScaling=true;
    cJohn.Scaling=100;
 cDog.ChangeRoom(0);
 cJohn.Animate(104, 3, eOnce, eBlock);
}
else {
 cJohn.FaceLocation(cJohn.x+100, cJohn.y);//RIGHT
 Wait(30);
 cJohn.LockView(36);
 cJohn.ManualScaling=true;
    cJohn.Scaling=100;
 cDog.ChangeRoom(0);
 cJohn.Animate(106, 3, eOnce, eBlock);
}

Khris

I'll hazard a wild guess: you aren't calculating the angle anywhere => it stays 0 => the else block (right) is executed every time.

You need to try to understand how and why the code we posted does what it does. It's no use if you blindly paste stuff without knowing what you're doing.

Digital Mosaic Games

#23
Quote from: Khris on Tue 26/04/2011 13:07:46
I'll hazard a wild guess: you aren't calculating the angle anywhere => it stays 0 => the else block (right) is executed every time.

You need to try to understand how and why the code we posted does what it does. It's no use if you blindly paste stuff without knowing what you're doing.

I knew that I need an angle function as I mentioned in one of my posts before and I already tried to script that in a kind of way like your distance function. And I know that the angle is something different and in my opinion more difficult to script as the distance. I understand the script but I think I don't know enough to script an angle function. The distance function is simple(the idea): y1-y2 & x1-x2 but the angle function is to compare something with a clock and which time it shows. I know maybe this sounds like cheese but I just want to tell you that I can't put the idea of the angle function in a script.

Here this was my try:

Code: ags
int GetAngleFrom(Character*c) {
   float ax = IntToFloat(c.x);
   float ay = IntToFloat(c.y);
   
   float angle = Maths.RadiansToDegrees(Maths.ArcTan2(ay, ax));
 }

Khris

That's not far off, however you're calculating the angle at (0;0) right now.

To calculate the proper angle, you need to calculate the vector from player to cDog.

This should do it:

Code: ags
int angle;

int GetDistanceFrom(this Character*, Character*c) {

  float dx = IntToFloat(c.x - this.x);
  float dy = IntToFloat(c.y - this.y);

  float dis = Maths.Sqrt(dx*dx + dy*dy);

  angle = FloatToInt(Maths.RadiansToDegrees(Maths.ArcTan2(dy, dx)), eRoundNearest);

  return FloatToInt(dis, eRoundNearest);
}


Now everytime you calculate the distance, the angle is stored in the script's int angle.
Thus as soon as the distance is low enough, you'll automatically have the current angle already stored.

Digital Mosaic Games

Thanks alot man! But I'm sorry that I have to admit that I still have a problem. :-\
At first the animations are now on the right place. It doesn't start with upwards but with right.
No problem but now in the game the enemy ever just attacks my player from 2 sites. Or should I better say there are just two animations which can appear(enemy from RIGHT and DOWN). And if the enemy should normally attack from LEFT he walks down to my player and the same DOWN-animation appears. The same when the enemy shoul usually attack from UP. He goes to the RIGHT and attacks from there. In my opinion the script must usually work. I havent found the fault in it. And believe me I tested a long time and I don't think it's a matter of probability.

Here's my Attack function at the moment:

Code: ags
function Attack()
{
  if( (angle > 45) && (angle <= 135) ){
    cJohn.FaceLocation(cJohn.x-100, cJohn.y);//player faces LEFT
 Wait(30);
 cJohn.LockView(36);
 cJohn.ManualScaling=true;
    cJohn.Scaling=100;
 cDog.ChangeRoom(0);
 cJohn.Animate(106, 3, eOnce, eBlock);//enemy faces RIGHT
 // ( I believe this would be upwards, and anticlockwise from there,)
}
else if( (angle > 135) && (angle <= 225) ){
 cJohn.FaceLocation(cJohn.x, cJohn.y-100);//player faces UP
    Wait(30);
    cJohn.LockView(36);
    cJohn.ManualScaling=true;
    cJohn.Scaling=100;
    cDog.ChangeRoom(0);
    cJohn.Animate(104, 3, eOnce, eBlock);//enemy faces DOWN
}
else if( (angle > 225) && (angle <= 315) ){
  cJohn.FaceLocation(cJohn.x+100, cJohn.y);//player faces RIGHT
 Wait(30);
 cJohn.LockView(36);
 cJohn.ManualScaling=true;
    cJohn.Scaling=100;
 cDog.ChangeRoom(0);
 cJohn.Animate(105, 3, eOnce, eBlock);//enemy faces LEFT
}
else if( (angle > 315) && (angle <= 45) ){
 cJohn.FaceLocation(cJohn.x, cJohn.y+100);//player faces DOWN
 Wait(30);
 cJohn.LockView(36);
 cJohn.ManualScaling=true;
    cJohn.Scaling=100;
 cDog.ChangeRoom(0);
 cJohn.Animate(107, 3, eOnce, eBlock);//enemy faces UP
}
    
//death funciton for player (audio files, restoregame GUIs, etc.)
}

Khris

#26
Quote from: Khris on Tue 19/04/2011 18:37:19
angle = Maths.RadiansToDegrees(Maths.ArcTan2(y/x));
// angle = -180 - 179.9
// e.g. -45 - 45 -> enemy is to the right of player

As you can see, back when I wrote that I told you the possible return values of angle, not 0-360 but -180 to 180, with 0 being to the right and positive being at the top. Scarab did get it wrong first though, but why didn't you use a Display() or something to check the value of angle? This is basic debugging.

Also, look at this:

Code: ags
else if( (angle > 315) && (angle <= 45) ){


It's wrong independently of what value angle is going to have since a number can't be greater than 315 and smaller than 45 at the same time. This also wasn't in the originally posted code, it just said "else".

Btw, there's lots of duplicate code in there.
All that changes depending on the angle is the loop and the direction cJohn's going to face in which should be replaceable by a FaceLocation(dog coords) command.

Consider this:

Code: ags
function Attack() {

  cJohn.FaceLocation(cDog.x, cDog.y); //player faces dog
  Wait(30);
  cJohn.LockView(36);
  cJohn.ManualScaling=true;
  cJohn.Scaling=100;
  cDog.ChangeRoom(0);
  if (angle < -135) angle += 360;
  int l = (angle+135)/90 + 104;  // convert angle to loop
  cJohn.Animate(l, 3, eOnce, eBlock);//enemy faces RIGHT
}


angle is -180 to 179
the if line turns this into -135 to 224 (so we don't have two separate ranges for left (-180 to -135 and 135 to 180)

now:
angle + 135 is 0 to 359
(angle+135)/90 = 0(down), 1(right), 2(up), 3(left)
add 104 and you get 104(down), 105(right), 106(up), 107(left)

Just rearrange the sprites in the loops to reflect that setting and you should be set.

Digital Mosaic Games

Okay! Everything works fine now! Thanks to all especially you Khris! ;)

SMF spam blocked by CleanTalk