Added EntityWrapper::Valid and bool overload to be check if an entity is valid and still exists in the world more easily

This commit is contained in:
2016-01-17 06:20:27 +01:00
parent f281d220de
commit 9f447afbdb
2 changed files with 30 additions and 5 deletions
+4 -2
View File
@@ -23,10 +23,12 @@ struct EntityWrapper
static const EntityWrapper Invalid; static const EntityWrapper Invalid;
bool HasComponent(const std::string& componentName); bool HasComponent(const std::string& componentName);
bool Valid();
ComponentWrapper operator[](const std::string& componentName); ComponentWrapper operator[](const char* componentName);
bool operator==(const EntityWrapper& e); bool operator==(const EntityWrapper& e);
explicit operator EntityID(); explicit operator EntityID() const;
operator bool();
}; };
#endif #endif
+26 -3
View File
@@ -13,18 +13,41 @@ bool EntityWrapper::HasComponent(const std::string& componentName)
return World->HasComponent(ID, componentName); return World->HasComponent(ID, componentName);
} }
ComponentWrapper EntityWrapper::operator[](const std::string& componentName) bool EntityWrapper::Valid()
{
if (this->World == nullptr) {
return false;
}
if (this->ID == EntityID_Invalid) {
return false;
}
if (!this->World->ValidEntity(this->ID)) {
this->ID = EntityID_Invalid;
return false;
}
return true;
}
ComponentWrapper EntityWrapper::operator[](const char* componentName)
{ {
if (World->HasComponent(ID, componentName)) { if (World->HasComponent(ID, componentName)) {
return World->GetComponent(ID, componentName); return World->GetComponent(ID, componentName);
} else { } else {
LOG_WARNING("EntityWrapper implicitly attached \"%s\" component to #%i as a result of a fetch request!", componentName.c_str(), ID); LOG_WARNING("EntityWrapper implicitly attached \"%s\" component to #%i as a result of a fetch request!", componentName, ID);
return World->AttachComponent(ID, componentName); return World->AttachComponent(ID, componentName);
} }
} }
EntityWrapper::operator EntityID() EntityWrapper::operator EntityID() const
{ {
return this->ID; return this->ID;
} }
EntityWrapper::operator bool()
{
return this->Valid();
}