Advertisement
Mudbill

MovePlayerTo v2

Jun 18th, 2021
1,302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. void StopMovePlayerTo()
  2. {
  3.     AddDebugMessage("Stopping movement loop.", false);
  4.     SetLocalVarInt("_MovePlayerTo_stop", 1);
  5. }
  6.  
  7. /**
  8.  * Moves a player smoothly towards a position.
  9.  * @param asEntity   The entity or area to move player towards.
  10.  * @param afTime     The time in seconds the movement takes.
  11.  * @param asCallback The function to run when complete. Syntax: void MyFunc(string &in asStep)
  12.  */
  13. void MovePlayerTo(string &in asEntity, float afTime, string &in asCallback)
  14. {
  15.     AddDebugMessage("MovePlayerTo: Starting towards '" + asEntity + "' in " + afTime + " seconds.", false);
  16.     SetPlayerMoveSpeedMul(0.0f);
  17.     SetPlayerJumpDisabled(true);
  18.  
  19.     SetLocalVarString("_MovePlayerTo_callback", asCallback);
  20.     SetLocalVarString("_MovePlayerTo_entity", asEntity);
  21.     SetLocalVarInt("_MovePlayerTo_stop", 0);
  22.     SetLocalVarInt("_MovePlayerTo_totalticks", (afTime * 60));
  23.  
  24.     AddDebugMessage("MovePlayerTo: Total ticks: " + (afTime * 60), false);
  25.     AddTimer("_MovePlayerTo_timeleft", afTime, "");
  26.  
  27.     _TimerMovePlayerTo(0 + "");
  28. }
  29.  
  30. void _TimerMovePlayerTo(string &in asStep)
  31. {
  32.     if (GetLocalVarInt("_MovePlayerTo_stop") == 1)
  33.         return;
  34.  
  35.     float timeLeft = GetTimerTimeLeft("_MovePlayerTo_timeleft");
  36.  
  37.     int ticksLeft = timeLeft * 60;
  38.     int tick = StringToInt(asStep);
  39.  
  40.     if (tick == GetLocalVarInt("_MovePlayerTo_totalticks"))
  41.     {
  42.         // Last tick
  43.         SetPlayerMoveSpeedMul(1.0f);
  44.         SetPlayerJumpDisabled(false);
  45.  
  46.         if (GetLocalVarString("_MovePlayerTo_callback") != "")
  47.         {
  48.             AddTimer(GetLocalVarString("_MovePlayerTo_entity"), 0.0f, GetLocalVarString("_MovePlayerTo_callback"));
  49.         }
  50.     }
  51.     else
  52.     {
  53.         // Calculate distance
  54.         cEntityData player = cEntityData("Player");
  55.         cEntityData entity = cEntityData(GetLocalVarString("_MovePlayerTo_entity"));
  56.         cVector3f distance = player.GetDistanceTo(entity);
  57.  
  58.         // Find increments
  59.         float x = -distance.x / ticksLeft;
  60.         float y = -distance.y / ticksLeft;
  61.         float z = -distance.z / ticksLeft;
  62.  
  63.         // Update position
  64.         SetPlayerPos(GetPlayerPosX() + x, GetPlayerPosY() + y, GetPlayerPosZ() + z);
  65.         AddTimer("" + (tick + 1), 0.0f, "_TimerMovePlayerTo");
  66.     }
  67. }
  68.  
  69. class cVector3f
  70. {
  71.     float x;
  72.     float y;
  73.     float z;
  74.  
  75.     cVector3f(float x, float y, float z)
  76.     {
  77.         this.x = x;
  78.         this.y = y;
  79.         this.z = z;
  80.     }
  81. }
  82.  
  83. class cEntityData
  84. {
  85. private
  86.     cVector3f position;
  87. private
  88.     string name;
  89. private
  90.     bool isPlayer;
  91.  
  92.     cEntityData(string &in asEntity)
  93.     {
  94.         name = asEntity;
  95.  
  96.         if (name == "Player")
  97.             isPlayer = true;
  98.         else
  99.             isPlayer = false;
  100.  
  101.         this.GetPos();
  102.     }
  103.  
  104.     string GetName()
  105.     {
  106.         return name;
  107.     }
  108.  
  109.     cVector3f GetPos()
  110.     {
  111.         position.x = isPlayer ? GetPlayerPosX() : GetEntityPosX(name);
  112.         position.y = isPlayer ? GetPlayerPosY() : GetEntityPosY(name);
  113.         position.z = isPlayer ? GetPlayerPosZ() : GetEntityPosZ(name);
  114.  
  115.         return position;
  116.     }
  117.  
  118.     cVector3f GetDistanceTo(cEntityData otherEnt)
  119.     {
  120.         cVector3f p1 = this.GetPos();
  121.         cVector3f p2 = otherEnt.GetPos();
  122.  
  123.         p1.x = p1.x - p2.x;
  124.         p1.y = p1.y - p2.y;
  125.         p1.z = p1.z - p2.z;
  126.  
  127.         return p1;
  128.     }
  129.  
  130.     void PlaceAt(float x, float y, float z)
  131.     {
  132.         if (isPlayer)
  133.             SetPlayerPos(x, y, z);
  134.         else
  135.             SetEntityPos(name, x, y, z);
  136.         AddDebugMessage("[EntityData] Moving '" + name + "' to: " + x + ", " + y + ", " + z, false);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement