-
Notifications
You must be signed in to change notification settings - Fork 1
/
felecs.rb
946 lines (831 loc) · 33.6 KB
/
felecs.rb
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
# frozen_string_literal: true
module FelECS
class Entities
# Creating a new Entity
# @param components [Components] Can be any number of components, identical duplicates will be automatically purged however different components from the same component manager are allowed.
# @return [Entity]
def initialize(*components)
# Add each component
add(*components)
self.class._data.push self
end
# A hash that uses component manager constant names as keys, and where the values of those keys are arrays that contain the the components attached to this entity.
# @return [Hash<Component_Manager, Array<Integer>>]
def components
@components ||= {}
end
# A single component from a component manager. Use this if you expect the component to only belong to one entity and you want to access it. Access the component using either parameter notation or array notation. Array notation is conventional for better readablility.
# @example
# @entity.component[@component_manager] # array notation(the standard)
# @entity.component(@component_manager) # method notation
# @param manager [ComponentManager] If you pass nil you can then use array notation to access the same value.
# @return [Component]
def component(manager = nil)
if manager.nil?
FelECS::Entities.component_redirect.entity = self
FelECS::Entities.component_redirect
else
if components[manager].nil?
raise "This entity(#{self}) doesnt have any components of this type: #{manager}"
elsif components[manager].length > 1
Warning.warn("This entity has MANY of this component but you called the method that is intended for having a single of this component type.\nYou may have a bug in your logic.")
end
components[manager].first
end
end
# Removes this Entity from the list and purges all references to this Entity from other Components, as well as its data.
# @return [Boolean] +true+
def delete
components.each do |_component_manager, component_array|
component_array.reverse_each do |component|
component.entities.delete(self)
end
end
FelECS::Entities._data.delete self
@components = {}
true
end
# Add any number components to the Entity.
# @param components_to_add [Component] Any number of components created from any component manager
# @return [Boolean] +true+
def add(*components_to_add)
components_to_add.each do |component|
if components[component.class].nil?
components[component.class] = [component]
component.entities.push self
check_systems component, :addition_triggers
elsif !components[component.class].include? component
components[component.class].push component
component.entities.push self
check_systems component, :addition_triggers
end
end
true
end
# triggers every system associated with this component's trigger
# @return [Boolean] +true+
# @!visibility private
def check_systems(component, trigger_type)
component_calls = component.class.send(trigger_type)
component.send(trigger_type).each do |system|
component_calls |= [system]
end
component_calls.sort_by(&:priority).reverse.each(&:call)
true
end
# Remove a component from the Entity
# @param components_to_remove [Component] A component created from any component manager
# @return [Boolean] +true+
def remove(*components_to_remove)
components_to_remove.each do |component|
check_systems component, :removal_triggers if component.entities.include? self
component.entities.delete self
components[component.class].delete component
components.delete component.class if components[component.class].empty?
end
true
end
# Export all data into a JSON String which can then be saved into a file
# TODO: This function is not yet complete
# @return [String] A JSON formatted String
# def to_json() end
class << self
# Selects every entity that has components of they type passed into
# this function and then executes the code block for those components
# and entities.
# @return [Nil]
def group(*component_managers, &block)
return nil if component_managers.empty?
if component_managers.length == 1
component_managers.first.each do |cmp|
block.call(cmp, cmp.entity)
end
else
arry = component_managers.first.select do |cmp|
ent = cmp.entity
keep = true
component_managers.drop(1).each do |mgr|
next unless ent.components[mgr].nil?
keep = false
break
end
keep
end
arry.each do |cmp|
ent = cmp.entity
cmp_arry = [cmp]
component_managers.drop(1).each do |cmp_mgr|
cmp_arry.push ent.component[cmp_mgr]
end
block.call(*cmp_arry, ent)
end
end
nil
end
# Makes component managers behave like arrays with additional
# methods for managing the array
# @!visibility private
def respond_to_missing?(method, *)
if _data.respond_to? method
true
else
super
end
end
# Makes component managers behave like arrays with additional
# methods for managing the array
# @!visibility private
def method_missing(method, *args, **kwargs, &block)
if _data.respond_to? method
_data.send(method, *args, **kwargs, &block)
else
super
end
end
# Fancy method redirection for when the `component` method is called
# in an Entity
# WARNING: This method will not correctly work with multithreading
# @!visibility private
def component_redirect
if @component_redirect
else
@component_redirect = Object.new
@component_redirect.instance_variable_set(:@entity, nil)
@component_redirect.define_singleton_method(:entity) do
instance_variable_get(:@entity)
end
@component_redirect.define_singleton_method(:entity=) do |value|
instance_variable_set(:@entity, value)
end
@component_redirect.define_singleton_method(:[]) do |component_manager|
entity.component(component_manager)
end
end
@component_redirect
end
# @return [Array<Entity>] Array of all Entities that exist
# @!visibility private
def _data
@data ||= []
end
# Creates a new entity using the data from a JSON string
# TODO: This function is not yet complete
# @param json_string [String] A string that was exported originally using the {FelECS::Entities#to_json to_json} function
# @param opts [Keywords] What values(its {FelECS::Entities#id ID} or the {FelECS::ComponentManager#id component IDs}) should be overwritten TODO: this might change
# def from_json(json_string, **opts) end
end
end
end
# frozen_string_literal: true
module FelECS
module Components
@component_map = []
class << self
# Creates a new {FelECS::ComponentManager component manager}.
#
# @example
# # Here color is set to default to red
# # while max and current are nil until set.
# # When you make a new component using this component manager
# # these are the values and accessors it will have.
# FelECS::Component.new('Health', :max, :current, color: 'red')
#
# @param component_name [String] Name of your new component manager. Must be stylized in the format of constants in Ruby
# @param attrs [:Symbols] New components made with this manager will include these symbols as accessors, the values of these accessors will default to nil
# @param attrs_with_defaults [Keyword: DefaultValue] New components made with this manager will include these keywords as accessors, their defaults set to the values given to the keywords
# @return [ComponentManager]
def new(component_name, *attrs, **attrs_with_defaults)
if FelECS::Components.const_defined?(component_name)
raise(NameError.new, "Component Manager '#{component_name}' is already defined")
end
const_set(component_name, Class.new(FelECS::ComponentManager) {})
update_const_cache
attrs.each do |attr|
if FelECS::Components.const_get(component_name).method_defined?(attr.to_s) || FelECS::Components.const_get(component_name).method_defined?("#{attr}=")
raise NameError, "The attribute name \"#{attr}\" is already a method"
end
FelECS::Components.const_get(component_name).attr_accessor attr
end
attrs_with_defaults.each do |attr, _default|
attrs_with_defaults[attr] = _default.dup
FelECS::Components.const_get(component_name).attr_reader attr
FelECS::Components.const_get(component_name).define_method("#{attr}=") do |value|
unless value.equal? send(attr)
instance_variable_set("@#{attr}", value)
attr_changed_trigger_systems(attr)
end
end
end
FelECS::Components.const_get(component_name).define_method(:set_defaults) do
attrs_with_defaults.each do |attr, default|
instance_variable_set("@#{attr}", default.dup)
end
end
FelECS::Components.const_get(component_name)
end
# Stores the components managers in {FelECS::Components}. This
# is needed because calling `FelECS::Components.constants`
# will not let you iterate over the value of the constants
# but will instead give you an array of symbols. This caches
# the convertion of those symbols to the actual value of the
# constants
# @!visibility private
def const_cache
@const_cache || update_const_cache
end
# Updates the array that stores the constants.
# Used internally by FelECS
# @!visibility private
def update_const_cache
@const_cache = constants.map do |constant|
const_get constant
end
end
# Forwards undefined methods to the array of constants
# if the array can handle the request. Otherwise tells
# the programmer their code errored
# @!visibility private
def respond_to_missing?(method, *)
if const_cache.respond_to? method
true
else
super
end
end
# Makes component module behave like arrays with additional
# methods for managing the array
# @!visibility private
def method_missing(method, *args, **kwargs, &block)
if const_cache.respond_to? method
const_cache.send(method, *args, **kwargs, &block)
else
super
end
end
end
end
# Component Managers are what is used to create individual components which can be attached to entities.
# When a Component is created from a Component Manager that has accessors given to it, you can set or get the values of those accessors using standard ruby message sending (e.g [email protected] = 5+), or by using the {#to_h} and {#update_attrs} methods instead.
class ComponentManager
# Allows overwriting the storage of triggers, such as for clearing.
# This method should generally only need to be used internally and
# not by a game developer.
# @!visibility private
attr_writer :addition_triggers, :removal_triggers, :attr_triggers
# Stores references to systems that should be triggered when a
# component from this manager is added.
# Do not edit this array as it is managed by FelECS automatically.
# @return [Array<System>]
def addition_triggers
@addition_triggers ||= []
end
# Stores references to systems that should be triggered when a
# component from this manager is removed.
# Do not edit this array as it is managed by FelECS automatically.
# @return [Array<System>]
def removal_triggers
@removal_triggers ||= []
end
# Stores references to systems that should be triggered when an
# attribute from this manager is changed.
# Do not edit this hash as it is managed by FelECS automatically.
# @return [Hash<Symbol, Array<System>>]
def attr_triggers
@attr_triggers ||= {}
end
# Creates a new component and sets the values of the attributes given to it. If an attritbute is not passed then it will remain as the default.
# @param attrs [Keyword: Value] You can pass any number of Keyword-Value pairs
# @return [Component]
def initialize(**attrs)
# Prepare the object
# (this is a function created with metaprogramming
# in FelECS::Components)
set_defaults
# Fill params
attrs.each do |key, value|
send "#{key}=", value
end
# Save Component
self.class.push self
end
class << self
# Makes component managers behave like arrays with additional
# methods for managing the array
# @!visibility private
def respond_to_missing?(method, *)
if _data.respond_to? method
true
else
super
end
end
# Makes component managers behave like arrays with additional
# methods for managing the array
# @!visibility private
def method_missing(method, *args, **kwargs, &block)
if _data.respond_to? method
_data.send(method, *args, **kwargs, &block)
else
super
end
end
# Allows overwriting the storage of triggers, such as for clearing.
# This method should generally only need to be used internally and
# not by a game developer.
# @!visibility private
attr_writer :addition_triggers, :removal_triggers, :attr_triggers
# Stores references to systems that should be triggered when this
# component is added to an enitity.
# Do not edit this array as it is managed by FelECS automatically.
# @return [Array<System>]
def addition_triggers
@addition_triggers ||= []
end
# Stores references to systems that should be triggered when this
# component is removed from an enitity.
# Do not edit this array as it is managed by FelECS automatically.
# @return [Array<System>]
def removal_triggers
@removal_triggers ||= []
end
# Stores references to systems that should be triggered when an
# attribute from this component changed.
# Do not edit this hash as it is managed by FelECS automatically.
# @return [Hash<Symbol, System>]
def attr_triggers
@attr_triggers ||= {}
end
# @return [Array<Component>] Array of all Components that belong to a given component manager
# @!visibility private
def _data
@data ||= []
end
end
# Entities that have this component
# @return [Array<Component>]
def entities
@entities ||= []
end
# A single entity. Use this if you expect the component to only belong to one entity and you want to access it.
# @return [Component]
def entity
if entities.empty?
Warning.warn("This component belongs to NO entities but you called the method that is intended for components belonging to a single entity.\nYou may have a bug in your logic.")
elsif entities.length > 1
Warning.warn("This component belongs to MANY entities but you called the method that is intended for components belonging to a single entity.\nYou may have a bug in your logic.")
end
entities.first
end
# Update attribute values using a hash or keywords.
# @return [Hash<Symbol, Value>] Hash of updated attributes
def update_attrs(**opts)
opts.each do |key, value|
send "#{key}=", value
end
end
# Execute systems that have been added to execute on variable change
# @return [Boolean] +true+
# @!visibility private
def attr_changed_trigger_systems(attr)
systems_to_execute = self.class.attr_triggers[attr]
systems_to_execute = [] if systems_to_execute.nil?
systems_to_execute |= attr_triggers[attr] unless attr_triggers[attr].nil?
systems_to_execute.sort_by(&:priority).reverse_each(&:call)
true
end
# Removes this component from the list and purges all references to this Component from other Entities, as well as its data.
# @return [Boolean] +true+.
def delete
addition_triggers.each do |system|
system.clear_triggers component_or_manager: self
end
entities.reverse_each do |entity|
entity.remove self
end
self.class._data.delete self
instance_variables.each do |var|
instance_variable_set(var, nil)
end
true
end
# @return [Hash<Symbol, Value>] A hash, where all the keys are attributes storing their respective values.
def to_h
return_hash = instance_variables.each_with_object({}) do |key, final|
final[key.to_s.delete_prefix('@').to_sym] = instance_variable_get(key)
end
return_hash.delete(:attr_triggers)
return_hash
end
# Export all data into a JSON String, which could then later be loaded or saved to a file
# TODO: This function is not yet complete
# @return [String] a JSON formatted String
# def to_json
# # should return a json or hash of all data in this component
# end
end
end
# frozen_string_literal: true
module FelECS
class Systems
# How early this System should be executed in a list of Systems
attr_accessor :priority
# The Constant name assigned to this System
# Allows overwriting the storage of triggers, such as for clearing.
# This method should generally only need to be used internally and
# not by a game developer.
# @!visibility private
attr_writer :addition_triggers, :removal_triggers, :attr_triggers
# Stores all the scenes this system is a part of.
attr_writer :scenes
def scenes
@scenes ||= []
end
def priority=(priority)
@priority = priority
scenes.each do |scene|
scene.systems = scene.systems.sort_by(&:priority)
end
end
# Stores references to components or their managers that trigger
# this component when a component or component from that manager
# is added to an entity.
# Do not edit this hash as it is managed by FelECS automatically.
# @return [Array<Component>]
def addition_triggers
@addition_triggers ||= []
end
# Stores references to components or their managers that trigger
# this component when a component or component from that manager
# is removed from an entity.
# Do not edit this hash as it is managed by FelECS automatically.
# @return [Array<Component>]
def removal_triggers
@removal_triggers ||= []
end
# Stores references to systems that should be triggered when an
# attribute from this manager is changed
# Do not edit this hash as it is managed by FelECS automatically.
# @return [Hash<Symbol, Array<Symbol>>]
def attr_triggers
@attr_triggers ||= {}
end
class << self
# Stores the systems in {FelECS::Components}. This
# is needed because calling `FelECS::Components.constants`
# will not let you iterate over the value of the constants
# but will instead give you an array of symbols. This caches
# the convertion of those symbols to the actual value of the
# constants
def const_cache
@const_cache || update_const_cache
end
# Updates the array that stores the constants.
# Used internally by FelECS
# @!visibility private
def update_const_cache
@const_cache = constants.map do |constant|
const_get constant
end
end
# Forwards undefined methods to the array of constants
# if the array can handle the request. Otherwise tells
# the programmer their code errored
# @!visibility private
def respond_to_missing?(method, *)
if const_cache.respond_to? method
true
else
super
end
end
# Makes system module behave like arrays with additional
# methods for managing the array
# @!visibility private
def method_missing(method, *args, **kwargs, &block)
if const_cache.respond_to? method
const_cache.send(method, *args, **kwargs, &block)
else
super
end
end
end
# Creates a new System which can be accessed as a constant under the namespace {FelECS::Systems}.
# The name given is what constant the system is assigned to
#
# @example
# FelECS::Systems.new('PassiveHeal', priority: -2) do
# FelECS::Components::Health.each do |component|
# component.hp += 5
# end
# end
# # Give it a low priority so other systems such as a
# # Poison system would kill the player first
#
# @param name [String] The name this system will use. Needs to to be in the Ruby Constant format.
# @param priority [Integer] Which priority order this system should be executed in relative to other systems. Higher means executed earlier.
# @param block [Proc] The code you wish to be executed when the system is triggered. Can be defined by using a +do end+ block or using +{ }+ braces.
def initialize(name, priority: 0, &block)
FelECS::Systems.const_set(name, self)
FelECS::Systems.update_const_cache
@priority = priority
@block = block
@scenes = []
end
# Manually execute the system a single time
def call
@block.call
end
# Redefine what code is executed by this System when it is called upon.
# @param block [Proc] The code you wish to be executed when the system is triggered. Can be defined by using a +do end+ block or using +{ }+ braces.
def redefine(&block)
@block = block
end
# Removes triggers from this system. This function is fairly flexible so it can accept a few different inputs
# For addition and removal triggers, you can optionally pass in a component, or a manager to clear specifically
# the relevant triggers for that one component or manager. If you do not pass a component or manager then it will
# clear triggers for all components and managers.
# For attr_triggers
# @example
# # To clear all triggers that execute this system when a component is added:
# FelECS::Systems::ExampleSystem.clear_triggers :addition_triggers
# # Same as above but for when a component is removed instead
# FelECS::Systems::ExampleSystem.clear_triggers :removal_triggers
# # Same as above but for when a component has a certain attribute changed
# FelECS::Systems::ExampleSystem.clear_triggers :attr_triggers
#
# # Clear a trigger from a specific component
# FelECS::Systems::ExampleSystem.clear_triggers :addition_triggers, FelECS::Component::ExampleComponent[0]
# # Clear a trigger from a specific component manager
# FelECS::Systems::ExampleSystem.clear_triggers :addition_triggers, FelECS::Component::ExampleComponent
#
# # Clear the trigger that executes a system when the ':example_attr' is changes
# FelECS::Systems::ExampleSystem.clear_triggers :attr_triggers, :example_attr
# @param trigger_types [:Symbols] One or more of the following trigger types: +:addition_triggers+, +:removal_triggers+, or +:attr_triggers+. If attr_triggers is used then you may pass attributes you wish to be cleared as symbols in this parameter as well
# @param component_or_manager [Component or ComponentManager] The object to clear triggers from. Use Nil to clear triggers from all components associated with this system.
# @return [Boolean] +true+
def clear_triggers(*trigger_types, component_or_manager: nil)
trigger_types = %i[addition_triggers removal_triggers attr_triggers] if trigger_types.empty?
if trigger_types.include? :attr_triggers
if (trigger_types - %i[addition_triggers
removal_triggers
attr_triggers]).empty?
if component_or_manager.nil?
# remove all attrs
attr_triggers.each do |cmp_or_mgr, attrs|
attrs.each do |attr|
next if cmp_or_mgr.attr_triggers[attr].nil?
cmp_or_mgr.attr_triggers[attr].delete self
end
self.attr_triggers = {}
end
else
# remove attrs relevant to comp_or_man
unless attr_triggers[component_or_manager].nil?
attr_triggers[component_or_manager].each do |attr|
component_or_manager.attr_triggers[attr].delete self
end
attr_triggers[component_or_manager] = []
end
end
elsif component_or_manager.nil?
(trigger_types - %i[addition_triggers removal_triggers attr_triggers]).each do |attr|
# remove attr
attr_triggers.each do |cmp_or_mgr, _attrs|
cmp_or_mgr.attr_triggers[attr].delete self
end
end
attr_triggers.delete(trigger_types - %i[addition_triggers
removal_triggers
attr_triggers])
else
# remove attr from component_or_manager
(trigger_types - %i[addition_triggers removal_triggers attr_triggers]).each do |attr|
next if component_or_manager.attr_triggers[attr].nil?
component_or_manager.attr_triggers[attr].delete self
end
attr_triggers[component_or_manager] -= trigger_types unless attr_triggers[component_or_manager].nil?
end
end
(trigger_types & %i[removal_triggers addition_triggers] - [:attr_triggers]).each do |trigger_type|
if component_or_manager.nil?
# remove all removal triggers
send(trigger_type).each do |trigger|
trigger.send(trigger_type).delete self
end
send("#{trigger_type}=", [])
else
# remove removal trigger relevant to comp/man
send(trigger_type).delete component_or_manager
component_or_manager.send(trigger_type).delete self
end
end
true
end
# Add a component or component manager so that it triggers this system when the component or a component from the component manager is added to an entity
# @param component_or_manager [Component or ComponentManager] The component or component manager to trigger this system when added
# @return [Boolean] +true+
def trigger_when_added(component_or_manager)
self.addition_triggers |= [component_or_manager]
component_or_manager.addition_triggers |= [self]
true
end
# Add a component or component manager so that it triggers this system when the component or a component from the component manager is removed from an entity
# @param component_or_manager [Component or ComponentManager] The component or component manager to trigger this system when removed
# @return [Boolean] +true+
def trigger_when_removed(component_or_manager)
self.removal_triggers |= [component_or_manager]
component_or_manager.removal_triggers |= [self]
true
end
# Add a component or component manager so that it triggers this system when a component's attribute is changed.
# @return [Boolean] +true+
def trigger_when_is_changed(component_or_manager, attr)
if component_or_manager.attr_triggers[attr].nil?
component_or_manager.attr_triggers[attr] = [self]
else
component_or_manager.attr_triggers[attr] |= [self]
end
if attr_triggers[component_or_manager].nil?
attr_triggers[component_or_manager] = [attr]
else
attr_triggers[component_or_manager] |= [attr]
end
true
end
end
end
# frozen_string_literal: true
module FelECS
class Scenes
# Allows overwriting the storage of systems, such as for clearing.
# This method should generally only need to be used internally and
# not by a game developer/
# @!visibility private
attr_writer :systems
# How early this Scene should be executed in a list of Scenes
attr_accessor :priority
def priority=(priority)
@priority = priority
FelECS::Stage.scenes = FelECS::Stage.scenes.sort_by(&:priority)
priority
end
# Create a new Scene using the name given
# @param name [String] String format must follow requirements of a constant
def initialize(name, priority: 0)
self.priority = priority
FelECS::Scenes.const_set(name, self)
end
# The list of Systems this Scene contains
# @return [Array<System>]
def systems
@systems ||= []
end
# Execute all systems in this Scene, in the order of their priority
# @return [Boolean] +true+
def call
systems.each(&:call)
true
end
# Adds any number of Systems to this Scene
# @return [Boolean] +true+
def add(*systems_to_add)
self.systems |= systems_to_add
self.systems = systems.sort_by(&:priority)
systems_to_add.each do |system|
system.scenes |= [self]
end
true
end
# Removes any number of Systems from this Scene
# @return [Boolean] +true+
def remove(*systems_to_remove)
self.systems -= systems_to_remove
true
end
# Removes all Systems from this Scene
# @return [Boolean] +true+
def clear
systems.each do |system|
system.scenes.delete self
end
systems.clear
# FelECS::Stage.update_systems_list if FelECS::Stage.scenes.include? self
true
end
end
end
# frozen_string_literal: true
module FelECS
module Stage
class << self
# Allows clearing of scenes and systems.
# Used internally by FelECS and shouldn't need to be ever used by developers
# @!visibility private
attr_writer :scenes
# Add any number of Scenes to the Stage
# @return [Boolean] +true+
def add(*scenes_to_add)
self.scenes |= scenes_to_add
self.scenes = scenes.sort_by(&:priority)
true
end
# Remove any number of Scenes from the Stage
# @return [Boolean] +true+
def remove(*scenes_to_remove)
self.scenes -= scenes_to_remove
true
end
# Clears all Scenes that were added to the Stage
# @return [Boolean] +true+
def clear
self.scenes.clear
true
end
# Executes one frame of the game. This executes all the Scenes added to the Stage in order of their priority.
# @return [Boolean] +true+
def call
self.scenes.each(&:call)
true
end
# Contains all the Scenes added to the Stage
# @return [Array<Scene>]
def scenes
@scenes ||= []
end
end
end
end
# frozen_string_literal: true
module FelECS
module Order
# Sets the priority of all items passed into this method
# according to the order they were passed.
# If an array is one of the elements then it will give all
# of those elements in the array the same priority.
# @param sortables [(Systems and Array<Systems>) or (Scenes and Array<Scenes>)]
# @return [Boolean] +true+.
def self.sort(*sortables)
sortables.each_with_index do |sorted, index|
if sorted.respond_to? :priority
sorted.priority = index
else
sorted.each do |item|
item.priority = index
end
end
end
true
end
end
end
# frozen_string_literal: true
# :nocov:
# Keeps the version of the Gem
module FelECS
# The version of the Gem
VERSION = '5.0.1'
end
# :nocov:
# frozen_string_literal: true
# The FelECS namespace where all its functionality resides under.
module FelECS
class << self
# :nocov:
# An alias for {FelECS::Stage.call}. It executes a single frame in the game.
def call
FelECS::Stage.call
end
# :nocov:
end
# Creates and manages Entities. Entities are just collections of Components.
# You can use array methods directly on this class to access Entities.
class Entities; end
# Creates component managers and allows accessing them them under the {FelECS::Components} namespace as Constants.
# You can use array methods directly on this class to access Component Managers.
#
# To see how component managers are used please look at the {FelECS::ComponentManager} documentation.
module Components; end
# Creates and manages Systems. Systems are the logic of the game and do not contain any data within them. Any systems you create are accessable under the {FelECS::Systems} namespace as Constants.
# You can use array methods directly on this class to access Systems.
class Systems; end
# Creates and manages Scenes. Scenes are collections of Systems, and execute all the Systems when called upon. Any scenes you create are accessable under the {FelECS::Scenes} namespace as Constants.
class Scenes; end
# Stores Scenes you add to it which you want to execute on each frame. When called upon will execute all Systems in the Scenes in the Stage and will execute them according to their priority order.
module Stage; end
# Sets the priority of a list of Systems or Scenes for you in the order you pass them to this class.
module Order; end
end
# An alias for {FelECS}
FECS = FelECS
# An alias for {FelECS::Entities}
FECS::Ent = FelECS::Entities
# An alias for {FelECS::Components}
FECS::Cmp = FelECS::Components
# An alias for {FelECS::Systems}
FECS::Sys = FelECS::Systems
# An alias for {FelECS::Scenes}
FECS::Scn = FelECS::Scenes
# An alias for {FelECS::Stage}
FECS::Stg = FelECS::Stage
# An alias for {FelECS::
FECS::Odr = FelECS::Order