Kzisor

bk barbsummon component

Feb 13th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. --Credits: Rezecib (workshop-343762271)
  2.  
  3. local BarbSummoner = Class(function(self, inst)
  4. self.inst = inst
  5. -- self.shouldremove = false
  6. self.barbs = {}
  7. -- self.save_records = {}
  8. end)
  9.  
  10. function BarbSummoner:Add()
  11. print("BarbSummoner:Add")
  12. --self.inst.components.sanity:AddSanityPenalty(barb.GUID, 0.275)
  13.  
  14. local theta = math.random() * 2 * PI
  15. local pt = inst:GetPosition()
  16. local radius = math.random(3, 6)
  17. local offset = FindWalkableOffset(pt, theta, radius, 12, true)
  18. if offset then
  19. local image = SpawnPrefab("bkbarb")
  20. if image == nil then return false end
  21. local pos = pt + offset
  22. image.Transform:SetPosition(pos:Get())
  23. image.components.follower:SetLeader(inst)
  24. image:InheritFromBK(currlevel)
  25. image:ListenForEvent("death", function(inst) self:Remove(inst) end)
  26. table.insert(self.barbs, image)
  27. end
  28.  
  29. return true
  30. end
  31.  
  32. function BarbSummoner:Remove(barb)
  33. print("BarbSummoner:Remove")
  34. for k,v in pairs(self.barbs) do
  35. if barb == v then
  36. --self.inst.components.sanity:RemoveSanityPenalty(barb.GUID, 0.275)
  37. table.remove(self.barbs, k)
  38. end
  39. end
  40. end
  41.  
  42. function BarbSummoner:RemoveAll()
  43. print("BarbSummoner:RemoveAll")
  44. for k,v in pairs(self.barbs) do
  45. -- local save_record = v:GetSaveRecord()
  46. -- table.insert(self.save_records, save_record)
  47. --v:Remove()
  48. table.remove(self.barbs, k)
  49. end
  50. end
  51.  
  52. function BarbSummoner:DoDespawn()
  53. print("BarbSummoner:DoDespawn")
  54. for k,v in pairs(self.barbs) do
  55. -- local save_record = v:GetSaveRecord()
  56. -- table.insert(self.save_records, save_record)
  57. v:Remove()
  58. end
  59. end
  60.  
  61. function BarbSummoner:OnSave()
  62. local barbs = {}
  63. local i = 1
  64. if #self.barbs > 0 then
  65. for k,v in pairs(self.barbs) do
  66. barbs[i] = v:GetSaveRecord()
  67. i = i + 1
  68. end
  69. end
  70. -- if #self.save_records > 0 then
  71. -- for k,v in pairs(self.save_records) do
  72. -- barbs[i] = v:GetSaveRecord()
  73. -- i = i + 1
  74. -- end
  75. -- end
  76. print("BarbSummoner:OnSave", i-1)
  77. -- if self.shouldremove then
  78. -- self:RemoveAll()
  79. -- end
  80. return { barbs = barbs}
  81. end
  82.  
  83. function BarbSummoner:OnLoad(data)
  84. print("BarbSummoner:OnLoad", #data.barbs)
  85. if data.barbs ~= nil and #data.barbs > 0 then
  86. for k,v in pairs(data.barbs) do
  87. local barb = SpawnSaveRecord(v)
  88. if barb.components.follower.leader ~= self.inst then
  89. -- self:Add(barb)
  90. barb.components.follower:SetLeader(self.inst)
  91.  
  92. local currlevel = self.inst.components.leveler:GetLevel(BK_LEVEL_SYSTEM) or 1
  93. barb:InheritFromBK(currlevel)
  94. print("BarbSummoner:OnLoad:InheritFromBK. currlevel="..currlevel)
  95. end
  96. end
  97. end
  98. end
  99.  
  100. function BarbSummoner:GetBarbCount()
  101. --return #self.barbs or 0
  102.  
  103. local count = 0
  104. for _ in pairs(self.barbs) do count = count + 1 end
  105. return count
  106. end
  107.  
  108. return BarbSummoner
Add Comment
Please, Sign In to add comment