// Template definitions for Entity. #ifndef __ENTITY__H__ #error "Include Entity.h instead of Entity.tpp" #endif template Cmpt* Entity::GetComponent() const { const auto& componentItr = this->m_Components.find(Cmpt::ComponentType); return componentItr == this->m_Components.end() ? nullptr : dynamic_cast(componentItr->second.get()); } template Cmpt* Entity::AddComponent(ConstructorValues...arguments) { auto component = GetComponent(); if (component) return dynamic_cast(component); auto& insertedComponent = m_Components.insert_or_assign(Cmpt::ComponentType, std::make_unique(this, std::forward(arguments)...)).first->second; return dynamic_cast(insertedComponent.get()); } template const T& Entity::GetVar(const std::u16string& name) const { auto* data = GetVarData(name); if (data == nullptr) { return LDFData::Default; } auto* typed = dynamic_cast*>(data); if (typed == nullptr) { return LDFData::Default; } return typed->GetValue(); } template T Entity::GetVarAs(const std::u16string& name) const { const auto data = GetVarAsString(name); T value; if (!GeneralUtils::TryParse(data, value)) { return LDFData::Default; } return value; } template void Entity::SetVar(const std::u16string& name, const T& value) { auto* data = GetVarData(name); if (data == nullptr) { auto* data = new LDFData(name, value); m_Settings.push_back(data); return; } auto* typed = dynamic_cast*>(data); if (typed == nullptr) { return; } typed->SetValue(value); } template void Entity::SetNetworkVar(const std::u16string& name, const T& value, const SystemAddress& sysAddr) { LDFData* newData = nullptr; for (auto* data : m_NetworkSettings) { if (data->GetKey() != name) continue; newData = dynamic_cast*>(data); if (newData != nullptr) { newData->SetValue(value); } else { // If we're changing types m_NetworkSettings.erase( std::remove(m_NetworkSettings.begin(), m_NetworkSettings.end(), data), m_NetworkSettings.end() ); delete data; } break; } if (newData == nullptr) { newData = new LDFData(name, value); } m_NetworkSettings.push_back(newData); SendNetworkVar(newData->GetString(true), sysAddr); } template void Entity::SetNetworkVar(const std::u16string& name, const std::vector& values, const SystemAddress& sysAddr) { std::stringstream updates; auto index = 1; for (const auto& value : values) { LDFData* newData = nullptr; const auto& indexedName = name + u"." + GeneralUtils::to_u16string(index); for (auto* data : m_NetworkSettings) { if (data->GetKey() != indexedName) continue; newData = dynamic_cast*>(data); newData->SetValue(value); break; } if (newData == nullptr) { newData = new LDFData(indexedName, value); } m_NetworkSettings.push_back(newData); if (index == values.size()) { updates << newData->GetString(true); } else { updates << newData->GetString(true) << "\n"; } index++; } SendNetworkVar(updates.str(), sysAddr); } template T Entity::GetNetworkVar(const std::u16string& name) { for (auto* data : m_NetworkSettings) { if (data == nullptr || data->GetKey() != name) continue; auto* typed = dynamic_cast*>(data); if (typed == nullptr) continue; return typed->GetValue(); } return LDFData::Default; }