2022-09-13 13:51:11 +08:00
|
|
|
/************************************************************************************//*!
|
|
|
|
\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
|
2022-09-13 15:18:56 +08:00
|
|
|
#include "ECS_Base/Components/SHComponent.h"
|
2022-09-13 13:51:11 +08:00
|
|
|
// 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>
|
2022-09-13 15:18:56 +08:00
|
|
|
/// Adds a Script of the specified type to this GameObject.
|
2022-09-13 13:51:11 +08:00
|
|
|
/// </summary>
|
2022-09-13 15:18:56 +08:00
|
|
|
/// <typeparam name="T">Type of Script to add.</typeparam>
|
|
|
|
/// <returns>Reference to the created Script.</returns>
|
|
|
|
generic<typename T> where T : ref class, Script
|
2022-09-13 13:51:11 +08:00
|
|
|
T AddScript();
|
|
|
|
/// <summary>
|
2022-09-13 15:18:56 +08:00
|
|
|
/// Retrieves a Script of the specified type from this GameObject.
|
|
|
|
/// If multiple Scripts of the same specified type are added on the same
|
2022-09-13 13:51:11 +08:00
|
|
|
/// GameObject, this will retrieve the first one added.
|
|
|
|
/// </summary>
|
2022-09-13 15:18:56 +08:00
|
|
|
/// <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
|
2022-09-13 13:51:11 +08:00
|
|
|
T GetScript();
|
|
|
|
/// <summary>
|
2022-09-13 15:18:56 +08:00
|
|
|
/// Retrieves a immutable list of Scripts of the specified type from this
|
2022-09-13 13:51:11 +08:00
|
|
|
/// GameObject.
|
|
|
|
/// </summary>
|
2022-09-13 15:18:56 +08:00
|
|
|
/// <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
|
2022-09-13 13:51:11 +08:00
|
|
|
System::Collections::Generic::IEnumerable<T>^ GetScripts();
|
|
|
|
/// <summary>
|
2022-09-13 15:18:56 +08:00
|
|
|
/// Removes all Scripts of the specified type from this GameObject.
|
2022-09-13 13:51:11 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Type of PLushieScripts to remove.</typeparam>
|
2022-09-13 15:18:56 +08:00
|
|
|
generic<typename T> where T : ref class, Script
|
2022-09-13 13:51:11 +08:00
|
|
|
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++"
|