using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Axibug.Runtime
{
///
/// 游戏入口。
///
public static partial class GameEntry
{
private static readonly GameLinkedList s_Components = new GameLinkedList();
///
/// 游戏框架所在的场景编号。
///
internal const int SceneId = 0;
///
/// 获取游戏框架组件。
///
/// 要获取的游戏框架组件类型。
/// 要获取的游戏框架组件。
public static T GetComponent() where T : GameComponent
{
return (T)GetComponent(typeof(T));
}
///
/// 获取游戏框架组件。
///
/// 要获取的游戏框架组件类型。
/// 要获取的游戏框架组件。
public static GameComponent GetComponent(Type type)
{
LinkedListNode current = s_Components.First;
while (current != null)
{
if (current.Value.GetType() == type)
{
return current.Value;
}
current = current.Next;
}
return null;
}
///
/// 获取游戏框架组件。
///
/// 要获取的游戏框架组件类型名称。
/// 要获取的游戏框架组件。
public static GameComponent GetComponent(string typeName)
{
LinkedListNode 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;
}
///
/// 关闭游戏框架。
///
/// 关闭游戏框架类型。
public static void Shutdown(ShutdownType shutdownType)
{
Log.Info("Shutdown Game Axibug ({0})...", shutdownType.ToString());
BaseComponent baseComponent = GetComponent();
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;
}
}
///
/// 注册游戏框架组件。
///
/// 要注册的游戏框架组件。
internal static void RegisterComponent(GameComponent gameFrameworkComponent)
{
if (gameFrameworkComponent == null)
{
Log.Error("Cao Cao component is invalid.");
return;
}
Type type = gameFrameworkComponent.GetType();
LinkedListNode 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);
}
}
}