void Entity::RemoveComponent(const ComponentPtr &component, AttributeChange::Type change) { if (component) { ComponentVector::iterator iter = std::find(components_.begin(), components_.end(), component); if (iter != components_.end()) { QString componentTypeName = component->TypeName(); componentTypeName.replace(0, 3, ""); componentTypeName = componentTypeName.toLower(); if(property(componentTypeName.toStdString().c_str()).isValid()) { QObject *obj = property(componentTypeName.toStdString().c_str()).value<QObject*>(); //Make sure that QObject is inherited by the IComponent. if (obj && dynamic_cast<IComponent*>(obj)) { //Make sure that name is matching incase there are many of same type of components in entity. if (dynamic_cast<IComponent*>(obj)->Name() == component->Name()) setProperty(componentTypeName.toStdString().c_str(), QVariant()); } } if (change != AttributeChange::Disconnected) emit ComponentRemoved((*iter).get(), change == AttributeChange::Default ? component->GetUpdateMode() : change); if (scene_) scene_->EmitComponentRemoved(this, (*iter).get(), change); (*iter)->SetParentEntity(0); components_.erase(iter); } else { LogWarning("Failed to remove component: " + component->TypeName() + " from entity: " + QString::number(GetId())); } } }
void Entity::AddComponent(const ComponentPtr &component, AttributeChange::Type change) { // Must exist and be free if (component && component->GetParentEntity() == 0) { QString componentTypeName = component->TypeName(); componentTypeName.replace(0, 3, ""); componentTypeName = componentTypeName.toLower(); if(!property(componentTypeName.toStdString().c_str()).isValid()) { QVariant var = QVariant::fromValue<QObject*>(component.get()); setProperty(componentTypeName.toStdString().c_str(), var); } component->SetParentEntity(this); components_.push_back(component); if (change != AttributeChange::Disconnected) emit ComponentAdded(component.get(), change == AttributeChange::Default ? component->GetUpdateMode() : change); if (scene_) scene_->EmitComponentAdded(this, component.get(), change); } }