@rbxts/replecs 0.2.3 → 0.3.0-test1

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,103 +1,94 @@
1
- local jecs = require "./jecs"
2
- local common = require "@self/common"
1
+ local jecs = require (script.Parent:WaitForChild('jecs'))
2
+ local common = require (script:WaitForChild('common'))
3
+ local types = require (script:WaitForChild('types'))
4
+ local utils = require (script:WaitForChild('utils'))
3
5
 
4
- local client = require "@self/client"
5
- local server = require "@self/server"
6
- local customid = require "@self/customid"
7
- local VERSION = require "@self/ver"
6
+ local client = require (script:WaitForChild('client'))
7
+ local server = require (script:WaitForChild('server'))
8
+ local customid = require (script:WaitForChild('customid'))
9
+ local VERSION = require (script:WaitForChild('ver'))
8
10
 
9
11
  export type Server = server.Server
10
12
  export type Client = client.Client
11
13
 
14
+ type Entity = common.Entity
15
+ type World = common.World
16
+ type Component<T = any> = common.Component<T>
17
+
12
18
  export type HandleContext = customid.HandleContext
13
19
  export type CustomId = customid.CustomId
14
- export type HandshakeInfo = common.HandshakeInfo
15
-
16
- type Entity = jecs.Entity
20
+ export type HandshakeInfo = types.HandshakeInfo
21
+ export type Serdes = types.Serdes
17
22
 
18
23
  export type ReplecsLib = {
19
24
  server: Server,
20
25
  client: Client,
26
+ components: types.Components,
21
27
 
22
28
  after_replication: (self: ReplecsLib, callback: () -> ()) -> (),
23
29
  register_custom_id: (self: ReplecsLib, custom_id: CustomId) -> (),
30
+
31
+ set_serdes: <T>(self: ReplecsLib, id: Component<T>, serdes: types.Serdes<T>) -> (),
32
+ remove_serdes: (self: ReplecsLib, id: Component) -> (),
24
33
  }
25
34
 
26
- type Replecs = common.Components & {
27
- create: (world: jecs.World?) -> ReplecsLib,
35
+ type Replecs = types.Components & {
36
+ __index: any,
37
+ VERSION: string,
38
+ create: (world: World?) -> ReplecsLib,
28
39
 
29
- create_server: (world: jecs.World?) -> Server,
30
- create_client: (world: jecs.World?) -> Client,
40
+ create_server: (world: World?) -> Server,
41
+ create_client: (world: World?) -> Client,
31
42
  create_custom_id: (identifier: string, handler: (ctx: HandleContext) -> Entity?) -> CustomId,
32
43
  }
33
44
 
34
- type MemberFilter = {
35
- [Player]: boolean,
36
- }
45
+ local replecs = {}
46
+ replecs.VERSION = VERSION
47
+ replecs.__index = replecs
37
48
 
38
- local replecs = {
39
- VERSION = VERSION,
40
- shared = jecs.tag(),
41
- networked = jecs.component(),
42
- reliable = jecs.component(),
43
- unreliable = jecs.component(),
44
- pair = jecs.component(),
45
-
46
- serdes = jecs.component(),
47
- custom = jecs.tag(),
48
- custom_handler = jecs.component(),
49
- global = jecs.component(),
50
- __alive_tracking__ = jecs.tag(),
51
- }
49
+ local PREREGISTRATION = common.preregistration
52
50
 
53
- replecs.__index = replecs
54
- replecs.Serdes = replecs.serdes
55
- replecs.Custom = replecs.custom
56
- replecs.CustomHandler = replecs.custom_handler
57
- replecs.Networked = replecs.networked
58
- replecs.Reliable = replecs.reliable
59
- replecs.Unreliable = replecs.unreliable
60
- replecs.Pair = replecs.pair
61
- replecs.Shared = replecs.shared
62
- replecs.Global = replecs.global
63
-
64
- jecs.meta(replecs.serdes, jecs.Name, "replecs.serdes")
65
- jecs.meta(replecs.custom, jecs.Name, "replecs.custom")
66
- jecs.meta(replecs.custom_handler, jecs.Name, "replecs.custom_handler")
67
- jecs.meta(replecs.networked, jecs.Name, "replecs.networked")
68
- jecs.meta(replecs.reliable, jecs.Name, "replecs.reliable")
69
- jecs.meta(replecs.unreliable, jecs.Name, "replecs.unreliable")
70
- jecs.meta(replecs.pair, jecs.Name, "replecs.pair")
71
- jecs.meta(replecs.shared, jecs.Name, "replecs.shared")
72
- jecs.meta(replecs.global, jecs.Name, "replecs.global")
73
-
74
- jecs.meta(replecs.__alive_tracking__, jecs.Name, "replecs.__alive_tracking__")
75
-
76
- jecs.meta(replecs.custom, jecs.Exclusive)
77
- jecs.meta(replecs.global, jecs.Exclusive)
78
-
79
- function replecs.create_server(world: jecs.World?): Server
80
- return server.create(world, replecs :: Replecs)
51
+ if PREREGISTRATION then
52
+ local components = replecs :: Replecs
53
+ utils.create_components(common.preregister_tag, common.preregister_component, components)
54
+ utils.add_component_names(components, common.add_preregistered_name)
55
+
56
+ jecs.meta(components.custom, jecs.Exclusive)
57
+ jecs.meta(components.global, jecs.Exclusive)
81
58
  end
82
59
 
83
- function replecs.create_client(world: jecs.World?): Client
84
- return client.create(world, replecs :: Replecs)
60
+ local COMPONENTS = if PREREGISTRATION then replecs :: Replecs else nil
61
+
62
+ function replecs.create_server(world: World?): Server
63
+ return server.create(world, COMPONENTS)
85
64
  end
86
65
 
87
- function replecs.create(world: jecs.World?): ReplecsLib
88
- assert(game, "This is a Roblox specific function")
66
+ function replecs.create_client(world: World?): Client
67
+ return client.create(world, COMPONENTS)
68
+ end
89
69
 
70
+ function replecs.create(world: World?): ReplecsLib
71
+ assert(game, "This is a Roblox specific function")
90
72
  local RunService = game:GetService "RunService"
91
- local lib = {} :: ReplecsLib
73
+ local self = {} :: ReplecsLib
74
+
75
+ if COMPONENTS then
76
+ self.components = COMPONENTS
77
+ elseif world then
78
+ self.components = utils.create_components(utils.tag_factory(world), utils.component_factory(world))
79
+ utils.add_component_names(self.components, function(component, name)
80
+ common.add_name(world, component, name)
81
+ end)
82
+ end
92
83
 
93
84
  if RunService:IsServer() then
94
- lib.server = server.create(world, replecs :: Replecs)
85
+ self.server = server.create(world, self.components)
95
86
  end
96
87
  if RunService:IsClient() then
97
- lib.client = client.create(world, replecs :: Replecs)
88
+ self.client = client.create(world, self.components)
98
89
  end
99
90
 
100
- return setmetatable(lib, replecs) :: any
91
+ return setmetatable(self, replecs) :: any
101
92
  end
102
93
 
103
94
  function replecs.create_custom_id(identifier: string, handler: (ctx: HandleContext) -> Entity?)
@@ -122,4 +113,22 @@ function replecs.register_custom_id(lib: ReplecsLib, custom_id: CustomId)
122
113
  end
123
114
  end
124
115
 
116
+ function replecs.set_serdes(lib: ReplecsLib, id: Component, serdes: Serdes)
117
+ if lib.server then
118
+ lib.server:set_serdes(id, serdes)
119
+ end
120
+ if lib.client then
121
+ lib.client:set_serdes(id, serdes)
122
+ end
123
+ end
124
+
125
+ function replecs.remove_serdes(lib: ReplecsLib, id: Component)
126
+ if lib.server then
127
+ lib.server:remove_serdes(id)
128
+ end
129
+ if lib.client then
130
+ lib.client:remove_serdes(id)
131
+ end
132
+ end
133
+
125
134
  return replecs :: Replecs