2023-09-14 11:13:53 +00:00
using System ;
2023-08-22 17:58:50 +00:00
using System.Collections.Generic ;
using UnityEngine ;
2023-09-08 13:05:43 +00:00
/// <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,
2023-10-08 14:56:11 +00:00
/// which include targetLevels, blocks, and their associated properties.
2023-09-08 13:05:43 +00:00
/// </remarks>
2023-08-22 17:58:50 +00:00
[CreateAssetMenu(menuName = "All Scene Prefab Set")]
public class SceneBlocksSet : ScriptableObject
{
2023-11-17 04:44:07 +00:00
public TargetLevelsSet [ ] targetLevels = new TargetLevelsSet [ 2 ] ;
public Targets [ ] targets = new Targets [ 2 ] ;
2023-08-22 17:58:50 +00:00
private GameObject hudObj ;
private MessageBoxController messageBoxController ;
2023-09-14 11:13:53 +00:00
private bool isHudEnabled = false ;
2023-08-22 17:58:50 +00:00
2023-09-08 13:05:43 +00:00
/// <summary>
/// Initialize the scene block set.
/// </summary>
/// <param name="hudObj">The HUD object used to access the message box controller.</param>
2023-09-14 11:13:53 +00:00
public void InitializeSceneBlocksSet ( GameObject hudObj = null )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
if ( hudObj ! = null )
{
isHudEnabled = true ;
this . hudObj = hudObj ;
messageBoxController = this . hudObj . GetComponent < MessageBoxController > ( ) ;
}
2023-10-08 14:56:11 +00:00
for ( int i = 0 ; i < targetLevels . Length ; i + + )
2023-08-22 17:58:50 +00:00
{
// initialize all level prefab set
2023-10-08 14:56:11 +00:00
targetLevels [ i ] . InitializeLevelsSet ( ) ;
2023-08-22 17:58:50 +00:00
}
}
2023-09-08 13:05:43 +00:00
/// <summary>
2023-09-14 11:13:53 +00:00
/// Gets all level prefab sets.
2023-09-08 13:05:43 +00:00
/// </summary>
2023-09-14 11:13:53 +00:00
/// <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>
2023-10-08 14:56:11 +00:00
public TargetLevelsSet GetAllLevlePrefabSet ( Targets ? targetType = null , int? index = null )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
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 )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
case ( Targets type , null ) :
levelIndex = type . ToIndex ( ) ;
break ;
2023-08-22 17:58:50 +00:00
2023-09-14 11:13:53 +00:00
case ( null , int i ) :
levelIndex = i ;
break ;
2023-08-22 17:58:50 +00:00
2023-09-14 11:13:53 +00:00
case ( Targets type , int i ) :
levelIndex = type . ToIndex ( ) ;
break ;
2023-08-22 17:58:50 +00:00
default :
2023-09-14 11:13:53 +00:00
PushSceneBlockMessages ( new List < string > { "[ERROR]ScenePrefabSet.GetAllLevlePrefabSet:" , " targetType or index not found!" } ,
new List < string > { messageBoxController . errorColor } ) ;
break ;
2023-08-22 17:58:50 +00:00
}
2023-10-08 14:56:11 +00:00
return targetLevels [ levelIndex ] ;
2023-08-22 17:58:50 +00:00
}
2023-09-08 13:05:43 +00:00
/// <summary>
2023-09-14 11:13:53 +00:00
/// Gets the prefab set for a single level.
2023-09-08 13:05:43 +00:00
/// </summary>
2023-09-14 11:13:53 +00:00
/// <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 )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
if ( level > = GetAllLevlePrefabSet ( targetType , index ) . singleLevelSet . Length )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
PushSceneBlockMessages ( new List < string > { "[ERROR]ScenePrefabSet.GetSingleLevelPrefabSet:" , " level out of range!" , "targetType = " , targetType . ToString ( ) , "level = " , level . ToString ( ) } ,
2023-08-22 17:58:50 +00:00
new List < string > { messageBoxController . errorColor } ) ;
2023-09-07 22:15:24 +00:00
Debug . LogError ( "ScenePrefabSet.GetSingleLevelPrefabSet:level out of range!" ) ;
2023-08-22 17:58:50 +00:00
return null ;
}
2023-09-14 11:13:53 +00:00
return GetAllLevlePrefabSet ( targetType , index ) . singleLevelSet [ level ] ;
2023-08-22 17:58:50 +00:00
}
2023-09-08 13:05:43 +00:00
/// <summary>
2023-09-14 11:13:53 +00:00
/// Gets a specific prefab for a given level, block type, target type, and index.
2023-09-08 13:05:43 +00:00
/// </summary>
2023-09-14 11:13:53 +00:00
/// <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 )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
if ( blockType > = GetSingleLevelPrefabSet ( level , targetType , index ) . prefabs . Length )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
PushSceneBlockMessages ( new List < string > { "[ERROR]ScenePrefabSet.GetPrefab:" , " blockType out of range!" , "targetType = " , targetType . ToString ( ) , "level = " , level . ToString ( ) , "blockType = " , blockType . ToString ( ) } ,
2023-08-22 17:58:50 +00:00
new List < string > { messageBoxController . errorColor } ) ;
2023-09-07 22:15:24 +00:00
Debug . LogError ( "ScenePrefabSet.GetPrefab:blockType out of range!" ) ;
2023-08-22 17:58:50 +00:00
return null ;
}
2023-09-14 11:13:53 +00:00
return GetSingleLevelPrefabSet ( level , targetType , index ) . prefabs [ blockType ] ;
2023-08-22 17:58:50 +00:00
}
2023-09-08 13:05:43 +00:00
/// <summary>
2023-10-08 14:56:11 +00:00
/// Gets the number of targetLevels for a specific target type and index.
2023-09-08 13:05:43 +00:00
/// </summary>
2023-09-14 11:13:53 +00:00
/// <param name="targetType">The target type (optional).</param>
/// <param name="index">The index (optional).</param>
2023-10-08 14:56:11 +00:00
/// <returns>The number of targetLevels.</returns>
2023-09-14 11:13:53 +00:00
/// <remarks>
2023-10-08 14:56:11 +00:00
/// If the target type (targetType) or index (index) is provided, it returns the number of targetLevels for the corresponding target type and index.
2023-09-14 11:13:53 +00:00
/// </remarks>
public int GetLevelNumber ( Targets ? targetType = null , int? index = null )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
return GetAllLevlePrefabSet ( targetType , index ) . singleLevelSet . Length ;
2023-08-22 17:58:50 +00:00
}
2023-09-08 13:05:43 +00:00
/// <summary>
2023-09-14 11:13:53 +00:00
/// Gets the number of blocks for a specific level, target type, and index.
2023-09-08 13:05:43 +00:00
/// </summary>
2023-09-14 11:13:53 +00:00
/// <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 )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
return GetSingleLevelPrefabSet ( level , targetType , index ) . prefabs . Length ;
2023-08-22 17:58:50 +00:00
}
2023-09-08 13:05:43 +00:00
/// <summary>
2023-09-14 11:13:53 +00:00
/// Gets the size of a specific block for a given level, block type, target type, and index.
2023-09-08 13:05:43 +00:00
/// </summary>
2023-09-14 11:13:53 +00:00
/// <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 )
2023-08-22 17:58:50 +00:00
{
2023-09-14 11:13:53 +00:00
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 ) ) ;
}
2023-08-22 17:58:50 +00:00
}
}