Guest User

entityPool.lua

a guest
Aug 26th, 2019
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1. -- Services
  2. local replicatedStorage = game:GetService("ReplicatedStorage")
  3.  
  4. local shared = replicatedStorage.shared
  5.  
  6. local sharedLib = shared.lib
  7. local signalLib = require(sharedLib.signalLib)
  8.  
  9. local entityPool = {}
  10. entityPool.entities = {}
  11. entityPool.componentEntityMap = {}
  12. entityPool.serialEntityCount = 0
  13.  
  14. -- Accepts a tuple of components that are registered to on construction.
  15. function entityPool.createEntity(componentName, componentDataCollection)
  16.     entityPool.serialEntityCount = entityPool.serialEntityCount + 1
  17.  
  18.     local entityId = entityPool.serialEntityCount
  19.  
  20.     entityPool.entities[entityId] = {} -- Associative array containing all components associated with entity.
  21.  
  22.     for _, componentData in pairs(componentDataCollection) do
  23.         entityPool.addComponentToEntity(entityId, componentName, componentData)
  24.     end
  25.  
  26.     return entityId
  27. end
  28.  
  29. function entityPool.getEntityById(entityId)
  30.     return entityPool.entities[entityId]
  31. end
  32.  
  33. function entityPool.removeEntity(entityId)
  34.     if not entityPool.entities[entityId] then return end
  35.  
  36.     entityPool.serialEntityCount = entityPool.serialEntityCount - 1
  37.  
  38.     -- Reference is found in various places in component entity map, remove to ensure gc.
  39.     for componentName in pairs(entityPool.entities[entityId]) do
  40.         entityPool.componentEntityMap[componentName][entityId] = nil
  41.     end
  42.  
  43.     entityPool.entities[entityId] = nil
  44. end
  45.  
  46. function entityPool.addComponentToEntity(entityId, componentName, componentData)
  47.     local entityComponentCollection = entityPool.entities[entityId]
  48.     if not entityComponentCollection then return end
  49.  
  50.     entityComponentCollection[componentName] = componentData
  51.  
  52.     if not entityPool.componentEntityMap[componentName] then
  53.         entityPool.componentEntityMap[componentName] = {}
  54.         warn("System for " .. componentName .. " has not yet been constructed")
  55.         print("Adding field to componentEntityMap for above component")
  56.     end
  57.  
  58.     entityPool.componentEntityMap[componentName][entityId] = componentData
  59.     print(entityComponentCollection[componentName], entityPool.componentEntityMap[componentName][entityId])
  60.  
  61.     signalLib.dispatchAsync("componentAttached", componentName, entityId)
  62. end
  63.  
  64. function entityPool.removeComponentFromEntity(entityId, componentName)
  65.     local entityComponentCollection = entityPool.entities[entityId]
  66.     if not entityComponentCollection then return end
  67.  
  68.     entityComponentCollection[componentName] = nil
  69.     entityPool.componentEntityMap[componentName][entityId] = nil
  70.  
  71.     signalLib.dispatchAsync("componentDetached", componentName, entityId)
  72. end
  73.  
  74. return entityPool
Advertisement
Add Comment
Please, Sign In to add comment