/************************************************************************************//*! \file GameObject.cxx \author Tng Kah Wei, kahwei.tng, 390009620 \par email: kahwei.tng\@digipen.edu \date Oct 28, 2021 \brief Contains the definition of the functions for the GameObject managed class. Note: This file is written in C++17/CLI. Copyright (C) 2021 DigiPen Institute of Technology. Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ // Precompiled Headers #include "SHpch.h" // Primary Header #include "GameObject.hxx" // External Dependencies #include "ECS_Base/Managers/SHEntityManager.h" // Project Headers #include "ECS.hxx" #include "Scripts/ScriptStore.hxx" namespace SHADE { /*---------------------------------------------------------------------------------*/ /* Static Functions */ /*---------------------------------------------------------------------------------*/ GameObject GameObject::Create() { return GameObject(SHEntityManager::CreateEntity()); } void GameObject::Destroy(GameObject obj) { SHEntityManager::DestroyEntity(static_cast(obj.GetEntity())); } System::Nullable GameObject::Find(System::String ^ name) { // Search the GameObjectLibrary for an Entity with the specified name throw gcnew System::NotImplementedException(); } /*---------------------------------------------------------------------------------*/ /* Properties */ /*---------------------------------------------------------------------------------*/ System::String^ GameObject::Name::get() { return Convert::ToCLI(GetNativeEntity().name); } bool GameObject::IsActiveSelf::get() { return GetNativeEntity().GetActive(); } bool GameObject::IsActiveInHierarchy::get() { return true; // TODO: Update once we have an equivalent on the Entity object } /*---------------------------------------------------------------------------------*/ /* GameObject Property Functions */ /*---------------------------------------------------------------------------------*/ void GameObject::SetName(System::String^ name) { GetNativeEntity().name = Convert::ToNative(name); } void GameObject::SetActive(bool active) { GetNativeEntity().SetActive(active); } /*---------------------------------------------------------------------------------*/ /* Component Functions */ /*---------------------------------------------------------------------------------*/ generic T GameObject::AddComponent() { return ECS::AddComponent(entity); } generic T GameObject::GetComponent() { return ECS::GetComponent(entity); } generic T GameObject::GetComponentInChildren() { return ECS::GetComponentInChildren(entity); } generic T GameObject::EnsureComponent() { return ECS::EnsureComponent(entity); } generic void GameObject::RemoveComponent() { ECS::RemoveComponent(entity); } /*---------------------------------------------------------------------------------*/ /* Script Access Functions */ /*---------------------------------------------------------------------------------*/ generic T GameObject::AddScript() { return ScriptStore::AddScript(entity); } generic T GameObject::GetScript() { return ScriptStore::GetScript(entity); } generic T GameObject::GetScriptInChildren() { return ScriptStore::GetScriptInChildren(entity); } generic System::Collections::Generic::IEnumerable^ GameObject::GetScripts() { return ScriptStore::GetScripts(entity); } generic void GameObject::RemoveScript() { ScriptStore::RemoveScript(entity); } /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ GameObject::GameObject(const SHEntity& entity) : entity { entity.GetEID() } {} GameObject::GameObject(Entity entity) : entity { entity } {} /*---------------------------------------------------------------------------------*/ /* Getters */ /*---------------------------------------------------------------------------------*/ SHEntity& GameObject::GetNativeEntity() { SHEntity* nativeEntity = SHEntityManager::GetEntityByID(entity); if (nativeEntity == nullptr) throw gcnew System::InvalidOperationException("[GameObject] Unable to obtain native Entity for GameObject."); return *nativeEntity; } /*---------------------------------------------------------------------------------*/ /* IEquatable */ /*---------------------------------------------------------------------------------*/ bool GameObject::Equals(GameObject other) { return entity == other.entity; } /*---------------------------------------------------------------------------------*/ /* Object */ /*---------------------------------------------------------------------------------*/ bool GameObject::Equals(Object^ o) { try { GameObject^ cmp = safe_cast(o); return Equals(cmp); } catch (System::InvalidCastException^) { return false; } } int GameObject::GetHashCode() { return entity.GetHashCode(); } bool GameObject::operator==(GameObject lhs, GameObject rhs) { return lhs.Equals(rhs); } bool GameObject::operator!=(GameObject lhs, GameObject rhs) { return !(lhs == rhs); } }