52 lines
1.3 KiB
Lua
52 lines
1.3 KiB
Lua
TrackEnemy = {}
|
|
TrackEnemy.__index = TrackEnemy
|
|
|
|
|
|
function TrackEnemy:new(model, name, health, speed)
|
|
local self = setmetatable({}, TrackEnemy)
|
|
self.model = model
|
|
self.name = name
|
|
self.health = health
|
|
self.speed = speed
|
|
self.alive = true
|
|
self.currentWaypoint = 1
|
|
return self
|
|
end
|
|
|
|
function TrackEnemy:damage(amount)
|
|
self.health = self.health - amount
|
|
if self.health <= 0 then
|
|
self.alive = false
|
|
self:die()
|
|
end
|
|
end
|
|
|
|
function TrackEnemy:die()
|
|
print(self.name .. " has died.")
|
|
if self.model then
|
|
self.model:Destroy()
|
|
end
|
|
end
|
|
|
|
function Trackenemy:moveToWaypoints(waypoints, dt)
|
|
if not self.currentWaypoint then self.currentWaypoint = 1 end
|
|
local target = waypoints[self.currentWaypoint]
|
|
if target then
|
|
local currentPos = self.model.PrimaryPart.Position
|
|
local targetPos = target.Position
|
|
local direction = (targetPos - currentPos).Unit
|
|
local distance = (targetPos - currentPos).Magnitude
|
|
local step = self.speed * dt
|
|
|
|
if distance <= step then
|
|
self.model.SetPrimaryPartCFrame(CFrame.new(targetPos))
|
|
self.currentWaypoint = self.currentWaypoint + 1
|
|
else
|
|
self.model:SetPrimaryPartCFrame(
|
|
CFrame.new(currentPos + direction + step)
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|