SHADE_Y3/SHADE_Managed/src/Engine/ECS.hxx

175 lines
8.1 KiB
C++
Raw Normal View History

/************************************************************************************//*!
\file ECS.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
// External Dependencies
#include "ECS_Base/Managers/SHComponentManager.h"
// Project Includes
#include "Components/Component.hxx"
namespace SHADE
{
/// <summary>
/// Static class which contains functions that map Pls::ECS's Component manipulation
/// functions to managed generic functions.
/// </summary>
private ref class ECS abstract sealed
{
public:
/*-----------------------------------------------------------------------------*/
/* Component Manipulation Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds a Component to the specified Entity.
/// </summary>
/// <typeparam name="T">Type of the Component to add.</typeparam>
/// <param name="entity">
/// Entity object that should have the specified Component added to.
/// </param>
/// <returns>Reference to the Component that was added.</returns>
generic<typename T> where T : BaseComponent
static T AddComponent(EntityID entity);
/// <summary>
/// Gets a Component from the specified Entity.
/// </summary>
/// <typeparam name="T">Type of the Component to get.</typeparam>
/// <param name="entity"> Entity object to get the Component from. </param>
/// <returns>
/// Reference to the Component or null if the Entity does not have the
/// specified Component.
/// </returns>
generic<typename T> where T : BaseComponent
static T GetComponent(EntityID entity);
/// <summary>
/// Retrieves the first Component from the specified GameObjectt's children that
/// matches the specified type.
/// </summary>
/// <typeparam name="T">Type of the Component to get.</typeparam>
/// <param name="entity"> Entity object to get the Component from. </param>
/// <returns>
/// Reference to the Component or null if the Entity does not have the
/// specified Component.
/// </returns>
generic<typename T> where T : BaseComponent
static T GetComponentInChildren(EntityID entity);
/// <summary>
/// Ensures a Component on the specified Entity.
/// </summary>
/// <typeparam name="T">Type of the Component to ensure.</typeparam>
/// <param name="entity"> Entity object to ensure the Component on. </param>
/// <returns>Reference to the Component.</returns>
generic<typename T> where T : BaseComponent
static T EnsureComponent(EntityID entity);
/// <summary>
/// Checks if the specified Entity has the specified Component.
/// </summary>
/// <typeparam name="T">Type of the Component to check for.</typeparam>
/// <param name="entity">Entity object to check for the Component.</param>
/// <returns>
/// True if the specified Entity has the specified Component. False otherwise.
/// </returns>
generic<typename T> where T : BaseComponent
static bool HasComponent(EntityID entity);
/// <summary>
/// Removes a Component from the specified Entity.
/// </summary>
/// <typeparam name="T">Type of the Component to remove.</typeparam>
/// <param name="entity">
/// Entity object that should have the specified Component removed from/
/// </param>
generic<typename T> where T : BaseComponent
static void RemoveComponent(EntityID entity);
internal:
/*-----------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Pointer to a function for Component manipulation operations.
/// </summary>
using ComponentFunc = void(*)(const EntityID&);
using ComponentHasFunc = bool(*)(const EntityID&);
/// <summary>
/// Contains a set of Component related data used for resolving operations for
/// each Component.
/// </summary>
value struct ComponentSet
{
public:
System::Type^ Type;
ComponentFunc AddFunction;
ComponentHasFunc HasFunction;
ComponentFunc RemoveFunction;
};
/*-----------------------------------------------------------------------------*/
/* Static Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Retrieves a pointer to the native unmanaged component of the specified
/// Entity.
/// </summary>
/// <returns>
/// Pointer to the native component. Will be nullptr if it does not exist.
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the Entity specified is invalid.
/// </exception>
/// <exception cref="System.NullReferenceException">
/// Thrown if an attempt to retrieve the native component fails.
/// </exception>
template<typename NativeComponent>
static NativeComponent* GetNativeComponent(Entity entity);
private:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Static constructor to initialize static data
/// </summary>
static ECS();
/*-----------------------------------------------------------------------------*/
/* Static Data Members */
/*-----------------------------------------------------------------------------*/
static System::Collections::Generic::List<ComponentSet> componentMap;
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Creates a ComponentSet for a pair of Native and Managed Components.
/// </summary>
/// <typeparam name="NativeType">Type of the Native Component.</typeparam>
/// <typeparam name="ManagedType">Type of the Managed Component.</typeparam>
/// <returns>ComponentSet for the parameters specified.</returns>
template<typename NativeType, typename ManagedType>
static ComponentSet createComponentSet();
/// <summary>
/// Creates an instance of the Managed representation of a Component with a
/// native Entity.
/// </summary>
/// <typeparam name="T">Type of Component to create.</typeparam>
/// <param name="entity">Native Entity that this Component is tied to.</param>
/// <returns>The created Managed representation of the Component.</returns>
generic<typename T> where T : BaseComponent
static T createManagedComponent(EntityID entity);
};
}
#include "ECS.h++"