SHADE_Y3/SHADE_Managed/src/Components/Component.hxx

201 lines
9.3 KiB
C++

/************************************************************************************//*!
\file Component.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 27, 2021
\brief Contains the definition of the managed Component classes with the
declaration of functions for working with it.
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/Components/SHComponent.h"
// Project Includes
#include "Engine/Entity.hxx"
#include "Scripts/Script.hxx"
namespace SHADE
{
/// <summary>
/// Class that serves as the base for a wrapper class to Components in native code.
/// </summary>
public ref class BaseComponent : public System::IEquatable<BaseComponent^>
{
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Retrieves the GameObject that this Component belongs to.
/// </summary>
property GameObject Owner
{
GameObject get() { return owner; }
}
/*-----------------------------------------------------------------------------*/
/* 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>
/// 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 Script of the specified type to this GameObject.
/// </summary>
/// <typeparam name="T">Type of Script to add.</typeparam>
/// <returns>Reference to the created Script.</returns>
generic<typename T> where T : ref class, Script
T AddScript();
/// <summary>
/// Retrieves a Script of the specified type from this GameObject.
/// If multiple Scripts of the same specified type are added on the same
/// GameObject, this will retrieve the first one added.
/// </summary>
/// <typeparam name="T">Type of Script to add.</typeparam>
/// <returns>Reference to the Script to retrieve.</returns>
generic<typename T> where T : ref class, Script
T GetScript();
/// <summary>
/// Retrieves a immutable list of Scripts of the specified type from this
/// GameObject.
/// </summary>
/// <typeparam name="T">Type of Scripts to Get.</typeparam>
/// <returns>Immutable list of Scripts of the specified type.</returns>
generic<typename T> where T : ref class, Script
System::Collections::Generic::IEnumerable<T>^ GetScripts();
/// <summary>
/// Removes all Scripts of the specified type from this GameObject.
/// </summary>
/// <typeparam name="T">Type of PLushieScripts to remove.</typeparam>
generic<typename T> where T : ref class, Script
void RemoveScript();
protected:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for BaseComponent to tie it to a specific Entity.
/// Constructors of derived Components should call this Constructor.
/// </summary>
/// <param name="entity">Entity that this Component will be tied to.</param>
BaseComponent(Entity entity);
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Entity that this Component belongs to.
/// </summary>
GameObject owner;
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(BaseComponent^ 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>
/// C++ template for the BaseComponent class used to generate common template-able
/// functions and types.
/// </summary>
/// <typeparam name="NativeType">
/// Type of the native component that this Component wraps.
/// </typeparam>
template<typename NativeType>
public ref class Component : public BaseComponent
{
internal:
/*-----------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Type of the native component that this Component wraps.
/// </summary>
using NativeComponent = NativeType;
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Retrieves a pointer to the native unmanaged component that is tied to the
/// Entity described by the owner value.
/// </summary>
/// <returns>
/// Pointer to the native component. Will be nullptr if it does not exist.
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the internal ID stored by this native component is invalid.
/// </exception>
/// <exception cref="System.NullReferenceException">
/// Thrown if an attempt to retrieve the native component fails.
/// </exception>
NativeComponent* GetNativeComponent();
protected:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for Component to tie it to a specific Entity.
/// Constructors of derived Components should call this Constructor.
/// </summary>
/// <param name="entity">Entity that this Component will be tied to.</param>
Component(Entity entity);
};
}
#include "Component.h++"