Advertisement
AlewAlow

idfk bruh ill jump off a building

Jul 11th, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. local characterMover1 = CharacterMover.new(character, PathfindingServicePathfinder.new()) -- works
  2. local characterMover2 = CharacterMover.new(character, SimplePathfinder.new()) -- works too
  3. -- and both give different results
  4. -- and it's really easy to add a new pathfinder that works in a different way
  5. -- by just making a class that has the method "FindPath"
  6.  
  7.  
  8. ----------- CHARACTER MOVER
  9.  
  10. local ServerScriptService = game:GetService("ServerScriptService")
  11. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  12.  
  13. local UPDATE_POINTS_RATE = 0.25
  14.  
  15. local CharacterMover = {}
  16. CharacterMover.__index = CharacterMover
  17.  
  18. function CharacterMover.new(character, pathfinder)
  19.     local self = setmetatable({}, CharacterMover)
  20.     self._character = character
  21.     self._pathfinder = pathfinder
  22.    
  23.     self._target = character:GetPivot().Position
  24.    
  25.     self._currentPoints = {}
  26.     self._currentPointIndex = 0
  27.     self._nextUpdatePointsTime = 0
  28.    
  29.     return self
  30. end
  31.  
  32. function CharacterMover:SetTarget(target)
  33.     self._target = target
  34. end
  35.  
  36. function CharacterMover:Update()
  37.     if self._nextUpdatePointsTime <= tick() then
  38.         self._nextUpdatePointsTime = tick() + UPDATE_POINTS_RATE
  39.         self:_updatePoints()
  40.     end
  41.    
  42.     local currentPoint = self._currentPoints[self._currentPointIndex]
  43.     if currentPoint then
  44.         local distance = (self._character:GetPivot().Position - currentPoint.Position).Magnitude
  45.         if distance < 5 then
  46.             self:_moveToNextPoint()
  47.         end
  48.     end
  49. end
  50.  
  51. function CharacterMover:_updatePoints()
  52.     self._currentPointIndex = 0
  53.     self._currentPoints = self._pathfinder:FindPath(
  54.         self._character:GetPivot().Position,
  55.         self._target
  56.     )
  57.    
  58.     self:_moveToNextPoint()
  59. end
  60.  
  61. function CharacterMover:_moveToNextPoint()
  62.     self._currentPointIndex += 1
  63.    
  64.     local point = self._currentPoints[self._currentPointIndex]
  65.     if not point then
  66.         return
  67.     end
  68.  
  69.     if point.Action == Enum.PathWaypointAction.Jump then
  70.         self._character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
  71.     end
  72.    
  73.     self._character.Humanoid:MoveTo(point.Position)
  74. end
  75.  
  76. return CharacterMover
  77.  
  78.  
  79. ------ PATHFINDERS
  80. -----------------
  81. -----------------
  82.  
  83.  
  84. local PathfindingServicePathfinder = {}
  85. PathfindingServicePathfinder.__index = PathfindingServicePathfinder
  86.  
  87. local function serializePathWaypoints(waypoints)
  88.     local points = {}
  89.     for i, waypoint in waypoints do
  90.         points[i] = {
  91.             Position = waypoint.Position,
  92.             Action = waypoint.Action,
  93.         }
  94.     end
  95.    
  96.     return points
  97. end
  98.  
  99.  
  100. function PathfindingServicePathfinder.new(path)
  101.     local self = setmetatable({}, PathfindingServicePathfinder)
  102.     self._path = path
  103.     return self
  104. end
  105.  
  106. function PathfindingServicePathfinder:FindPath(start, target)
  107.     local success, errorMessage = pcall(function()
  108.         self._path:ComputeAsync(start, target)
  109.     end)
  110.  
  111.     if not success then
  112.         warn("An error occured while using PathfindingService: "..errorMessage)
  113.         return {}
  114.     end
  115.    
  116.     local waypoints = self._path:GetWaypoints()
  117.     return serializePathWaypoints(waypoints)
  118. end
  119.  
  120. return PathfindingServicePathfinder
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. local SimplePathfinder = {}
  128. SimplePathfinder.__index = SimplePathfinder
  129.  
  130. local function createPoint(position)
  131.     return {
  132.         Position = position,
  133.         Action = Enum.PathWaypointAction.Walk,
  134.     }
  135. end
  136.  
  137. function SimplePathfinder.new(agentParams)
  138.     return setmetatable({}, SimplePathfinder)
  139. end
  140.  
  141. function SimplePathfinder:FindPath(start, target)
  142.     return {
  143.         createPoint(start),
  144.         createPoint(target),
  145.     }
  146. end
  147.  
  148. return SimplePathfinder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement