diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5215620..494c193 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,7 +6,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest] - crystal: [1.0.0, latest, nightly] + crystal: [1.14.0, latest, nightly] runs-on: ${{ matrix.os }} steps: - name: Checkout diff --git a/shard.yml b/shard.yml index 877c4e9..f84fc56 100644 --- a/shard.yml +++ b/shard.yml @@ -1,10 +1,10 @@ name: entitas -version: 1.4.5 +version: 1.5.0 authors: - Holden Omans -crystal: ">= 0.36.1, < 2.0.0" +crystal: ">= 1.14.0, < 2.0.0" license: MIT diff --git a/spec/entitas/events_spec.cr b/spec/entitas/events_spec.cr index cdfd9c2..c8cb89f 100644 --- a/spec/entitas/events_spec.cr +++ b/spec/entitas/events_spec.cr @@ -23,7 +23,7 @@ private class RemoveEventTest property listener : Test4Entity property contexts : Contexts - property remove_comp_when_empty : Bool + property? remove_comp_when_empty : Bool property value : String? = nil def initialize(@contexts, @remove_comp_when_empty) @@ -37,13 +37,13 @@ private class RemoveEventTest def on_standard_event(entity, component : StandardEvent) # logger.warn { "on_standard_event" } - @listener.remove_any_standard_event_listener(self, remove_comp_when_empty) + @listener.remove_any_standard_event_listener(self, remove_comp_when_empty?) @value = component.value end def on_flag_entity_event(entity, component : FlagEntityEvent) # logger.warn { "on_flag_entity_event" } - listener.remove_flag_entity_event_listener(self, remove_comp_when_empty) + listener.remove_flag_entity_event_listener(self, remove_comp_when_empty?) @value = "true" end end diff --git a/src/entitas/collector.cr b/src/entitas/collector.cr index d012bbc..436730e 100644 --- a/src/entitas/collector.cr +++ b/src/entitas/collector.cr @@ -91,7 +91,7 @@ module Entitas self.entities.size end - def each + def each(& : TEntity ->) self.entities.each do |entity| yield entity end diff --git a/src/entitas/contexts.cr b/src/entitas/contexts.cr index 2ed3bdd..1215dcb 100644 --- a/src/entitas/contexts.cr +++ b/src/entitas/contexts.cr @@ -23,7 +23,7 @@ class Entitas::Contexts self.all_contexts.each &.reset end - def each + def each(& : Entitas::IContext? ->) self.all_contexts.each do |ctx| yield ctx end diff --git a/src/entitas/controller.cr b/src/entitas/controller.cr index 7db235b..d21ad3a 100644 --- a/src/entitas/controller.cr +++ b/src/entitas/controller.cr @@ -12,7 +12,7 @@ module Entitas # Will allow you to interact with the returned `Contexts` with a `Mutex` lock # preventing `#update` from being called in another thread - def with_contexts + def with_contexts(& : Entitas::Contexts ->) self.synchronize do yield self.contexts end @@ -23,10 +23,11 @@ module Entitas end def find_systems(klass : Class) : Array(Entitas::System) - if self.systems.nil? + sys = self.systems + if sys.nil? Array(Entitas::System).new else - self.systems.not_nil!.find_systems(klass) + sys.find_systems(klass) end end diff --git a/src/entitas/generators/component.cr b/src/entitas/generators/component.cr index 129737a..79aced9 100644 --- a/src/entitas/generators/component.cr +++ b/src/entitas/generators/component.cr @@ -56,7 +56,7 @@ class Entitas::Component include Entitas::IComponent {% end %} # end if !comp.ancestors.includes?(Entitas::IComponent) - {{comp.id}}.setup_base_comp + setup_base_comp end ### Create a Helper module for the component diff --git a/src/entitas/interfaces/i_component.cr b/src/entitas/interfaces/i_component.cr index 4c1be33..e5ba8c8 100644 --- a/src/entitas/interfaces/i_component.cr +++ b/src/entitas/interfaces/i_component.cr @@ -2,7 +2,7 @@ module Entitas::IComponent Log = ::Log.for(self) # Will return true if the class is a unique component for a context - abstract def is_unique? : Bool + abstract def unique? : Bool abstract def init(**args) abstract def reset diff --git a/src/entitas/macros/component.cr b/src/entitas/macros/component.cr index 64ddef6..a7b8cbf 100644 --- a/src/entitas/macros/component.cr +++ b/src/entitas/macros/component.cr @@ -59,7 +59,7 @@ module Entitas::IComponent {% end %} # end verbatim do end - {{@type.id}}.create_initializers + create_initializers end end @@ -133,7 +133,7 @@ module Entitas::IComponent def to_json(json : JSON::Builder) json.object do json.field "name", {{@type.id.stringify}} - json.field "unique", is_unique? + json.field "unique", unique? json.field("data") do json.object do {% for var_name in comp_variables.keys %} @@ -206,32 +206,31 @@ module Entitas::IComponent # :nodoc: macro setup_unique + {% unique = @type.annotation(::Component::Unique) ? true : false %} - {% is_unique = @type.annotation(::Component::Unique) ? true : false %} - - {% if flag?(:entitas_debug_generator) %}{% puts " - setup_unique for #{@type.id} : #{is_unique}" %}{% end %} + {% if flag?(:entitas_debug_generator) %}{% puts " - setup_unique for #{@type.id} : #{unique}" %}{% end %} # If the component has the unique annotation, # set the class method to `true` # The framework will make sure that only one instance of a unique component can be present in your context - {% if is_unique %} + {% if unique %} # Will return true if the class is a unique component for a context - def is_unique? : Bool + def unique? : Bool true end # :ditto: - def self.is_unique? : Bool + def self.unique? : Bool true end {% else %} # Will return true if the class is a unique component for a context - def is_unique? : Bool + def unique? : Bool false end # :ditto: - def self.is_unique? : Bool + def self.unique? : Bool false end {% end %} @@ -244,10 +243,8 @@ module Entitas::IComponent {% raise "#{@type.id} is not a Entitas::IComponent" %} {% end %} - class ::{{@type.id}} - {{@type.id}}.setup_events - {{@type.id}}.setup_unique - end + setup_unique + setup_events end end @@ -279,7 +276,7 @@ class Entitas::Component {% comp_map[comp] = comp_methods %} {% end %} - alias ComponentTypes = Union(Entitas::Component.class, {{*comp_map.keys.map(&.name.+(".class"))}}) + alias ComponentTypes = Union(Entitas::Component.class, {{comp_map.keys.map(&.name.+(".class")).splat}}) {% if comp_map.empty? %} enum Index diff --git a/src/entitas/macros/events.cr b/src/entitas/macros/events.cr index 26b33c1..eedbe80 100644 --- a/src/entitas/macros/events.cr +++ b/src/entitas/macros/events.cr @@ -1,3 +1,5 @@ +require "../interfaces/i_component" + module Entitas::Events # Will create `Entitas::Events` struct for the provided `name`. `opts` defines the struct variables. # @@ -18,7 +20,7 @@ module Entitas::Events # end # ``` macro create_event(name, opts) - struct Entitas::Events::{{name.id}} + struct ::Entitas::Events::{{name.id}} {% for a, t in opts %} getter {{a.id}} : {{t.id}} {% end %} @@ -106,7 +108,7 @@ end macro emit_event(event, *args) {% if flag?(:entitas_enable_logging) %}Log.debug { "Emitting event {{event.id}}" }{% end %} - self.receive_{{event.id.underscore.id}}_event(Entitas::Events::{{event.id}}.new({{*args}})) + self.receive_{{event.id.underscore.id}}_event(Entitas::Events::{{event.id}}.new({{args.splat}})) end # Wrapper for multiple `accept_event` calls @@ -212,7 +214,7 @@ macro component_event(contexts, comp, target, _type = EventType::Added, priority {% if Entitas::Component.all_subclasses.find(&.name.==(listener_component_module.gsub(/^::/, ""))) %} {% if flag?(:entitas_debug_generator) %}{% puts " WARN: #{listener_component_module.id} already exists" %}{% end %} - @[::Context({{*contexts}})] + @[::Context({{contexts.splat}})] class {{listener_component_module.id}} < Entitas::Component; end {% else %} {% listener_component_meth_name = listener_component_name.underscore %} @@ -225,7 +227,9 @@ macro component_event(contexts, comp, target, _type = EventType::Added, priority {% end %} end - @[::Context({{*contexts}})] + {% if flag?(:entitas_debug_generator) %}{% puts "- defining listener #{listener_component_module.id}" %}{% end %} + + @[::Context({{contexts.splat}})] class {{listener_component_module.id}} < Entitas::Component prop :value, Set({{listener_module.id}}), default: Set({{listener_module.id}}).new @@ -250,7 +254,7 @@ macro component_event(contexts, comp, target, _type = EventType::Added, priority end def remove_{{listener_component_meth_name.id}}(value : {{listener_module.id}}, remove_comp_when_empty = false) - {% if flag?(:entitas_enable_logging) %}Log.debug { "remove_{{listener_component_meth_name.id}} - remove_comp_when_empty: #{remove_comp_when_empty}, value: #{value}" }{% end %} + {% if flag?(:entitas_enable_logging) %}Log.debug { "remove_{{listener_component_meth_name.id}} - remove_comp_when_empty: #{remove_comp_when_empty?}, value: #{value}" }{% end %} %listeners = self.{{listener_component_meth_name.id}}.value %listeners.delete(value) if(remove_comp_when_empty && %listeners.empty?) diff --git a/src/entitas/multi_reactive_system.cr b/src/entitas/multi_reactive_system.cr index 1f41e57..c759724 100644 --- a/src/entitas/multi_reactive_system.cr +++ b/src/entitas/multi_reactive_system.cr @@ -15,7 +15,7 @@ module Entitas private property collected_buffer : Array(IEntity) = Array(IEntity).new private property buffer : Array(IEntity) = Array(IEntity).new private property to_string_cache : String? = nil - protected property _filter : Proc(IEntity, Bool) = ->(entity : IEntity) { true } + protected property _filter : Proc(IEntity, Bool) = ->(_entity : IEntity) { true } def initialize(@collectors : Array(ICollector)); end diff --git a/src/entitas/reactive_system.cr b/src/entitas/reactive_system.cr index f3a8e4e..da07f65 100644 --- a/src/entitas/reactive_system.cr +++ b/src/entitas/reactive_system.cr @@ -14,7 +14,7 @@ module Entitas private getter collector : ICollector private property buffer : Array(Entitas::IEntity) = Array(Entitas::IEntity).new private property to_string_cache : String? = nil - protected property _filter : Proc(Entitas::IEntity, Bool) = ->(entity : Entitas::IEntity) { true } + protected property _filter : Proc(Entitas::IEntity, Bool) = ->(_entity : Entitas::IEntity) { true } def initialize(@collector : ICollector); end