using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using System.Collections;
public class Finances : MonoBehaviour {
#region PowerUps
private static int _magn;
///
/// Current magnetism level
///
public static int MagnetismLevel
{
get
{
return _magn;
}
set
{
if (value < 0)
{
return;
}
if (_magn != value)
{
_magn = value;
PlayerPrefs.SetInt("Magnetism", _magn);
PlayerPrefs.Save();
}
}
}
private static int _rage;
///
/// Current Rage level
///
public static int RageLevel
{
get
{
return _rage;
}
set
{
if (value < 0)
{
return;
}
if (_rage != value)
{
_rage = value;
PlayerPrefs.SetInt("Rage", _rage);
PlayerPrefs.Save();
}
}
}
public static int GetUpgradePrice(PowerupsMainControllerCS.PowerUps powerUp)
{
switch (powerUp)
{
case PowerupsMainControllerCS.PowerUps.Magnetism:
switch (MagnetismLevel)
{
case 1: return 5000;
case 2: return 10000;
case 3: return 15000;
case 4: return 20000;
}
break;
case PowerupsMainControllerCS.PowerUps.Rage:
switch (RageLevel)
{
case 1: return 5000;
case 2: return 10000;
case 3: return 15000;
case 4: return 20000;
}
break;
case PowerupsMainControllerCS.PowerUps.SuperSledge:
switch (MagnetismLevel)
{
case 1: return 5000;
case 2: return 10000;
case 3: return 15000;
case 4: return 20000;
}
break;
}
return 0;
}
public static void Upgrade(PowerupsMainControllerCS.PowerUps powerUp)
{
var price = GetUpgradePrice(powerUp);
if (Coins < price)
{
return;
}
if (GetUpgradeLevel(powerUp) >= 5)
{
return;
}
Coins -= price;
switch (powerUp)
{
case PowerupsMainControllerCS.PowerUps.SuperSledge:
MagnetismLevel++;
break;
case PowerupsMainControllerCS.PowerUps.Magnetism:
MagnetismLevel++;
break;
case PowerupsMainControllerCS.PowerUps.Rage:
RageLevel++;
break;
}
}
public static int GetUpgradeLevel(PowerupsMainControllerCS.PowerUps powerUp)
{
switch (powerUp)
{
case PowerupsMainControllerCS.PowerUps.Magnetism:
return MagnetismLevel;
case PowerupsMainControllerCS.PowerUps.Rage:
return RageLevel;
case PowerupsMainControllerCS.PowerUps.SuperSledge:
return MagnetismLevel;
}
return 0;
}
#endregion
#region currency
public enum Lots
{
ut_kr_iap_001,
ut_kr_iap_002,
ut_kr_iap_003
}
public static string CurrencySymbol { get; private set; }
private static int _coins;
///
/// Current player amount of coins
///
public static int Coins
{
get
{
return _coins;
}
set
{
if (value < 0)
{
return;
}
if (_coins != value)
{
_coins = value;
PlayerPrefs.SetInt("Coins", _coins);
PlayerPrefs.Save();
}
}
}
private static int _bestScore;
///
/// Current player amount of coins
///
public static int BestScore
{
get
{
return _bestScore;
}
set
{
if (value < 0)
{
return;
}
if (_bestScore != value)
{
_bestScore = value;
PlayerPrefs.SetInt("BestScore", _bestScore);
PlayerPrefs.Save();
UILeaderBoard.SummitScore(_bestScore);
}
}
}
private static readonly Dictionary Prices = new Dictionary();
public static string GetLotID(Lots lot)
{
return string.Format("{0}",lot);
}
public static Lots GetLotID(string lot)
{
return (Lots)Enum.Parse(typeof(Lots), lot);
}
public static string GetPrice(Lots lot)
{
return (Prices.ContainsKey(lot) ? Prices[lot] : "");
}
public static int GetReward(Lots lot)
{
switch (lot)
{
case Lots.ut_kr_iap_001: return 5000;
case Lots.ut_kr_iap_002: return 10000;
case Lots.ut_kr_iap_003: return 20000;
}
return 0;
}
public NGUIMenuScript hNGUIMenuScript;
#endregion
///
///
///
public enum Costumes
{
Knight,
Underpants
}
void Start ()
{
_coins = PlayerPrefs.GetInt("Coins",1000);
_magn = PlayerPrefs.GetInt("Magnetism", 1);
_rage = PlayerPrefs.GetInt("Rage", 1);
_bestScore = PlayerPrefs.GetInt("BestScore", 0);
if (_magn == 0)
{
_magn = 1;
}
if (_rage == 0)
{
_rage = 1;
}
PlayerPrefs.SetInt(Costumes.Knight.ToString(), 1);
PlayerPrefs.Save();
#if UNITY_IOS
/*StoreKitBinding.requestProductData(Enum.GetNames(typeof(Lots)));
StoreKitManager.productListReceivedEvent += onReceive;
StoreKitManager.purchaseSuccessfulEvent += onPurchaseDone;
StoreKitManager.purchaseFailedEvent += onPurchaseFail;*/
#endif
foreach (Costumes costume in Enum.GetValues(typeof(Costumes)))
{
if (!CostumesInfo.ContainsKey(costume))
{
CostumesInfo.Add(costume, PlayerPrefs.GetInt(costume.ToString(), 0) == 1);
}
}
}
private void Reset()
{
#if UNITY_EDITOR
if (Application.isEditor)
{
PlayerPrefs.SetInt("FirstPlay", 1);
PlayerPrefs.SetInt("FirstTimeInfoShown", 0);
Coins = 1000;
MagnetismLevel = 0;
RageLevel = 0;
BestScore = 0;
foreach (Costumes costume in Enum.GetValues(typeof(Costumes)))
{
PlayerPrefs.SetInt(costume.ToString(), 0);
}
PlayerPrefs.SetInt(Costumes.Knight.ToString(), 1);
UIEnergy.Instance.Restore();
PlayerPrefs.Save();
UnityEditor.EditorApplication.isPlaying = false;
}
#endif
}
void Update()
{
if (hNGUIMenuScript)
{
var menu = hNGUIMenuScript.getCurrentMenu();
if (menu == NGUIMenuScript.NGUIMenus.ShopHome || menu == NGUIMenuScript.NGUIMenus.ShopCostumes
|| menu == NGUIMenuScript.NGUIMenus.ShopIAPs || menu == NGUIMenuScript.NGUIMenus.ShopPowerups
|| menu == NGUIMenuScript.NGUIMenus.ShopUtilities)
hNGUIMenuScript.updateCurrencyOnHeader(hNGUIMenuScript.getCurrentMenu());
}
if (Input.GetKeyDown(KeyCode.Return))
{
Reset();
}
}
#region PurchaseCallbacks
#if UNITY_IOS
private void onPurchaseFail(string message)
{
Debug.LogError("onPurchaseFail with message: "+ message);
}
/* private void onPurchaseDone(StoreKitTransaction product)
{
Coins += GetReward(GetLotID(product.productIdentifier));
Debug.Log("onPurchaseDone " + product.productIdentifier);
}*/
/*private void onReceive(List products)
{
foreach (var product in products)
{
try
{
var id = GetLotID(product.productIdentifier);
if (!Prices.ContainsKey(id))
{
Prices.Add(id,product.price);
}
}
catch (Exception) { }
}
}*/
#endif
#endregion
public static int GetCostumeCost(Costumes costume)
{
switch (costume)
{
case Costumes.Knight: return 0;
case Costumes.Underpants: return 20000;
}
return 0;
}
private static readonly Dictionary CostumesInfo = new Dictionary();
public static bool HasCostume(Costumes costume)
{
return CostumesInfo[costume];
}
public static void BuyCostume(Costumes costume)
{
if (HasCostume(costume))
{
return;
}
if (Coins >= GetCostumeCost(costume))
{
PlayerPrefs.SetInt(costume.ToString(),1);
PlayerPrefs.Save();
Coins -= GetCostumeCost(costume);
CostumesInfo[costume] = true;
}
}
}