Component.lua 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. --[[
  2. ECS Component class
  3. @author : Eiyeron Fulmincendii
  4. A component is a data container for entities. It determines also which systems the holder entity
  5. will be processed on.
  6. ]]--
  7. local class = require("class")
  8. local object = require("object")
  9. local __cid = 0
  10. local component = class(object)
  11. local function eq(a, b) return a.__cid == b.__cid end
  12. local function le(a, b) return a.__cid <= b.__cid end
  13. local function lt(a, b) return a.__cid < b.__cid end
  14. local function make_component(...)
  15. local c = class(component, ...)
  16. __cid = __cid + 1
  17. -- Component unique ID. If you don't want to break everything, avoid touching this.
  18. c.__cid = __cid
  19. -- Override this variable at class creation to have a more explicit tostring result.
  20. c.__name = "Component"
  21. -- Used in SystemFilters
  22. c.__lt = lt
  23. c.__le = le
  24. c.__eq = eq
  25. local mt = getmetatable(c)
  26. -- Used in SystemFilters
  27. mt.__lt = lt
  28. mt.__le = le
  29. mt.__eq = eq
  30. -- Class type functions.
  31. mt.__tostring = function(a) return string.format("{Component class %s (id:%d)}", a.__name, a.__cid) end
  32. return c
  33. end
  34. return make_component