SystemFilter.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 utils = require("utils")
  11. local SystemFilter = class(object)
  12. function SystemFilter:init(required_components)
  13. self.required_components = required_components
  14. self.systems = {}
  15. self.registered_entries = {}
  16. end
  17. --[[
  18. Called by World to register an entity for the systems to process it.
  19. ]]--
  20. function SystemFilter:registerEntry(entity)
  21. local index = #self.registered_entries+1
  22. for i,reg in ipairs(self.registered_entries) do
  23. if not reg then
  24. index = i
  25. break
  26. end
  27. end
  28. self.registered_entries[index] = entity
  29. end
  30. --[[
  31. Called by World to unregister an entity.
  32. ]]--
  33. function SystemFilter:unregisterEntry(entity)
  34. for i,entity in ipairs(self.registered_entries) do
  35. if entity.__eid == entity.__eid then
  36. self.registered_entries[i] = nil
  37. return
  38. end
  39. end
  40. end
  41. function SystemFilter:hasEntity(entity)
  42. for _,current_entity in ipairs(self.registered_entries) do
  43. if current_entity.__eid == entity.__eid then
  44. return true
  45. end
  46. end
  47. return false
  48. end
  49. --[[
  50. Called by World to register a compatible system.
  51. ]]--
  52. function SystemFilter:registerSystem(system)
  53. if not self.systems[system.__sid] then
  54. self.systems[system.__sid] = system
  55. return true
  56. end
  57. return false
  58. end
  59. --[[
  60. Checks if the givent component list matches the current filter's list.
  61. Warning : it requires the list to be sorted for now.
  62. ]]
  63. function SystemFilter:compareComponentList(component_list)
  64. if #component_list ~= #self.required_components then
  65. return false
  66. end
  67. -- Compare every registered system
  68. for j=0,#self.required_components do
  69. if component_list[j] ~= self.required_components[j] then
  70. return false
  71. end
  72. end
  73. return true
  74. end
  75. --[[
  76. Returns true if the entity has all the components for the current filter.
  77. ]]
  78. function SystemFilter:checkEntityCompatibility(entity)
  79. local num_valid_components = 0
  80. for i,component in ipairs(self.required_components) do
  81. if entity:hasComponent(component) then
  82. num_valid_components = num_valid_components + 1
  83. end
  84. end
  85. return num_valid_components == #self.required_components
  86. end
  87. --[[
  88. Called by world to dispatch the update event to the systems.
  89. ]]
  90. function SystemFilter:update(dt)
  91. for j,system in ipairs(self.systems) do
  92. system.update(dt, self.registered_entries)
  93. end
  94. end
  95. return SystemFilter