entities = {} -- Bace component class of the ECS class Component depends: {} new: (name, properties) => @name = name @properties = properties or {} join: (e) => @ leave: (e) => @ -- Base entity of our ECS class Entity id: 1 new: (id, components) => -- [id], [components] id_cur = #entities while id == nil id_cur += 1 if entities[id_cur] == nil id = id_cur assert(entities[id] == nil, "Attempted to create entity with the same id as one that already exists: " .. tostring(id)) entities[id] = @ @id = id -- Bookkeeping for O(1) access for componenets @c_by_type = {} --Entity is responsible for the component -> entity link @components = componenets or {} for name, component in pairs(components) component.entity = @ @c_by_type[component.__class] = @c_by_type[component.__class] or {} @c_by_type[component.__class][name] = component component\join(@) add: (cid, component) => component.entity = @ component\join(@) if cid == nil cid = #@components while @components[cid] cid += 1 assert(@components[cid] == nil, "Already had a component with id" .. tostring(cid)) @components[cid] = component @ remove: (cid) => component = @components[cid] component.entity = nil component\leave(@) component get: (cid) => @components[cid] class NetworkedComponent extends Component pack: () => assert(@entity, "Tried to pack on a NetworkedComponent without an Entity") return am.to_json({ id: @entity.id data: @properties time: am.current_time! }) class PredictedComponent extends Component new: (name, properties, netc_name, calculate) => super(name, properties) @netc_name = netc_name @calculate = calculate join: (entity) => @net = entity[@netc_name] forward: () => for proeprty, calculation in pairs(@calculate) @properties[property] = calculation(@) class GraphicsComponent extends Component new: (name, properties) => assert(properties.node , "Failed to find node for graphics component") super(name, properties) { Component: Component Entity: Entity NetworkedComponent: NetworkedComponent PredictedComponent: PredictedComponent GraphicsComponent: GraphicsComponent } }