using System;
using System.Collections.Generic;
using UnityEngine;
///
/// A ScriptableObject for storing and managing various sets of scene blocks.
///
///
/// This class is responsible for organizing and initializing different sets of scene blocks,
/// which include targetLevels, blocks, and their associated properties.
///
[CreateAssetMenu(menuName = "All Scene Prefab Set")]
public class SceneBlocksSet : ScriptableObject
{
public TargetLevelsSet[] targetLevels = new TargetLevelsSet[3];
public Targets[] targets = new Targets[3];
private GameObject hudObj;
private MessageBoxController messageBoxController;
private bool isHudEnabled = false;
///
/// Initialize the scene block set.
///
/// The HUD object used to access the message box controller.
public void InitializeSceneBlocksSet(GameObject hudObj = null)
{
if (hudObj != null)
{
isHudEnabled = true;
this.hudObj = hudObj;
messageBoxController = this.hudObj.GetComponent();
}
for (int i = 0; i < targetLevels.Length; i++)
{
// initialize all level prefab set
targetLevels[i].InitializeLevelsSet();
}
}
///
/// Gets all level prefab sets.
///
/// The target type (optional).
/// The index (optional).
/// The level prefab sets.
///
/// 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.
///
public TargetLevelsSet GetAllLevlePrefabSet(Targets? targetType = null, int? index = null)
{
if (targetType == null && index == null)
{
PushSceneBlockMessages(new List { "[ERROR]ScenePrefabSet.GetAllLevlePrefabSet:", " targetType or index not found!" },
new List { 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 { "[ERROR]ScenePrefabSet.GetAllLevlePrefabSet:", " targetType or index not found!" },
new List { messageBoxController.errorColor });
break;
}
return targetLevels[levelIndex];
}
///
/// Gets the prefab set for a single level.
///
/// The level index.
/// The target type (optional).
/// The index (optional).
/// The prefab set for a single level.
///
/// 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.
///
public BlocksSet GetSingleLevelPrefabSet(int level, Targets? targetType = null, int? index = null)
{
if (level >= GetAllLevlePrefabSet(targetType, index).singleLevelSet.Length)
{
PushSceneBlockMessages(new List { "[ERROR]ScenePrefabSet.GetSingleLevelPrefabSet:", " level out of range!", "targetType = ", targetType.ToString(), "level = ", level.ToString() },
new List { messageBoxController.errorColor });
Debug.LogError("ScenePrefabSet.GetSingleLevelPrefabSet:level out of range!");
return null;
}
return GetAllLevlePrefabSet(targetType, index).singleLevelSet[level];
}
///
/// Gets a specific prefab for a given level, block type, target type, and index.
///
/// The level index.
/// The block type index.
/// The target type (optional).
/// The index (optional).
/// The prefab object.
///
/// 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.
///
public GameObject GetPrefab(int level, int blockType, Targets? targetType = null, int? index = null)
{
if (blockType >= GetSingleLevelPrefabSet(level, targetType, index).prefabs.Length)
{
PushSceneBlockMessages(new List { "[ERROR]ScenePrefabSet.GetPrefab:", " blockType out of range!", "targetType = ", targetType.ToString(), "level = ", level.ToString(), "blockType = ", blockType.ToString() },
new List { messageBoxController.errorColor });
Debug.LogError("ScenePrefabSet.GetPrefab:blockType out of range!");
return null;
}
return GetSingleLevelPrefabSet(level, targetType, index).prefabs[blockType];
}
///
/// Gets the number of targetLevels for a specific target type and index.
///
/// The target type (optional).
/// The index (optional).
/// The number of targetLevels.
///
/// If the target type (targetType) or index (index) is provided, it returns the number of targetLevels for the corresponding target type and index.
///
public int GetLevelNumber(Targets? targetType = null, int? index = null)
{
return GetAllLevlePrefabSet(targetType, index).singleLevelSet.Length;
}
///
/// Gets the number of blocks for a specific level, target type, and index.
///
/// The level index.
/// The target type (optional).
/// The index (optional).
/// The number of blocks.
///
/// If the target type (targetType) or index (index) is provided, it returns the number of blocks for the corresponding level, target type, and index.
///
public int GetBlockNumber(int level, Targets? targetType = null, int? index = null)
{
return GetSingleLevelPrefabSet(level, targetType, index).prefabs.Length;
}
///
/// Gets the size of a specific block for a given level, block type, target type, and index.
///
/// The level index.
/// The block type index.
/// The target type (optional).
/// The index (optional).
/// The block size.
///
/// 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.
///
public float GetBlockSize(int level, int blockType, Targets? targetType = null, int? index = null)
{
return GetPrefab(level, blockType, targetType, index).GetComponent().blockSize;
}
///
/// Pushes a message to the message box or debug log.推送消息到消息框或调试日志中。
///
/// The list of messages.
/// The list of colors.
///
/// 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.
///
private void PushSceneBlockMessages(List messageList, List colorList)
{
if (isHudEnabled)
{
messageBoxController.PushMessage(messageList, colorList);
}
else
{
Debug.Log(string.Join(" ", messageList));
}
}
}