bool SimComponent::_registerComponents( SimComponent *owner ) { // This method will return true if the object contains no components. See the // documentation for SimComponent::onComponentRegister for more information // on this behavior. bool ret = true; // If this doesn't contain components, don't even lock the list. if( hasComponents() ) { VectorPtr<SimComponent *> &components = lockComponentList(); for( SimComponentIterator i = components.begin(); i != components.end(); i++ ) { if( !(*i)->onComponentRegister( owner ) ) { ret = false; break; } AssertFatal( (*i)->mOwner == owner, "Component failed to call parent onComponentRegister!" ); // Recurse if( !(*i)->_registerComponents( owner ) ) { ret = false; break; } } unlockComponentList(); } return ret; }
bool AIComponent::hasTarget() { auto t = getTarget(); if (t != nullptr) { es::ComponentFilter targetFilter; targetFilter.requires(FactionComponent::getId()); targetFilter.requires(TransformComponent::getId()); targetFilter.requires(StatComponent::getId()); if (t->hasComponents(targetFilter) && hasParent()) { sf::Vector2f ePos = mParent->getComponent<TransformComponent>().getPosition(); sf::Vector2f tPos = t->getComponent<TransformComponent>().getPosition(); if (t->getId() != mParent->getId() && t->getComponent<StatComponent>().isAlive() && thor::length(ePos - tPos) < mOutOfView && mParent->hasComponent<FactionComponent>()) { if (mParent->getComponent<FactionComponent>().getFaction().isEnemy(t->getComponent<FactionComponent>().getFactionId())) { return true; } } else { mTarget = 0; } } else { mTarget = 0; } } return false; }
void SimComponent::_registerInterfaces( SimComponent *owner ) { // First call this to expose the interfaces that this component will cache // before examining the list of subcomponents registerInterfaces( owner ); // Early out to avoid mutex lock and such if( !hasComponents() ) return; VectorPtr<SimComponent *> &components = lockComponentList(); for( SimComponentIterator i = components.begin(); i != components.end(); i++ ) { (*i)->mOwner = owner; // Tell the component itself to register it's interfaces (*i)->registerInterfaces( owner ); (*i)->mOwner = NULL; // This tests to see if the object's onComponentRegister call will call up to the parent. // Recurse (*i)->_registerInterfaces( owner ); } unlockComponentList(); }
void BaseSystem::OnEntityAdded(EventSp event) { auto entityCreatedEvent = std::dynamic_pointer_cast<EntityCreatedEvent>(event); auto entity = entityCreatedEvent->entity; if (entity->hasComponents(components)) { logger.log(getName() + ": entity added"); entities[entity->id] = entity; } }
void ScriptSystem::OnScriptComponentAttached(EventSp event) { auto componentAttachedEvent = std::dynamic_pointer_cast<ScriptComponentAttachedEvent>(event); auto entity = componentAttachedEvent->entity; if (entity->hasComponents(components)) { logger.log(getName() + ": entity added"); entities[entity->id] = entity; } }
void EntityManager::getEntitiesWithComponents(std::function<void(Entity*)> func, std::bitset<MAX_COMPONENTS>& bits) { for(pEntity& e : _entities_alive) { if(!e) continue; if(Exists(e->ID) && hasComponents(e, bits)) { func(e); } } }
void SimComponent::_unregisterComponents() { if( !hasComponents() ) return; VectorPtr<SimComponent *> &components = lockComponentList(); for( SimComponentIterator i = components.begin(); i != components.end(); i++ ) { (*i)->onComponentUnRegister(); AssertFatal( (*i)->mOwner == NULL, "Component failed to call parent onUnRegister" ); // Recurse (*i)->_unregisterComponents(); } unlockComponentList(); }