SystemFilter.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. --[[
  2. ECS SystemFilter class
  3. @author : Eiyeron Fulmincendii
  4. A SystemFilter is just an internal class to avoid multiplying the same requirement list for every System.
  5. Instead of blindly storing the given requirement lists on World's side, this class intends to store all Systems
  6. that share the same component requirement list.
  7. ]]--
  8. local class = require("class")
  9. local object = require("object")
  10. local SystemFilter = class(object)
  11. function SystemFilter:init(required_components)
  12. self.required_components = required_components
  13. self.systems = {}
  14. self.registered_entries = {}
  15. end
  16. --[[
  17. Called by World to register an entity for the systems to process it.
  18. ]]--
  19. function SystemFilter:registerEntry(entity)
  20. local index = #self.registered_entries+1
  21. for i,reg in ipairs(self.registered_entries) do
  22. if not reg then
  23. index = i
  24. break
  25. end
  26. end
  27. self.registered_entries[index] = entity
  28. end
  29. --[[
  30. Called by World to unregister an entity.
  31. ]]--
  32. function SystemFilter:unregisterEntry(entity)
  33. for i,entity in ipairs(self.registered_entries) do
  34. if entity.__eid == entity.__eid then
  35. self.registered_entries[i] = nil
  36. return
  37. end
  38. end
  39. end
  40. function SystemFilter:hasEntity(entity)
  41. for _,current_entity in ipairs(self.registered_entries) do
  42. if current_entity.__eid == entity.__eid then
  43. return true
  44. end
  45. end
  46. return false
  47. end
  48. --[[
  49. Called by World to register a compatible system.
  50. ]]--
  51. function SystemFilter:registerSystem(system)
  52. if not self.systems[system.__sid] then
  53. self.systems[system.__sid] = system
  54. return true
  55. end
  56. return false
  57. end
  58. --[[
  59. Checks if the givent component list matches the current filter's list.
  60. Warning : it requires the list to be sorted for now.
  61. ]]
  62. function SystemFilter:compareComponentList(component_list)
  63. if #component_list ~= #self.required_components then
  64. return false
  65. end
  66. -- Compare every registered system
  67. for j=0,#self.required_components do
  68. if component_list[j] ~= self.required_components[j] then
  69. return false
  70. end
  71. end
  72. return true
  73. end
  74. --[[
  75. Returns true if the entity has all the components for the current filter.
  76. ]]
  77. function SystemFilter:checkEntityCompatibility(entity)
  78. local num_valid_components = 0
  79. for i,component in ipairs(self.required_components) do
  80. if entity:hasComponent(component) then
  81. num_valid_components = num_valid_components + 1
  82. end
  83. end
  84. return num_valid_components == #self.required_components
  85. end
  86. --[[
  87. Called by world to dispatch the update event to the systems.
  88. ]]
  89. function SystemFilter:update(dt)
  90. for j,system in ipairs(self.systems) do
  91. system.update(dt, self.registered_entries)
  92. end
  93. end
  94. return SystemFilter