Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local characterMover1 = CharacterMover.new(character, PathfindingServicePathfinder.new()) -- works
- local characterMover2 = CharacterMover.new(character, SimplePathfinder.new()) -- works too
- -- and both give different results
- -- and it's really easy to add a new pathfinder that works in a different way
- -- by just making a class that has the method "FindPath"
- ----------- CHARACTER MOVER
- local ServerScriptService = game:GetService("ServerScriptService")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local UPDATE_POINTS_RATE = 0.25
- local CharacterMover = {}
- CharacterMover.__index = CharacterMover
- function CharacterMover.new(character, pathfinder)
- local self = setmetatable({}, CharacterMover)
- self._character = character
- self._pathfinder = pathfinder
- self._target = character:GetPivot().Position
- self._currentPoints = {}
- self._currentPointIndex = 0
- self._nextUpdatePointsTime = 0
- return self
- end
- function CharacterMover:SetTarget(target)
- self._target = target
- end
- function CharacterMover:Update()
- if self._nextUpdatePointsTime <= tick() then
- self._nextUpdatePointsTime = tick() + UPDATE_POINTS_RATE
- self:_updatePoints()
- end
- local currentPoint = self._currentPoints[self._currentPointIndex]
- if currentPoint then
- local distance = (self._character:GetPivot().Position - currentPoint.Position).Magnitude
- if distance < 5 then
- self:_moveToNextPoint()
- end
- end
- end
- function CharacterMover:_updatePoints()
- self._currentPointIndex = 0
- self._currentPoints = self._pathfinder:FindPath(
- self._character:GetPivot().Position,
- self._target
- )
- self:_moveToNextPoint()
- end
- function CharacterMover:_moveToNextPoint()
- self._currentPointIndex += 1
- local point = self._currentPoints[self._currentPointIndex]
- if not point then
- return
- end
- if point.Action == Enum.PathWaypointAction.Jump then
- self._character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
- end
- self._character.Humanoid:MoveTo(point.Position)
- end
- return CharacterMover
- ------ PATHFINDERS
- -----------------
- -----------------
- local PathfindingServicePathfinder = {}
- PathfindingServicePathfinder.__index = PathfindingServicePathfinder
- local function serializePathWaypoints(waypoints)
- local points = {}
- for i, waypoint in waypoints do
- points[i] = {
- Position = waypoint.Position,
- Action = waypoint.Action,
- }
- end
- return points
- end
- function PathfindingServicePathfinder.new(path)
- local self = setmetatable({}, PathfindingServicePathfinder)
- self._path = path
- return self
- end
- function PathfindingServicePathfinder:FindPath(start, target)
- local success, errorMessage = pcall(function()
- self._path:ComputeAsync(start, target)
- end)
- if not success then
- warn("An error occured while using PathfindingService: "..errorMessage)
- return {}
- end
- local waypoints = self._path:GetWaypoints()
- return serializePathWaypoints(waypoints)
- end
- return PathfindingServicePathfinder
- local SimplePathfinder = {}
- SimplePathfinder.__index = SimplePathfinder
- local function createPoint(position)
- return {
- Position = position,
- Action = Enum.PathWaypointAction.Walk,
- }
- end
- function SimplePathfinder.new(agentParams)
- return setmetatable({}, SimplePathfinder)
- end
- function SimplePathfinder:FindPath(start, target)
- return {
- createPoint(start),
- createPoint(target),
- }
- end
- return SimplePathfinder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement