-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflecs.lua
More file actions
771 lines (595 loc) · 17.6 KB
/
flecs.lua
File metadata and controls
771 lines (595 loc) · 17.6 KB
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
local ffi = require 'ffi'
local flecs = {}
local aux = {}
local clib
-- aux {{{
flecs.aux = aux
function aux.class(tableOrNil)
local table = tableOrNil or {}
table.__index = table
return table
end
function aux.id(first, second)
return second and clib.ecs_make_pair(first, second) or first
end
function aux.string(string)
return string ~= nil and ffi.string(string) or nil
end
function aux.free(pointer)
clib.ecs_os_api.free_(pointer)
end
function aux.len(array)
local result = 1
while array[result] ~= nil do
result = result + 1
end
return array[0] == nil and result - 1 or result
end
-- Used to fix arrays that are passed to `ffi.new` as initialisers as LuaJIT
-- repeats single elements
function aux.fix_array(array, init)
if array ~= nil and aux.len(array) == 1 then
table.insert(array, init)
end
end
-- }}}
-- flecs {{{
function flecs.pair(first, second)
return aux.id(first, second)
end
-- }}}
-- flecs.World {{{
local World = aux.class()
flecs.World = World
function World.__new()
return ffi.gc(clib.ecs_init(), clib.ecs_fini)
end
-- Group world_frame
function World:quit()
clib.ecs_quit(self)
end
function World:should_quit()
return clib.ecs_should_quit(self)
end
function World:set_target_fps(value)
clib.ecs_set_target_fps(value)
end
-- Group creating_entities
function World:entity(descOrNil)
local desc = descOrNil or {}
aux.fix_array(desc.add, 0)
return clib.ecs_entity_init(self, ffi.new('ecs_entity_desc_t', desc))
end
function World:component(desc)
local ctype = desc.ctype or ffi.typeof(world:identifier(desc.entity))
return clib.ecs_component_init(self, ffi.new('ecs_component_desc_t', {
entity = desc.entity,
type = { size = ffi.sizeof(ctype), alignment = ffi.alignof(ctype) },
}))
end
function World:clone(dstOrNil, src, copy)
local dst = dstOrNil
if copy == nil then
dst = nil
src = dstOrNil
copy = src
end
return clib.ecs_clone(self, dst, src, copy)
end
-- Group adding_removing
function World:add(entity, first, second)
clib.ecs_add_id(self, entity, aux.id(first, second))
end
function World:remove(entity, first, second)
clib.ecs_remove_id(self, entity, aux.id(first, second))
end
function World:override(entity, first, second)
clib.ecs_override_id(self, entity, aux.id(first, second))
end
-- Group enabling_disabling
local function enable(world, entity, first, second, value)
if first then
clib.ecs_enable_id(world, entity, aux.id(first, second), value)
else
clib.ecs_enable(world, entity, value)
end
end
function World:enable(entity, first, second)
return enable(self, entity, first, second, true)
end
function World:disable(entity, first, second)
return enable(self, entity, first, second, false)
end
function World:is_enabled(entity, first, second)
return clib.ecs_is_enabled_id(self, entity, aux.id(first, second))
end
-- Group deleting
function World:clear(entity)
clib.ecs_clear(self, entity)
end
function World:delete(entity)
clib.ecs_delete(self, entity)
end
function World:delete_with(first, second)
clib.ecs_delete_with(self, aux.id(first, second))
end
function World:remove_all(first, second)
clib.ecs_remove_all(self, aux.id(first, second))
end
-- Group getting
local function get(world, entity, symbol, first, second)
local ctype = ffi.typeof('$ const*', ffi.typeof(symbol))
return ffi.cast(ctype, clib.ecs_get_id(world, entity, aux.id(first, second)))
end
function World:get(entity, first, second)
return get(self, entity, self:identifier(first), first, second)
end
function World:get_second(entity, first, second)
return get(self, entity, self:identifier(second), first, second)
end
-- Group setting
local function get_mut(world, entity, symbol, first, second)
local ctype = ffi.typeof('$*', ffi.typeof(symbol))
return ffi.cast(ctype, clib.ecs_get_mut_id(world, entity, aux.id(first, second)))
end
function World:get_mut(entity, first, second)
return get_mut(self, entity, self:identifier(first), first, second)
end
function World:get_mut_second(entity, first, second)
return get_mut(self, entity, self:identifier(second), first, second)
end
local function emplace(world, entity, symbol, first, second)
local ctype = ffi.typeof('$*', ffi.typeof(symbol))
return ffi.cast(ctype, clib.ecs_emplace_id(world, entity, aux.id(first, second)))
end
function World:emplace(entity, first, second)
return emplace(self, entity, self:identifier(first), first, second)
end
function World:emplace_second(entity, first, second)
return emplace(self, entity, self:identifier(second), first, second)
end
function World:modified(entity, first, second)
clib.ecs_modified_id(self, entity, aux.id(first, second))
end
local function set(world, entity, symbol, first, secondOrValue, valueOrNil)
local value = valueOrNil or secondOrValue
local second = valueOrNil and secondOrValue or nil
local ctype = ffi.typeof(symbol)
if type(value) ~= 'cdata' then
value = ffi.new(ctype, value)
end
clib.ecs_set_id(world, entity, aux.id(first, second), ffi.sizeof(ctype), value)
end
function World:set(entity, first, secondOrValue, valueOrNil)
set(self, entity, self:identifier(first), first, secondOrValue, valueOrNil)
end
function World:set_second(entity, first, secondOrValue, valueOrNil)
set(self, entity, self:identifier(second), first, secondOrValue, valueOrNil)
end
-- Group metadata
function World:is_valid(entity)
return clib.ecs_is_valid(self, entity)
end
function World:is_alive(entity)
return clib.ecs_is_alive(self, entity)
end
function World:alive(entity)
return clib.ecs_get_alive(self, entity)
end
function World:ensure(entity)
clib.ecs_ensure(self, entity)
end
function World:ensure_id(entity)
clib.ecs_ensure_id(self, entity)
end
function World:exists(entity)
return clib.ecs_exists(self, entity)
end
function World:typeid(entity)
return clib.ecs_get_typeid(self, entity)
end
function World:is_tag(entity)
return clib.ecs_id_is_tag(self, entity)
end
function World:is_in_use(entity)
return clib.ecs_id_in_use(self, entity)
end
function World:name(entity)
return aux.string(clib.ecs_get_name(self, entity))
end
function World:symbol(entity)
return aux.string(clib.ecs_get_symbol(self, entity))
end
function World:identifier(entity)
return self:symbol(entity) or self:name(entity)
end
function World:set_name(entity, value)
clib.ecs_set_name(self, entity, value)
end
function World:set_symbol(entity, value)
clib.ecs_set_symbol(self, entity, value)
end
function World:set_alias(entity, value)
clib.ecs_set_alias(self, entity, value)
end
function World:has(entity, first, second)
return clib.ecs_has_id(self, entity, aux.id(first, second))
end
function World:get_target(entity, relationship, indexOrNil)
local index = indexOrNil or 0
return clib.ecs_get_target(self, entity, relationship, index)
end
function World:get_target_for(entity, relationship, id)
return clib.ecs_get_target_for_id(self, entity, relationship, id)
end
function World:count(entity)
return clib.ecs_count_id(self, entity)
end
-- Group lookup
function World:lookup(parentOrNil, name)
local parent = name and parentOrNil or 0
name = name or parentOrNil
return clib.ecs_lookup_child(self, parent, name)
end
function World:lookup_path(parentOrNil, path)
local parent = path and parentOrNil or 0
path = path or parentOrNil
return clib.ecs_lookup_path_w_sep(self, parent, path, '.', nil, true)
end
function World:lookup_symbol(symbol, lookup_as_path)
return clib.ecs_lookup_symbol(self, symbol, lookup_as_path)
end
-- Group paths
function World:get_path(parentOrNil, child)
local parent = child and parentOrNil or 0
child = child or parentOrNil
return aux.string(ffi.gc(
clib.ecs_get_path_w_sep(self, parent, child, '.', nil),
aux.free
))
end
-- Group scopes
function World:set_scope(entity)
return clib.ecs_set_scope(self, entity)
end
function World:scope()
return clib.ecs_get_scope(self)
end
function World:set_with(entity)
return clib.ecs_set_with(self, entity)
end
function World:with()
return clib.ecs_get_with(self)
end
function World:set_name_prefix(value)
return aux.string(clib.ecs_set_name_prefix(self, value))
end
-- Group filters
function World:filter(descOrNil)
local desc = descOrNil or {}
aux.fix_array(desc.terms, {})
return ffi.gc(
clib.ecs_filter_init(self, ffi.new('ecs_filter_desc_t', desc)),
clib.ecs_filter_fini
)
end
-- Group queries
function World:query(descOrNil)
local desc = descOrNil or {}
aux.fix_array(desc.filter.terms, {})
return clib.ecs_query_init(self, ffi.new('ecs_query_desc_t', desc))
end
-- Group iterators
function World:iter(iterable)
if ffi.istype('ecs_filter_t', iterable) then
return clib.ecs_filter_iter(self, iterable)
elseif ffi.istype('ecs_query_t', iterable) then
return clib.ecs_query_iter(self, iterable)
end
end
-- Group staging
function World:defer_begin()
return clib.ecs_defer_begin(self)
end
function World:is_deferred()
return clib.ecs_is_deferred(self)
end
function World:defer_end()
return clib.ecs_defer_end(self)
end
function World:defer_suspend()
return clib.ecs_defer_suspend(self)
end
function World:defer_resume()
return clib.ecs_defer_resume(self)
end
function World:is_readonly()
return clib.ecs_stage_is_readonly(self)
end
function World:is_async()
return clib.ecs_stage_is_async(self)
end
function World:struct(descOrNil)
local desc = descOrNil or {}
aux.fix_array(desc.members, {})
return clib.ecs_struct_init(self, ffi.new('ecs_struct_desc_t', desc))
end
-- }}}
-- flecs.Filter {{{
local Filter = aux.class()
flecs.Filter = Filter
function Filter:find_this_var()
return clib.ecs_filter_find_this_var(self)
end
function Filter:term_count()
return self._term_count
end
function Filter:field_count()
return self._field_count
end
function Filter:is_owned()
return self._owned
end
function Filter:is_terms_owned()
return self._terms_owned
end
function Filter:flags()
return self._flags
end
function Filter:name()
return aux.string(self._name)
end
-- }}}
-- flecs.Query {{{
local Query = aux.class()
flecs.Query = Query
function Query:filter()
return clib.ecs_query_get_filter(self)
end
function Query:is_changed()
return clib.ecs_query_changed(self, nil)
end
function Query:is_orphaned()
return clib.ecs_query_orphaned(self)
end
function Query:count()
return clib.ecs_query_entity_count(self)
end
function Query:entity()
return clib.ecs_query_entity(self)
end
-- }}}
-- flecs.Iter {{{
local Iter = aux.class()
flecs.Iter = Iter
function Iter:is_true()
return clib.ecs_iter_is_true(self)
end
function Iter:first()
return clib.ecs_iter_first(self)
end
function Iter:world()
return self._world
end
function Iter:entity(index)
return self._entities[index]
end
function Iter:system()
return self._system
end
function Iter:count()
return self._count
end
function Iter:field_is_readonly(j)
return clib.ecs_field_is_readonly(self, j)
end
function Iter:field_is_writeonly(j)
return clib.ecs_field_is_writeonly(self, j)
end
function Iter:field_is_set(j)
return clib.ecs_field_is_set(self, j)
end
function Iter:field_id(j)
return clib.ecs_field_id(self, j)
end
function Iter:field_src(j)
return clib.ecs_field_src(self, j)
end
function Iter:field_size(j)
return clib.ecs_field_size(self, j)
end
function Iter:field_is_self(j)
return clib.ecs_field_is_self(self, j)
end
function Iter:field(j)
if clib.ecs_field_size(self, j) == 0 then
return nil
end
local ctype = ffi.typeof(self:world():identifier(self:field_id(j)))
local pointer = clib.ecs_field_w_size(self, ffi.sizeof(ctype), j)
if self:field_is_readonly(j) then
return ffi.cast(ffi.typeof('$ const*', ctype), pointer)
else
return ffi.cast(ffi.typeof('$*', ctype), pointer)
end
end
function Iter:field_count()
return self._field_count
end
function Iter:fields(index)
index = index or 0
local fields = {}
for j = 1, self:field_count() do
local field = self:field(j)
if self:field_is_self(j) and field ~= nil then
table.insert(fields, field + index)
else
table.insert(fields, field)
end
end
return table.unpack(fields)
end
function Iter:each()
local i = 0
return function()
i = i + 1
if i <= self:count() then
return self:entity(i - 1), self:fields(i - 1)
end
end
end
function Iter:next()
return clib.ecs_iter_next(self)
end
function Iter:fini()
return clib.ecs_iter_fini(self)
end
function Iter:delta_time()
return self._delta_time
end
-- }}}
-- flecs.g {{{
local function bind_g(flecs)
flecs.g = {
MetaTypeSerialized = clib.FLECS__EEcsMetaTypeSerialized,
bool = clib.FLECS__Eecs_bool_t,
char = clib.FLECS__Eecs_char_t,
byte = clib.FLECS__Eecs_byte_t,
u8 = clib.FLECS__Eecs_u8_t,
u16 = clib.FLECS__Eecs_u16_t,
u32 = clib.FLECS__Eecs_u32_t,
u64 = clib.FLECS__Eecs_u64_t,
uptr = clib.FLECS__Eecs_uptr_t,
i8 = clib.FLECS__Eecs_i8_t,
i16 = clib.FLECS__Eecs_i16_t,
i32 = clib.FLECS__Eecs_i32_t,
i64 = clib.FLECS__Eecs_i64_t,
iptr = clib.FLECS__Eecs_iptr_t,
f32 = clib.FLECS__Eecs_f32_t,
f64 = clib.FLECS__Eecs_f64_t,
string = clib.FLECS__Eecs_string_t,
entity = clib.FLECS__Eecs_entity_t,
OpArray = clib.EcsOpArray,
OpVector = clib.EcsOpVector,
OpPush = clib.EcsOpPush,
OpPop = clib.EcsOpPop,
OpScope = clib.EcsOpScope,
OpEnum = clib.EcsOpEnum,
OpBitmask = clib.EcsOpBitmask,
OpPrimitive = clib.EcsOpPrimitive,
OpBool = clib.EcsOpBool,
OpChar = clib.EcsOpChar,
OpByte = clib.EcsOpByte,
OpU8 = clib.EcsOpU8,
OpU16 = clib.EcsOpU16,
OpU32 = clib.EcsOpU32,
OpU64 = clib.EcsOpU64,
OpI8 = clib.EcsOpI8,
OpI16 = clib.EcsOpI16,
OpI32 = clib.EcsOpI32,
OpI64 = clib.EcsOpI64,
OpF32 = clib.EcsOpF32,
OpF64 = clib.EcsOpF64,
OpUPtr = clib.EcsOpUPtr,
OpIPtr = clib.EcsOpIPtr,
OpString = clib.EcsOpString,
OpEntity = clib.EcsOpEntity,
-- Builtin component ids
Component = clib.FLECS__EEcsComponent,
Identifier = clib.FLECS__EEcsIdentifier,
Iterable = clib.FLECS__EEcsIterable,
Poly = clib.FLECS__EEcsPoly,
Query = clib.EcsQuery,
Observer = clib.EcsObserver,
System = clib.EcsSystem,
TickSource = clib.FLECS__EEcsTickSource,
Timer = clib.FLECS__EEcsTimer,
RateFilter = clib.FLECS__EEcsRateFilter,
Flecs = clib.EcsFlecs,
FlecsCore = clib.EcsFlecsCore,
World = clib.EcsWorld,
Wildcard = clib.EcsWildcard,
Any = clib.EcsAny,
This = clib.EcsThis,
Variable = clib.EcsVariable,
Transitive = clib.EcsTransitive,
Reflexive = clib.EcsReflexive,
Final = clib.EcsFinal,
DontInherit = clib.EcsDontInherit,
Symmetric = clib.EcsSymmetric,
Exclusive = clib.EcsExclusive,
Acyclic = clib.EcsAcyclic,
With = clib.EcsWith,
OneOf = clib.EcsOneOf,
Tag = clib.EcsTag,
Union = clib.EcsUnion,
Name = clib.EcsName,
Symbol = clib.EcsSymbol,
Alias = clib.EcsAlias,
ChildOf = clib.EcsChildOf,
IsA = clib.EcsIsA,
DependsOn = clib.EcsDependsOn,
SlotOf = clib.EcsSlotOf,
Module = clib.EcsModule,
Private = clib.EcsPrivate,
Prefab = clib.EcsPrefab,
Disabled = clib.EcsDisabled,
OnAdd = clib.EcsOnAdd,
OnRemove = clib.EcsOnRemove,
OnSet = clib.EcsOnSet,
UnSet = clib.EcsUnSet,
Monitor = clib.EcsMonitor,
OnDelete = clib.EcsOnDelete,
OnTableEmpty = clib.EcsOnTableEmpty,
OnTableFill = clib.EcsOnTableFill,
OnDeleteTarget = clib.EcsOnDeleteTarget,
Remove = clib.EcsRemove,
Delete = clib.EcsDelete,
Panic = clib.EcsPanic,
DefaultChildComponent = clib.EcsDefaultChildComponent,
Empty = clib.EcsEmpty,
Pipeline = clib.FLECS__EEcsPipeline,
PreFrame = clib.EcsPreFrame,
OnLoad = clib.EcsOnLoad,
PostLoad = clib.EcsPostLoad,
PreUpdate = clib.EcsPreUpdate,
OnUpdate = clib.EcsOnUpdate,
OnValidate = clib.EcsOnValidate,
PostUpdate = clib.EcsPostUpdate,
PreStore = clib.EcsPreStore,
OnStore = clib.EcsOnStore,
PostFrame = clib.EcsPostFrame,
Phase = clib.EcsPhase,
}
end
-- }}}
-- flecs.init {{{
function flecs:bind_metatypes()
self.World = ffi.metatype('ecs_world_t', World)
self.Query = ffi.metatype('ecs_query_t', Query)
self.Filter = ffi.metatype('ecs_filter_t', Filter)
self.Iter = ffi.metatype('ecs_iter_t', Iter)
end
function flecs:init(optionsOrNil)
local options = optionsOrNil or {}
if clib == nil then
if options.clib then
clib = ffi.load(options.clib)
else
clib = ffi.C
end
if options.cdef then
ffi.cdef(options.cdef)
end
if options.world then
self.world = ffi.cast('ecs_world_t*', options.world)
end
if options.bind_metatypes then
self:bind_metatypes(options)
end
bind_g(self)
end
return self
end
-- }}}
return setmetatable(flecs, { __call = flecs.init })