Advertisement
inkoalawetrust

MVP_MoveTowards

Apr 5th, 2023 (edited)
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | Source Code | 0 0
  1.     //https://pastebin.com/Pge0WJck
  2.     Double LastTurnDir;
  3.     //Originally written by Custodian. Modified by me (inkoalawetrust).
  4.     //Makes the actor move towards the specified actor.
  5.     //Other: The actor to move to.
  6.     //DetourFactor: How long the actor is allowed to move in a direction after hitting an obstacle, before heading straight for the target again.
  7.     //AngleLimit: How much the actor can turn per detour. Used to not have it snap back 180 or anything like that.
  8.     void MVP_MoveTowards (Actor Other, Double DetourFactor = 1.0, Double AngleLimit = 20)
  9.     {
  10.         If (!Other) Return;
  11.         double LastAngle = angle;
  12.         double NextAngle = LastAngle;
  13.  
  14.         //MoveCount is greater than 0 when avoiding an obstacle
  15.         if (MoveCount)
  16.         {
  17.             MoveCount--;
  18.         }
  19.         else
  20.         {
  21.             lastturndir = 0;
  22.             //move directly towards Other
  23.             if (Other) NextAngle = MVP_GetAngleTo(Other,AngleLimit);
  24.         }
  25.  
  26.         //absolute position of next movement
  27.         vector2 NextPos = Vec2Angle(speed, NextAngle);
  28.         bool moved = TryMove(NextPos, 0, false);
  29.  
  30.         //if I hit an obstacle while avoiding another, try moving straight towards Other
  31.         if (!moved && MoveCount)
  32.         {
  33.             MoveCount = Int(random(16,32)*DetourFactor);
  34.             if (Other) NextAngle = MVP_GetAngleTo(Other,AngleLimit);
  35.             NextPos = Vec2Angle(speed, NextAngle);
  36.             moved = TryMove(NextPos, 0, false);
  37.         }
  38.  
  39.         //test movement angles until I find one that works, avoid that obstacle for MoveCount tics
  40.         if (!moved)
  41.         {
  42.             MoveCount = Int(random(16,32)*DetourFactor);
  43.  
  44.             //try moving the same relative direction as last time
  45.             if (lastturndir == 0) lastTurnDir = random(0,1) ? 1.0 : -1.0;
  46.            
  47.             //find viable movement direction
  48.             for (double i = 1; i < 6; i++)
  49.             {
  50.                 NextAngle = LastAngle + (i * 30 * lastturndir);
  51.                 NextPos = Vec2Angle(speed, NextAngle);
  52.                 moved = TryMove(NextPos, 0, false);
  53.                 if (moved) break;
  54.  
  55.                 lastTurnDir *= -1.0;
  56.  
  57.                 NextAngle = LastAngle + (i * 30 * lastturndir);
  58.                 NextPos = Vec2Angle(speed, NextAngle);
  59.                 moved = TryMove(NextPos, 0, false);
  60.                 if (moved) break;
  61.             }
  62.         }
  63.  
  64.         //face movement direction
  65.         angle = Normalize180(NextAngle);
  66.     }
  67.    
  68.     //Gets the angle to the other actor, and also has a limit that can be used to get the angle to the other actor up to a certain point.
  69.     //Basically this is what A_Face()'s max_turn property does, sans the actual angle change on the caller.
  70.     Double MVP_GetAngleTo (Actor Other, Double AngleLimit = 20)
  71.     {
  72.         If (!Other) Return 0;
  73.         If (AngleLimit == 0) Return AngleTo (Other);
  74.         Double AngleTo = AngleTo (Other);
  75.         Double Delta = -DeltaAngle (Angle,AngleTo);
  76.        
  77.         If (AngleLimit != 0)
  78.         {
  79.             If (Delta > 0)
  80.                 Return Angle - AngleLimit;
  81.             Else
  82.                 Return Angle + AngleLimit;
  83.         }
  84.        
  85.         Return 0;
  86.     }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement