From a62cb370cff5fbed0a38d9fa34470518068b13b1 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Sun, 9 Jun 2024 23:58:01 +0200 Subject: [PATCH] Everything are entities! --- lib/init.lua | 81 ++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/lib/init.lua b/lib/init.lua index b83d988..402473c 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -853,77 +853,72 @@ end -- __nominal_type_dont_use could not be any or T as it causes a type error -- or produces a union -export type Component = T & {__nominal_type_dont_use: never} -export type Entity = Component -export type Relationship = Component -type ctype = Component - -export type QueryShim = typeof(setmetatable( - {} :: { - --- Excludes the given selection from the query - without: (QueryShim, U...) -> QueryShim - }, - {} :: { - __iter: (QueryShim) -> () -> (Entity, T...) - } -)) +export type Entity = number & {__nominal_type_dont_use: T} +export type Pair = number +export type QueryShim = typeof(setmetatable({ + without = function(...): QueryShim + return nil :: any + end +}, { + __iter = function(): () -> (number, T...) + return nil :: any + end +})) export type WorldShim = typeof(setmetatable( {} :: { --- Creates a new entity - entity: (WorldShim) -> Entity, + entity: (WorldShim) -> Entity, --- Creates a new entity located in the first 256 ids. --- These should be used for static components for fast access. - component: (WorldShim) -> Component, + component: (WorldShim) -> Entity, --- Gets the target of an relationship. For example, when a user calls --- `world:target(id, ChildOf(parent))`, you will obtain the parent entity. - target: (WorldShim, id: Entity, relation: Relationship) -> Entity?, + target: (WorldShim, id: Entity, relation: Entity) -> Entity?, --- Deletes an entity and all it's related components and relationships. delete: (WorldShim, id: Entity) -> (), --- Adds a component to the entity with no value - add: (WorldShim, id: Entity, component: Component) -> (), + add: (WorldShim, id: Entity, component: Entity) -> (), --- Assigns a value to a component on the given entity - set: (WorldShim, id: Entity, component: Component, data: T) -> (), + set: (WorldShim, id: Entity, component: Entity, data: T) -> (), --- Removes a component from the given entity - remove: (WorldShim, id: Entity, component: Component) -> (), + remove: (WorldShim, id: Entity, component: Entity) -> (), --- Retrieves the value of up to 4 components. These values may be nil. get: - ((WorldShim, id: any, ctype) -> A) - & ((WorldShim, id: Entity, ctype, ctype) -> (A, B)) - & ((WorldShim, id: Entity, ctype, ctype, ctype) -> (A, B, C)) - & (WorldShim, id: Entity, ctype, ctype, ctype, ctype) -> (A, B, C, D), + ((WorldShim, id: any, Entity) -> A) + & ((WorldShim, id: Entity, Entity, Entity) -> (A, B)) + & ((WorldShim, id: Entity, Entity, Entity, Entity) -> (A, B, C)) + & (WorldShim, id: Entity, Entity, Entity, Entity, Entity) -> (A, B, C, D), --- Searches the world for entities that match a given query - query: - ((WorldShim, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype, ctype, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype, ctype, ctype, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype) -> QueryShim) - & ((WorldShim, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ctype, ...ctype) -> QueryShim) + query: ((WorldShim, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity, Entity, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity, Entity, Entity, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity) -> QueryShim) + & ((WorldShim, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity, Entity, ...Entity) -> QueryShim) }, {} :: { __iter: (world: WorldShim) -> () -> (number, {[unknown]: unknown?}) } - )) return table.freeze({ World = (World :: any) :: {new: () -> WorldShim}, - OnAdd = (ON_ADD :: any) :: Component, - OnRemove = (ON_REMOVE :: any) :: Component, - OnSet = (ON_SET :: any) :: Component, - Wildcard = (WILDCARD :: any) :: Component, - w = (WILDCARD :: any) :: Component, + OnAdd = (ON_ADD :: any) :: Entity, + OnRemove = (ON_REMOVE :: any) :: Entity, + OnSet = (ON_SET :: any) :: Entity, + Wildcard = (WILDCARD :: any) :: Entity, + w = (WILDCARD :: any) :: Entity, Rest = REST, IS_PAIR = ECS_IS_PAIR, @@ -934,6 +929,6 @@ return table.freeze({ ECS_PAIR_RELATION = ECS_PAIR_RELATION, ECS_PAIR_OBJECT = ECS_PAIR_OBJECT, - pair = (ECS_PAIR :: any) :: (pred: Entity, obj: Entity) -> Relationship, + pair = (ECS_PAIR :: any) :: (pred: Entity, obj: Entity) -> number, getAlive = getAlive })