Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ServerScriptService = game:GetService("ServerScriptService")
- local info = ServerScriptService.Info
- local AliveCharacters = require(info.AliveCharacters)
- local updateTick = 0
- local Enemy = {}
- Enemy.__index = Enemy
- function Enemy:Init(enemyInfo, startPosition, zone)
- self.name = enemyInfo.name
- -- stats
- self.health = enemyInfo.MaxHealth
- self.maxHealth = enemyInfo.MaxHealth
- self.respawnTime = enemyInfo.RespawnTime
- --self.moveSpeed = enemyInfo.moveSpeed
- self.spawnRange = enemyInfo.SpawnRange
- self.chaseRange = enemyInfo.ChaseRange
- -- physical parts
- self.model = enemyInfo.enemyModel
- self.instance = nil
- self.humanoid = nil
- self.primaryPart = nil
- -- states
- self.dead = false
- self.retreating = false
- -- combat
- self.target = nil
- self.attackers = {}
- --misc
- self.players = zone.players
- self.startPosition = startPosition
- self.hideParent = game.ReplicatedStorage
- self.showParent = game.Workspace
- self.enabled = false
- end
- function Enemy:SetEnabled(bool)
- if bool and not self.enabled then
- print("enemy Enable")
- self.enabled = bool
- self:Start()
- elseif self.enabled then
- print("enemy disable")
- self.enabled = bool
- self:Stop()
- end
- end
- function Enemy:isInSpawnRange()
- return (self.primaryPart.Position - self.startPosition).magnitude < self.spawnRange
- end
- function Enemy:isInChaseRange(character)
- return (character.HumanoidRootPart.Position - self.primaryPart.Position).magnitude < self.chaseRange
- end
- function Enemy:MoveToTarget()
- self.humanoid:MoveTo(AliveCharacters[self.target].PrimaryPart.Position)
- end
- function Enemy:MoveLoop()
- while self.enabled do
- local target = self.target
- if not self:isInSpawnRange() then
- self.target = nil
- self:Retreat()
- end
- if not self.dead and not self.retreating then
- if target then
- if AliveCharacters[target] then
- self:MoveToTarget(target)
- else
- self:Retreat()
- end
- else
- self:LookForTarget()
- end
- end
- wait()
- end
- end
- function Enemy:LookForTarget()
- for player, _ in pairs(self.players) do
- local character = AliveCharacters[player]
- if character then
- if self:isInChaseRange(character) then
- self.target = player
- end
- end
- end
- end
- --[[
- function Enemy:Chase(player)
- local character = AliveCharacters[player]
- if not character or not self:isInChaseRange(character) then return end
- self.target = character
- local characterPrimary = character.PrimaryPart
- while self.target == character and self.enabled and not self.dead and self:isInSpawnRange() and not self.retreating do
- self.humanoid:MoveTo(characterPrimary.Position)
- character = AliveCharacters[player]
- wait()
- end
- if not self:isInSpawnRange() or not self.target then
- self:Retreat()
- end
- end
- -- loops through players in zone for a nearby one and then chases the player
- -- only active when the enemy is idle
- function Enemy:SearchForTarget()
- while self.enabled and not self.dead and not self.target and not self.retreating do
- for player, _ in pairs(self.players) do
- local character = AliveCharacters[player]
- if character then
- if self:isInChaseRange(character) then
- self:Chase(player)
- print("CHASE")
- end
- end
- end
- wait()
- end
- end
- --]]
- function Enemy:Retreat()
- self.retreating = true
- self.target = nil
- local connection
- self.humanoid:MoveTo(self.startPosition)
- connection = self.humanoid.MoveToFinished:Connect(function()
- self.retreating = false
- connection:Disconnect()
- end)
- end
- function Enemy:Die()
- if self.dead then return end
- self.dead = true
- self:HideInstance()
- -- respawn
- -- reward and clear attackers
- delay(self.respawnTime, function()
- self.dead = false
- self.health = self.maxHealth
- self:ShowInstance()
- end)
- end
- function Enemy:SetTarget(player)
- local character = AliveCharacters[player]
- if character then
- self.target = player
- end
- end
- function Enemy:TakeDamage(num, player)
- if self.retreating or self.dead or not self.enabled then return end
- self.health = self.health - num
- print(self.health, self.maxHealth)
- self:SetTarget(player)
- if self.health <= 0 then
- self:Die()
- end
- end
- function Enemy:ShowInstance()
- if not self.enabled then return end
- self.instance.Parent = self.showParent
- self.instance:MoveTo(self.startPosition)
- end
- function Enemy:HideInstance()
- self.instance.Parent = self.hideParent
- end
- function Enemy:CreateInstance()
- local newInstance = self.model:Clone()
- newInstance.Parent = self.hideParent
- self.instance = newInstance
- self.humanoid = newInstance.Humanoid
- self.primaryPart = newInstance.primaryPart
- return newInstance
- end
- return Enemy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement