86 lines
2.3 KiB
Lua
86 lines
2.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.baseSpeed = speed
|
|
self.speed = speed
|
|
self.alive = true
|
|
self.currentWaypoint = 1
|
|
self.effects = {}
|
|
return self
|
|
end
|
|
|
|
function TrackEnemy:applyEffect(effectName, params)
|
|
self.effects[effectName] = {
|
|
duration = params.duration,
|
|
tick = params.tick,
|
|
value = params.value,
|
|
elapsed = 0
|
|
}
|
|
end
|
|
|
|
function TrackEnemy:updateEffects(dt)
|
|
for name, effect in pairs(self.effects) do
|
|
effect.elapsed = effect.elapsed + dt
|
|
if name == "burn" and effect.tick then
|
|
if effect.elapsed >= effect.tick then
|
|
self:damage(effect.value)
|
|
effect.elapsed = effect.elapsed - effect.tick
|
|
end
|
|
elseif name == "slow" then
|
|
self.speed = self.baseSpeed * effect.value
|
|
elseif name == "freeze" then
|
|
self.speed = 0
|
|
end
|
|
|
|
effect.duration = effect.duration - dt
|
|
if effect.duration <= 0 then
|
|
self.effects[name] = nil
|
|
self.speed = self.baseSpeed
|
|
end
|
|
end
|
|
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)
|
|
self:updateEffects(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
|
|
|