From b46c146f68b8765175fe36e9ebc32ad22d869a24 Mon Sep 17 00:00:00 2001 From: Ernestas Monkevicius Date: Sun, 5 Sep 2021 08:14:57 +0100 Subject: [PATCH] Notes. Extra specs. Assure engine is accessible in systems. --- shard.yml | 2 +- spec/engine_spec.cr | 21 +++++++++++++++++---- src/fast_ecs/component.cr | 4 ++++ src/fast_ecs/system.cr | 5 ++++- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/shard.yml b/shard.yml index e80aea9..23f2ad4 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: fast-ecs -version: 0.1.0 +version: 0.1.1 authors: - Ernestas Monkevicius diff --git a/spec/engine_spec.cr b/spec/engine_spec.cr index 47b34d8..e948bb2 100644 --- a/spec/engine_spec.cr +++ b/spec/engine_spec.cr @@ -32,6 +32,17 @@ class TestyComponentB < Fast::ECS::Component end end +module TestyComponentC + class C < Fast::ECS::Component + property :number + + def initialize(id : Int32, number : Int32) + super(id) + @number = number + end + end +end + Spectator.describe Fast::ECS::Engine do mock TestySystem do # stub instance_method(some_number : Int32) @@ -326,22 +337,24 @@ Spectator.describe Fast::ECS::Engine do describe "#query" do let(:testy_component_b1) { TestyComponentB.new(testy_component_a1.id, 123) } let(:testy_component_b2) { TestyComponentB.new(testy_component_a2.id, 123) } + let(:testy_component_cc1) { TestyComponentC::C.new(testy_component_a1.id, 123) } context "when there is at least one entity with requested components" do before_each do subject.add_component(testy_component_a1) subject.add_component(testy_component_b1) + subject.add_component(testy_component_cc1) end it "does finds those components" do components = [] of Array(Fast::ECS::Component) - subject.query(TestyComponentA, TestyComponentB) do |query_set| - compA, compB = query_set - components.push([compA, compB]) + subject.query(TestyComponentA, TestyComponentB, TestyComponentC::C) do |query_set| + compA, compB, compCC = query_set + components.push([compA, compB, compCC]) end - expect(components).to eq [[testy_component_a1, testy_component_b1]] + expect(components).to eq [[testy_component_a1, testy_component_b1, testy_component_cc1]] end end diff --git a/src/fast_ecs/component.cr b/src/fast_ecs/component.cr index 946a4da..d1dc02f 100644 --- a/src/fast_ecs/component.cr +++ b/src/fast_ecs/component.cr @@ -1,3 +1,7 @@ + +# TODO: suggestion was to use flyweight pattern for components to increase RAM efficiency? +# https://refactoring.guru/design-patterns/flyweight + module Fast::ECS # NOTE: custom components will extend this. # NOTE: NO METHODS ON COMPONENTS !!! diff --git a/src/fast_ecs/system.cr b/src/fast_ecs/system.cr index bd86c13..0f6e9ab 100644 --- a/src/fast_ecs/system.cr +++ b/src/fast_ecs/system.cr @@ -1,12 +1,15 @@ module Fast::ECS abstract class System - getter :engine delegate delta_time, to: @engine.not_nil! def add_engine(engine : Engine) @engine = engine end + def engine + @engine.not_nil! + end + # init stuff, engine should run this when system right after system is added abstract def start