AkiraPixelWind/Assets/Axibug/Script/Runtime/Base/GameEntry.cs
2022-12-29 18:20:40 +08:00

141 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Axibug.Runtime
{
/// <summary>
/// 游戏入口。
/// </summary>
public static partial class GameEntry
{
private static readonly GameLinkedList<GameComponent> s_Components = new GameLinkedList<GameComponent>();
/// <summary>
/// 游戏框架所在的场景编号。
/// </summary>
internal const int SceneId = 0;
/// <summary>
/// 获取游戏框架组件。
/// </summary>
/// <typeparam name="T">要获取的游戏框架组件类型。</typeparam>
/// <returns>要获取的游戏框架组件。</returns>
public static T GetComponent<T>() where T : GameComponent
{
return (T)GetComponent(typeof(T));
}
/// <summary>
/// 获取游戏框架组件。
/// </summary>
/// <param name="type">要获取的游戏框架组件类型。</param>
/// <returns>要获取的游戏框架组件。</returns>
public static GameComponent GetComponent(Type type)
{
LinkedListNode<GameComponent> current = s_Components.First;
while (current != null)
{
if (current.Value.GetType() == type)
{
return current.Value;
}
current = current.Next;
}
return null;
}
/// <summary>
/// 获取游戏框架组件。
/// </summary>
/// <param name="typeName">要获取的游戏框架组件类型名称。</param>
/// <returns>要获取的游戏框架组件。</returns>
public static GameComponent GetComponent(string typeName)
{
LinkedListNode<GameComponent> current = s_Components.First;
while (current != null)
{
Type type = current.Value.GetType();
if (type.FullName == typeName || type.Name == typeName)
{
return current.Value;
}
current = current.Next;
}
return null;
}
/// <summary>
/// 关闭游戏框架。
/// </summary>
/// <param name="shutdownType">关闭游戏框架类型。</param>
public static void Shutdown(ShutdownType shutdownType)
{
Log.Info("Shutdown Game Axibug ({0})...", shutdownType.ToString());
BaseComponent baseComponent = GetComponent<BaseComponent>();
if (baseComponent != null)
{
baseComponent.Shutdown();
baseComponent = null;
}
s_Components.Clear();
if (shutdownType == ShutdownType.None)
{
return;
}
if (shutdownType == ShutdownType.Restart)
{
SceneManager.LoadScene(SceneId);
return;
}
if (shutdownType == ShutdownType.Quit)
{
Application.Quit();
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
return;
}
}
/// <summary>
/// 注册游戏框架组件。
/// </summary>
/// <param name="gameFrameworkComponent">要注册的游戏框架组件。</param>
internal static void RegisterComponent(GameComponent gameFrameworkComponent)
{
if (gameFrameworkComponent == null)
{
Log.Error("Cao Cao component is invalid.");
return;
}
Type type = gameFrameworkComponent.GetType();
LinkedListNode<GameComponent> current = s_Components.First;
while (current != null)
{
if (current.Value.GetType() == type)
{
Log.Error("Cao Cao component type '{0}' is already exist.", type.FullName);
return;
}
current = current.Next;
}
s_Components.AddLast(gameFrameworkComponent);
}
}
}