From ba707eebb995eb46141d3c5e1701cd7252ba81c8 Mon Sep 17 00:00:00 2001 From: realtradam Date: Wed, 16 Jun 2021 07:09:43 -0400 Subject: entities, components, and systems improved --- spec/entity_manager_spec.rb | 31 +++++++- spec/system_manager_spec.rb | 185 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+), 4 deletions(-) create mode 100644 spec/system_manager_spec.rb (limited to 'spec') diff --git a/spec/entity_manager_spec.rb b/spec/entity_manager_spec.rb index 532e583..2db4787 100644 --- a/spec/entity_manager_spec.rb +++ b/spec/entity_manager_spec.rb @@ -54,15 +54,38 @@ describe 'Entities' do expect(@ent1.components[@component_manager].include?(@cmp2.id)).to be true end + it 'can get id from to_i' do + expect(@ent0.id).to eq(@ent0.to_i) + expect(@ent1.id).to eq(@ent1.to_i) + expect(@ent2.id).to eq(@ent2.to_i) + end + it 'can have components removed' do @ent0.add @cmp0 expect(@ent0.remove @cmp0).to be true + expect(@cmp0.entities.empty?).to be true expect(@ent0.components[@component_manager].empty?).to be true end - it 'can get id from to_i' do - expect(@ent0.id).to eq(@ent0.to_i) - expect(@ent1.id).to eq(@ent1.to_i) - expect(@ent2.id).to eq(@ent2.to_i) + it 'can have many components added then removed' do + @ent0.add @cmp0, @cmp1, @cmp2 + @ent1.add @cmp0, @cmp1 + @ent2.add @cmp1, @cmp2 + expect(@ent0.components).to eq({@component_manager => [0,1,2]}) + expect(@cmp0.entities).to eq([0,1]) + expect(@cmp1.entities).to eq([0,1,2]) + expect(@cmp2.entities).to eq([0,2]) + @ent1.delete + expect(@cmp0.entities).to eq([0]) + expect(@cmp1.entities).to eq([0,2]) + expect(@cmp2.entities).to eq([0,2]) + @cmp1.delete + expect(@ent0.components).to eq({@component_manager => [0,2]}) + @component_manager.each(&:delete) + expect(@component_manager.each.to_a).to eq([]) + expect(@ent0.components).to eq({@component_manager => []}) + expect(@ent2.components).to eq({@component_manager => []}) + FelFlame::Entities.each(&:delete) + expect(FelFlame::Entities.each.to_a).to eq([]) end end diff --git a/spec/system_manager_spec.rb b/spec/system_manager_spec.rb new file mode 100644 index 0000000..c2b8cf6 --- /dev/null +++ b/spec/system_manager_spec.rb @@ -0,0 +1,185 @@ +require_relative '../felflame.rb' + +describe 'Components' do + + before :all do + @component_manager ||= FelFlame::Components.new('TestSystems', health: 10, name: 'imp') + end + + before :each do + end + + after :each do + #TODO: order breaks it + @component_manager.each(&:delete) + FelFlame::Entities.each(&:delete) + FelFlame::Systems.each(&:clear_triggers) + end + + it 'can create a system' do + FelFlame::Systems.new('Test100') do + 'Works' + end + expect(FelFlame::Systems::Test100.call).to eq('Works') + end + + it 'can be redefined' do + FelFlame::Systems.new('Test101') do + 'neat' + end + FelFlame::Systems::Test101.redefine do + 'very neat' + end + expect(FelFlame::Systems::Test101.call).to eq('very neat') + end + + it 'can iterate over the sorted systems by priority' do + FelFlame::Systems.new('Test102', priority: 1) {} + FelFlame::Systems.new('Test103', priority: 50) {} + FelFlame::Systems.new('Test104', priority: 7) {} + answer_key = ['Test103', 'Test104', 'Test102'] + test = FelFlame::Systems.each.to_a + # converts the system name to the constant, compares their positions making sure they are sorted + # higher priority should be placed first + expect(test.map(&:const_name).find_index(answer_key[0])).to be <= test.map(&:const_name).find_index(answer_key[1]) + expect(test.map(&:const_name).find_index(answer_key[0])).to be <= test.map(&:const_name).find_index(answer_key[2]) + expect(test.map(&:const_name).find_index(answer_key[1])).to be >= test.map(&:const_name).find_index(answer_key[0]) + expect(test.map(&:const_name).find_index(answer_key[1])).to be <= test.map(&:const_name).find_index(answer_key[2]) + expect(test.map(&:const_name).find_index(answer_key[2])).to be >= test.map(&:const_name).find_index(answer_key[0]) + expect(test.map(&:const_name).find_index(answer_key[2])).to be >= test.map(&:const_name).find_index(answer_key[1]) + end + + it 'can manipulate components' do + init1 = 27 + init2 = 130 + multiple = 3 + first = @component_manager.new(health: init1) + second = @component_manager.new(health: init2) + FelFlame::Systems.new('Test105') do + @component_manager.each do |component| + component.health -= multiple + end + end + FelFlame::Systems::Test105.call + expect(first.health).to eq(init1 -= multiple) + expect(second.health).to eq(init2 -= multiple) + 10.times do + FelFlame::Systems::Test105.call + end + expect(first.health).to eq(init1 - (multiple * 10)) + expect(second.health).to eq(init2 - (multiple * 10)) + end + it 'can trigger when a single Component is added' do + FelFlame::Systems.new 'Test107' do + @component_manager.each do |component| + component.health += 5 + end + end + @cmp0 = @component_manager.new + @cmp1 = @component_manager.new health: 20 + FelFlame::Systems::Test107.trigger_when_added @cmp0 + expect(@cmp0.health).to eq(10) + expect(@cmp1.health).to eq(20) + @entity0 = FelFlame::Entities.new + @entity1 = FelFlame::Entities.new @cmp0 + expect(@cmp0.health).to eq(15) + expect(@cmp1.health).to eq(25) + @entity0.add @cmp0 + expect(@cmp0.health).to eq(20) + expect(@cmp1.health).to eq(30) + end + + it 'can trigger when a Component from a manager is added' do + FelFlame::Systems.new 'Test106' do + @component_manager.each do |component| + component.health += 5 + end + end + @cmp1 = @component_manager.new + @cmp2 = @component_manager.new health: 20 + FelFlame::Systems::Test106.trigger_when_added @component_manager + expect(@cmp1.health).to eq(10) + expect(@cmp2.health).to eq(20) + @entity1 = FelFlame::Entities.new + @entity2 = FelFlame::Entities.new @cmp2 + expect(@cmp1.health).to eq(15) + expect(@cmp2.health).to eq(25) + FelFlame::Systems::Test106.trigger_when_added @cmp1 + @entity1.add @cmp1 + expect(@cmp1.health).to eq(20) + expect(@cmp2.health).to eq(30) + end + + it 'can trigger when a single Component is removed' do + FelFlame::Systems.new 'Test108' do + @component_manager.each do |component| + component.health += 5 + end + end + @cmp0 = @component_manager.new + @cmp1 = @component_manager.new health: 20 + FelFlame::Systems::Test108.trigger_when_removed @cmp0 + expect(@cmp0.health).to eq(10) + expect(@cmp1.health).to eq(20) + @entity0 = FelFlame::Entities.new + @entity1 = FelFlame::Entities.new @cmp0 + expect(@cmp0.health).to eq(10) + expect(@cmp1.health).to eq(20) + @entity1.remove @cmp0 + expect(@cmp0.health).to eq(15) + expect(@cmp1.health).to eq(25) + @entity0.add @cmp1, @cmp0 + @entity0.remove @cmp1 + expect(@cmp0.health).to eq(15) + expect(@cmp1.health).to eq(25) + end + + it 'can trigger when a Component from a manager is removed' do + FelFlame::Systems.new 'Test109' do + @component_manager.each do |component| + component.health += 5 + end + end + @cmp0 = @component_manager.new + @cmp1 = @component_manager.new health: 20 + FelFlame::Systems::Test109.trigger_when_removed @component_manager + expect(@cmp0.health).to eq(10) + expect(@cmp1.health).to eq(20) + @entity0 = FelFlame::Entities.new + @entity1 = FelFlame::Entities.new @cmp0 + expect(@cmp0.health).to eq(10) + expect(@cmp1.health).to eq(20) + @entity1.remove @cmp0 + expect(@cmp0.health).to eq(15) + expect(@cmp1.health).to eq(25) + FelFlame::Systems::Test109.trigger_when_removed @cmp1 + @entity0.add @cmp1, @cmp0 + @entity0.remove @cmp1 + expect(@cmp0.health).to eq(20) + expect(@cmp1.health).to eq(30) + end +=begin + it 'can trigger when a single Component is added' do + FelFlame::Systems.new 'Test110' do + @component_manager.each do |component| + component.health += 5 + end + end + @cmp0 = @component_manager.new + @cmp1 = @component_manager.new health: 20 + FelFlame::Systems::Test110.trigger_when_is_changed @cmp0, :name + expect(@cmp0.health).to eq(10) + expect(@cmp1.health).to eq(20) + @entity0 = FelFlame::Entities.new + @entity1 = FelFlame::Entities.new @cmp0 + expect(@cmp0.health).to eq(10) + expect(@cmp1.health).to eq(20) + @cmp0.name = 'different' + expect(@cmp0.health).to eq(15) + expect(@cmp1.health).to eq(25) + @cmp1.name = 'different' + expect(@cmp0.health).to eq(15) + expect(@cmp1.health).to eq(25) + end +=end +end -- cgit v1.2.3