grymlahv

workable

Jul 14th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. local function onworkable(self)
  2. if self.maxwork ~= nil and self.workleft < self.maxwork and self.workable then
  3. self.inst:AddTag("workrepairable")
  4. else
  5. self.inst:RemoveTag("workrepairable")
  6. end
  7. if self.action ~= nil then
  8. if self.workleft > 0 and self.workable then
  9. self.inst:AddTag(self.action.id.."_workable")
  10. else
  11. self.inst:RemoveTag(self.action.id.."_workable")
  12. end
  13. end
  14. end
  15.  
  16. local function onaction(self, action, old_action)
  17. if self.workleft > 0 and self.workable then
  18. if old_action ~= nil then
  19. self.inst:RemoveTag(old_action.id.."_workable")
  20. end
  21. if action ~= nil then
  22. self.inst:AddTag(action.id.."_workable")
  23. end
  24. end
  25. end
  26.  
  27. local Workable = Class(function(self, inst)
  28. self.inst = inst
  29. self.onwork = nil
  30. self.onfinish = nil
  31. self.workleft = 10
  32. self.maxwork = -1
  33. self.action = ACTIONS.CHOP
  34. self.savestate = false
  35. self.workable = true
  36. end,
  37. nil,
  38. {
  39. workleft = onworkable,
  40. maxwork = onworkable,
  41. action = onaction,
  42. workable = onworkable,
  43. })
  44.  
  45. function Workable:OnRemoveFromEntity()
  46. self.inst:RemoveTag("workrepairable")
  47. if self.action ~= nil then
  48. self.inst:RemoveTag(self.action.id.."_workable")
  49. end
  50. end
  51.  
  52. function Workable:GetDebugString()
  53. return "workleft: "..tostring(self.workleft)
  54. .." maxwork: "..tostring(self.maxwork)
  55. .." workable: "..tostring(self.workable)
  56. end
  57.  
  58. function Workable:SetWorkAction(act)
  59. self.action = act
  60. end
  61.  
  62. function Workable:GetWorkAction()
  63. return self.action
  64. end
  65.  
  66. function Workable:Destroy(destroyer)
  67. if self:CanBeWorked() then
  68. self:WorkedBy(destroyer, self.workleft)
  69. end
  70. end
  71.  
  72. function Workable:SetWorkable(able)
  73. self.workable = able
  74. end
  75.  
  76. function Workable:SetWorkLeft(work)
  77. self.workable = true
  78. self.workleft = self.maxwork > 0 and math.clamp(work or 10, 1, self.maxwork) or math.max(1, work or 10)
  79. end
  80.  
  81. function Workable:CanBeWorked()
  82. return self.workable and self.workleft > 0
  83. end
  84.  
  85. function Workable:SetOnLoadFn(fn)
  86. if type(fn) == "function" then
  87. self.onloadfn = fn
  88. end
  89. end
  90.  
  91. function Workable:SetMaxWork(work)
  92. self.maxwork = math.max(1, work or 10)
  93. end
  94.  
  95. function Workable:OnSave()
  96. return self.savestate
  97. and {
  98. maxwork = self.maxwork,
  99. workleft = self.workleft,
  100. }
  101. or {}
  102. end
  103.  
  104. function Workable:OnLoad(data)
  105. self.workleft = data.workleft or self.workleft
  106. self.maxwork = data.maxwork or self.maxwork
  107. if self.onloadfn ~= nil then
  108. self.onloadfn(self.inst, data)
  109. end
  110. end
  111.  
  112. function Workable:WorkedBy(worker, numworks)
  113. numworks = numworks or 1
  114. self.workleft = self.workleft - numworks
  115. self.lastworktime = GetTime()
  116.  
  117. worker:PushEvent("working", { target = self.inst })
  118. self.inst:PushEvent("worked", { worker = worker, workleft = self.workleft })
  119.  
  120. if self.onwork ~= nil then
  121. self.onwork(self.inst, worker, self.workleft, numworks)
  122. end
  123.  
  124. if self.workleft <= 0 then
  125. local isplant =
  126. self.inst:HasTag("plant") and
  127. not self.inst:HasTag("burnt") and
  128. not (self.inst.components.diseaseable ~= nil and self.inst.components.diseaseable:IsDiseased())
  129. local pos = isplant and self.inst:GetPosition() or nil
  130.  
  131. if self.onfinish ~= nil then
  132. self.onfinish(self.inst, worker)
  133. end
  134. self.inst:PushEvent("workfinished", { worker = worker })
  135. worker:PushEvent("finishedwork", { target = self.inst, action = self.action })
  136. if isplant then
  137. TheWorld:PushEvent("plantkilled", { doer = worker, pos = pos, workaction = self.action }) --this event is pushed in other places too
  138. end
  139. end
  140. end
  141.  
  142. function Workable:SetOnWorkCallback(fn)
  143. self.onwork = fn
  144. end
  145.  
  146. function Workable:SetOnFinishCallback(fn)
  147. self.onfinish = fn
  148. end
  149.  
  150. return Workable
Advertisement
Add Comment
Please, Sign In to add comment