Files
bansonic_beta_main/Assets/scripts/selectYourSongFirst/load_teammatesProfile.cs
T

175 lines
5.7 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class load_teammatesProfile : MonoBehaviour
{
private static Dictionary<int, AllyHero_SO> heroById;
private static bool heroCacheReady;
private static bool heroCacheBuilding;
[Header("Inspector")]
public Image teammate_profile_01;
public Image teammate_profile_02;
public Image teammate_profile_03;
public Image teammate_profile_04;
public Image teammate_profile_05;
[Header("boarder")]
public Image teammate_profile_boarder_01;
public Image teammate_profile_boarder_02;
public Image teammate_profile_boarder_03;
public Image teammate_profile_boarder_04;
public Image teammate_profile_boarder_05;
[Header("Level Colors")]
public Color levelColor_C;
public Color levelColor_B;
public Color levelColor_A;
public Color levelColor_S;
// Start is called once before the first execution of Update after the MonoBehaviour is created
IEnumerator Start()
{
yield return EnsureHeroCache();
update_teammates_profile();
}
private IEnumerator EnsureHeroCache()
{
if (heroCacheReady) yield break;
if (heroCacheBuilding)
{
while (!heroCacheReady)
{
yield return null;
}
yield break;
}
heroCacheBuilding = true;
var allHeroes = Resources.LoadAll<AllyHero_SO>("");
heroById = new Dictionary<int, AllyHero_SO>();
if (allHeroes != null)
{
for (int i = 0; i < allHeroes.Length; i++)
{
var hero = allHeroes[i];
if (hero != null && !heroById.ContainsKey(hero.ally_heroID))
{
heroById.Add(hero.ally_heroID, hero);
}
if ((i % 32) == 0)
{
yield return null;
}
}
}
heroCacheReady = true;
heroCacheBuilding = false;
}
private IEnumerator EnsureHeroCacheThenUpdate()
{
yield return EnsureHeroCache();
update_teammates_profile();
}
public void update_teammates_profile()
{
if (!heroCacheReady)
{
StartCoroutine(EnsureHeroCacheThenUpdate());
return;
}
// Load selected hero IDs from PlayerPrefs
int heroId1 = PlayerPrefs.GetInt("selected_heroSlot01_heroID", 0);
int heroId2 = PlayerPrefs.GetInt("selected_heroSlot02_heroID", 0);
int heroId3 = PlayerPrefs.GetInt("selected_heroSlot03_heroID", 0);
int heroId4 = PlayerPrefs.GetInt("selected_heroSlot04_heroID", 0);
int heroId5 = PlayerPrefs.GetInt("selected_heroSlot05_heroID", 0);
// Set sprites for each profile image
teammate_profile_01.sprite = GetHeroSquareProfile(heroId1);
teammate_profile_02.sprite = GetHeroSquareProfile(heroId2);
teammate_profile_03.sprite = GetHeroSquareProfile(heroId3);
teammate_profile_04.sprite = GetHeroSquareProfile(heroId4);
teammate_profile_05.sprite = GetHeroSquareProfile(heroId5);
// Set border colors based on hero level
teammate_profile_boarder_01.color = GetBorderColor(GetHeroLevel(heroId1, 1));
teammate_profile_boarder_02.color = GetBorderColor(GetHeroLevel(heroId2, 2));
teammate_profile_boarder_03.color = GetBorderColor(GetHeroLevel(heroId3, 3));
teammate_profile_boarder_04.color = GetBorderColor(GetHeroLevel(heroId4, 4));
teammate_profile_boarder_05.color = GetBorderColor(GetHeroLevel(heroId5, 5));
}
private Sprite GetHeroSquareProfile(int heroId)
{
if (heroId == 0 || heroById == null) return null;
AllyHero_SO hero;
if (heroById.TryGetValue(heroId, out hero) && hero != null)
{
return hero.ally_hero_squareProfile;
}
return null;
}
private string GetHeroLevel(int heroId, int slotIndex)
{
if (heroId == 0) return "C";
if (heroById == null) return "C";
AllyHero_SO hero;
if (!heroById.TryGetValue(heroId, out hero) || hero == null) return "C";
int exp = hero.ally_currentEXP;
int keySlot = Mathf.Clamp(slotIndex, 1, 5);
string expKey = $"selected_heroSlot0{keySlot}_exp";
if (PlayerPrefs.HasKey(expKey))
exp = PlayerPrefs.GetInt(expKey, exp);
return GetRatingFromSO(hero, exp);
}
private string GetRatingFromSO(AllyHero_SO so, int currentExp)
{
if (so == null || so.levelStats == null || so.levelStats.Count == 0) return "C";
List<AllyHero_SO.AllyLevelInfo> sorted = new List<AllyHero_SO.AllyLevelInfo>();
foreach (var l in so.levelStats) if (l != null) sorted.Add(l);
sorted.Sort((a, b) => a.requiredEXP.CompareTo(b.requiredEXP));
int selectedIndex = 0;
for (int i = 0; i < sorted.Count; i++)
{
if (currentExp >= sorted[i].requiredEXP)
{
selectedIndex = i;
}
else
{
break;
}
}
if (selectedIndex <= 0) return "C";
if (selectedIndex == 1) return "B";
if (selectedIndex == 2) return "A";
return "S";
}
private Color GetBorderColor(string level)
{
Color color;
switch (level)
{
case "C": color = levelColor_C; break;
case "B": color = levelColor_B; break;
case "A": color = levelColor_A; break;
case "S": color = levelColor_S; break;
default: color = Color.white; break;
}
color.a = 1f;
return color;
}
}