World.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. -- The world modules is a container for Entity objects
  2. -- call each table member's funcname field, if it's a function, with the supplied args
  3. local function ForEachCall(table, funcname, ...)
  4. for _,v in pairs(table) do
  5. if type(v[funcname]) == "function" then
  6. v[funcname]( ... )
  7. end
  8. end
  9. end
  10. local World = {}
  11. World.Entities = {}
  12. function World:Init()
  13. end
  14. function World:Start(moduleManager)
  15. end
  16. -- Type checking is for losers
  17. function World:AddEntity(entity)
  18. self.Entities[entity] = entity
  19. entity:Create(World)
  20. end
  21. function World:RemoveEntity(entity) -- removes entity from world
  22. entity:Removing()
  23. self.Entities[entity] = nil
  24. end
  25. function World:GetEntity(entity)
  26. return self.Entities[entity]
  27. end
  28. function World:GetEntities()
  29. return self.Entities
  30. end
  31. function World:PreStep(time,dt)
  32. ForEachCall(self.Entities, "PreStep", time, dt)
  33. end
  34. function World:Step(time,dt)
  35. ForEachCall(self.Entities, "Step", time, dt)
  36. end
  37. function World:PostStep(time,dt)
  38. ForEachCall(self.Entities, "PostStep", time, dt)
  39. end
  40. -- Only called on client
  41. function World:RenderStepped()
  42. ForEachCall(self.Entities, "RenderStepped")
  43. end
  44. return World