main.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. --[[
  2. Just a small test
  3. ]]--
  4. local utils = require("utils")
  5. local class = require("class")
  6. local Component = require("Component")
  7. local World = require("ECSWorld")
  8. local SystemFilter = require("SystemFilter")
  9. local System = require("System")
  10. local world = ECSWorld:new()
  11. local LifeComponent = Component()
  12. LifeComponent.__name = "LifeComponent"
  13. function LifeComponent:init(life, life_max)
  14. self.life = life
  15. self.life_max = life_max
  16. end
  17. local ManaComponent = Component()
  18. ManaComponent.__name = "ManaComponent"
  19. function ManaComponent:init(mana, mana_max)
  20. self.mana = mana
  21. self.mana_max = mana_max
  22. end
  23. local CreatureSystem = System()
  24. function CreatureSystem.update(dt, entities)
  25. for i,entity in ipairs(entities) do
  26. print(string.format(
  27. [[HP : %d/%d
  28. MP : %d/%d]],
  29. entity:getComponent(LifeComponent).life,
  30. entity:getComponent(LifeComponent).life_max,
  31. entity:getComponent(ManaComponent).mana,
  32. entity:getComponent(ManaComponent).mana_max))
  33. end
  34. end
  35. local e = world:createEntity()
  36. world:registerSystem(CreatureSystem, LifeComponent, ManaComponent)
  37. utils.printTable(LifeComponent)
  38. print(LifeComponent)
  39. e:addComponent(LifeComponent, 10, 20)
  40. e:addComponent(ManaComponent, 10, 20)
  41. world:update(0)
  42. e:removeComponent(ManaComponent)
  43. world:update(0)