SHADE_Y3/SHADE_Managed/src/Scripts/Script.hxx

275 lines
12 KiB
C++

/************************************************************************************//*!
\file Script.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2021
\brief Contains the definition of the Script 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.
*//*************************************************************************************/
#pragma once
// Project Includes
#include "Engine/GameObject.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------------------*/
ref class BaseComponent;
/*---------------------------------------------------------------------------------*/
/* Class Definitions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Class that forms the basis of all "script"-objects that can be attached to
/// Entities to update each Entity's Components via C# code.
/// </summary>
public ref class Script
{
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// GameObject that this Script belongs to.
/// </summary>
property GameObject Owner
{
GameObject get() { return owner; }
}
/*-----------------------------------------------------------------------------*/
/* Component Access Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds a Component to the GameObject that this Script belongs to.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to add. Must be derived from BaseComponent.
/// </typeparam>
/// <returns>Reference to the Component that was added.</returns>
generic<typename T> where T : BaseComponent
T AddComponent();
/// <summary>
/// Gets a Component from the GameObject that this Script belongs to.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to get. Must be derived from BaseComponent.
/// </typeparam>
/// <returns>Reference to the Component that was retrieved.</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. Must be derived from BaseComponent.
/// </typeparam>
/// <returns>Reference to the Component that was retrieved.</returns>
generic<typename T> where T : BaseComponent
T GetComponentInChildren();
/// <summary>
/// Ensures a Component on the GameObject that this Script belongs to.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to ensure. Must be derived from BaseComponent.
/// </typeparam>
/// <returns>Reference to the Component.</returns>
generic<typename T> where T : BaseComponent
T EnsureComponent();
/// <summary>
/// Removes a Component from the GameObject that this Script belongs to.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to remove. Must be derived from BaseComponent.
/// </typeparam>
generic<typename T> where T : BaseComponent
void RemoveComponent();
/*-----------------------------------------------------------------------------*/
/* Script Access Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds a Script to this GameObject.
/// </summary>
/// <typeparam name="T">
/// Type of script to add.
/// This needs to be a default constructable Script.
/// </typeparam>
/// <returns>Reference to the script added</returns>
generic<typename T> where T : ref class, Script
T AddScript();
/// <summary>
/// Retrieves the first Script from this GameObject that matches the specified
/// type.
/// </summary>
/// <typeparam name="T">
/// Type of script to get.
/// This needs to be a default constructable Script.
/// </typeparam>
/// <returns>Reference to the script added</returns>
generic<typename T> where T : ref class, Script
T GetScript();
/// <summary>
/// Retrieves the first Script from this GameObject's children that matches the
/// specified type.
/// </summary>
/// <typeparam name="T">
/// Type of script to get.
/// This needs to be a default constructable Script.
/// </typeparam>
/// <returns>Reference to the script added</returns>
generic<typename T> where T : ref class, Script
T GetScriptInChildren();
/// <summary>
/// Retrieves a immutable list of scripts from the specified Entity that
/// matches the specified type.
/// <br/>
/// Note that this function allocates. It should be used sparingly.
/// </summary>
/// <typeparam name="T">
/// Type of scripts to get.
/// This needs to be a default constructable Script.
/// </typeparam>
/// <returns>
/// Immutable list of references to 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 script to remove.
/// This needs to be a default constructable Script.
/// </typeparam>
generic<typename T> where T : ref class, Script
void RemoveScript();
internal:
/*-----------------------------------------------------------------------------*/
/* "All-Time" Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Used to call onAttached(). This is called immediately when this script is
/// attached to a GameObject.
/// </summary>
void OnAttached();
/// <summary>
/// Used to call onDetached(). This is called immediately when this script is
/// detached from a GameObject.
/// </summary>
void OnDetached();
/*-----------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Used to call awake(). This should be called on the first frame that the
/// attached GameObject is active if they are a part of the scene.
/// </summary>
void Awake();
/// <summary>
/// Used to call start(). This should be called on the first frame that the
/// attached GameObject is active but always after Awake().
/// </summary>
void Start();
/// <summary>
/// Used to call fixedUpdate(). This should be called in sync with Physics
/// update steps and thus in most cases will execute more than Update() will.
/// This will be called immediately before a Physics update step.
/// </summary>
void FixedUpdate();
/// <summary>
/// Used to call update(). This should be called every frame before physics and
/// collision resolution.
/// </summary>
void Update();
/// <summary>
/// Used to call lateUpdate(). This should be called every frame after physics
/// and collision resolution but before rendering.
/// </summary>
void LateUpdate();
/// <summary>
/// Used to call onDestroy(). This should be called at the end of the frame
/// where the attached GameObject or this script is destroyed directly or
/// indirectly due to destruction of the owner.
/// </summary>
void OnDestroy();
protected:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for Script to tie it to a specific GameObject.
/// Constructors of derived Scripts should call this Constructor.
/// </summary>
/// <param name="gameObj">
/// GameObject that this Script will be tied to.
/// </param>
Script(GameObject gameObj);
/*-----------------------------------------------------------------------------*/
/* Virtual "All-Time" Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Called immediately once this script is attached to a GameObject.
/// </summary>
virtual void onAttached();
/// <summary>
/// Called immediately once this script is detached from a GameObject.
/// </summary>
virtual void onDetatched();
/*-----------------------------------------------------------------------------*/
/* Virtual Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Called on the first frame that the attached GameObject is active if they are
/// a part of the scene.
/// </summary>
virtual void awake();
/// <summary>
/// Called on the first frame that the attached GameObject is active but always
/// after Awake().
/// </summary>
virtual void start();
/// <summary>
/// Called every frame in sync with Physics update steps and thus in most cases
/// will execute more than update() will. This will be called immediately before
/// a Physics update step.
/// </summary>
virtual void fixedUpdate();
/// <summary>
/// Called every frame before physics and collision resolution.
/// </summary>
virtual void update();
/// <summary>
/// Called every frame after physics and collision resolution but before
/// rendering.
/// </summary>
virtual void lateUpdate();
/// <summary>
/// Called just before the end of the frame where the attached GameObject or
/// this script is destroyed directly or indirectly due to destruction of the
/// owner.
/// </summary>
virtual void onDestroy();
private:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
GameObject owner;
};
} // namespace PlushieAPI