entities = {} -- Bace component class of the ECS class Component depends: {} new: (name, properties) => @name = name @properties = properties or {} join: (e) => @ post_join: (e) => @ leave: (e) => @ __tostring: () => return string.format("%s<%s> {\n%s\n}",@@__name,@name or "no name given",tostring(@properties)) -- 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 = components 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 for name, component in pairs(@components) assert(component.join, "Component " .. name .. " does not have a join method.") component\join(@) component\post_join(@) add: (cid, component) => component.entity = @ component\join(@) component\post_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 @c_by_type[component.__class][cid] = component @ remove: (cid) => component = @components[cid] component.entity = nil component\leave(@) component @components[cid] = nil @c_by_type[component.__class][cid] = nil destroy: () => for name, component in pairs(@components) @remove(name) get: (cid) => @components[cid] __tostring: () => return string.format("%s {\n%s\n}",@@__name,tostring(@components)) 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 assert(calculate and type(calculate) == "table", "Calculate must be a table") @calculate = calculate join: (entity) => @net = @entity\get(@netc_name) forward: () => for property, calculation in pairs(@calculate) @properties[property] = calculation(@) -- componnt moved to world.moon --class GraphicsComponent extends Component -- new: (name, properties) => -- print("Got name", name, "and properties", properties) -- assert(properties and properties.node , "Failed to find node for graphics component") -- super(name, properties) -- static: () => -- @@static -- node: () => -- @properties.node class PhysicsComponent extends Component new: (name, properties) => assert(properties and properties.shape, "Failed to find a shape for physics component") super(name, properties) class ScriptComponent extends Component new: (name, properties) => assert(properties and properties.script, "Failed to find script name for script component") super(name, properties) { Component: Component Entity: Entity NetworkedComponent: NetworkedComponent PredictedComponent: PredictedComponent GraphicsComponent: GraphicsComponent PhysicsComponent: PhysicsComponent ScriptComponent: ScriptComponent }