From b0e9469e4ec571627d6374406a45c9f3f0310b1d Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Mon, 12 Sep 2022 19:00:54 +0800 Subject: [PATCH] Added Entity and WIP GameObject --- SHADE_Managed/SHADE_Managed.vcxproj.filters | 12 + SHADE_Managed/premake5.lua | 1 + SHADE_Managed/src/Scripts/Entity.cxx | 28 ++ SHADE_Managed/src/Scripts/Entity.hxx | 41 +++ SHADE_Managed/src/Scripts/GameObject.cxx | 205 ++++++++++++++ SHADE_Managed/src/Scripts/GameObject.hxx | 282 ++++++++++++++++++++ 6 files changed, 569 insertions(+) create mode 100644 SHADE_Managed/src/Scripts/Entity.cxx create mode 100644 SHADE_Managed/src/Scripts/Entity.hxx create mode 100644 SHADE_Managed/src/Scripts/GameObject.cxx create mode 100644 SHADE_Managed/src/Scripts/GameObject.hxx diff --git a/SHADE_Managed/SHADE_Managed.vcxproj.filters b/SHADE_Managed/SHADE_Managed.vcxproj.filters index 86a64c06..182a3937 100644 --- a/SHADE_Managed/SHADE_Managed.vcxproj.filters +++ b/SHADE_Managed/SHADE_Managed.vcxproj.filters @@ -25,6 +25,12 @@ Math + + Scripts + + + Scripts + @@ -40,5 +46,11 @@ Math + + Scripts + + + Scripts + \ No newline at end of file diff --git a/SHADE_Managed/premake5.lua b/SHADE_Managed/premake5.lua index 61f42fc2..27ed920f 100644 --- a/SHADE_Managed/premake5.lua +++ b/SHADE_Managed/premake5.lua @@ -31,6 +31,7 @@ project "SHADE_Managed" "%{IncludeDir.imnodes}", "%{IncludeDir.yamlcpp}", "%{IncludeDir.RTTR}/include", + "%{wks.location}/SHADE_Engine/src" } flags diff --git a/SHADE_Managed/src/Scripts/Entity.cxx b/SHADE_Managed/src/Scripts/Entity.cxx new file mode 100644 index 00000000..bd915c42 --- /dev/null +++ b/SHADE_Managed/src/Scripts/Entity.cxx @@ -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(entity)); + } +} diff --git a/SHADE_Managed/src/Scripts/Entity.hxx b/SHADE_Managed/src/Scripts/Entity.hxx new file mode 100644 index 00000000..525522c2 --- /dev/null +++ b/SHADE_Managed/src/Scripts/Entity.hxx @@ -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 +{ + /// + /// Managed representation of a native ECS Entity. + /// + using Entity = System::UInt32; + + /// + /// Static class that contains useful utility functions for working with Entity. + /// + private ref class EntityUtils abstract sealed + { + public: + /// + /// Checks if the specified entity is valid. This is done by checking if it + /// matches Pls::Entity::INVALID. + /// + /// The Entity to check. + /// True if the specified Entity is valid. + static bool IsValid(Entity^ entity); + }; +} diff --git a/SHADE_Managed/src/Scripts/GameObject.cxx b/SHADE_Managed/src/Scripts/GameObject.cxx new file mode 100644 index 00000000..3fe1c92f --- /dev/null +++ b/SHADE_Managed/src/Scripts/GameObject.cxx @@ -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(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() + { + 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 + T GameObject::AddComponent() + { + throw gcnew System::NotImplementedException(); + //return ECS::AddComponent(GetNativeEntity()); + } + + generic + T GameObject::GetComponent() + { + throw gcnew System::NotImplementedException(); + //return ECS::GetComponent(GetNativeEntity()); + } + + generic + T GameObject::GetComponentInChildren() + { + throw gcnew System::NotImplementedException(); + //return ECS::GetComponentInChildren(GetNativeEntity()); + } + + generic + T GameObject::EnsureComponent() + { + throw gcnew System::NotImplementedException(); + //return ECS::EnsureComponent(GetNativeEntity()); + } + + generic + void GameObject::RemoveComponent() + { + throw gcnew System::NotImplementedException(); + //ECS::RemoveComponent(GetNativeEntity()); + } + + /*---------------------------------------------------------------------------------*/ + /* Script Access Functions */ + /*---------------------------------------------------------------------------------*/ + generic + T GameObject::AddScript() + { + throw gcnew System::NotImplementedException(); + //return ScriptStore::AddScript(entity); + } + + generic + T GameObject::GetScript() + { + throw gcnew System::NotImplementedException(); + //return ScriptStore::GetScript(entity); + } + + generic + T GameObject::GetScriptInChildren() + { + throw gcnew System::NotImplementedException(); + //return ScriptStore::GetScriptInChildren(entity); + } + + generic + System::Collections::Generic::IEnumerable^ GameObject::GetScripts() + { + throw gcnew System::NotImplementedException(); + //return ScriptStore::GetScripts(entity); + } + + generic + void GameObject::RemoveScript() + { + throw gcnew System::NotImplementedException(); + //ScriptStore::RemoveScript(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(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); + } +} diff --git a/SHADE_Managed/src/Scripts/GameObject.hxx b/SHADE_Managed/src/Scripts/GameObject.hxx new file mode 100644 index 00000000..0b030354 --- /dev/null +++ b/SHADE_Managed/src/Scripts/GameObject.hxx @@ -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 */ + /*---------------------------------------------------------------------------------*/ + /// + /// Lightweight object for an PlushieEngine Entity that allows for easy access + /// to Component and Script operations. + /// + public value class GameObject : public System::IEquatable + { + public: + /*-----------------------------------------------------------------------------*/ + /* Static Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// 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(). + /// + /// GameObject that represents the newly created GameObject. + static GameObject Create(); + /// + /// Destroys the specified GameObject. Note that the specified GameObject will no + /// longer be a valid GameObject after this function is called. + /// + /// The GameObject to be destroyed. + static void Destroy(GameObject obj); + /// + /// 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". + /// + /// Name of the GameObject to find. + /// GameObject that has the specified name. Null if not found. + static System::Nullable Find(System::String^ name); + + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Name of the object that this Entity represents. + /// + property System::String^ Name + { + System::String^ get(); + } + /// + /// 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. + /// + property bool IsActiveSelf + { + bool get(); + } + /// + /// Whether or not this Entity is active in the Scene hierarchy. + /// + property bool IsActiveInHierarchy + { + bool get(); + } + + /*-----------------------------------------------------------------------------*/ + /* GameObject Property Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Sets the name of this GameObject. + /// + /// The name to set. + void SetName(System::String^ name); + /// + /// Sets the active state of this GameObject. + ///
+ /// The actual "activeness" of this GameObject is still dependent on the parents' + /// active states. + ///
+ /// + /// Whether to activate or deactivate this GameObject. + /// + void SetActive(bool active); + + /*-----------------------------------------------------------------------------*/ + /* Component Access Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Adds a Component to this GameObject. + /// + /// Type of the Component to add. + /// Reference to the Component that was added. + generic where T : BaseComponent + T AddComponent(); + /// + /// Gets a Component from this GameObject. + /// + /// Type of the Component to get. + /// + /// Reference to the Component or null if this GameObject does not have the + /// specified Component. + /// + generic where T : BaseComponent + T GetComponent(); + /// + /// Retrieves the first Component from this GameObject's children that matches + /// the specified type. + /// + /// Type of the Component to get. + /// + /// Reference to the Component or null if neither of this GameObject's children + /// does not have the specified Component. + /// + generic where T : BaseComponent + T GetComponentInChildren(); + /// + /// Ensures a Component on this GameObject. + /// + /// Type of the Component to ensure. + /// + /// Reference to the Component. + /// + generic where T : BaseComponent + T EnsureComponent(); + /// + /// Removes a Component from this GameObject. If no Component exists to begin + /// with, nothing happens. + /// + /// Type of the Component to get. + generic where T : BaseComponent + void RemoveComponent(); + + /*-----------------------------------------------------------------------------*/ + /* Script Access Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Adds a PlushieScript of the specified type to this GameObject. + /// + /// Type of PlushieScript to add. + /// Reference to the created PlushieScript. + generic where T : ref class, PlushieScript + T AddScript(); + /// + /// 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. + /// + /// Type of PlushieScript to retrieve. + /// Reference to the PlushieScript to retrieve. + generic where T : ref class, PlushieScript + T GetScript(); + /// + /// 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. + /// + /// Type of PlushieScript to retrieve. + /// Reference to the PlushieScript to retrieve. + generic where T : ref class, PlushieScript + T GetScriptInChildren(); + /// + /// Retrieves a immutable list of PlushieScripts of the specified type from this + /// GameObject. + /// + /// Type of PlushieScripts to retrieve. + /// Immutable list of PlushieScripts of the specified type. + generic where T : ref class, PlushieScript + System::Collections::Generic::IEnumerable^ GetScripts(); + /// + /// Removes all PlushieScripts of the specified type from this GameObject. + /// + /// Type of PLushieScripts to remove. + generic where T : ref class, PlushieScript + void RemoveScript(); + + internal: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructor for the GameObject. + /// + /// + /// The ECS Entity that this GameObject should represent. + /// + GameObject(SHEntity entity); + /// + /// Constructor for the GameObject. + /// + /// + /// Managed numerical representation of the ECS Entity that this GameObject + /// should represent. + /// + GameObject(Entity entity); + + /*-----------------------------------------------------------------------------*/ + /* Getters */ + /*-----------------------------------------------------------------------------*/ + /// + /// Retrieves the CLR Entity object that this GameObject represents. + /// + /// Entity object that this GameObject represents. + inline Entity GetEntity() { return entity; } + /// + /// Retrieves the native Entity object that this GameObject represents. + /// + /// Native Entity object that this GameObject represents. + SHEntity GetNativeEntity(); + + private: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + Entity entity; + + public: + /*-----------------------------------------------------------------------------*/ + /* IEquatable */ + /*-----------------------------------------------------------------------------*/ + /// + /// Compares equality with an object of the same type. + /// + /// The object to compare with. + /// True if both objects are the same. + virtual bool Equals(GameObject other); + + /*-----------------------------------------------------------------------------*/ + /* Object */ + /*-----------------------------------------------------------------------------*/ + /// + /// Compares equality with another unboxed object. + /// + /// The unboxed object to compare with. + /// True if both objects are the same. + bool Equals(Object^ o) override; + /// + /// Gets a unique hash for this object. + /// + /// Unique hash for this object. + int GetHashCode() override; + /// + /// Checks if two GameObject references are the same. + /// + /// GameObject to check. + /// Another GameObject to check with. + /// True if both Components are the same. + static bool operator==(GameObject lhs, GameObject rhs); + /// + /// Checks if two GameObject references are different. + /// + /// GameObject to check. + /// Another GameObject to check with. + /// True if both Components are different. + static bool operator!=(GameObject lhs, GameObject rhs); + }; + +} +