194 lines
8.7 KiB
C#
194 lines
8.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// A ScriptableObject for storing and managing various sets of scene blocks.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This class is responsible for organizing and initializing different sets of scene blocks,
|
|
/// which include targetLevels, blocks, and their associated properties.
|
|
/// </remarks>
|
|
[CreateAssetMenu(menuName = "All Scene Prefab Set")]
|
|
public class SceneBlocksSet : ScriptableObject
|
|
{
|
|
public TargetLevelsSet[] targetLevels = new TargetLevelsSet[2];
|
|
public Targets[] targets = new Targets[2];
|
|
|
|
private GameObject hudObj;
|
|
private MessageBoxController messageBoxController;
|
|
private bool isHudEnabled = false;
|
|
|
|
/// <summary>
|
|
/// Initialize the scene block set.
|
|
/// </summary>
|
|
/// <param name="hudObj">The HUD object used to access the message box controller.</param>
|
|
public void InitializeSceneBlocksSet(GameObject hudObj = null)
|
|
{
|
|
if (hudObj != null)
|
|
{
|
|
isHudEnabled = true;
|
|
this.hudObj = hudObj;
|
|
messageBoxController = this.hudObj.GetComponent<MessageBoxController>();
|
|
}
|
|
for (int i = 0; i < targetLevels.Length; i++)
|
|
{
|
|
// initialize all level prefab set
|
|
targetLevels[i].InitializeLevelsSet();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all level prefab sets.
|
|
/// </summary>
|
|
/// <param name="targetType">The target type (optional).</param>
|
|
/// <param name="index">The index (optional).</param>
|
|
/// <returns>The level prefab sets.</returns>
|
|
/// <remarks>
|
|
/// If the target type (targetType) or index (index) is provided, it returns the corresponding level prefab set.
|
|
/// If neither the target type (targetType) nor the index (index) is provided, an exception will be thrown, and an error message will be logged.
|
|
/// </remarks>
|
|
public TargetLevelsSet GetAllLevlePrefabSet(Targets? targetType = null, int? index = null)
|
|
{
|
|
if (targetType == null && index == null)
|
|
{
|
|
PushSceneBlockMessages(new List<string> { "[ERROR]ScenePrefabSet.GetAllLevlePrefabSet:", " targetType or index not found!" },
|
|
new List<string> { messageBoxController.errorColor });
|
|
throw new ArgumentNullException("Both targetType and index cannot be null.");
|
|
}
|
|
int levelIndex = 0;
|
|
switch (targetType, index)
|
|
{
|
|
case (Targets type, null):
|
|
levelIndex = type.ToIndex();
|
|
break;
|
|
|
|
case (null, int i):
|
|
levelIndex = i;
|
|
break;
|
|
|
|
case (Targets type, int i):
|
|
levelIndex = type.ToIndex();
|
|
break;
|
|
|
|
default:
|
|
PushSceneBlockMessages(new List<string> { "[ERROR]ScenePrefabSet.GetAllLevlePrefabSet:", " targetType or index not found!" },
|
|
new List<string> { messageBoxController.errorColor });
|
|
break;
|
|
}
|
|
return targetLevels[levelIndex];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the prefab set for a single level.
|
|
/// </summary>
|
|
/// <param name="level">The level index.</param>
|
|
/// <param name="targetType">The target type (optional).</param>
|
|
/// <param name="index">The index (optional).</param>
|
|
/// <returns>The prefab set for a single level.</returns>
|
|
/// <remarks>
|
|
/// If the target type (targetType) or index (index) is provided, it returns the prefab set for the corresponding level.
|
|
/// If the level index is out of range, an exception will be thrown, and an error message will be logged.
|
|
/// </remarks>
|
|
public BlocksSet GetSingleLevelPrefabSet(int level, Targets? targetType = null, int? index = null)
|
|
{
|
|
if (level >= GetAllLevlePrefabSet(targetType, index).singleLevelSet.Length)
|
|
{
|
|
PushSceneBlockMessages(new List<string> { "[ERROR]ScenePrefabSet.GetSingleLevelPrefabSet:", " level out of range!", "targetType = ", targetType.ToString(), "level = ", level.ToString() },
|
|
new List<string> { messageBoxController.errorColor });
|
|
Debug.LogError("ScenePrefabSet.GetSingleLevelPrefabSet:level out of range!");
|
|
return null;
|
|
}
|
|
return GetAllLevlePrefabSet(targetType, index).singleLevelSet[level];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a specific prefab for a given level, block type, target type, and index.
|
|
/// </summary>
|
|
/// <param name="level">The level index.</param>
|
|
/// <param name="blockType">The block type index.</param>
|
|
/// <param name="targetType">The target type (optional).</param>
|
|
/// <param name="index">The index (optional).</param>
|
|
/// <returns>The prefab object.</returns>
|
|
/// <remarks>
|
|
/// If the target type (targetType) or index (index) is provided, it returns the specific prefab for the given level, block type, target type, and index.
|
|
/// If the block type index is out of range, an exception will be thrown, and an error message will be logged.
|
|
/// </remarks>
|
|
public GameObject GetPrefab(int level, int blockType, Targets? targetType = null, int? index = null)
|
|
{
|
|
if (blockType >= GetSingleLevelPrefabSet(level, targetType, index).prefabs.Length)
|
|
{
|
|
PushSceneBlockMessages(new List<string> { "[ERROR]ScenePrefabSet.GetPrefab:", " blockType out of range!", "targetType = ", targetType.ToString(), "level = ", level.ToString(), "blockType = ", blockType.ToString() },
|
|
new List<string> { messageBoxController.errorColor });
|
|
Debug.LogError("ScenePrefabSet.GetPrefab:blockType out of range!");
|
|
return null;
|
|
}
|
|
return GetSingleLevelPrefabSet(level, targetType, index).prefabs[blockType];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the number of targetLevels for a specific target type and index.
|
|
/// </summary>
|
|
/// <param name="targetType">The target type (optional).</param>
|
|
/// <param name="index">The index (optional).</param>
|
|
/// <returns>The number of targetLevels.</returns>
|
|
/// <remarks>
|
|
/// If the target type (targetType) or index (index) is provided, it returns the number of targetLevels for the corresponding target type and index.
|
|
/// </remarks>
|
|
public int GetLevelNumber(Targets? targetType = null, int? index = null)
|
|
{
|
|
return GetAllLevlePrefabSet(targetType, index).singleLevelSet.Length;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the number of blocks for a specific level, target type, and index.
|
|
/// </summary>
|
|
/// <param name="level">The level index.</param>
|
|
/// <param name="targetType">The target type (optional).</param>
|
|
/// <param name="index">The index (optional).</param>
|
|
/// <returns>The number of blocks.</returns>
|
|
/// <remarks>
|
|
/// If the target type (targetType) or index (index) is provided, it returns the number of blocks for the corresponding level, target type, and index.
|
|
/// </remarks>
|
|
public int GetBlockNumber(int level, Targets? targetType = null, int? index = null)
|
|
{
|
|
return GetSingleLevelPrefabSet(level, targetType, index).prefabs.Length;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the size of a specific block for a given level, block type, target type, and index.
|
|
/// </summary>
|
|
/// <param name="level">The level index.</param>
|
|
/// <param name="blockType">The block type index.</param>
|
|
/// <param name="targetType">The target type (optional).</param>
|
|
/// <param name="index">The index (optional).</param>
|
|
/// <returns>The block size.</returns>
|
|
/// <remarks>
|
|
/// If the target type (targetType) or index (index) is provided, it returns the size of the block for the specified level, block type, target type, and index.
|
|
/// </remarks>
|
|
public float GetBlockSize(int level, int blockType, Targets? targetType = null, int? index = null)
|
|
{
|
|
return GetPrefab(level, blockType, targetType, index).GetComponent<SceneBlock>().blockSize;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pushes a message to the message box or debug log.推送消息到消息框或调试日志中。
|
|
/// </summary>
|
|
/// <param name="messageList">The list of messages.</param>
|
|
/// <param name="colorList">The list of colors.</param>
|
|
/// <remarks>
|
|
/// If the message box is enabled (isHudEnabled is true), the message will be pushed to the message box, and the messages will be colored according to the provided color list.
|
|
/// If the message box is not enabled (isHudEnabled is false), the messages will be logged as plain text in the debug log.
|
|
/// </remarks>
|
|
private void PushSceneBlockMessages(List<string> messageList, List<string> colorList)
|
|
{
|
|
if (isHudEnabled)
|
|
{
|
|
messageBoxController.PushMessage(messageList, colorList);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log(string.Join(" ", messageList));
|
|
}
|
|
}
|
|
} |