Files
bansonic_beta_main/Assets/enemyListBalls/load_enemyBall_light.cs
T

164 lines
6.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class load_enemyBall_light : MonoBehaviour
{
[Header("prefabs")]
public GameObject enemyBallLightPrefab;
public Transform obj_to_put; // Changed to Transform for easier parenting
[Header("colors")]
public Color symbol_default_color = Color.white;
public Color symbol_dead_color = Color.gray;
public Color symbol_simple_color = Color.green;
public Color symbol_elite_color = Color.blue;
public Color symbol_boss_color = Color.red;
public Color symbol_legendary_color = Color.yellow;
public Color symbol_guardian_color = Color.red;
[Header("ball sprites")]
public Sprite symbol_default_sprite;
public Sprite symbol_dead_sprite;
public Sprite symbol_simple_sprite;
public Sprite symbol_elite_sprite;
public Sprite symbol_boss_sprite;
public Sprite symbol_legendary_sprite;
public Sprite symbol_guardian_sprite;
private List<global::enemyBallLightPrefab> instantiatedBalls = new List<global::enemyBallLightPrefab>();
private int currentEnemyIndex = 0;
public void InitializeEnemyBalls(List<EnemyData_SO.EnemyType> enemyList)
{
// Clear existing balls list references
instantiatedBalls.Clear();
currentEnemyIndex = 0;
// Clean up all children in obj_to_put to ensure a fresh start
if (obj_to_put != null)
{
for (int i = obj_to_put.childCount - 1; i >= 0; i--)
{
Destroy(obj_to_put.GetChild(i).gameObject);
}
}
if (enemyList == null || enemyList.Count == 0) return;
// Instantiate balls based on enemy count
foreach (var enemyType in enemyList)
{
if (enemyBallLightPrefab == null || obj_to_put == null) continue;
GameObject ballObj = Instantiate(enemyBallLightPrefab, obj_to_put);
global::enemyBallLightPrefab ballScript = ballObj.GetComponent<global::enemyBallLightPrefab>();
if (ballScript != null)
{
// Set sprite based on enemy type (changed from applying color to applying sprite)
if (ballScript.ballImage_img != null)
{
Sprite s = GetSpriteForEnemyType(enemyType);
if (s != null)
{
ballScript.ballImage_img.sprite = s;
// ensure image has default color so sprite displays normally
ballScript.ballImage_img.color = Color.white;
}
else
{
// fallback: keep existing sprite but ensure default color
ballScript.ballImage_img.color = symbol_default_color;
}
}
// Initial cursor state: only first one active
if (ballScript.cursor_img != null)
{
ballScript.cursor_img.gameObject.SetActive(instantiatedBalls.Count == 0);
}
instantiatedBalls.Add(ballScript);
}
}
}
public void OnEnemyDefeated()
{
// Check if we have valid balls list
if (instantiatedBalls.Count == 0) return;
// Ensure index is within bounds
if (currentEnemyIndex >= instantiatedBalls.Count) return;
// Change the current enemy's ball to dead sprite
if (instantiatedBalls[currentEnemyIndex].ballImage_img != null)
{
if (symbol_dead_sprite != null)
{
instantiatedBalls[currentEnemyIndex].ballImage_img.sprite = symbol_dead_sprite;
instantiatedBalls[currentEnemyIndex].ballImage_img.color = Color.white;
}
else
{
// fallback to setting color if dead sprite not assigned
instantiatedBalls[currentEnemyIndex].ballImage_img.color = symbol_dead_color;
}
}
// Turn off cursor for current (defeated) enemy
// UNLESS it's the last enemy, per requirements: "若最后一个敌人死亡,则最后一个敌人的cursor不消失"
if (currentEnemyIndex < instantiatedBalls.Count - 1)
{
if (instantiatedBalls[currentEnemyIndex].cursor_img != null)
{
instantiatedBalls[currentEnemyIndex].cursor_img.gameObject.SetActive(false);
}
}
// Move to next enemy
currentEnemyIndex++;
// Turn on cursor for next enemy (if exists)
if (currentEnemyIndex < instantiatedBalls.Count)
{
if (instantiatedBalls[currentEnemyIndex].cursor_img != null)
{
instantiatedBalls[currentEnemyIndex].cursor_img.gameObject.SetActive(true);
}
}
}
private Color GetColorForEnemyType(EnemyData_SO.EnemyType enemyType)
{
// Assuming enemy types map to standard conventions or simple integer mapping
// Adjust these mappings based on actual EnemyInfo.enemyType values if known
// For now using a switch based on common patterns or provided requirements
switch (enemyType)
{
case EnemyData_SO.EnemyType.Simple: return symbol_simple_color;
case EnemyData_SO.EnemyType.Elite: return symbol_elite_color;
case EnemyData_SO.EnemyType.Boss: return symbol_boss_color;
case EnemyData_SO.EnemyType.Legend: return symbol_legendary_color;
case EnemyData_SO.EnemyType.Guardian: return symbol_guardian_color;
default: return symbol_default_color;
}
}
private Sprite GetSpriteForEnemyType(EnemyData_SO.EnemyType enemyType)
{
switch (enemyType)
{
case EnemyData_SO.EnemyType.Simple: return symbol_simple_sprite;
case EnemyData_SO.EnemyType.Elite: return symbol_elite_sprite;
case EnemyData_SO.EnemyType.Boss: return symbol_boss_sprite;
case EnemyData_SO.EnemyType.Legend: return symbol_legendary_sprite;
case EnemyData_SO.EnemyType.Guardian: return symbol_guardian_sprite;
default: return symbol_default_sprite;
}
}
}