Added Entity and WIP GameObject

This commit is contained in:
Kah Wei 2022-09-12 19:00:54 +08:00
parent 2fb61609e9
commit b0e9469e4e
6 changed files with 569 additions and 0 deletions

View File

@ -25,6 +25,12 @@
<Filter>Math</Filter> <Filter>Math</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\SHpch.h" /> <ClInclude Include="src\SHpch.h" />
<ClInclude Include="src\Scripts\Entity.hxx">
<Filter>Scripts</Filter>
</ClInclude>
<ClInclude Include="src\Scripts\GameObject.hxx">
<Filter>Scripts</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\Engine\DisposableAssemblyLoadContext.cxx"> <ClCompile Include="src\Engine\DisposableAssemblyLoadContext.cxx">
@ -40,5 +46,11 @@
<Filter>Math</Filter> <Filter>Math</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\SHpch.cpp" /> <ClCompile Include="src\SHpch.cpp" />
<ClCompile Include="src\Scripts\Entity.cxx">
<Filter>Scripts</Filter>
</ClCompile>
<ClCompile Include="src\Scripts\GameObject.cxx">
<Filter>Scripts</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -31,6 +31,7 @@ project "SHADE_Managed"
"%{IncludeDir.imnodes}", "%{IncludeDir.imnodes}",
"%{IncludeDir.yamlcpp}", "%{IncludeDir.yamlcpp}",
"%{IncludeDir.RTTR}/include", "%{IncludeDir.RTTR}/include",
"%{wks.location}/SHADE_Engine/src"
} }
flags flags

View File

@ -0,0 +1,28 @@
/************************************************************************************//*!
\file Entity.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 EntityUtils managed
static 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 "Entity.hxx"
// External Dependencies
#include "Engine/ECS_Base/System/SHEntityManager.h"
namespace SHADE
{
bool EntityUtils::IsValid(Entity^ entity)
{
return SHEntityManager::IsValidEID(static_cast<EntityID>(entity));
}
}

View File

@ -0,0 +1,41 @@
/************************************************************************************//*!
\file Entity.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2021
\brief Contains the definitions of a managed Entity identifier and declarations
of useful utility functions for working with Entity identifiers.
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.
*//*************************************************************************************/
#pragma once
// External Dependencies
#include "Engine/ECS_Base/Entity/SHEntity.h"
namespace SHADE
{
/// <summary>
/// Managed representation of a native ECS Entity.
/// </summary>
using Entity = System::UInt32;
/// <summary>
/// Static class that contains useful utility functions for working with Entity.
/// </summary>
private ref class EntityUtils abstract sealed
{
public:
/// <summary>
/// Checks if the specified entity is valid. This is done by checking if it
/// matches Pls::Entity::INVALID.
/// </summary>
/// <param name="entity">The Entity to check.</param>
/// <returns>True if the specified Entity is valid.</returns>
static bool IsValid(Entity^ entity);
};
}

View File

@ -0,0 +1,205 @@
/************************************************************************************//*!
\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 "Engine/ECS_Base/System/SHEntityManager.h"
// Project Headers
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Static Functions */
/*---------------------------------------------------------------------------------*/
GameObject GameObject::Create()
{
throw gcnew System::NotImplementedException();
}
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()
{
throw gcnew System::NotImplementedException();
}
bool GameObject::IsActiveSelf::get()
{
throw gcnew System::NotImplementedException();
}
bool GameObject::IsActiveInHierarchy::get()
{
throw gcnew System::NotImplementedException();
}
/*---------------------------------------------------------------------------------*/
/* GameObject Property Functions */
/*---------------------------------------------------------------------------------*/
void GameObject::SetName(System::String^ name)
{
throw gcnew System::NotImplementedException();
}
void GameObject::SetActive(bool active)
{
throw gcnew System::NotImplementedException();
}
/*---------------------------------------------------------------------------------*/
/* Component Functions */
/*---------------------------------------------------------------------------------*/
generic <typename T>
T GameObject::AddComponent()
{
throw gcnew System::NotImplementedException();
//return ECS::AddComponent<T>(GetNativeEntity());
}
generic <typename T>
T GameObject::GetComponent()
{
throw gcnew System::NotImplementedException();
//return ECS::GetComponent<T>(GetNativeEntity());
}
generic <typename T>
T GameObject::GetComponentInChildren()
{
throw gcnew System::NotImplementedException();
//return ECS::GetComponentInChildren<T>(GetNativeEntity());
}
generic <typename T>
T GameObject::EnsureComponent()
{
throw gcnew System::NotImplementedException();
//return ECS::EnsureComponent<T>(GetNativeEntity());
}
generic <typename T>
void GameObject::RemoveComponent()
{
throw gcnew System::NotImplementedException();
//ECS::RemoveComponent<T>(GetNativeEntity());
}
/*---------------------------------------------------------------------------------*/
/* Script Access Functions */
/*---------------------------------------------------------------------------------*/
generic <typename T>
T GameObject::AddScript()
{
throw gcnew System::NotImplementedException();
//return ScriptStore::AddScript<T>(entity);
}
generic <typename T>
T GameObject::GetScript()
{
throw gcnew System::NotImplementedException();
//return ScriptStore::GetScript<T>(entity);
}
generic <typename T>
T GameObject::GetScriptInChildren()
{
throw gcnew System::NotImplementedException();
//return ScriptStore::GetScriptInChildren<T>(entity);
}
generic <typename T>
System::Collections::Generic::IEnumerable<T>^ GameObject::GetScripts()
{
throw gcnew System::NotImplementedException();
//return ScriptStore::GetScripts<T>(entity);
}
generic <typename T>
void GameObject::RemoveScript()
{
throw gcnew System::NotImplementedException();
//ScriptStore::RemoveScript<T>(entity);
}
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
GameObject::GameObject(EntityID entity)
: entity { entity }
{}
GameObject::GameObject(Entity entity)
: entity { entity }
{}
/*---------------------------------------------------------------------------------*/
/* Getters */
/*---------------------------------------------------------------------------------*/
SHEntity GameObject::GetNativeEntity()
{
throw gcnew System::NotImplementedException();
//return Convert::ToNative(entity);
}
/*---------------------------------------------------------------------------------*/
/* 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);
}
}

View File

@ -0,0 +1,282 @@
/************************************************************************************//*!
\file GameObject.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2021
\brief Contains the definitions of the GameObject managed class which define an
abstraction for working with Entities in managed code.
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.
*//*************************************************************************************/
#pragma once
// Project Includes
#include "Entity.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------------------*/
ref class PlushieScript;
ref class BaseComponent;
/*---------------------------------------------------------------------------------*/
/* Class Definitions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Lightweight object for an PlushieEngine Entity that allows for easy access
/// to Component and Script operations.
/// </summary>
public value class GameObject : public System::IEquatable<GameObject>
{
public:
/*-----------------------------------------------------------------------------*/
/* Static Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Creates a new GameObject in the current Scene. If multiple Scenes are loaded,
/// and you would like to create an object in a specific Scene, call the Scene's
/// CreateGameObject().
/// </summary>
/// <returns>GameObject that represents the newly created GameObject.</returns>
static GameObject Create();
/// <summary>
/// Destroys the specified GameObject. Note that the specified GameObject will no
/// longer be a valid GameObject after this function is called.
/// </summary>
/// <param name="obj">The GameObject to be destroyed.</param>
static void Destroy(GameObject obj);
/// <summary>
/// Retrieves a GameObject with the specified name. If there are multiple
/// GameObjects with the same name, the first found GameObject will be retrieved.
/// There is no guaranteed order of which GameObject is considered "first".
/// </summary>
/// <param name="name">Name of the GameObject to find.</param>
/// <returns>GameObject that has the specified name. Null if not found.</returns>
static System::Nullable<GameObject> Find(System::String^ name);
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Name of the object that this Entity represents.
/// </summary>
property System::String^ Name
{
System::String^ get();
}
/// <summary>
/// Whether or not this Entity alone, is active. This does not mean that this
/// object is active in the scene. For example, if this Entity's parent is not
/// active, then this Entity would also be not active.
/// </summary>
property bool IsActiveSelf
{
bool get();
}
/// <summary>
/// Whether or not this Entity is active in the Scene hierarchy.
/// </summary>
property bool IsActiveInHierarchy
{
bool get();
}
/*-----------------------------------------------------------------------------*/
/* GameObject Property Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Sets the name of this GameObject.
/// </summary>
/// <param name="name">The name to set.</param>
void SetName(System::String^ name);
/// <summary>
/// Sets the active state of this GameObject.
/// <br/>
/// The actual "activeness" of this GameObject is still dependent on the parents'
/// active states.
/// </summary>
/// <param name="active">
/// Whether to activate or deactivate this GameObject.
/// </param>
void SetActive(bool active);
/*-----------------------------------------------------------------------------*/
/* Component Access Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds a Component to this GameObject.
/// </summary>
/// <typeparam name="T">Type of the Component to add. </typeparam>
/// <returns>Reference to the Component that was added.</returns>
generic<typename T> where T : BaseComponent
T AddComponent();
/// <summary>
/// Gets a Component from this GameObject.
/// </summary>
/// <typeparam name="T">Type of the Component to get.</typeparam>
/// <returns>
/// Reference to the Component or null if this GameObject does not have the
/// specified Component.
/// </returns>
generic<typename T> where T : BaseComponent
T GetComponent();
/// <summary>
/// Retrieves the first Component from this GameObject's children that matches
/// the specified type.
/// </summary>
/// <typeparam name="T">Type of the Component to get.</typeparam>
/// <returns>
/// Reference to the Component or null if neither of this GameObject's children
/// does not have the specified Component.
/// </returns>
generic<typename T> where T : BaseComponent
T GetComponentInChildren();
/// <summary>
/// Ensures a Component on this GameObject.
/// </summary>
/// <typeparam name="T">Type of the Component to ensure.</typeparam>
/// <returns>
/// Reference to the Component.
/// </returns>
generic<typename T> where T : BaseComponent
T EnsureComponent();
/// <summary>
/// Removes a Component from this GameObject. If no Component exists to begin
/// with, nothing happens.
/// </summary>
/// <typeparam name="T">Type of the Component to get.</typeparam>
generic<typename T> where T : BaseComponent
void RemoveComponent();
/*-----------------------------------------------------------------------------*/
/* Script Access Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds a PlushieScript of the specified type to this GameObject.
/// </summary>
/// <typeparam name="T">Type of PlushieScript to add.</typeparam>
/// <returns>Reference to the created PlushieScript.</returns>
generic<typename T> where T : ref class, PlushieScript
T AddScript();
/// <summary>
/// Retrieves a PlushieScript of the specified type from this GameObject.
/// If multiple PlushieScripts of the same specified type are added on the same
/// GameObject, this will retrieve the first one added.
/// </summary>
/// <typeparam name="T">Type of PlushieScript to retrieve.</typeparam>
/// <returns>Reference to the PlushieScript to retrieve.</returns>
generic<typename T> where T : ref class, PlushieScript
T GetScript();
/// <summary>
/// Retrieves a PlushieScript of the specified type from child GameObjects.
/// If multiple PlushieScripts of the same specified type are added on the same
/// child GameObject, this will retrieve the first one added.
/// </summary>
/// <typeparam name="T">Type of PlushieScript to retrieve.</typeparam>
/// <returns>Reference to the PlushieScript to retrieve.</returns>
generic<typename T> where T : ref class, PlushieScript
T GetScriptInChildren();
/// <summary>
/// Retrieves a immutable list of PlushieScripts of the specified type from this
/// GameObject.
/// </summary>
/// <typeparam name="T">Type of PlushieScripts to retrieve.</typeparam>
/// <returns>Immutable list of PlushieScripts of the specified type.</returns>
generic<typename T> where T : ref class, PlushieScript
System::Collections::Generic::IEnumerable<T>^ GetScripts();
/// <summary>
/// Removes all PlushieScripts of the specified type from this GameObject.
/// </summary>
/// <typeparam name="T">Type of PLushieScripts to remove.</typeparam>
generic<typename T> where T : ref class, PlushieScript
void RemoveScript();
internal:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for the GameObject.
/// </summary>
/// <param name="entity">
/// The ECS Entity that this GameObject should represent.
/// </param>
GameObject(SHEntity entity);
/// <summary>
/// Constructor for the GameObject.
/// </summary>
/// <param name="entity">
/// Managed numerical representation of the ECS Entity that this GameObject
/// should represent.
/// </param>
GameObject(Entity entity);
/*-----------------------------------------------------------------------------*/
/* Getters */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Retrieves the CLR Entity object that this GameObject represents.
/// </summary>
/// <returns>Entity object that this GameObject represents.</returns>
inline Entity GetEntity() { return entity; }
/// <summary>
/// Retrieves the native Entity object that this GameObject represents.
/// </summary>
/// <returns>Native Entity object that this GameObject represents.</returns>
SHEntity GetNativeEntity();
private:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
Entity entity;
public:
/*-----------------------------------------------------------------------------*/
/* IEquatable */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Compares equality with an object of the same type.
/// </summary>
/// <param name="other">The object to compare with.</param>
/// <returns>True if both objects are the same.</returns>
virtual bool Equals(GameObject other);
/*-----------------------------------------------------------------------------*/
/* Object */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Compares equality with another unboxed object.
/// </summary>
/// <param name="o">The unboxed object to compare with.</param>
/// <returns>True if both objects are the same.</returns>
bool Equals(Object^ o) override;
/// <summary>
/// Gets a unique hash for this object.
/// </summary>
/// <returns>Unique hash for this object.</returns>
int GetHashCode() override;
/// <summary>
/// Checks if two GameObject references are the same.
/// </summary>
/// <param name="lhs">GameObject to check.</param>
/// <param name="rhs">Another GameObject to check with.</param>
/// <returns>True if both Components are the same.</returns>
static bool operator==(GameObject lhs, GameObject rhs);
/// <summary>
/// Checks if two GameObject references are different.
/// </summary>
/// <param name="lhs">GameObject to check.</param>
/// <param name="rhs">Another GameObject to check with.</param>
/// <returns>True if both Components are different.</returns>
static bool operator!=(GameObject lhs, GameObject rhs);
};
}