apps/runner/src/eu/runner/entity.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "eu/runner/entity.h" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | |||
| 5 | namespace eu::runner | ||
| 6 | { | ||
| 7 | |||
| 8 | |||
| 9 | /////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 10 | // Implementations | ||
| 11 | |||
| 12 | ✗ | void Alive::kill() | |
| 13 | { | ||
| 14 | ✗ | if(health == 0) | |
| 15 | { | ||
| 16 | ✗ | health = 1; | |
| 17 | } | ||
| 18 | ✗ | } | |
| 19 | |||
| 20 | ✗ | void Alive::update_for_frame() | |
| 21 | { | ||
| 22 | ✗ | if(health > 0) | |
| 23 | { | ||
| 24 | ✗ | health += 1; | |
| 25 | } | ||
| 26 | ✗ | } | |
| 27 | |||
| 28 | ✗ | bool Alive::is_pending_removal() const | |
| 29 | { | ||
| 30 | ✗ | return health > 0; | |
| 31 | } | ||
| 32 | |||
| 33 | ✗ | bool Alive::delete_owner() const | |
| 34 | { | ||
| 35 | ✗ | return health > 10; | |
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | // ------------------------------------------------------------------------ | ||
| 40 | // EntitySystemWithPrio | ||
| 41 | |||
| 42 | ✗ | EntitySystemWithPrio::EntitySystemWithPrio(EntitySystem* s, int p) | |
| 43 | ✗ | : system(s) | |
| 44 | ✗ | , prio(p) | |
| 45 | { | ||
| 46 | ✗ | } | |
| 47 | |||
| 48 | |||
| 49 | // ------------------------------------------------------------------------ | ||
| 50 | // EntitySystemUpdateStageList | ||
| 51 | |||
| 52 | ✗ | void EntitySystemUpdateStageList::update(UpdateStage stage) | |
| 53 | { | ||
| 54 | ✗ | for(auto& es: systems) | |
| 55 | { | ||
| 56 | ✗ | es.system->update(stage); | |
| 57 | } | ||
| 58 | ✗ | } | |
| 59 | |||
| 60 | ✗ | void EntitySystemUpdateStageList::add(EntitySystem* sys, int prio) | |
| 61 | { | ||
| 62 | ✗ | systems.emplace_back(sys, prio); | |
| 63 | ✗ | std::sort(systems.begin(), systems.end(), [](const auto& lhs, const auto& rhs) | |
| 64 | { | ||
| 65 | ✗ | return lhs.prio < rhs.prio; | |
| 66 | }); | ||
| 67 | ✗ | } | |
| 68 | |||
| 69 | ✗ | void EntitySystemUpdateStageList::remove(EntitySystem* sys) | |
| 70 | { | ||
| 71 | ✗ | core::update_and_erase(&systems, | |
| 72 | ✗ | [sys](const EntitySystemWithPrio& es) | |
| 73 | ✗ | { return es.system == sys;} | |
| 74 | ); | ||
| 75 | ✗ | } | |
| 76 | |||
| 77 | |||
| 78 | // ------------------------------------------------------------------------ | ||
| 79 | // Entity | ||
| 80 | ✗ | bool Entity::is_spatial_entity() const { return root_component != nullptr; } | |
| 81 | |||
| 82 | ✗ | void Entity::activate() | |
| 83 | { | ||
| 84 | // thread safe?: | ||
| 85 | // register each component with all local systems | ||
| 86 | // create per-stage local system update lists | ||
| 87 | // creates entity attachment (if required) | ||
| 88 | // not thread safe: | ||
| 89 | // registers each component with all global systems | ||
| 90 | ✗ | } | |
| 91 | |||
| 92 | ✗ | void Entity::update(UpdateStage stage) | |
| 93 | { | ||
| 94 | ✗ | systems.update(stage); | |
| 95 | |||
| 96 | ✗ | if (stage == UpdateStage::end_frame) | |
| 97 | { | ||
| 98 | ✗ | alive.update_for_frame(); | |
| 99 | ✗ | update_and_remove_alives(&components, &dead_components); | |
| 100 | } | ||
| 101 | ✗ | } | |
| 102 | |||
| 103 | ✗ | void Entity::deactivate() | |
| 104 | { | ||
| 105 | ✗ | } | |
| 106 | |||
| 107 | ✗ | void Entity::load() | |
| 108 | { | ||
| 109 | ✗ | } | |
| 110 | |||
| 111 | ✗ | void Entity::unload() | |
| 112 | { | ||
| 113 | ✗ | } | |
| 114 | |||
| 115 | |||
| 116 | // ------------------------------------------------------------------------ | ||
| 117 | // EntitySystemUpdate | ||
| 118 | |||
| 119 | ✗ | void EntitySystemUpdate::update(UpdateStage stage) | |
| 120 | { | ||
| 121 | ✗ | systems[static_cast<std::size_t>(stage)].update(stage); | |
| 122 | ✗ | } | |
| 123 | |||
| 124 | ✗ | void EntitySystemUpdate::add(EntitySystem* sys, UpdateStage stage, int prio) | |
| 125 | { | ||
| 126 | ✗ | systems[static_cast<std::size_t>(stage)].add(sys, prio); | |
| 127 | ✗ | } | |
| 128 | |||
| 129 | ✗ | void EntitySystemUpdate::remove(EntitySystem* sys, UpdateStage stage) | |
| 130 | { | ||
| 131 | ✗ | systems[static_cast<std::size_t>(stage)].remove(sys); | |
| 132 | ✗ | } | |
| 133 | |||
| 134 | |||
| 135 | // ------------------------------------------------------------------------ | ||
| 136 | // ComponentType | ||
| 137 | |||
| 138 | // ------------------------------------------------------------------------ | ||
| 139 | // ComponentFactory | ||
| 140 | ✗ | void ComponentFactory::add(const ComponentType* ty) | |
| 141 | { | ||
| 142 | ✗ | types.insert({ty->name, ty}); | |
| 143 | ✗ | } | |
| 144 | |||
| 145 | ✗ | const ComponentType* ComponentFactory::from_name_or_null(Hsh name) const | |
| 146 | { | ||
| 147 | ✗ | auto found = types.find(name); | |
| 148 | ✗ | if(found != types.end()) { return found->second; } | |
| 149 | ✗ | else { return nullptr; } | |
| 150 | } | ||
| 151 | |||
| 152 | // ------------------------------------------------------------------------ | ||
| 153 | // SpatialComponent | ||
| 154 | |||
| 155 | ✗ | void SpatialComponent::set_local_transform(const m4& m) | |
| 156 | { | ||
| 157 | ✗ | local_transform = m; | |
| 158 | ✗ | update_world_transform(); | |
| 159 | ✗ | } | |
| 160 | |||
| 161 | ✗ | void SpatialComponent::update_world_transform() | |
| 162 | { | ||
| 163 | // calculate world transform based on world | ||
| 164 | // update world bounds | ||
| 165 | |||
| 166 | // update world transforms on children | ||
| 167 | ✗ | for(SpatialComponent* child: children) | |
| 168 | { | ||
| 169 | ✗ | child->update_world_transform(); | |
| 170 | } | ||
| 171 | ✗ | } | |
| 172 | |||
| 173 | |||
| 174 | // ------------------------------------------------------------------------ | ||
| 175 | // EntitySystemType | ||
| 176 | |||
| 177 | // ------------------------------------------------------------------------ | ||
| 178 | // EntitySystemFactory | ||
| 179 | ✗ | void EntitySystemFactory::add(const EntitySystemType* ty) | |
| 180 | { | ||
| 181 | ✗ | types.insert({ty->name, ty}); | |
| 182 | ✗ | } | |
| 183 | |||
| 184 | ✗ | const EntitySystemType* EntitySystemFactory::from_name_or_null(Hsh name) | |
| 185 | { | ||
| 186 | ✗ | auto found = types.find(name); | |
| 187 | ✗ | if(found != types.end()) { return found->second; } | |
| 188 | ✗ | else { return nullptr; } | |
| 189 | } | ||
| 190 | |||
| 191 | // ------------------------------------------------------------------------ | ||
| 192 | // WorldSystemType | ||
| 193 | |||
| 194 | // ------------------------------------------------------------------------ | ||
| 195 | // WorldSystemFactory | ||
| 196 | ✗ | void WorldSystemFactory::add(const WorldSystemType* ty) | |
| 197 | { | ||
| 198 | ✗ | types.insert({ty->name, ty}); | |
| 199 | ✗ | } | |
| 200 | |||
| 201 | ✗ | const WorldSystemType* WorldSystemFactory::from_name_or_null(Hsh name) | |
| 202 | { | ||
| 203 | ✗ | auto found = types.find(name); | |
| 204 | ✗ | if(found != types.end()) { return found->second; } | |
| 205 | ✗ | else { return nullptr; } | |
| 206 | } | ||
| 207 | |||
| 208 | // ------------------------------------------------------------------------ | ||
| 209 | // Component | ||
| 210 | |||
| 211 | // ------------------------------------------------------------------------ | ||
| 212 | // SpatialComponent | ||
| 213 | |||
| 214 | ✗ | void attach(World*, Entity* parent, Entity* child) | |
| 215 | { | ||
| 216 | ✗ | assert(parent->is_spatial_entity() && child->is_spatial_entity()); | |
| 217 | // todo(Gustav): implement attachments | ||
| 218 | ✗ | } | |
| 219 | |||
| 220 | // ------------------------------------------------------------------------ | ||
| 221 | // RequestedComponents | ||
| 222 | |||
| 223 | // ------------------------------------------------------------------------ | ||
| 224 | // EntitySystem | ||
| 225 | |||
| 226 | // ------------------------------------------------------------------------ | ||
| 227 | // WorldSystem | ||
| 228 | |||
| 229 | // ------------------------------------------------------------------------ | ||
| 230 | // WorldSystemWithPrio | ||
| 231 | ✗ | WorldSystemWithPrio::WorldSystemWithPrio(WorldSystem* s, int p) | |
| 232 | ✗ | : system(s) | |
| 233 | ✗ | , prio(p) | |
| 234 | { | ||
| 235 | ✗ | } | |
| 236 | |||
| 237 | // ------------------------------------------------------------------------ | ||
| 238 | // WorldSystemUpdateStageList | ||
| 239 | ✗ | void WorldSystemUpdateStageList::update(UpdateStage stage) | |
| 240 | { | ||
| 241 | ✗ | for(auto& es: systems) | |
| 242 | { | ||
| 243 | ✗ | es.system->update(stage); | |
| 244 | } | ||
| 245 | ✗ | } | |
| 246 | |||
| 247 | ✗ | void WorldSystemUpdateStageList::add(WorldSystem* sys, int prio) | |
| 248 | { | ||
| 249 | ✗ | systems.emplace_back(sys, prio); | |
| 250 | ✗ | std::sort(systems.begin(), systems.end(), [](const auto& lhs, const auto& rhs) | |
| 251 | { | ||
| 252 | ✗ | return lhs.prio < rhs.prio; | |
| 253 | }); | ||
| 254 | ✗ | } | |
| 255 | |||
| 256 | ✗ | void WorldSystemUpdateStageList::remove(WorldSystem* sys) | |
| 257 | { | ||
| 258 | ✗ | std::erase_if(systems, [sys](const WorldSystemWithPrio& es) { return es.system == sys;}); | |
| 259 | ✗ | } | |
| 260 | |||
| 261 | // ------------------------------------------------------------------------ | ||
| 262 | // WorldSystemUpdate | ||
| 263 | |||
| 264 | ✗ | void WorldSystemUpdate::update(UpdateStage stage) | |
| 265 | { | ||
| 266 | ✗ | systems[static_cast<std::size_t>(stage)].update(stage); | |
| 267 | ✗ | } | |
| 268 | |||
| 269 | ✗ | void WorldSystemUpdate::add(WorldSystem* system, UpdateStage stage, int prio) | |
| 270 | { | ||
| 271 | ✗ | systems[static_cast<std::size_t>(stage)].add(system, prio); | |
| 272 | ✗ | } | |
| 273 | |||
| 274 | ✗ | void WorldSystemUpdate::remove(WorldSystem* system, UpdateStage stage) | |
| 275 | { | ||
| 276 | ✗ | systems[static_cast<std::size_t>(stage)].remove(system); | |
| 277 | ✗ | } | |
| 278 | |||
| 279 | // ------------------------------------------------------------------------ | ||
| 280 | // World | ||
| 281 | |||
| 282 | ✗ | void World::update(UpdateStage stage) | |
| 283 | { | ||
| 284 | // todo(Gustav): implement threading for entity | ||
| 285 | // todo(Gustav): take care of updating parent before child | ||
| 286 | // parallelized, spatial parent is updated before child (worker threads: number of cores - 1) | ||
| 287 | // place attached entities on the same thread as parent, schedule parent to update before the child | ||
| 288 | ✗ | for(auto& ent: entities) | |
| 289 | { | ||
| 290 | ✗ | ent->update(stage); | |
| 291 | } | ||
| 292 | |||
| 293 | // todo(Gustav): implement threading for world | ||
| 294 | // sequential, can use worker threads if needed | ||
| 295 | ✗ | system_update.update(stage); | |
| 296 | ✗ | } | |
| 297 | |||
| 298 | } | ||
| 299 |