diff options
| author | Alexander M Pickering <alex@cogarr.net> | 2025-01-09 18:11:46 -0600 |
|---|---|---|
| committer | Alexander M Pickering <alex@cogarr.net> | 2025-01-09 18:11:46 -0600 |
| commit | decb72f936060a65bff18e9cbf33642ea3a71cd0 (patch) | |
| tree | 3b07bb1bfc1e4f0e39ec4ff8e0c243cd4fab0d61 /src/ecs.moon | |
| parent | 726876d42270f8974fd495be91127ea7585472ff (diff) | |
| download | ggj25-decb72f936060a65bff18e9cbf33642ea3a71cd0.tar.gz ggj25-decb72f936060a65bff18e9cbf33642ea3a71cd0.tar.bz2 ggj25-decb72f936060a65bff18e9cbf33642ea3a71cd0.zip | |
Work
Diffstat (limited to 'src/ecs.moon')
| -rw-r--r-- | src/ecs.moon | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/ecs.moon b/src/ecs.moon new file mode 100644 index 0000000..4fdecc9 --- /dev/null +++ b/src/ecs.moon @@ -0,0 +1,88 @@ + +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 +} + +} |
