Liberi
An exergame built for kids with CP!
WiskinUpgradeManager.cs
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections;
4 
8 public class WiskinUpgradeManager : MonoBehaviour
9 {
10  public Sprite GemSlotLockedSprite;
11 
12  static WiskinUpgradeManager _instance;
13 
14  Animator _animator;
15  int _isShownAnimParam = Animator.StringToHash("IsShown");
16  Animator _upgradePointAnimator;
17  int _bounceAnimParam = Animator.StringToHash("Bounce");
18  InventoryTile[] _mainTiles = new InventoryTile[4];
19  InventoryTile[] _gemTiles = new InventoryTile[8];
20  Image[] _upgradePoints = new Image[10];
21 
25  UJeli _guardianStyle;
26 
27  VendorItemInfo[] _upgradeInfos;
28  VendorItemInfo[] _gemSlotInfos;
29 
30  int _currentSelectionIndex;
31  int _currentSubSelectionIndex;
32 
33  Text _vendorName;
34  Text _itemName;
35  Text _itemPrice;
36  Text _itemDescription;
37  Image _currencyIcon;
38 
39  void Awake ()
40  {
41  _instance = this;
42  _animator = GetComponent<Animator>();
43 
44  _vendorName = transform.FindChild("vendor_name").GetComponent<Text>();
45  _itemName = transform.FindChild("item_name").GetComponent<Text>();
46  _itemPrice = transform.FindChild("item_price").GetComponent<Text>();
47  _itemDescription = transform.FindChild("item_description").GetComponent<Text>();
48  _currencyIcon = transform.FindChild("currency_icon").GetComponent<Image>();
49 
50  _upgradePointAnimator = transform.FindChild("upgrade_point_container").GetComponent<Animator>();
51  }
52 
53  void Start ()
54  {
55  Transform grid = transform.FindChild("grid");
56 
57  _mainTiles[0] = transform.FindChild("upgrade_tile").GetComponent<InventoryTile>();
58 
59  for (int i = 0; i < 3; i++)
60  _mainTiles[i+1] = transform.FindChild("gem_slot_tile_" + i.ToString()).GetComponent<InventoryTile>();
61 
62  for (int i = 0; i < _gemTiles.Length; i++)
63  {
64  InventoryTile newTile = Sync.SpawnLocal("inventory_tile", Vector3.zero).GetComponent<InventoryTile>();
65  newTile.name = "gem_tile";
66  newTile.transform.SetParent(grid, false);
67  _gemTiles[i] = newTile;
68  }
69 
70  for (int i = 0; i < _upgradePoints.Length; i++)
71  {
72  _upgradePoints[i] = transform.FindChild("upgrade_point_container/points/point_" + i.ToString()).GetComponent<Image>();
73  }
74  }
75 
79  public static void Show (VendorItemInfo[] upgradeInfos, VendorItemInfo[] gemSlotInfos, VendorItemInfo[] gemInfos, string vendorName)
80  {
81  _instance._animator.SetBool(_instance._isShownAnimParam, true);
82  _instance._vendorName.text = vendorName;
83 
84  int upgradeLevel = 0;
85 
86  _instance._guardianStyle = Game.LocalPlayerProfile.Styles["guardian"];
87 
88  if (_instance._guardianStyle.HasChild("upgrade_level"))
89  upgradeLevel = _instance._guardianStyle["upgrade_level"].IntValue;
90 
91  _instance._upgradeInfos = upgradeInfos;
92  _instance.UpdateUpgradeTile(upgradeLevel);
93 
94  _instance._gemSlotInfos = gemSlotInfos;
95  _instance.UpdateGemSlots();
96 
97  for (int i = 0; i < gemInfos.Length; i++)
98  {
99  InventoryTile tile = _instance._gemTiles[i];
100 
101  if (gemInfos[i].Item.Type == "gem")
102  {
103  GemItem gemItem = ItemRegistry.GetItem<GemItem>(gemInfos[i].ItemID);
104 
105  tile.ItemName = Localizer.GetString(gemItem.DisplayNameID);
106  tile.ItemDescription = Localizer.GetString(gemItem.DescriptionID);
107  tile.ItemPrice = gemInfos[i].Price;
108  tile.PrimaryIcon = gemItem.GUISprite;
109  tile.ResetRotationAndScale();
110  }
111  }
112 
113  for (int i = gemInfos.Length; i < _instance._gemTiles.Length; i++)
114  {
115  InventoryTile tile = _instance._gemTiles[i];
116  tile.gameObject.SetActive(false);
117  }
118  }
119 
123  void UpdateUpgradeTile (int upgradeLevel)
124  {
125  for (int i = 0; i < _upgradePoints.Length; i++)
126  _upgradePoints[i].enabled = (upgradeLevel > i);
127 
128  InventoryTile upgradeTile = _instance._mainTiles[0];
129  int currentDamage = WiskinConstants.GUARDIAN_BASE_DAMAGE + WiskinConstants.GUARDIAN_DAMAGE_PER_LEVEL * upgradeLevel;
130 
131  if (upgradeLevel >= WiskinConstants.MAX_GUARDIAN_UPGRADE)
132  {
133  upgradeTile.ItemName = Localizer.GetString("wiskin.max_upgrade.name");
134  upgradeTile.ItemDescription = string.Format(Localizer.GetString("wiskin.max_upgrade.desc"),currentDamage); ;
135  }
136  else
137  {
138  upgradeTile.ItemName = Localizer.GetString("wiskin.upgrade.name");
139  upgradeTile.ItemDescription = string.Format(Localizer.GetString("wiskin.upgrade.desc"),currentDamage, currentDamage + WiskinConstants.GUARDIAN_DAMAGE_PER_LEVEL); ;
140  upgradeTile.ItemPrice = _upgradeInfos[upgradeLevel].Price;
141  }
142  }
143 
147  void UpdateGemSlots ()
148  {
149  for (int i = 0; i < _gemSlotInfos.Length; i++)
150  {
151  InventoryTile gemSlotTile = _instance._mainTiles[i+1];
152 
153  gemSlotTile.ItemName = Localizer.GetString("wiskin.gem_slot.name");
154  gemSlotTile.ItemDescription = Localizer.GetString("wiskin.gem_slot.desc");
155  gemSlotTile.ItemPrice = _gemSlotInfos[i].Price;
156 
157  // Show the locked Icon if the player has not unlocked this gemslot
158  if (!_guardianStyle.HasChild(_gemSlotInfos[i].ItemID))
159  gemSlotTile.PrimaryIcon = _instance.GemSlotLockedSprite;
160 
161  else if (string.IsNullOrEmpty(_guardianStyle[_gemSlotInfos[i].ItemID].Value))
162  gemSlotTile.PrimaryIcon = null;
163 
164  else
165  {
166  GemItem equippedGem = ItemRegistry.GetItem<GemItem>(_guardianStyle[_gemSlotInfos[i].ItemID].Value);
167  gemSlotTile.PrimaryIcon = equippedGem.GUISprite;
168  gemSlotTile.ItemDescription += "\n\n" + Localizer.GetString(equippedGem.DisplayNameID) + "\n" + Localizer.GetString(equippedGem.DescriptionID);
169  }
170  }
171  }
172 
176  public static void UpdateSelectionIndex (int newSelectionIndex, bool isPurchasable, int upgradeLevel)
177  {
178  _instance._mainTiles[_instance._currentSelectionIndex].SetSelected(false);
179  _instance._mainTiles[newSelectionIndex].SetSelected(true);
180  _instance._currentSelectionIndex = newSelectionIndex;
181 
182  if (_instance._currentSelectionIndex == 0)
183  _instance.UpdateUpgradeTile(upgradeLevel);
184  else
185  _instance.UpdateGemSlots();
186 
187  _instance._itemName.text = _instance._mainTiles[newSelectionIndex].ItemName;
188  _instance._itemDescription.text = _instance._mainTiles[newSelectionIndex].ItemDescription;
189 
190  if (isPurchasable)
191  {
192  _instance._itemPrice.text = _instance._mainTiles[newSelectionIndex].ItemPrice.ToString("N0");
193  _instance._currencyIcon.enabled = true;
194  }
195  else
196  {
197  _instance._itemPrice.text = "";
198  _instance._currencyIcon.enabled = false;
199  }
200  }
201 
202  public static void EnterSubSelection ()
203  {
204  _instance._mainTiles[_instance._currentSelectionIndex].SetSelected(false);
205  _instance._mainTiles[_instance._currentSelectionIndex].SetSpecial(true);
206  }
207 
208  public static void ExitSubSelection ()
209  {
210  _instance._gemTiles[_instance._currentSubSelectionIndex].SetSelected(false);
211  _instance._mainTiles[_instance._currentSelectionIndex].SetSpecial(false);
212  }
213 
217  public static void UpdateSubSelectionIndex (int newSubSelectionIndex, bool isOwned)
218  {
219  if (newSubSelectionIndex == -1)
220  {
221  _instance._gemTiles[_instance._currentSubSelectionIndex].SetSelected(false);
222  return;
223  }
224 
225  _instance._gemTiles[_instance._currentSubSelectionIndex].SetSelected(false);
226  _instance._gemTiles[newSubSelectionIndex].SetSelected(true);
227  _instance._currentSubSelectionIndex = newSubSelectionIndex;
228 
229  _instance._itemName.text = _instance._gemTiles[newSubSelectionIndex].ItemName;
230  _instance._itemDescription.text = _instance._gemTiles[newSubSelectionIndex].ItemDescription;
231 
232  if (!isOwned)
233  {
234  _instance._itemPrice.text = _instance._gemTiles[newSubSelectionIndex].ItemPrice.ToString("N0");
235  _instance._currencyIcon.enabled = true;
236  }
237  else
238  {
239  _instance._itemPrice.text = "";
240  _instance._currencyIcon.enabled = false;
241  }
242  }
243 
244  public static void BounceSelectedIcon ()
245  {
246  if (_instance._currentSelectionIndex < _instance._mainTiles.Length)
247  _instance._mainTiles[_instance._currentSelectionIndex].Bounce();
248  else
249  _instance._gemTiles[_instance._currentSelectionIndex].Bounce();
250  }
251 
252  public static void BounceUpgradeBar ()
253  {
254  _instance._upgradePointAnimator.SetTrigger(_instance._bounceAnimParam);
255  }
256 
260  public static void Hide ()
261  {
262  _instance._animator.SetBool(_instance._isShownAnimParam, false);
263 
264  for (int i = 0; i < _instance._mainTiles.Length; i++)
265  {
266  InventoryTile tile = _instance._mainTiles[i];
267  tile.SetSelected(false);
268  }
269 
270  for (int i = 0; i < _instance._gemTiles.Length; i++)
271  {
272  InventoryTile tile = _instance._gemTiles[i];
273  tile.SetSelected(false);
274  }
275  }
276 }
static string GetString(string key, SystemLanguage language=SystemLanguage.Unknown)
Gets the localized string for the given string key.
Definition: Localizer.cs:55
string DisplayNameID
The Localization ID of the Item's actual name as it will appear in inventory/shops.
Definition: Item.cs:13
static void Hide()
Hide the Upgrade shop GUI.
Localization manager.
Definition: Localizer.cs:9
static void UpdateSelectionIndex(int newSelectionIndex, bool isPurchasable, int upgradeLevel)
Update the selection index, in the context of this GUI, this is either the upgrade button or one of t...
Class to manage the visual representation of the Wiskin Upgrade Shop.
void SetSpecial(bool state)
Makes a tile specially-selected (in the case of gem slot sub menu navigation)
Derived class for storing Wiskin Defense gems.
Definition: GemItem.cs:7
A Class for storing items. Can be used as is or be derived from. See GemItem or GuardianWeapon for ex...
Definition: Item.cs:8
void SetSelected(bool state)
Makes a tile appear selected (glowing orange selection box, slightly scaled).
virtual string Type
Derived classes should override this property with a relevant string used for comparisons related to ...
Definition: Item.cs:28
Sprite GUISprite
The Sprite used to represent this item in the GUI.
Definition: Item.cs:21
A visual representation of an Inventory item. Used by the StoreUIManager class to display items the v...
Definition: InventoryTile.cs:9
static void Show(VendorItemInfo[] upgradeInfos, VendorItemInfo[] gemSlotInfos, VendorItemInfo[] gemInfos, string vendorName)
Show the Wiskin Defence Upgrade GUI and set up all of its necessary tiles.
void Bounce()
Plays a bounce animation on the tile to indicate an action was done on it (purchase/equip, etc).
A class that loads all Item Serializeable Objects from the Resources folder and provides the ability ...
Definition: ItemRegistry.cs:9
void ResetRotationAndScale()
Resets the rotation and scale of the primary icon to its default values.
static void UpdateSubSelectionIndex(int newSubSelectionIndex, bool isOwned)
Update the sub selection index, in the context of this GUI, this is lower container that holds the ge...
string DescriptionID
The Localization ID of the Item's description/flavour text.
Definition: Item.cs:17
static GameObject SpawnLocal(string prefabId, Vector3 position, UJeli details=null)
Spawn an object locally without propagating onto the network.
Definition: Sync.Static.cs:623
Unity version of Jeli markup class.
Definition: UJeli.cs:10
This class server two main functions: 1) As a MonoBehaviour, it allows for network synchronization of...
Definition: Sync.cs:13