SHADE_Y3/SHADE_Managed/src/Engine/GameObject.cxx

202 lines
6.7 KiB
C++

/************************************************************************************//*!
\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<EntityID>(obj.GetEntity()));
}
System::Nullable<GameObject> 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 <typename T>
T GameObject::AddComponent()
{
return ECS::AddComponent<T>(entity);
}
generic <typename T>
T GameObject::GetComponent()
{
return ECS::GetComponent<T>(entity);
}
generic <typename T>
T GameObject::GetComponentInChildren()
{
return ECS::GetComponentInChildren<T>(entity);
}
generic <typename T>
T GameObject::EnsureComponent()
{
return ECS::EnsureComponent<T>(entity);
}
generic <typename T>
void GameObject::RemoveComponent()
{
ECS::RemoveComponent<T>(entity);
}
/*---------------------------------------------------------------------------------*/
/* Script Access Functions */
/*---------------------------------------------------------------------------------*/
generic <typename T>
T GameObject::AddScript()
{
return ScriptStore::AddScript<T>(entity);
}
generic <typename T>
T GameObject::GetScript()
{
return ScriptStore::GetScript<T>(entity);
}
generic <typename T>
T GameObject::GetScriptInChildren()
{
return ScriptStore::GetScriptInChildren<T>(entity);
}
generic <typename T>
System::Collections::Generic::IEnumerable<T>^ GameObject::GetScripts()
{
return ScriptStore::GetScripts<T>(entity);
}
generic <typename T>
void GameObject::RemoveScript()
{
ScriptStore::RemoveScript<T>(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<GameObject^>(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);
}
}