2022-09-12 19:00:54 +08:00
|
|
|
/************************************************************************************//*!
|
|
|
|
\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
|
2022-09-13 15:18:56 +08:00
|
|
|
#include "ECS_Base/System/SHEntityManager.h"
|
2022-09-12 19:00:54 +08:00
|
|
|
// Project Headers
|
2022-09-13 15:18:56 +08:00
|
|
|
#include "ECS.hxx"
|
|
|
|
#include "Scripts/ScriptStore.hxx"
|
2022-09-12 19:00:54 +08:00
|
|
|
|
|
|
|
namespace SHADE
|
|
|
|
{
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
/* Static Functions */
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
GameObject GameObject::Create()
|
|
|
|
{
|
2022-09-13 18:42:43 +08:00
|
|
|
return GameObject(SHEntityManager::CreateEntity());
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2022-09-13 18:42:43 +08:00
|
|
|
return Convert::ToCLI(GetNativeEntity().name);
|
|
|
|
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
bool GameObject::IsActiveSelf::get()
|
|
|
|
{
|
2022-09-13 18:42:43 +08:00
|
|
|
return GetNativeEntity().isActive;
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
bool GameObject::IsActiveInHierarchy::get()
|
|
|
|
{
|
|
|
|
throw gcnew System::NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
/* GameObject Property Functions */
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
void GameObject::SetName(System::String^ name)
|
|
|
|
{
|
2022-09-13 18:42:43 +08:00
|
|
|
GetNativeEntity().name = Convert::ToNative(name);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
void GameObject::SetActive(bool active)
|
|
|
|
{
|
2022-09-13 18:42:43 +08:00
|
|
|
GetNativeEntity().isActive = active;
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
/* Component Functions */
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
generic <typename T>
|
|
|
|
T GameObject::AddComponent()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
return ECS::AddComponent<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generic <typename T>
|
|
|
|
T GameObject::GetComponent()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
return ECS::GetComponent<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generic <typename T>
|
|
|
|
T GameObject::GetComponentInChildren()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
return ECS::GetComponentInChildren<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generic <typename T>
|
|
|
|
T GameObject::EnsureComponent()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
return ECS::EnsureComponent<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generic <typename T>
|
|
|
|
void GameObject::RemoveComponent()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
ECS::RemoveComponent<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
/* Script Access Functions */
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
generic <typename T>
|
|
|
|
T GameObject::AddScript()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
return ScriptStore::AddScript<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generic <typename T>
|
|
|
|
T GameObject::GetScript()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
return ScriptStore::GetScript<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generic <typename T>
|
|
|
|
T GameObject::GetScriptInChildren()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
return ScriptStore::GetScriptInChildren<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generic <typename T>
|
|
|
|
System::Collections::Generic::IEnumerable<T>^ GameObject::GetScripts()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
return ScriptStore::GetScripts<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generic <typename T>
|
|
|
|
void GameObject::RemoveScript()
|
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
ScriptStore::RemoveScript<T>(entity);
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
/* Constructors */
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
2022-09-13 18:37:05 +08:00
|
|
|
GameObject::GameObject(const SHEntity& entity)
|
|
|
|
: entity { entity.GetEID() }
|
2022-09-12 19:00:54 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
GameObject::GameObject(Entity entity)
|
|
|
|
: entity { entity }
|
|
|
|
{}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
/* Getters */
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
2022-09-13 15:18:56 +08:00
|
|
|
SHEntity& GameObject::GetNativeEntity()
|
2022-09-12 19:00:54 +08:00
|
|
|
{
|
2022-09-13 15:18:56 +08:00
|
|
|
SHEntity* nativeEntity = SHEntityManager::GetEntityByID(entity);
|
|
|
|
if (nativeEntity == nullptr)
|
|
|
|
throw gcnew System::InvalidOperationException("[GameObject] Unable to obtain native Entity for GameObject.");
|
|
|
|
|
|
|
|
return *nativeEntity;
|
2022-09-12 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------------*/
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|