Advertisement
Quoteory

WIP OOP RPG Enemy

Nov 26th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.66 KB | None | 0 0
  1. local ServerScriptService = game:GetService("ServerScriptService")
  2. local info = ServerScriptService.Info
  3. local AliveCharacters = require(info.AliveCharacters)
  4.  
  5. local updateTick = 0
  6.  
  7. local Enemy = {}
  8. Enemy.__index = Enemy
  9.  
  10.  
  11.  
  12. function Enemy:Init(enemyInfo, startPosition, zone)
  13.     self.name = enemyInfo.name
  14.     -- stats
  15.     self.health = enemyInfo.MaxHealth
  16.     self.maxHealth = enemyInfo.MaxHealth
  17.     self.respawnTime = enemyInfo.RespawnTime
  18.     --self.moveSpeed = enemyInfo.moveSpeed
  19.     self.spawnRange = enemyInfo.SpawnRange
  20.     self.chaseRange = enemyInfo.ChaseRange
  21.     -- physical parts
  22.     self.model = enemyInfo.enemyModel
  23.     self.instance = nil
  24.     self.humanoid = nil
  25.     self.primaryPart = nil
  26.     -- states
  27.     self.dead = false
  28.     self.retreating = false
  29.     -- combat
  30.     self.target = nil
  31.     self.attackers = {}
  32.     --misc
  33.     self.players = zone.players
  34.    
  35.     self.startPosition = startPosition
  36.     self.hideParent = game.ReplicatedStorage
  37.     self.showParent = game.Workspace
  38.     self.enabled = false
  39. end
  40.  
  41. function Enemy:SetEnabled(bool)
  42.     if bool and not self.enabled then
  43.         print("enemy Enable")
  44.         self.enabled = bool
  45.         self:Start()
  46.     elseif self.enabled then
  47.         print("enemy disable")
  48.         self.enabled = bool
  49.         self:Stop()
  50.     end
  51. end
  52.  
  53.  
  54.  
  55. function Enemy:isInSpawnRange()
  56.     return (self.primaryPart.Position - self.startPosition).magnitude < self.spawnRange
  57. end
  58.  
  59. function Enemy:isInChaseRange(character)
  60.     return (character.HumanoidRootPart.Position - self.primaryPart.Position).magnitude < self.chaseRange
  61. end
  62.  
  63. function Enemy:MoveToTarget()
  64.     self.humanoid:MoveTo(AliveCharacters[self.target].PrimaryPart.Position)
  65. end
  66.  
  67. function Enemy:MoveLoop()
  68.     while self.enabled do
  69.         local target = self.target
  70.        
  71.         if not self:isInSpawnRange() then
  72.             self.target = nil
  73.             self:Retreat()
  74.         end
  75.        
  76.         if not self.dead and not self.retreating then
  77.             if target then
  78.                 if AliveCharacters[target] then
  79.                     self:MoveToTarget(target)
  80.                 else
  81.                     self:Retreat()
  82.                 end
  83.             else
  84.                 self:LookForTarget()
  85.             end
  86.         end
  87.         wait()
  88.     end
  89. end
  90.  
  91. function Enemy:LookForTarget()
  92.     for player, _ in pairs(self.players) do
  93.         local character = AliveCharacters[player]
  94.         if character then
  95.             if self:isInChaseRange(character) then
  96.                 self.target = player
  97.             end
  98.         end
  99.     end    
  100. end
  101.  
  102. --[[
  103. function Enemy:Chase(player)
  104.     local character = AliveCharacters[player]
  105.     if not character or not self:isInChaseRange(character) then return end
  106.    
  107.     self.target = character
  108.     local characterPrimary = character.PrimaryPart
  109.    
  110.     while self.target == character and self.enabled and not self.dead and self:isInSpawnRange() and not self.retreating do
  111.         self.humanoid:MoveTo(characterPrimary.Position)
  112.         character = AliveCharacters[player]
  113.         wait()
  114.     end
  115.    
  116.     if not self:isInSpawnRange() or not self.target then
  117.         self:Retreat()
  118.     end
  119. end
  120.  
  121. -- loops through players in zone for a nearby one and then chases the player
  122. -- only active when the enemy is idle
  123. function Enemy:SearchForTarget()
  124.     while self.enabled and not self.dead and not self.target and not self.retreating do
  125.         for player, _ in pairs(self.players) do
  126.             local character = AliveCharacters[player]
  127.             if character then
  128.                 if self:isInChaseRange(character) then
  129.                     self:Chase(player)
  130.                     print("CHASE")
  131.                 end
  132.             end
  133.         end
  134.         wait()
  135.     end
  136. end
  137. --]]
  138.  
  139. function Enemy:Retreat()
  140.     self.retreating = true
  141.     self.target = nil
  142.     local connection
  143.     self.humanoid:MoveTo(self.startPosition)   
  144.     connection = self.humanoid.MoveToFinished:Connect(function()
  145.         self.retreating = false
  146.         connection:Disconnect()
  147.     end)
  148. end
  149.  
  150. function Enemy:Die()
  151.     if self.dead then return end
  152.     self.dead = true
  153.     self:HideInstance()
  154.     -- respawn
  155.     -- reward and clear attackers
  156.     delay(self.respawnTime, function()
  157.         self.dead = false
  158.         self.health = self.maxHealth
  159.         self:ShowInstance()
  160.     end)
  161. end
  162.  
  163. function Enemy:SetTarget(player)
  164.     local character = AliveCharacters[player]
  165.     if character then
  166.         self.target = player
  167.     end
  168. end
  169.  
  170. function Enemy:TakeDamage(num, player)
  171.     if self.retreating or self.dead or not self.enabled then return end
  172.     self.health = self.health - num
  173.     print(self.health, self.maxHealth)
  174.    
  175.     self:SetTarget(player)
  176.    
  177.     if self.health <= 0 then
  178.         self:Die()
  179.     end
  180. end
  181.  
  182. function Enemy:ShowInstance()
  183.     if not self.enabled then return end
  184.     self.instance.Parent = self.showParent
  185.     self.instance:MoveTo(self.startPosition)
  186. end
  187.  
  188. function Enemy:HideInstance()
  189.     self.instance.Parent = self.hideParent
  190. end
  191.  
  192. function Enemy:CreateInstance()
  193.     local newInstance = self.model:Clone()
  194.     newInstance.Parent = self.hideParent
  195.     self.instance = newInstance
  196.     self.humanoid = newInstance.Humanoid
  197.     self.primaryPart = newInstance.primaryPart
  198.     return newInstance
  199. end
  200.  
  201. return Enemy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement