using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
///
/// This component searches all children for a bunch of other components such as names, descriptions, stats, etc
/// and updates them with all necessary information whenever it is updated.
/// It is assumed that children of a view info panel never get destroyed separately, as it is treated as one object.
/// Similarly, it is assumed that all children already exist at the time of startup.
///
public class ViewItemInfoPanel : MonoBehaviour
{
private List names;
private List stats;
private List types;
private List prices;
private List descr;
private List rarity;
private List icons;
private Item lastItem; // Should we want to refresh for whatever reason
private ShopModel lastShop;
private void Awake()
{
UpdateComponentInfo();
}
private void UpdateComponentInfo()
{
names = GetComponentsInChildren().ToList();
Debug.Assert(names != null,this);
stats = GetComponentsInChildren().ToList();
types = GetComponentsInChildren().ToList();
prices = GetComponentsInChildren().ToList();
descr = GetComponentsInChildren().ToList();
rarity = GetComponentsInChildren().ToList();
icons = GetComponentsInChildren().ToList();
//if (names == null) names = ;
// TODO: Create and add all other types in here as components!
}
public void Refresh()
{
SetItemInfo(lastItem,lastShop);
}
// When this is set, it updates all relevant game objects to reflect the item's propertiese.
public void SetItemInfo(Item item,ShopModel owner = null)
{
lastShop = owner;
lastItem = item;
var multiplier = owner?.PriceModifier ?? 1;
if (item == null)
{
gameObject.SetActive(false); // If the seelcted item doesn't exist, disable the whole panel
return;
}
if(names == null) UpdateComponentInfo(); // Assume that if the names component is null, we probably never initialised
//gameObject.SetActive(true);
foreach (var name in names)
{
name.SetName(item.name);
}
foreach (var itemStatsDisplay in stats)
{
itemStatsDisplay.SetStats(item.GetStats());
}
foreach (var type in types)
{
type.SetType(item.GetItemType().ToString());
}
foreach (var price in prices)
{
var cost = owner?.GetPriceForItem(item);
price.SetPrice(cost ?? (int) (item.basePrice * multiplier)); // If the shop has any special pricing, show that here
}
foreach (var descriptionDisplay in descr)
{
descriptionDisplay.SetDescription(item.description);
}
foreach (var itemClass in rarity)
{
itemClass.SetClass(item.rarity);
}
foreach (var icon in icons)
{
icon.SetIcon(item.iconName);
}
}
}