@rbxts/replecs 0.2.3-test1 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,69 +1,194 @@
1
- local jecs = require "../jecs"
2
- local customid = require "./customid"
1
+ local jecs = require (script.Parent.Parent:WaitForChild('jecs'))
3
2
 
4
- export type FunctionFilter = (player: Player) -> boolean
5
- export type AnyFilter = PlayerFilter | Player
6
- type Entity<T = any> = jecs.Entity<T>
3
+ export type Entity = jecs.Entity
4
+ export type World = jecs.World
5
+ export type Component<T = any> = jecs.Entity<T>
7
6
 
8
- export type PlayerFilter = {
9
- [Player]: boolean,
10
- }
7
+ local common = {}
11
8
 
12
- export type SharedInfo<T> = {
13
- lookup: { [string]: T },
14
- keys: { string },
15
- indexes: { [number]: T },
16
- members: { [T]: number },
17
- }
18
- export type HandshakeInfo = {
19
- components: { [string]: boolean },
20
- custom_ids: { [string]: boolean },
21
- serdes: { [string]: {
22
- includes_variants: boolean?,
23
- bytespan: number?,
24
- } },
25
- }
9
+ local WILDCARD = jecs.Wildcard
10
+ local ECS_NAME = jecs.Name
11
+ local PREREGISTRATION = true
12
+ local RELATIONSHIPS = true
26
13
 
27
- export type Serdes = {
28
- includes_variants: boolean?,
29
- bytespan: number?,
30
- serialize: (value: any) -> (buffer, { any }?),
31
- deserialize: (buffer, { any }?) -> any,
14
+ local IMPLICITLY_SHARED: { [string]: Entity } = {
15
+ ["jecs.ChildOf"] = jecs.ChildOf,
32
16
  }
33
17
 
34
- export type Components = {
35
- shared: Entity,
36
- networked: Entity<AnyFilter?>,
37
- reliable: Entity<AnyFilter?>,
38
- unreliable: Entity<AnyFilter?>,
39
- pair: Entity<AnyFilter?>,
40
-
41
- serdes: Entity<Serdes>,
42
- custom: Entity,
43
- custom_handler: Entity<(val: any) -> Entity?>,
44
- global: Entity<number>,
45
- __alive_tracking__: Entity,
46
- }
18
+ function LOGERROR(...: string): never
19
+ error("REPLECS ERROR - " .. table.concat({ ... }, " "))
20
+ end
21
+ function LOGWARN(...: string)
22
+ warn("REPLECS WARN - " .. table.concat({ ... }, " "))
23
+ end
47
24
 
48
- export type Shared = {
49
- components: SharedInfo<Entity>,
50
- custom_ids: SharedInfo<customid.CustomId>,
25
+ function LOG_COMPONENT(world: World, component: Entity)
26
+ return ` name: {world:get(component, ECS_NAME) or "(no name)"}, id: {component}`
27
+ end
51
28
 
52
- serdes: { [Entity]: Serdes },
53
- }
29
+ local function IS_PAIR(id: Component): boolean
30
+ return jecs.IS_PAIR(id)
31
+ end
54
32
 
55
- export type WorldHooks = {
56
- added: (
57
- world: jecs.World,
58
- component: Entity,
59
- fn: (entity: Entity, id: jecs.Id, value: any) -> ()
60
- ) -> () -> (),
61
- changed: (
62
- world: jecs.World,
63
- component: Entity,
64
- fn: (entity: Entity, id: jecs.Id, value: any) -> ()
65
- ) -> () -> (),
66
- removed: (world: jecs.World, component: Entity, fn: (entity: Entity, id: jecs.Id) -> ()) -> () -> (),
67
- }
33
+ local function PAIR(relation: Entity, target: Entity): Entity
34
+ return jecs.pair(relation, target)
35
+ end
36
+
37
+ local function PAIR_FIRST(world: World, id: Entity): Entity
38
+ if not IS_PAIR(id) then
39
+ LOGERROR(`expected a pair, got: {LOG_COMPONENT(world, id)}`)
40
+ end
41
+ return jecs.pair_first(world, id)
42
+ end
43
+
44
+ local function PAIR_SECOND(world: World, id: Entity): Entity
45
+ if not IS_PAIR(id) then
46
+ LOGERROR(`expected a pair, got: {LOG_COMPONENT(world, id)}`)
47
+ end
48
+ return jecs.pair_second(world, id)
49
+ end
50
+
51
+ local function LISTEN_RELATION(relation: Entity)
52
+ return relation
53
+ end
54
+
55
+ local function ADD_NAME(world: World, component: Component, name: string)
56
+ world:set(component, ECS_NAME, name)
57
+ end
58
+
59
+ local function ADD_PREREGISTERED_NAME(component: Component, name: string)
60
+ jecs.meta(component, ECS_NAME, name)
61
+ end
62
+
63
+ local function ITERATE_ALL_RELATIONS(world: World, entity: Entity, relation: Component, callback: (id: Component) -> ())
64
+ local entity_index = jecs.entity_index_try_get_fast(world.entity_index :: any, entity :: any)
65
+ if not entity_index then
66
+ return
67
+ end
68
+ local archetype = entity_index.archetype
69
+ local wildcard = PAIR(relation, WILDCARD) :: any
70
+ local idr = world.component_index[wildcard]
71
+ if not idr then
72
+ return
73
+ end
74
+
75
+ local archetype_id = archetype.id
76
+ local count = idr.counts[archetype_id]
77
+ if not count then
78
+ return
79
+ end
80
+
81
+ local start = idr.records[archetype_id]
82
+ if not start then
83
+ return
84
+ end
85
+
86
+ for i = start, start + count - 1 do
87
+ local pair = archetype.types[i]
88
+ callback(pair)
89
+ end
90
+ end
91
+
92
+ local function WORLD_ADD(world: World, entity: Entity, id: Component)
93
+ world:add(entity, id)
94
+ end
95
+
96
+ local function WORLD_SET<T>(world: World, entity: Entity, id: Component<T>, value: T)
97
+ world:set(entity, id, value)
98
+ end
99
+
100
+ local function WORLD_REMOVE(world: World, entity: Entity, id: Component)
101
+ world:remove(entity, id)
102
+ end
103
+
104
+ local function WORLD_DELETE(world: World, entity: Entity)
105
+ world:delete(entity)
106
+ end
107
+
108
+ local function WORLD_GET<T>(world: World, entity: Entity, id: Component<T>): T
109
+ return world:get(entity, id) :: T
110
+ end
111
+
112
+ local function WORLD_TARGET(world: World, entity: Entity, rel: Component, index: number?): jecs.Entity?
113
+ return world:target(entity, rel, index)
114
+ end
115
+
116
+ local function WORLD_HAS(world: World, entity: Entity, id: Component)
117
+ return world:has(entity, id)
118
+ end
119
+
120
+ local function WORLD_ENTITY(world: World)
121
+ return world:entity()
122
+ end
123
+
124
+ local function WORLD_TAG(world: World)
125
+ return world:entity()
126
+ end
127
+ local function WORLD_COMPONENT(world: World)
128
+ return world:component()
129
+ end
130
+
131
+ local PREREGISTER_COMPONENT = jecs.component
132
+ local PREREGISTER_TAG = jecs.tag
133
+
134
+ local function IS_COMPONENT(world: World, id: Component)
135
+ return not jecs.is_tag(world, id)
136
+ end
137
+
138
+ local function HOOK_ADDED(world: World, id: Component, callback: (e: Entity, id: Component, value: any) -> ()): () -> ()
139
+ return world:added(id, callback)
140
+ end
141
+
142
+ local function HOOK_CHANGED(
143
+ world: World,
144
+ id: Component,
145
+ callback: (e: Entity, id: Component, value: any) -> ()
146
+ ): () -> ()
147
+ return world:changed(id, callback)
148
+ end
149
+
150
+ local function HOOK_REMOVED(world: World, id: Component, callback: (e: Entity, id: Component) -> ()): () -> ()
151
+ return world:removed(id, callback)
152
+ end
153
+
154
+ common.preregistration = PREREGISTRATION
155
+ common.relationships = RELATIONSHIPS
156
+ common.wildcard = WILDCARD
157
+ common.ecs_name = ECS_NAME
158
+ common.implicitly_shared = IMPLICITLY_SHARED
159
+
160
+ common.is_pair = IS_PAIR
161
+ common.pair = PAIR
162
+ common.pair_first = PAIR_FIRST
163
+ common.pair_second = PAIR_SECOND
164
+ common.add_name = ADD_NAME
165
+ common.add_preregistered_name = ADD_PREREGISTERED_NAME
166
+ common.iterate_all_relations = ITERATE_ALL_RELATIONS
167
+
168
+ common.world_set = WORLD_SET
169
+ common.world_remove = WORLD_REMOVE
170
+ common.world_delete = WORLD_DELETE
171
+ common.world_add = WORLD_ADD
172
+
173
+ common.world_get = WORLD_GET
174
+ common.world_target = WORLD_TARGET
175
+ common.world_has = WORLD_HAS
176
+
177
+ common.world_entity = WORLD_ENTITY
178
+ common.world_tag = WORLD_TAG
179
+ common.world_component = WORLD_COMPONENT
180
+ common.preregister_component = PREREGISTER_COMPONENT
181
+ common.preregister_tag = PREREGISTER_TAG
182
+
183
+ common.is_component = IS_COMPONENT
184
+ common.listen_relation = LISTEN_RELATION
185
+
186
+ common.hook_added = HOOK_ADDED
187
+ common.hook_changed = HOOK_CHANGED
188
+ common.hook_removed = HOOK_REMOVED
189
+
190
+ common.log_error = LOGERROR
191
+ common.log_warn = LOGWARN
192
+ common.log_component = LOG_COMPONENT
68
193
 
69
- return nil
194
+ return common
@@ -1,15 +1,15 @@
1
- local jecs = require "../jecs"
1
+ local common = require (script.Parent:WaitForChild('common'))
2
2
 
3
- type Entity<T = any> = jecs.Entity<T>
4
- type Id<T = any> = jecs.Id<T>
5
- type World = jecs.World
3
+ type Entity = common.Entity
4
+ type Component<T = any> = common.Component<T>
5
+ type World = common.World
6
6
 
7
7
  export type HandleContext = {
8
8
  entity_id: number,
9
- component: <T>(component: Entity<T>) -> T,
10
- target: (relation: jecs.Id, index: number?) -> Entity?,
11
- pair_value: <T>(relation: jecs.Id<T>, target: Entity) -> T?,
12
- has_pair: (relation: jecs.Id, target: Entity) -> boolean,
9
+ component: <T>(component: Component<T>) -> T,
10
+ target: (relation: Component, index: number?) -> Entity?,
11
+ pair_value: <T>(relation: Component<T>, target: Entity) -> T?,
12
+ has_pair: (relation: Component, target: Entity) -> boolean,
13
13
 
14
14
  entity: (server_entity: number) -> Entity,
15
15
  has: (tag: Entity) -> boolean,
@@ -23,6 +23,7 @@ export type CustomId = {
23
23
  }
24
24
 
25
25
  local custom_id = {}
26
+ custom_id.__index = custom_id
26
27
 
27
28
  function custom_id.handle(custom: CustomId, callback: (ctx: HandleContext) -> Entity?)
28
29
  custom.handle_callback = callback