diff --git a/lib/ecs/entity_registry.rb b/lib/ecs/entity_registry.rb index faa4b62..14c9235 100644 --- a/lib/ecs/entity_registry.rb +++ b/lib/ecs/entity_registry.rb @@ -5,11 +5,12 @@ module ECS class EntityRegistry include Logging - attr_reader :entities, :entities_by_tag, :entity_components, + attr_reader :world, :entities, :entities_by_tag, :entity_components, :component_entities # :reek:DuplicateMethodCall - def initialize + def initialize(world:) + @world = world @entities = [] @entities_by_tag = Hash.new { |hash, key| hash[key] = [] } @entity_components = Hash.new { |hash, key| hash[key] = [] } diff --git a/lib/ecs/system.rb b/lib/ecs/system.rb index 3950a6c..b3fdcc9 100644 --- a/lib/ecs/system.rb +++ b/lib/ecs/system.rb @@ -3,8 +3,9 @@ module ECS # Basic system implementation # - # System must implement method +run+ that accepts +time_delta+ keyword - # argument + # System must implement method +process_entoty+ that accepts +entity+ and + # components specified with +watch_components+ method (if any) or redefine + # +run+ for special cases. # # @see ECS::Systems::Movement#process_tick Movement system implementation # @example @@ -124,14 +125,17 @@ def debugging? private + # @api private def ensure_world raise 'World must be set. System cannot work without it.' unless world end + # @api private def log_run logger.debug { "Running #{self.class.name} with delta #{time_delta} ms" } end + # @api private def log_data(entity, components) logger.debug do "Processing entity #{entity} with components #{components.inspect}" diff --git a/lib/ecs/world.rb b/lib/ecs/world.rb index 39d7990..f57369f 100644 --- a/lib/ecs/world.rb +++ b/lib/ecs/world.rb @@ -12,10 +12,10 @@ class World # :reek:Attribute { enabled: false } attr_accessor :time_delta - def initialize(width:, height:, entity_registry: EntityRegistry.new) + def initialize(width:, height:) @width = width @height = height - @entity_registry = entity_registry + @entity_registry = EntityRegistry.new(world: self) @update_systems = Set.new @draw_systems = Set.new diff --git a/spec/unit/ecs/entity_registry_spec.rb b/spec/unit/ecs/entity_registry_spec.rb index 29c0778..1d72584 100644 --- a/spec/unit/ecs/entity_registry_spec.rb +++ b/spec/unit/ecs/entity_registry_spec.rb @@ -3,7 +3,8 @@ require 'ecs_helper' RSpec.describe ECS::EntityRegistry do - let(:registry) { described_class.new } + let(:registry) { world.entity_registry } + let(:world) { ECS::World.new(width: 100, height: 100) } describe '#create_entity' do subject(:create_entity) { registry.create_entity } diff --git a/spec/unit/ecs/system_spec.rb b/spec/unit/ecs/system_spec.rb index bec10af..d344e91 100644 --- a/spec/unit/ecs/system_spec.rb +++ b/spec/unit/ecs/system_spec.rb @@ -20,13 +20,13 @@ class Bar < ECS::Component ->(bl) { system.entities_with(:foo, :bar, &bl) } end - let(:entity_registry) { ECS::EntityRegistry.new } + let(:world) { ECS::World.new(width: 100, height: 100) } + let(:entity_registry) { world.entity_registry } let(:entity) { entity_registry.create_entity } let(:foo_component) { Foo.new } let(:bar_component) { Bar.new } before do - world = instance_double('ECS::World', entity_registry: entity_registry) system.world = world entity_registry.add_component(entity, foo_component) @@ -41,13 +41,13 @@ class Bar < ECS::Component describe '#component' do subject(:result) { system.component(entity, component_name) } - let(:entity_registry) { ECS::EntityRegistry.new } + let(:world) { ECS::World.new(width: 100, height: 100) } + let(:entity_registry) { world.entity_registry } let(:entity) { entity_registry.create_entity } let(:component) { Foo.new } let(:component_name) { :foo } before do - world = instance_double('ECS::World', entity_registry: entity_registry) system.world = world entity_registry.add_component(entity, component)