|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2017 See AUTHORS file. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + ******************************************************************************/ |
| 16 | + |
| 17 | +#include <string> |
| 18 | +#include <list> |
| 19 | +#include <utility> |
| 20 | + |
| 21 | +#include "Ashley/core/Engine.hpp" |
| 22 | +#include "Ashley/core/Component.hpp" |
| 23 | +#include "Ashley/core/Family.hpp" |
| 24 | +#include "Ashley/core/ComponentMapper.hpp" |
| 25 | + |
| 26 | +#include "Ashley/systems/SortedIteratingSystem.hpp" |
| 27 | + |
| 28 | +#include "AshleyTestCommon.hpp" |
| 29 | + |
| 30 | +#include "gtest/gtest.h" |
| 31 | + |
| 32 | +using ashley::Engine; |
| 33 | +using ashley::Entity; |
| 34 | +using ashley::Component; |
| 35 | +using ashley::Family; |
| 36 | +using ashley::ComponentMapper; |
| 37 | +using ashley::SortedIteratingSystem; |
| 38 | + |
| 39 | +class ComponentB : public Component { |
| 40 | + |
| 41 | +}; |
| 42 | + |
| 43 | +class ComponentC : public Component { |
| 44 | + |
| 45 | +}; |
| 46 | + |
| 47 | +class OrderedComponent : public ashley::Component { |
| 48 | +public: |
| 49 | + std::string name { "" }; |
| 50 | + int zLayer = 0; |
| 51 | + |
| 52 | + OrderedComponent() { |
| 53 | + } |
| 54 | + |
| 55 | + OrderedComponent(std::string &&name, int zLayer) : |
| 56 | + name { std::move(name) }, |
| 57 | + zLayer { zLayer } { |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +bool zComparator(Entity *e1, Entity *e2) { |
| 62 | + const ComponentMapper<OrderedComponent> zMapper = ComponentMapper<OrderedComponent>::getMapper(); |
| 63 | + const auto o1 = zMapper.get(e1); |
| 64 | + const auto o2 = zMapper.get(e2); |
| 65 | + return o1->zLayer < o2->zLayer; |
| 66 | +} |
| 67 | + |
| 68 | +class SortedIteratingSystemMock final : public SortedIteratingSystem { |
| 69 | +public: |
| 70 | + std::list<std::string> expectedNames; |
| 71 | + const ComponentMapper<OrderedComponent> zMapper = ComponentMapper<OrderedComponent>::getMapper(); |
| 72 | + uint64_t numUpdates = 0; |
| 73 | + |
| 74 | + explicit SortedIteratingSystemMock(Family *family) : |
| 75 | + SortedIteratingSystem(family, zComparator, 0) { |
| 76 | + } |
| 77 | + |
| 78 | + void update(float deltaTime) override final { |
| 79 | + SortedIteratingSystem::update(deltaTime); |
| 80 | + ASSERT_TRUE(expectedNames.empty()); |
| 81 | + } |
| 82 | + |
| 83 | + void processEntity(Entity * const entity, float deltaTime) override final { |
| 84 | + ++numUpdates; |
| 85 | + const auto z = zMapper.get(entity); |
| 86 | + |
| 87 | + ASSERT_NE(z, nullptr); |
| 88 | + ASSERT_FALSE(expectedNames.empty()); |
| 89 | + ASSERT_EQ(expectedNames.front(), z->name); |
| 90 | + expectedNames.pop_front(); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +namespace { |
| 95 | +class SortedIteratingSystemTest : public ::testing::Test { |
| 96 | +protected: |
| 97 | + const ComponentMapper<OrderedComponent> zMapper = ComponentMapper<OrderedComponent>::getMapper(); |
| 98 | + Engine engine; |
| 99 | + |
| 100 | + Family *sortFamily { Family::getFor( { typeid(OrderedComponent) }) }; |
| 101 | + |
| 102 | + const float delta = 1.0f; |
| 103 | +}; |
| 104 | + |
| 105 | +TEST_F(SortedIteratingSystemTest, ShouldIterateEntitiesWithCorrectFamily) { |
| 106 | + const auto family = Family::getFor( { typeid(OrderedComponent), typeid(ComponentB) }); |
| 107 | + auto mockSystem = engine.addSystem<SortedIteratingSystemMock>(family); |
| 108 | + auto e = engine.addEntity(); |
| 109 | + |
| 110 | + e->add<OrderedComponent>("A", 0); |
| 111 | + engine.update(delta); |
| 112 | + |
| 113 | + ASSERT_EQ(mockSystem->numUpdates, 0); |
| 114 | + |
| 115 | + e->add<ComponentB>(); |
| 116 | + mockSystem->expectedNames.emplace_back("A"); |
| 117 | + engine.update(delta); |
| 118 | + |
| 119 | + ASSERT_EQ(mockSystem->numUpdates, 1); |
| 120 | + |
| 121 | + e->add<ComponentC>(); |
| 122 | + mockSystem->expectedNames.emplace_back("A"); |
| 123 | + engine.update(delta); |
| 124 | + |
| 125 | + ASSERT_EQ(mockSystem->numUpdates, 2); |
| 126 | + |
| 127 | + e->remove<OrderedComponent>(); |
| 128 | + e->add<ComponentC>(); |
| 129 | + engine.update(delta); |
| 130 | + |
| 131 | + ASSERT_EQ(mockSystem->numUpdates, 2); |
| 132 | +} |
| 133 | + |
| 134 | +TEST_F(SortedIteratingSystemTest, EntityOrdering) { |
| 135 | + auto mockSystem = engine.addSystem<SortedIteratingSystemMock>(sortFamily); |
| 136 | + |
| 137 | + auto a_t = std::unique_ptr<Entity>(new Entity()); |
| 138 | + a_t->add<OrderedComponent>("A", 0); |
| 139 | + |
| 140 | + auto b_t = std::unique_ptr<Entity>(new Entity()); |
| 141 | + b_t->add<OrderedComponent>("B", 1); |
| 142 | + |
| 143 | + auto c_t = std::unique_ptr<Entity>(new Entity()); |
| 144 | + c_t->add<OrderedComponent>("C", 3); |
| 145 | + |
| 146 | + auto d_t = std::unique_ptr<Entity>(new Entity()); |
| 147 | + d_t->add<OrderedComponent>("D", 2); |
| 148 | + |
| 149 | + auto a = engine.addEntity(std::move(a_t)); |
| 150 | + auto b = engine.addEntity(std::move(b_t)); |
| 151 | + auto c = engine.addEntity(std::move(c_t)); |
| 152 | + |
| 153 | + { |
| 154 | + SCOPED_TRACE("A, B & C"); |
| 155 | + |
| 156 | + mockSystem->expectedNames.emplace_back("A"); |
| 157 | + mockSystem->expectedNames.emplace_back("B"); |
| 158 | + mockSystem->expectedNames.emplace_back("C"); |
| 159 | + engine.update(0.0f); |
| 160 | + } |
| 161 | + |
| 162 | + auto d = engine.addEntity(std::move(d_t)); |
| 163 | + |
| 164 | + { |
| 165 | + SCOPED_TRACE("A, B, D & C"); |
| 166 | + mockSystem->expectedNames.emplace_back("A"); |
| 167 | + mockSystem->expectedNames.emplace_back("B"); |
| 168 | + mockSystem->expectedNames.emplace_back("D"); |
| 169 | + mockSystem->expectedNames.emplace_back("C"); |
| 170 | + engine.update(0.0f); |
| 171 | + } |
| 172 | + |
| 173 | + zMapper.get(a)->zLayer = 3; |
| 174 | + zMapper.get(b)->zLayer = 2; |
| 175 | + zMapper.get(c)->zLayer = 1; |
| 176 | + zMapper.get(d)->zLayer = 0; |
| 177 | + mockSystem->forceSort(); |
| 178 | + |
| 179 | + { |
| 180 | + SCOPED_TRACE("D, C, B, A"); |
| 181 | + mockSystem->expectedNames.emplace_back("D"); |
| 182 | + mockSystem->expectedNames.emplace_back("C"); |
| 183 | + mockSystem->expectedNames.emplace_back("B"); |
| 184 | + mockSystem->expectedNames.emplace_back("A"); |
| 185 | + engine.update(0.0f); |
| 186 | + } |
| 187 | + |
| 188 | +} |
| 189 | + |
| 190 | +} |
0 commit comments