Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: entitas
version: 1.4.5
version: 1.5.0

authors:
- Holden Omans <holden.omans@gmail.com>

crystal: ">= 0.36.1, < 2.0.0"
crystal: ">= 1.14.0, < 2.0.0"

license: MIT

Expand Down
6 changes: 3 additions & 3 deletions spec/entitas/events_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/entitas/collector.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module Entitas
self.entities.size
end

def each
def each(& : TEntity ->)
self.entities.each do |entity|
yield entity
end
Expand Down
2 changes: 1 addition & 1 deletion src/entitas/contexts.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/entitas/controller.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/entitas/generators/component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/entitas/interfaces/i_component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 12 additions & 15 deletions src/entitas/macros/component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module Entitas::IComponent
{% end %} # end verbatim do
end

{{@type.id}}.create_initializers
create_initializers
end
end

Expand Down Expand Up @@ -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 %}
Expand Down Expand Up @@ -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 %}
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/entitas/macros/events.cr
Original file line number Diff line number Diff line change
@@ -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.
#
Expand All @@ -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 %}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 %}
Expand All @@ -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

Expand All @@ -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?)
Expand Down
2 changes: 1 addition & 1 deletion src/entitas/multi_reactive_system.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/entitas/reactive_system.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading