Finances.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. using System.Collections;
  6. public class Finances : MonoBehaviour {
  7. #region PowerUps
  8. private static int _magn;
  9. /// <summary>
  10. /// Current magnetism level
  11. /// </summary>
  12. public static int MagnetismLevel
  13. {
  14. get
  15. {
  16. return _magn;
  17. }
  18. set
  19. {
  20. if (value < 0)
  21. {
  22. return;
  23. }
  24. if (_magn != value)
  25. {
  26. _magn = value;
  27. PlayerPrefs.SetInt("Magnetism", _magn);
  28. PlayerPrefs.Save();
  29. }
  30. }
  31. }
  32. private static int _rage;
  33. /// <summary>
  34. /// Current Rage level
  35. /// </summary>
  36. public static int RageLevel
  37. {
  38. get
  39. {
  40. return _rage;
  41. }
  42. set
  43. {
  44. if (value < 0)
  45. {
  46. return;
  47. }
  48. if (_rage != value)
  49. {
  50. _rage = value;
  51. PlayerPrefs.SetInt("Rage", _rage);
  52. PlayerPrefs.Save();
  53. }
  54. }
  55. }
  56. public static int GetUpgradePrice(PowerupsMainControllerCS.PowerUps powerUp)
  57. {
  58. switch (powerUp)
  59. {
  60. case PowerupsMainControllerCS.PowerUps.Magnetism:
  61. switch (MagnetismLevel)
  62. {
  63. case 1: return 5000;
  64. case 2: return 10000;
  65. case 3: return 15000;
  66. case 4: return 20000;
  67. }
  68. break;
  69. case PowerupsMainControllerCS.PowerUps.Rage:
  70. switch (RageLevel)
  71. {
  72. case 1: return 5000;
  73. case 2: return 10000;
  74. case 3: return 15000;
  75. case 4: return 20000;
  76. }
  77. break;
  78. case PowerupsMainControllerCS.PowerUps.SuperSledge:
  79. switch (MagnetismLevel)
  80. {
  81. case 1: return 5000;
  82. case 2: return 10000;
  83. case 3: return 15000;
  84. case 4: return 20000;
  85. }
  86. break;
  87. }
  88. return 0;
  89. }
  90. public static void Upgrade(PowerupsMainControllerCS.PowerUps powerUp)
  91. {
  92. var price = GetUpgradePrice(powerUp);
  93. if (Coins < price)
  94. {
  95. return;
  96. }
  97. if (GetUpgradeLevel(powerUp) >= 5)
  98. {
  99. return;
  100. }
  101. Coins -= price;
  102. switch (powerUp)
  103. {
  104. case PowerupsMainControllerCS.PowerUps.SuperSledge:
  105. MagnetismLevel++;
  106. break;
  107. case PowerupsMainControllerCS.PowerUps.Magnetism:
  108. MagnetismLevel++;
  109. break;
  110. case PowerupsMainControllerCS.PowerUps.Rage:
  111. RageLevel++;
  112. break;
  113. }
  114. }
  115. public static int GetUpgradeLevel(PowerupsMainControllerCS.PowerUps powerUp)
  116. {
  117. switch (powerUp)
  118. {
  119. case PowerupsMainControllerCS.PowerUps.Magnetism:
  120. return MagnetismLevel;
  121. case PowerupsMainControllerCS.PowerUps.Rage:
  122. return RageLevel;
  123. case PowerupsMainControllerCS.PowerUps.SuperSledge:
  124. return MagnetismLevel;
  125. }
  126. return 0;
  127. }
  128. #endregion
  129. #region currency
  130. public enum Lots
  131. {
  132. ut_kr_iap_001,
  133. ut_kr_iap_002,
  134. ut_kr_iap_003
  135. }
  136. public static string CurrencySymbol { get; private set; }
  137. private static int _coins;
  138. /// <summary>
  139. /// Current player amount of coins
  140. /// </summary>
  141. public static int Coins
  142. {
  143. get
  144. {
  145. return _coins;
  146. }
  147. set
  148. {
  149. if (value < 0)
  150. {
  151. return;
  152. }
  153. if (_coins != value)
  154. {
  155. _coins = value;
  156. PlayerPrefs.SetInt("Coins", _coins);
  157. PlayerPrefs.Save();
  158. }
  159. }
  160. }
  161. private static int _bestScore;
  162. /// <summary>
  163. /// Current player amount of coins
  164. /// </summary>
  165. public static int BestScore
  166. {
  167. get
  168. {
  169. return _bestScore;
  170. }
  171. set
  172. {
  173. if (value < 0)
  174. {
  175. return;
  176. }
  177. if (_bestScore != value)
  178. {
  179. _bestScore = value;
  180. PlayerPrefs.SetInt("BestScore", _bestScore);
  181. PlayerPrefs.Save();
  182. UILeaderBoard.SummitScore(_bestScore);
  183. }
  184. }
  185. }
  186. private static readonly Dictionary<Lots, string> Prices = new Dictionary<Lots, string>();
  187. public static string GetLotID(Lots lot)
  188. {
  189. return string.Format("{0}",lot);
  190. }
  191. public static Lots GetLotID(string lot)
  192. {
  193. return (Lots)Enum.Parse(typeof(Lots), lot);
  194. }
  195. public static string GetPrice(Lots lot)
  196. {
  197. return (Prices.ContainsKey(lot) ? Prices[lot] : "");
  198. }
  199. public static int GetReward(Lots lot)
  200. {
  201. switch (lot)
  202. {
  203. case Lots.ut_kr_iap_001: return 5000;
  204. case Lots.ut_kr_iap_002: return 10000;
  205. case Lots.ut_kr_iap_003: return 20000;
  206. }
  207. return 0;
  208. }
  209. public NGUIMenuScript hNGUIMenuScript;
  210. #endregion
  211. /// <summary>
  212. ///
  213. /// </summary>
  214. public enum Costumes
  215. {
  216. Knight,
  217. Underpants
  218. }
  219. void Start ()
  220. {
  221. _coins = PlayerPrefs.GetInt("Coins",1000);
  222. _magn = PlayerPrefs.GetInt("Magnetism", 1);
  223. _rage = PlayerPrefs.GetInt("Rage", 1);
  224. _bestScore = PlayerPrefs.GetInt("BestScore", 0);
  225. if (_magn == 0)
  226. {
  227. _magn = 1;
  228. }
  229. if (_rage == 0)
  230. {
  231. _rage = 1;
  232. }
  233. PlayerPrefs.SetInt(Costumes.Knight.ToString(), 1);
  234. PlayerPrefs.Save();
  235. #if UNITY_IOS
  236. /*StoreKitBinding.requestProductData(Enum.GetNames(typeof(Lots)));
  237. StoreKitManager.productListReceivedEvent += onReceive;
  238. StoreKitManager.purchaseSuccessfulEvent += onPurchaseDone;
  239. StoreKitManager.purchaseFailedEvent += onPurchaseFail;*/
  240. #endif
  241. foreach (Costumes costume in Enum.GetValues(typeof(Costumes)))
  242. {
  243. if (!CostumesInfo.ContainsKey(costume))
  244. {
  245. CostumesInfo.Add(costume, PlayerPrefs.GetInt(costume.ToString(), 0) == 1);
  246. }
  247. }
  248. }
  249. private void Reset()
  250. {
  251. #if UNITY_EDITOR
  252. if (Application.isEditor)
  253. {
  254. PlayerPrefs.SetInt("FirstPlay", 1);
  255. PlayerPrefs.SetInt("FirstTimeInfoShown", 0);
  256. Coins = 1000;
  257. MagnetismLevel = 0;
  258. RageLevel = 0;
  259. BestScore = 0;
  260. foreach (Costumes costume in Enum.GetValues(typeof(Costumes)))
  261. {
  262. PlayerPrefs.SetInt(costume.ToString(), 0);
  263. }
  264. PlayerPrefs.SetInt(Costumes.Knight.ToString(), 1);
  265. UIEnergy.Instance.Restore();
  266. PlayerPrefs.Save();
  267. UnityEditor.EditorApplication.isPlaying = false;
  268. }
  269. #endif
  270. }
  271. void Update()
  272. {
  273. if (hNGUIMenuScript)
  274. {
  275. var menu = hNGUIMenuScript.getCurrentMenu();
  276. if (menu == NGUIMenuScript.NGUIMenus.ShopHome || menu == NGUIMenuScript.NGUIMenus.ShopCostumes
  277. || menu == NGUIMenuScript.NGUIMenus.ShopIAPs || menu == NGUIMenuScript.NGUIMenus.ShopPowerups
  278. || menu == NGUIMenuScript.NGUIMenus.ShopUtilities)
  279. hNGUIMenuScript.updateCurrencyOnHeader(hNGUIMenuScript.getCurrentMenu());
  280. }
  281. if (Input.GetKeyDown(KeyCode.Return))
  282. {
  283. Reset();
  284. }
  285. }
  286. #region PurchaseCallbacks
  287. #if UNITY_IOS
  288. private void onPurchaseFail(string message)
  289. {
  290. Debug.LogError("onPurchaseFail with message: "+ message);
  291. }
  292. /* private void onPurchaseDone(StoreKitTransaction product)
  293. {
  294. Coins += GetReward(GetLotID(product.productIdentifier));
  295. Debug.Log("onPurchaseDone " + product.productIdentifier);
  296. }*/
  297. /*private void onReceive(List<StoreKitProduct> products)
  298. {
  299. foreach (var product in products)
  300. {
  301. try
  302. {
  303. var id = GetLotID(product.productIdentifier);
  304. if (!Prices.ContainsKey(id))
  305. {
  306. Prices.Add(id,product.price);
  307. }
  308. }
  309. catch (Exception) { }
  310. }
  311. }*/
  312. #endif
  313. #endregion
  314. public static int GetCostumeCost(Costumes costume)
  315. {
  316. switch (costume)
  317. {
  318. case Costumes.Knight: return 0;
  319. case Costumes.Underpants: return 20000;
  320. }
  321. return 0;
  322. }
  323. private static readonly Dictionary<Costumes,bool> CostumesInfo = new Dictionary<Costumes, bool>();
  324. public static bool HasCostume(Costumes costume)
  325. {
  326. return CostumesInfo[costume];
  327. }
  328. public static void BuyCostume(Costumes costume)
  329. {
  330. if (HasCostume(costume))
  331. {
  332. return;
  333. }
  334. if (Coins >= GetCostumeCost(costume))
  335. {
  336. PlayerPrefs.SetInt(costume.ToString(),1);
  337. PlayerPrefs.Save();
  338. Coins -= GetCostumeCost(costume);
  339. CostumesInfo[costume] = true;
  340. }
  341. }
  342. }