Skip to content
Open
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
5 changes: 3 additions & 2 deletions lib/ecs/entity_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [] }
Expand Down
8 changes: 6 additions & 2 deletions lib/ecs/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions lib/ecs/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion spec/unit/ecs/entity_registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/ecs/system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down