CSEngine
Loading...
Searching...
No Matches
SGameObjectFromSPrefab.h
1#pragma once
2
3#include <utility>
4
5#include "SGameObject.h"
6
7namespace CSE {
8
10 public:
12 explicit SGameObjectFromSPrefab(std::string name) : SGameObject(std::move(name)) {}
13 explicit SGameObjectFromSPrefab(std::string name, std::string hash)
14 : SGameObject(std::move(name), std::move(hash)) {}
15 ~SGameObjectFromSPrefab() override = default;
16
17 void SetRefHash(std::string hash) {
18 m_refHash = std::move(hash);
19 }
20
21 std::string GetRefHash() const {
22 return m_refHash;
23 }
24
25 SComponent* GetSComponentByRefHash(const std::string& hash) const {
26 const auto& rawData = split(hash, '?');
27 const auto& hashData = rawData[0];
28 const auto& componentName = rawData[1];
29 if(m_refHash == hashData) {
30 const auto& components = GetComponents();
31 for (const auto& component : components) {
32 if(componentName == component->GetClassType()) {
33 return component;
34 }
35 }
36 return nullptr;
37 }
38
39 const auto& children = GetChildren();
40 for(const auto& child : children) {
41 const auto& component = static_cast<SGameObjectFromSPrefab*>(child)->GetSComponentByRefHash(hash);
42 if(component != nullptr)
43 return component;
44 }
45 return nullptr;
46 }
47
48 template <class T>
49 T* GetComponentByRefHash(const std::string& hash) const;
50
51 std::string GetRefID(const SComponent* component) const {
52 if (component == nullptr) return "";
53
54 const auto& object = static_cast<SGameObjectFromSPrefab*>(component->GetGameObject());
55 return object->m_refHash + "?" + component->GetClassType();
56 }
57
58 private:
59 std::string m_refHash;
60 };
61
62 template <class T>
63 T* SGameObjectFromSPrefab::GetComponentByRefHash(const std::string& hash) const {
64 return static_cast<T*>(GetSComponentByRefHash(hash));
65 }
66}