Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void StopMovePlayerTo()
- {
- AddDebugMessage("Stopping movement loop.", false);
- SetLocalVarInt("_MovePlayerTo_stop", 1);
- }
- /**
- * Moves a player smoothly towards a position.
- * @param asEntity The entity or area to move player towards.
- * @param afTime The time in seconds the movement takes.
- * @param asCallback The function to run when complete. Syntax: void MyFunc(string &in asStep)
- */
- void MovePlayerTo(string &in asEntity, float afTime, string &in asCallback)
- {
- AddDebugMessage("MovePlayerTo: Starting towards '" + asEntity + "' in " + afTime + " seconds.", false);
- SetPlayerMoveSpeedMul(0.0f);
- SetPlayerJumpDisabled(true);
- SetLocalVarString("_MovePlayerTo_callback", asCallback);
- SetLocalVarString("_MovePlayerTo_entity", asEntity);
- SetLocalVarInt("_MovePlayerTo_stop", 0);
- SetLocalVarInt("_MovePlayerTo_totalticks", (afTime * 60));
- AddDebugMessage("MovePlayerTo: Total ticks: " + (afTime * 60), false);
- AddTimer("_MovePlayerTo_timeleft", afTime, "");
- _TimerMovePlayerTo(0 + "");
- }
- void _TimerMovePlayerTo(string &in asStep)
- {
- if (GetLocalVarInt("_MovePlayerTo_stop") == 1)
- return;
- float timeLeft = GetTimerTimeLeft("_MovePlayerTo_timeleft");
- int ticksLeft = timeLeft * 60;
- int tick = StringToInt(asStep);
- if (tick == GetLocalVarInt("_MovePlayerTo_totalticks"))
- {
- // Last tick
- SetPlayerMoveSpeedMul(1.0f);
- SetPlayerJumpDisabled(false);
- if (GetLocalVarString("_MovePlayerTo_callback") != "")
- {
- AddTimer(GetLocalVarString("_MovePlayerTo_entity"), 0.0f, GetLocalVarString("_MovePlayerTo_callback"));
- }
- }
- else
- {
- // Calculate distance
- cEntityData player = cEntityData("Player");
- cEntityData entity = cEntityData(GetLocalVarString("_MovePlayerTo_entity"));
- cVector3f distance = player.GetDistanceTo(entity);
- // Find increments
- float x = -distance.x / ticksLeft;
- float y = -distance.y / ticksLeft;
- float z = -distance.z / ticksLeft;
- // Update position
- SetPlayerPos(GetPlayerPosX() + x, GetPlayerPosY() + y, GetPlayerPosZ() + z);
- AddTimer("" + (tick + 1), 0.0f, "_TimerMovePlayerTo");
- }
- }
- class cVector3f
- {
- float x;
- float y;
- float z;
- cVector3f(float x, float y, float z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- }
- class cEntityData
- {
- private
- cVector3f position;
- private
- string name;
- private
- bool isPlayer;
- cEntityData(string &in asEntity)
- {
- name = asEntity;
- if (name == "Player")
- isPlayer = true;
- else
- isPlayer = false;
- this.GetPos();
- }
- string GetName()
- {
- return name;
- }
- cVector3f GetPos()
- {
- position.x = isPlayer ? GetPlayerPosX() : GetEntityPosX(name);
- position.y = isPlayer ? GetPlayerPosY() : GetEntityPosY(name);
- position.z = isPlayer ? GetPlayerPosZ() : GetEntityPosZ(name);
- return position;
- }
- cVector3f GetDistanceTo(cEntityData otherEnt)
- {
- cVector3f p1 = this.GetPos();
- cVector3f p2 = otherEnt.GetPos();
- p1.x = p1.x - p2.x;
- p1.y = p1.y - p2.y;
- p1.z = p1.z - p2.z;
- return p1;
- }
- void PlaceAt(float x, float y, float z)
- {
- if (isPlayer)
- SetPlayerPos(x, y, z);
- else
- SetEntityPos(name, x, y, z);
- AddDebugMessage("[EntityData] Moving '" + name + "' to: " + x + ", " + y + ", " + z, false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement