-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathECS_concat.lua
3304 lines (2722 loc) · 99 KB
/
ECS_concat.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
ECS Lua v2.2.0
ECS Lua is a fast and easy to use ECS (Entity Component System) engine for game development.
This is a minified version of ECS Lua, to see the full source code visit
https://github.com/nidorx/ecs-lua
Discussions about this script are at https://devforum.roblox.com/t/841175
------------------------------------------------------------------------------
MIT License
Copyright (c) 2021 Alex Rodin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local __M__, __F__ = {}, {}
local function __REQUIRE__(m)
if (not __M__[m]) then
__M__[m] = { r = __F__[m]() }
end
return __M__[m].r
end
__F__["Archetype"] = function()
-- src/Archetype.lua
local archetypes = {}
local CACHE_WITH = {}
local CACHE_WITHOUT = {}
-- Version of the last registered archetype. Used to cache the systems execution plan
local Version = 0
--[[
An Archetype is a unique combination of component types. The EntityRepository uses the archetype to group all
entities that have the same sets of components.
An entity can change archetype fluidly over its lifespan. For example, when you add or remove components,
the archetype of the affected entity changes.
An archetype object is not a container; rather it is an identifier to each unique combination of component
types that an application has created at run time, either directly or implicitly.
You can create archetypes directly using ECS.Archetype.Of(Components[]). You also implicitly create archetypes
whenever you add or remove a component from an entity. An Archetype object is an immutable singleton;
creating an archetype with the same set of components, either directly or implicitly, results in the same
archetype.
The ECS framework uses archetypes to group entities that have the same structure together. The ECS framework stores
component data in blocks of memory called chunks. A given chunk stores only entities having the same archetype.
You can get the Archetype object for a chunk from its Archetype property.
Use ECS.Archetype.Of(Components[]) to get a Archetype reference.
]]
local Archetype = {}
Archetype.__index = Archetype
--[[
Gets the reference to an archetype from the informed components
@param componentClasses {ComponentClass[]} Component that define this archetype
@return Archetype
]]
function Archetype.Of(componentClasses)
local ids = {}
local cTypes = {}
for _, cType in ipairs(componentClasses) do
if (cType.IsCType and not cType.isComponent) then
if cType.IsQualifier then
if cTypes[cType] == nil then
cTypes[cType] = true
table.insert(ids, cType.Id)
end
cType = cType.SuperClass
end
if cTypes[cType] == nil then
cTypes[cType] = true
table.insert(ids, cType.Id)
end
end
end
table.sort(ids)
local id = "_" .. table.concat(ids, "_")
if archetypes[id] == nil then
archetypes[id] = setmetatable({
id = id,
_components = cTypes
}, Archetype)
Version = Version + 1
end
return archetypes[id]
end
--[[
Get the version of archetype definitions
@return number
]]
function Archetype.Version()
return Version
end
--[[
Checks whether this archetype has the informed component
@param componentClass {ComponentClass}
@return bool
]]
function Archetype:Has(componentClass)
return (self._components[componentClass] == true)
end
--[[
Gets the reference to an archetype that has the current components + the informed component
@param componentClass {ComponentClass}
@return Archetype
]]
function Archetype:With(componentClass)
if self._components[componentClass] == true then
-- component exists in that list, returns the archetype itself
return self
end
local cache = CACHE_WITH[self]
if not cache then
cache = {}
CACHE_WITH[self] = cache
end
local other = cache[componentClass]
if other == nil then
local componentTs = {componentClass}
for component,_ in pairs(self._components) do
table.insert(componentTs, component)
end
other = Archetype.Of(componentTs)
cache[componentClass] = other
end
return other
end
--[[
Gets the reference to an archetype that has the current components + the informed components
@param componentClasses {ComponentClass[]}
@return Archetype
]]
function Archetype:WithAll(componentClasses)
local cTypes = {}
for component,_ in pairs(self._components) do
table.insert(cTypes, component)
end
for _,component in ipairs(componentClasses) do
if self._components[component] == nil then
table.insert(cTypes, component)
end
end
return Archetype.Of(cTypes)
end
--[[
Gets the reference to an archetype that has the current components - the informed component
@param componentClass {ComponentClass}
@return Archetype
]]
function Archetype:Without(componentClass)
if self._components[componentClass] == nil then
-- component does not exist in this list, returns the archetype itself
return self
end
local cache = CACHE_WITHOUT[self]
if not cache then
cache = {}
CACHE_WITHOUT[self] = cache
end
local other = cache[componentClass]
if other == nil then
local componentTs = {}
for component,_ in pairs(self._components) do
if component ~= componentClass then
table.insert(componentTs, component)
end
end
other = Archetype.Of(componentTs)
cache[componentClass] = other
end
return other
end
--[[
Gets the reference to an archetype that has the current components - the informed components
@param componentClasses {ComponentClass[]}
@return Archetype
]]
function Archetype:WithoutAll(componentClasses)
local toIgnoreIdx = {}
for _,component in ipairs(componentClasses) do
toIgnoreIdx[component] = true
end
local cTypes = {}
for component,_ in pairs(self._components) do
if toIgnoreIdx[component] == nil then
table.insert(cTypes, component)
end
end
return Archetype.Of(cTypes)
end
-- Generic archetype, for entities that do not have components
Archetype.EMPTY = Archetype.Of({})
return Archetype
end
__F__["Component"] = function()
-- src/Component.lua
local Utility = __REQUIRE__("Utility")
local ComponentFSM = __REQUIRE__("ComponentFSM")
local copyDeep = Utility.copyDeep
local mergeDeep = Utility.mergeDeep
local CLASS_SEQ = 0
--[[
@param initializer {function(table) => table}
@param superClass {ComponentClass}
@return ComponentClass
]]
local function createComponentClass(initializer, superClass)
CLASS_SEQ = CLASS_SEQ + 1
local ComponentClass = {
Id = CLASS_SEQ,
IsCType = true,
-- Primary component
SuperClass = superClass
}
ComponentClass.__index = ComponentClass
if superClass == nil then
superClass = ComponentClass
superClass._Qualifiers = { ["Primary"] = ComponentClass }
superClass._QualifiersArr = { ComponentClass }
superClass._Initializers = {}
else
superClass.HasQualifier = true
ComponentClass.IsQualifier = true
ComponentClass.HasQualifier = true
end
local Qualifiers = superClass._Qualifiers
local QualifiersArr = superClass._QualifiersArr
setmetatable(ComponentClass, {
__call = function(t, value)
return ComponentClass.New(value)
end,
__index = function(t, key)
if (key == "States") then
return superClass.__States
end
if (key == "Case" or key == "StateInitial") then
return rawget(superClass, key)
end
end,
__newindex = function(t, key, value)
if (key == "Case" or key == "States" or key == "StateInitial") then
-- (FMS) Finite State Machine
if ComponentClass == superClass then
if (key == "States") then
if not superClass.IsFSM then
ComponentFSM.AddCapability(superClass, value)
for _, qualifiedClass in pairs(Qualifiers) do
if qualifiedClass ~= superClass then
ComponentFSM.AddMethods(superClass, qualifiedClass)
end
end
end
else
rawset(t, key, value)
end
end
else
rawset(t, key, value)
end
end
})
if superClass.IsFSM then
ComponentFSM.AddMethods(superClass, ComponentClass)
end
--[[
Gets a qualifier for this type of component. If the qualifier does not exist, a new class will be created,
otherwise it brings the already registered class qualifier reference with the same name.
@param qualifier {string|ComponentClass}
@return ComponentClass
]]
function ComponentClass.Qualifier(qualifier)
if type(qualifier) ~= "string" then
for _, qualifiedClass in ipairs(QualifiersArr) do
if qualifiedClass == qualifier then
return qualifier
end
end
return nil
end
local qualifiedClass = Qualifiers[qualifier]
if qualifiedClass == nil then
qualifiedClass = createComponentClass(initializer, superClass)
Qualifiers[qualifier] = qualifiedClass
table.insert(QualifiersArr, qualifiedClass)
end
return qualifiedClass
end
--[[
Get all qualified class
@param ... {string|ComponentClass} (Optional) Allows to filter the specific qualifiers
@return ComponentClass[]
]]
function ComponentClass.Qualifiers(...)
local filter = {...}
if #filter == 0 then
return QualifiersArr
else
local qualifiers = {}
local cTypes = {}
for _,qualifier in ipairs({...}) do
local qualifiedClass = ComponentClass.Qualifier(qualifier)
if qualifiedClass and cTypes[qualifiedClass] == nil then
cTypes[qualifiedClass] = true
table.insert(qualifiers, qualifiedClass)
end
end
return qualifiers
end
end
--[[
Constructor
@param value {any} If the value is not a table, it will be converted to the format "{ value = value}"
@return Component
]]
function ComponentClass.New(value)
if (value ~= nil and type(value) ~= "table") then
-- local MyComponent = Component({ value = Vector3.new(0, 0, 0) })
-- local component = MyComponent(Vector3.new(10, 10, 10))
value = { value = value }
end
local component = setmetatable(initializer(value) or {}, ComponentClass)
for _, fn in ipairs(superClass._Initializers) do
fn(component)
end
component.isComponent = true
component._qualifiers = { [ComponentClass] = component }
return component
end
--[[
Get this component's class
@return ComponentClass
]]
function ComponentClass:GetType()
return ComponentClass
end
--[[
Check if this component is of the type informed
@param componentClass {ComponentClass}
@return bool
]]
function ComponentClass:Is(componentClass)
return componentClass == ComponentClass or componentClass == superClass
end
--[[
Get the instance for the primary qualifier of this class
@return Component|nil
]]
function ComponentClass:Primary()
return self._qualifiers[superClass]
end
--[[
Get the instance for the given qualifier of this class
@param name {string|ComponentClass}
@return Component|nil
]]
function ComponentClass:Qualified(qualifier)
return self._qualifiers[ComponentClass.Qualifier(qualifier)]
end
--[[
Get all instances for all qualifiers of that class
@return Component[]
]]
function ComponentClass:QualifiedAll()
local qualifiedAll = {}
for name, qualifiedClass in pairs(Qualifiers) do
qualifiedAll[name] = self._qualifiers[qualifiedClass]
end
return qualifiedAll
end
--[[
Merges data from the other component into the current component. This method should not be invoked, it is used
by the entity to ensure correct retrieval of a component's qualifiers.
@param other {Component}
]]
function ComponentClass:Merge(other)
if superClass.HasQualifier then
if self == other then
return
end
if self._qualifiers == other._qualifiers then
return
end
if not other:Is(superClass) then
return
end
local selfClass = ComponentClass
local otherClass = other:GetType()
-- does anyone know the reference to the primary entity?
local primaryQualifiers
if selfClass == superClass then
primaryQualifiers = self._qualifiers
elseif otherClass == superClass then
primaryQualifiers = other._qualifiers
elseif self._qualifiers[superClass] ~= nil then
primaryQualifiers = self._qualifiers[superClass]._qualifiers
elseif other._qualifiers[superClass] ~= nil then
primaryQualifiers = other._qualifiers[superClass]._qualifiers
end
if primaryQualifiers ~= nil then
if self._qualifiers ~= primaryQualifiers then
for qualifiedClass, component in pairs(self._qualifiers) do
if superClass ~= qualifiedClass then
primaryQualifiers[qualifiedClass] = component
component._qualifiers = primaryQualifiers
end
end
end
if other._qualifiers ~= primaryQualifiers then
for qualifiedClass, component in pairs(other._qualifiers) do
if superClass ~= qualifiedClass then
primaryQualifiers[qualifiedClass] = component
component._qualifiers = primaryQualifiers
end
end
end
else
-- none of the instances know the Primary, use the current object reference
for qualifiedClass, component in pairs(other._qualifiers) do
if selfClass ~= qualifiedClass then
self._qualifiers[qualifiedClass] = component
component._qualifiers = self._qualifiers
end
end
end
end
end
--[[
Unlink this component with the other qualifiers
]]
function ComponentClass:Detach()
if not superClass.HasQualifier then
return
end
-- remove old unlink
self._qualifiers[ComponentClass] = nil
-- new link
self._qualifiers = { [ComponentClass] = self }
end
return ComponentClass
end
local function defaultInitializer(value)
return value or {}
end
--[[
A Component is an object that can store data but should have not behaviour (As that should be handled by systems).
]]
local Component = {}
--[[
Register a new ComponentClass
@param template {table|function(table?) -> table}
When `table`, this template will be used for creating component instances
When it's a `function`, it will be invoked when a new component is instantiated. The creation parameter of the
component is passed to template function
If the template type is different from `table` and `function`, **ECS Lua** will generate a template in the format
`{ value = template }`.
@return ComponentClass
]]
function Component.Create(template)
local initializer = defaultInitializer
if template ~= nil then
local ttype = type(template)
if (ttype == "function") then
initializer = template
else
if (ttype ~= "table") then
template = { value = template }
end
initializer = function(value)
local data = copyDeep(template)
if (value ~= nil) then
mergeDeep(data, value)
end
return data
end
end
end
return createComponentClass(initializer, nil)
end
return Component
end
__F__["ComponentFSM"] = function()
-- src/ComponentFSM.lua
--[[
Facilitate the construction and use of a Finite State Machine (FSM) using ECS
Example:
local Movement = ECS.Component({ Speed = 0 })
Movement.States = {
Standing = "*",
Walking = {"Standing", "Running"},
Running = {"Walking"}
}
Movement.StateInitial = "Standing"
Movement.Case = {
Standing = function(self, previous)
self.Speed = 0
end,
Walking = function(self, previous)
self.Speed = 5
end,
Running = function(self, previous)
self.Speed = 10
end
}
local movement = entity[Movement]
print(movement:GetState()) -- "Standing"
movement:SetState("Walking")
print(movement:GetPrevState()) -- "Standing"
movement:GetStateTime()
if (movement:GetState() == "Standing") then
movement.Speed = 0
end
]]
local Query = __REQUIRE__("Query")
--[[
Filter used in Query and QueryResult
@see QueryResult.lua
Ex. ECS.Query.All(Movement.In("Standing", "Walking"))
]]
local queryFilterCTypeStateIn = Query.Filter(function(entity, config)
local states = config.States
local isSuperClass = config.IsSuperClass
local componentClass = config.ComponentClass
if isSuperClass then
local qualifiers = componentClass.Qualifiers()
for _, qualifier in ipairs(qualifiers) do
local component = entity[qualifier]
if (component ~= nil and states[component:GetState()] == true) then
return true
end
end
return false
else
local component = entity[componentClass]
if component == nil then
return false
end
return states[component:GetState()] == true
end
end)
local ComponentFSM = {}
--[[
Adds FSM capability to a ComponentClass
@param superClass {ComonentClass}
@param states { {[key=string] => string|string[]}}
@see Component.lua - createComponentClass() - ComponentClass__newindex
]]
function ComponentFSM.AddCapability(superClass, states)
superClass.IsFSM = true
local cTypeStates = setmetatable({}, {
__newindex = function(states, newState, value)
if (type(value) ~= "table") then
value = {value}
end
if table.find(value, "*") then
rawset(states, newState, "*")
else
local idxSelf = table.find(value, newState)
if idxSelf ~= nil then
table.remove(value, idxSelf)
if #value == 0 then
value = "*"
end
end
rawset(states, newState, value)
end
end
})
rawset(superClass, "__States", cTypeStates)
for state,value in pairs(states) do
if superClass.StateInitial == nil then
superClass.StateInitial = state
end
cTypeStates[state] = value
end
ComponentFSM.AddMethods(superClass, superClass)
table.insert(superClass._Initializers, function(component)
component:SetState(superClass.StateInitial)
end)
end
--[[
Adds FSM state change methods to a ComponentClass
@param superClass {ComponentClass}
@param componentClass {ComponentClass}
]]
function ComponentFSM.AddMethods(superClass, componentClass)
componentClass.IsFSM = true
local cTypeStates = superClass.States
--[[
Creates a clause used to filter repository entities in a Query or QueryResult
@param ... {string[]}
@return Clause
Ex. ECS.Query.All(Movement.In("Walking", "Running"))
]]
function componentClass.In(...)
local states = {}
local count = 0
for _,state in ipairs({...}) do
if (cTypeStates[state] ~= nil and states[state] == nil) then
count = count + 1
states[state] = true
end
end
if count == 0 then
-- In any state
return {}
end
return queryFilterCTypeStateIn({
States = states,
IsSuperClass = (componentClass == superClass),
ComponentClass = componentClass,
})
end
--[[
Defines the current state of the FSM
@param newState {string}
]]
function componentClass:SetState(newState)
if (newState == nil or cTypeStates[newState] == nil) then
return
end
local actual = self:GetState()
if (actual == newState) then
return
end
if (actual ~= nil ) then
local transtions = cTypeStates[actual]
if (transtions ~= "*" and table.find(transtions, newState) == nil) then
-- not allowed
return
end
end
self._state = newState
self._statePrev = actual
self._stateTime = os.clock()
local action = superClass.Case and superClass.Case[newState]
if action then
action(self, actual)
end
end
--[[
Get the current state of the FSM
@return string
]]
function componentClass:GetState()
return self._state or superClass.StateInitial
end
--[[
Get the previous state of the FSM
@return string|nil
]]
function componentClass:GetPrevState()
return self._statePrev or nil
end
--[[
Gets the time it changed to the current state
]]
function componentClass:GetStateTime()
return self._stateTime or 0
end
end
return ComponentFSM
end
__F__["ECS"] = function()
-- src/ECS.lua
--[[
ECS Lua v2.2.0
ECS Lua is a fast and easy to use ECS (Entity Component System) engine for game development.
https://github.com/nidorx/ecs-lua
Discussions about this script are at https://devforum.roblox.com/t/841175
------------------------------------------------------------------------------
MIT License
Copyright (c) 2020 Alex Rodin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local Query = __REQUIRE__("Query")
local World = __REQUIRE__("World")
local System = __REQUIRE__("System")
local Archetype = __REQUIRE__("Archetype")
local Component = __REQUIRE__("Component")
local function setLoopManager(manager)
World.LoopManager = manager
end
pcall(function()
if (game and game.ClassName == "DataModel") then
-- is roblox
setLoopManager(__REQUIRE__("RobloxLoopManager")())
end
end)
--[[
@TODO
- Server entities
- Client - Server sincronization (snapshot, delta, spatial index, grid manhatham distance)
- Table pool (avoid GC)
- System readonly? Paralel execution
- Debugging?
- Benchmark (Local Script vs ECS implementation)
- Basic physics (managed)
- SharedComponent?
- Serializaton
- world:Serialize()
- world:Serialize(entity)
- entity:Serialize()
- component:Serialize()
]]
local ECS = {
Query = Query,
World = World.New,
System = System.Create,
Archetype = Archetype,
Component = Component.Create,
SetLoopManager = setLoopManager
}
if _G.ECS == nil then
_G.ECS = ECS
else
local warn = _G.warn or print
warn("ECS Lua was not registered in the global variables, there is already another object registered.")
end
return ECS
end
__F__["Entity"] = function()
-- src/Entity.lua
--[[
The entity is a fundamental part of the Entity Component System. Everything in your game that has data or an
identity of its own is an entity. However, an entity does not contain either data or behavior itself. Instead,
the data is stored in the components and the behavior is provided by the systems that process those components.
]]
local Archetype = __REQUIRE__("Archetype")
local SEQ = 0
--[[
[GET]
01) comp1 = entity[CompType1]
02) comp1 = entity:Get(CompType1)
03) comp1, comp2, comp3 = entity:Get(CompType1, CompType2, CompType3)
]]
local function getComponent(entity, ...)
local values = {...}
local data = entity._data
if (#values == 1) then
local cType = values[1]
if (cType.IsCType and not cType.isComponent) then
-- 01) comp1 = entity[CompType1]
-- 02) comp1 = entity:Get(CompType1)
return data[cType]
else
return nil
end
end
-- 03) comp1, comp2, comp3 = entity:Get(CompType1, CompType2, CompType3)
local components = {}
for i,cType in ipairs(values) do
if (cType.IsCType and not cType.isComponent) then
table.insert(components, data[cType])
end
end
return table.unpack(components)
end
--[[
Merges the qualifiers of a new added component.
@param entity {Entity}
@param newComponent {Component}
@param newComponentClass {ComponentClass}
]]
local function mergeComponents(entity, newComponent, newComponentClass)
local data = entity._data
local otherComponent
-- get first instance
for _,oCType in ipairs(newComponentClass.Qualifiers()) do
if oCType ~= newComponentClass then
otherComponent = data[oCType]
if otherComponent then
break
end
end
end
if otherComponent then
otherComponent:Merge(newComponent)
end
end
--[[
[SET]
01) entity[CompType1] = nil
02) entity[CompType1] = value
03) entity:Set(CompType1, nil)
04) entity:Set(CompType1, value)
05) entity:Set(comp1)
06) entity:Set(comp1, comp2, ...)
]]
local function setComponent(entity, ...)
local values = {...}
local data = entity._data
local archetypeOld = entity.archetype
local archetypeNew = archetypeOld
local toMerge = {}
local cType = values[1]
if (cType and cType.IsCType and not cType.isComponent) then
local value = values[2]
local component
-- 01) entity[CompType1] = nil
-- 02) entity[CompType1] = value
-- 03) entity:Set(CompType1, nil)