Prepare script lifecycle and data layer
This commit is contained in:
@@ -8,5 +8,35 @@ TransformComponent& Component::transform() const {
|
||||
return *m_gameObject->GetTransform();
|
||||
}
|
||||
|
||||
Scene* Component::GetScene() const {
|
||||
return m_gameObject ? m_gameObject->GetScene() : nullptr;
|
||||
}
|
||||
|
||||
void Component::SetEnabled(bool enabled) {
|
||||
if (m_enabled == enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool wasEffectivelyEnabled = m_enabled;
|
||||
bool isEffectivelyEnabled = enabled;
|
||||
|
||||
if (m_gameObject) {
|
||||
wasEffectivelyEnabled = m_enabled && m_gameObject->IsActiveInHierarchy();
|
||||
isEffectivelyEnabled = enabled && m_gameObject->IsActiveInHierarchy();
|
||||
}
|
||||
|
||||
m_enabled = enabled;
|
||||
|
||||
if (wasEffectivelyEnabled == isEffectivelyEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEffectivelyEnabled) {
|
||||
OnEnable();
|
||||
} else {
|
||||
OnDisable();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,9 @@
|
||||
#include "Components/CameraComponent.h"
|
||||
#include "Components/GameObject.h"
|
||||
#include "Components/LightComponent.h"
|
||||
#include "Components/MeshFilterComponent.h"
|
||||
#include "Components/MeshRendererComponent.h"
|
||||
#include "Scripting/ScriptComponent.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
@@ -28,6 +31,9 @@ ComponentFactoryRegistry::ComponentFactoryRegistry() {
|
||||
RegisterFactory("Light", &CreateBuiltInComponent<LightComponent>);
|
||||
RegisterFactory("AudioSource", &CreateBuiltInComponent<AudioSourceComponent>);
|
||||
RegisterFactory("AudioListener", &CreateBuiltInComponent<AudioListenerComponent>);
|
||||
RegisterFactory("MeshFilter", &CreateBuiltInComponent<MeshFilterComponent>);
|
||||
RegisterFactory("MeshRenderer", &CreateBuiltInComponent<MeshRendererComponent>);
|
||||
RegisterFactory("ScriptComponent", &CreateBuiltInComponent<XCEngine::Scripting::ScriptComponent>);
|
||||
}
|
||||
|
||||
void ComponentFactoryRegistry::RegisterFactory(const std::string& typeName, CreateComponentFn createFn) {
|
||||
|
||||
@@ -43,6 +43,41 @@ std::unordered_map<GameObject::ID, GameObject*>& GameObject::GetGlobalRegistry()
|
||||
return registry;
|
||||
}
|
||||
|
||||
void GameObject::NotifyComponentsBecameActive() {
|
||||
for (auto& comp : m_components) {
|
||||
if (comp->IsEnabled()) {
|
||||
comp->OnEnable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameObject::NotifyComponentsBecameInactive() {
|
||||
for (auto& comp : m_components) {
|
||||
if (comp->IsEnabled()) {
|
||||
comp->OnDisable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameObject::PropagateActiveInHierarchyChange(bool oldParentActiveInHierarchy, bool newParentActiveInHierarchy) {
|
||||
const bool wasActiveInHierarchy = oldParentActiveInHierarchy && m_activeSelf;
|
||||
const bool isActiveInHierarchy = newParentActiveInHierarchy && m_activeSelf;
|
||||
|
||||
if (wasActiveInHierarchy != isActiveInHierarchy) {
|
||||
if (isActiveInHierarchy) {
|
||||
NotifyComponentsBecameActive();
|
||||
} else {
|
||||
NotifyComponentsBecameInactive();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto* child : m_children) {
|
||||
if (child) {
|
||||
child->PropagateActiveInHierarchyChange(wasActiveInHierarchy, isActiveInHierarchy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameObject::SetParent(GameObject* parent) {
|
||||
SetParent(parent, true);
|
||||
}
|
||||
@@ -52,6 +87,9 @@ void GameObject::SetParent(GameObject* parent, bool worldPositionStays) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool oldParentActiveInHierarchy = m_parent ? m_parent->IsActiveInHierarchy() : true;
|
||||
const bool wasActiveInHierarchy = oldParentActiveInHierarchy && m_activeSelf;
|
||||
|
||||
if (m_parent) {
|
||||
auto& siblings = m_parent->m_children;
|
||||
siblings.erase(std::remove(siblings.begin(), siblings.end(), this), siblings.end());
|
||||
@@ -72,6 +110,21 @@ void GameObject::SetParent(GameObject* parent, bool worldPositionStays) {
|
||||
}
|
||||
|
||||
GetTransform()->SetParent(parent ? parent->GetTransform() : nullptr, worldPositionStays);
|
||||
|
||||
const bool newParentActiveInHierarchy = m_parent ? m_parent->IsActiveInHierarchy() : true;
|
||||
const bool isActiveInHierarchy = newParentActiveInHierarchy && m_activeSelf;
|
||||
if (wasActiveInHierarchy != isActiveInHierarchy) {
|
||||
if (isActiveInHierarchy) {
|
||||
NotifyComponentsBecameActive();
|
||||
} else {
|
||||
NotifyComponentsBecameInactive();
|
||||
}
|
||||
for (auto* child : m_children) {
|
||||
if (child) {
|
||||
child->PropagateActiveInHierarchyChange(wasActiveInHierarchy, isActiveInHierarchy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GameObject* GameObject::GetChild(size_t index) const {
|
||||
@@ -101,9 +154,26 @@ void GameObject::DetachFromParent() {
|
||||
}
|
||||
|
||||
void GameObject::SetActive(bool active) {
|
||||
if (m_activeSelf != active) {
|
||||
m_activeSelf = active;
|
||||
if (m_parent == nullptr || m_parent->IsActiveInHierarchy()) {
|
||||
if (m_activeSelf == active) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool parentActiveInHierarchy = m_parent ? m_parent->IsActiveInHierarchy() : true;
|
||||
const bool wasActiveInHierarchy = parentActiveInHierarchy && m_activeSelf;
|
||||
|
||||
m_activeSelf = active;
|
||||
|
||||
const bool isActiveInHierarchy = parentActiveInHierarchy && m_activeSelf;
|
||||
if (wasActiveInHierarchy != isActiveInHierarchy) {
|
||||
if (isActiveInHierarchy) {
|
||||
NotifyComponentsBecameActive();
|
||||
} else {
|
||||
NotifyComponentsBecameInactive();
|
||||
}
|
||||
for (auto* child : m_children) {
|
||||
if (child) {
|
||||
child->PropagateActiveInHierarchyChange(wasActiveInHierarchy, isActiveInHierarchy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,35 +225,78 @@ void GameObject::Awake() {
|
||||
}
|
||||
|
||||
void GameObject::Start() {
|
||||
for (auto& comp : m_components) {
|
||||
if (comp->IsEnabled()) {
|
||||
comp->Start();
|
||||
if (!IsActiveInHierarchy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_started) {
|
||||
for (auto& comp : m_components) {
|
||||
if (comp->IsEnabled()) {
|
||||
comp->Start();
|
||||
}
|
||||
}
|
||||
m_started = true;
|
||||
}
|
||||
|
||||
for (auto* child : m_children) {
|
||||
if (child) {
|
||||
child->Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameObject::Update(float deltaTime) {
|
||||
if (!IsActiveInHierarchy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& comp : m_components) {
|
||||
if (comp->IsEnabled()) {
|
||||
comp->Update(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto* child : m_children) {
|
||||
if (child) {
|
||||
child->Update(deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameObject::FixedUpdate() {
|
||||
if (!IsActiveInHierarchy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& comp : m_components) {
|
||||
if (comp->IsEnabled()) {
|
||||
comp->FixedUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto* child : m_children) {
|
||||
if (child) {
|
||||
child->FixedUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameObject::LateUpdate(float deltaTime) {
|
||||
if (!IsActiveInHierarchy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& comp : m_components) {
|
||||
if (comp->IsEnabled()) {
|
||||
comp->LateUpdate(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto* child : m_children) {
|
||||
if (child) {
|
||||
child->LateUpdate(deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameObject::OnDestroy() {
|
||||
@@ -193,9 +306,10 @@ void GameObject::OnDestroy() {
|
||||
}
|
||||
|
||||
void GameObject::Destroy() {
|
||||
OnDestroy();
|
||||
if (m_scene) {
|
||||
m_scene->DestroyGameObject(this);
|
||||
} else {
|
||||
OnDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,6 +317,7 @@ void GameObject::Serialize(std::ostream& os) const {
|
||||
os << "name=" << m_name << ";";
|
||||
os << "active=" << (m_activeSelf ? "1" : "0") << ";";
|
||||
os << "id=" << m_id << ";";
|
||||
os << "uuid=" << m_uuid << ";";
|
||||
os << "transform=";
|
||||
m_transform->Serialize(os);
|
||||
os << ";";
|
||||
@@ -233,6 +348,9 @@ void GameObject::Deserialize(std::istream& is) {
|
||||
} else if (strcmp(key, "id") == 0) {
|
||||
is >> m_id;
|
||||
if (is.peek() == ';') is.get();
|
||||
} else if (strcmp(key, "uuid") == 0) {
|
||||
is >> m_uuid;
|
||||
if (is.peek() == ';') is.get();
|
||||
} else if (strcmp(key, "transform") == 0) {
|
||||
m_transform->Deserialize(is);
|
||||
if (is.peek() == ';') is.get();
|
||||
|
||||
Reference in New Issue
Block a user