Advertisement
Quoteory

Enemy OOP

Dec 5th, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.86 KB | None | 0 0
  1. local ServerScriptService = game:GetService("ServerScriptService")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3.  
  4. local server = ServerScriptService.Server
  5. local serverModules = server.ServerModules
  6. local CharacterUtil = require(serverModules.CharacterUtil)
  7. local EnemyManager = require(serverModules.EnemyManager)
  8.  
  9. local sharedModules = ReplicatedStorage.Shared.SharedModules
  10. local AliveCharacters = require(sharedModules.AliveCharacters)
  11.  
  12.  
  13. local EnemySwordClass = {}
  14. EnemySwordClass.__index = EnemySwordClass
  15.  
  16. function EnemySwordClass.new(newInstance, zone, enemyInfo, spawnLocation)
  17.    local self = setmetatable({
  18.       instance = newInstance,
  19.       humanoid = newInstance.Humanoid,
  20.       walkAnim = nil,
  21.       attackAnim = nil,
  22.       rootPart = newInstance.PrimaryPart,
  23.       sword = enemyInfo.sword:Clone(),
  24.       zone = zone,
  25.       players = zone.players,
  26.       enemies = zone.enemies,
  27.       parent = zone.enemyFolder,
  28.  
  29.       enemyInfo = enemyInfo,
  30.       respawnTime = enemyInfo.respawnTime,
  31.       health = enemyInfo.maxHealth,
  32.       maxHealth = enemyInfo.maxHealth,
  33.       damage = enemyInfo.damage,
  34.       attackRange = enemyInfo.attackRange,
  35.       moveSpeed = enemyInfo.moveSpeed,
  36.       attackDuration = enemyInfo.attackDuration,
  37.       aggroRange = enemyInfo.aggroRange,
  38.       chaseRange = enemyInfo.chaseRange,
  39.       attackers = {},
  40.       spawnLocation = spawnLocation,
  41.  
  42.       target = nil,
  43.       targetPrimary = nil,
  44.       dead = false,
  45.       retreating = false,
  46.       attacking = false,
  47.       active = true,
  48.    }, EnemySwordClass)
  49.    self:Spawn()
  50.    return self
  51. end
  52.  
  53. function EnemySwordClass:InChaseRange()
  54.    return (self.rootPart.Position - self.spawnLocation.Position).magnitude < self.chaseRange
  55. end
  56.  
  57. function EnemySwordClass:InAggroRange(position)
  58.    return (self.rootPart.Position - position).magnitude < self.aggroRange
  59. end
  60.  
  61. function EnemySwordClass:InAttackRange(position)
  62.    return (self.rootPart.Position - position).magnitude <= self.attackRange
  63. end
  64.  
  65. function EnemySwordClass:AttackTarget(humanoid, moveSpeed)
  66.    local damage = self.damage
  67.    local attackDuration = self.attackDuration
  68.    local attackAnim = self.attackAnim
  69.    humanoid.WalkSpeed = 0
  70.    attackAnim:Play()
  71.    attackAnim:AdjustSpeed(attackAnim.Length / attackDuration)
  72.    wait(attackDuration)
  73. end
  74.  
  75. function EnemySwordClass:ChaseTarget(players)
  76.    local humanoid = self.humanoid
  77.    local moveSpeed = self.moveSpeed
  78.    local target = self.target
  79.    local targetPrimary = self.targetPrimary
  80.    local stationary = false
  81.    while self.active and target and targetPrimary and players[target] and AliveCharacters[target] do
  82.  
  83.       local targetPrimaryPosition = targetPrimary.Position
  84.  
  85.       if not self:InChaseRange() then
  86.          self:Retreat()
  87.          break
  88.       end
  89.  
  90.       if self:InAttackRange(targetPrimaryPosition) then
  91.          stationary = true
  92.          self:AttackTarget(humanoid, moveSpeed)
  93.       else
  94.          humanoid:MoveTo(targetPrimaryPosition)
  95.          if stationary then stationary = false humanoid.WalkSpeed = moveSpeed end
  96.       end
  97.       target = self.target
  98.       targetPrimary = self.targetPrimary
  99.       wait()
  100.    end
  101.    self:Retreat()
  102. end
  103.  
  104. function EnemySwordClass:SetTarget(target)
  105.    local targetCharacter = AliveCharacters[target]
  106.  
  107.    if targetCharacter then
  108.       self.target = target
  109.       self.targetPrimary = targetCharacter.PrimaryPart
  110.    else
  111.       self.target = nil
  112.       self.targetPrimary = nil
  113.    end
  114. end
  115.  
  116. function EnemySwordClass:LookForTarget(players)
  117.    for playerName, _ in pairs(players) do
  118.       local character = AliveCharacters[playerName]
  119.       if character and self:InAggroRange(character.PrimaryPart.Position) then
  120.          return playerName
  121.       end
  122.    end
  123. end
  124.  
  125. function EnemySwordClass:LookForTargetLoop()
  126.    local players = self.players
  127.    spawn(function()
  128.       while self.active and not self.dead and not self.retreating do
  129.          local target = self:LookForTarget(players)
  130.          if target then
  131.             self:SetTarget(target)
  132.             self:ChaseTarget(players)
  133.          end
  134.          wait(.1)
  135.       end
  136.    end)
  137. end
  138.  
  139. function EnemySwordClass:Retreat()
  140.    local humanoid = self.humanoid
  141.    local moveSpeed = self.moveSpeed
  142.    local maxHealth = self.maxHealth
  143.    local connection
  144.    self.retreating = true
  145.    self:SetTarget(nil)
  146.  
  147.    connection = humanoid.MoveToFinished:Connect(function(reached)
  148.       -- TODO do actions based on if moveto reached
  149.       if not reached then
  150.          CharacterUtil.TeleportToLocation(self.instance, self.spawnLocation.Position)
  151.          -- teleports enemy if they get stuck on their way back to their spawn
  152.       end
  153.       self.health = maxHealth
  154.       self.retreating = false
  155.       self:LookForTargetLoop()
  156.       connection:Disconnect()
  157.    end)
  158.    humanoid.WalkSpeed = moveSpeed
  159.    humanoid:MoveTo(self.spawnLocation.Position)
  160. end
  161.  
  162. function EnemySwordClass:TakeDamage(playerName, damage)
  163.    local maxHealth = self.maxHealth
  164.    local origHealth = self.health
  165.    local newHealth = (origHealth - damage)
  166.    if newHealth <= 0 then
  167.       self:Die()
  168.    else
  169.       -- TODO set target
  170.       self:SetTarget(playerName)
  171.       self.health = newHealth
  172.    end
  173. end
  174.  
  175. function EnemySwordClass:Die()
  176.    self.dead = true
  177.    self:SetTarget(nil)
  178.    EnemyManager:CreateEnemy(self.zone, self.enemyInfo, self.spawnLocation, self.respawnTime)
  179.    self:Destroy()
  180. end
  181.  
  182. function EnemySwordClass:EquipSword()
  183.    local instance = self.instance
  184.    local sword = self.sword
  185.    sword.Parent = instance
  186.    CharacterUtil.WeldWeaponToHand(instance, sword)
  187. end
  188.  
  189. function EnemySwordClass:LoadAnimations()
  190.    local humanoid = self.humanoid
  191.  
  192.    local walkAnim = Instance.new("Animation")
  193.    walkAnim.AnimationId = "http://www.roblox.com/asset/?id=180426354"
  194.    self.walkAnim = humanoid:LoadAnimation(walkAnim)
  195.  
  196.    local attackAnim = Instance.new("Animation")
  197.    attackAnim.AnimationId = "http://www.roblox.com/asset/?id=4476665450"
  198.    self.attackAnim = humanoid:LoadAnimation(attackAnim)
  199. end
  200.  
  201. function EnemySwordClass:AnimationController()
  202.    local humanoid = self.humanoid
  203.    local walkAnim = self.walkAnim
  204.    humanoid.Running:Connect(function(speed)
  205.       if not walkAnim.IsPlaying and speed > 0 then
  206.          walkAnim:Play()
  207.       elseif walkAnim.IsPlaying and speed <= 0 then
  208.          walkAnim:Stop()
  209.       end
  210.       wait()
  211.    end)
  212. end
  213.  
  214. function EnemySwordClass:Spawn()
  215.    local instance = self.instance
  216.    local humanoid = self.humanoid
  217.    instance.Parent = self.parent
  218.    CharacterUtil.TeleportToLocation(instance, self.spawnLocation.Position)
  219.    self:EquipSword()
  220.    self:LoadAnimations()
  221.    self:AnimationController()
  222.    self:LookForTargetLoop()
  223. end
  224.  
  225. function EnemySwordClass:Destroy()
  226.    local instance = self.instance
  227.    self.active = false
  228.    self.zone.enemies[instance] = nil
  229.    instance:Destroy()
  230. end
  231.  
  232. return EnemySwordClass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement