/* * * * Buying items * * * */ ///<summary>Updates gold amount and relevant item count as appropriate. Also updates expiration date if applicable.</summary> public static bool buyItem(int id) { StoreItem item = STORE_ITEMS[id]; string key = item.getKey(); int numBought = DataAndSettingsManager.getNumBoughtForStoreItem(key); int gold = DataAndSettingsManager.getGoldAmount(); if (gold >= item.getCost() && (id < getNumExpendables() || numBought < 1)) { DataAndSettingsManager.setNumBoughtForStoreItem(key, numBought + 1); DataAndSettingsManager.setGoldAmount(gold -= item.getCost()); if (item.hasLifespan()) { DateTime expiration = DataAndSettingsManager.getExpirationDateForStoreItem(key); DateTime now = DateTime.Now; TimeSpan lifespan = new TimeSpan(item.getLifespanHours(), 0, 0); if (expiration.CompareTo(now) < 0) { // the item has already expired, so set a new expiration date DataAndSettingsManager.setExpirationDateForStoreItem(key, now.Add(lifespan)); } else { // the item hasn't expired yet, so advance the expiration date further DataAndSettingsManager.setExpirationDateForStoreItem(key, expiration.Add(lifespan)); } //Debug.Log("expiration date was " + expiration.ToString()); } return(true); } return(false); }
///<summary>Called when Unity IAP successfully makes a purchase with the appropriate store.</summary> public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) { bool validPurchase = true; string purchasedProductID = args.purchasedProduct.definition.id; #if UNITY_IOS || UNITY_ANDROID validPurchase = validateReceipt(args.purchasedProduct.receipt, purchasedProductID); #endif if (validPurchase) { if (purchasedProductID.Equals(PRODUCT_ID_100_GOLD)) { int gold = DataAndSettingsManager.getGoldAmount(); DataAndSettingsManager.setGoldAmount(gold + 100); storeMenu.updateGoldLabel(); } else { #if UNITY_IOS // update our copy of the unified iOS receipt appReceipt = args.purchasedProduct.receipt; // should already be formatted for Unity #endif } FindObjectOfType <AlertPrompt>().showMessage("Purchase succeeded!"); } else { FindObjectOfType <AlertPrompt>().showMessage("Purchase failed validation."); } return(PurchaseProcessingResult.Complete); }
///<summary>Calculates highscore, average, and gold earned, and writes the data.</summary> private void updateAndSaveData() { this.score = GameStateManager.getScore(); this.highscore = DataAndSettingsManager.getHighscore(); if (this.score > this.highscore) { this.highscore = this.score; DataAndSettingsManager.setHighscore(this.highscore); } this.gold = GameStateManager.getGoldAmount(); this.isHardMode = DataAndSettingsManager.getHardModeState(); this.goldFromApples = GameStateManager.getApples() / 2 - this.goldFromApplesBeforeRevive; int addition = this.goldFromApples; if (this.isHardMode) { addition = (int)(addition * 1.5); } this.gold += addition; DataAndSettingsManager.setGoldAmount(this.gold); this.goldFromApplesBeforeRevive += this.goldFromApples; float average = DataAndSettingsManager.getAverageScore(); int numGames = DataAndSettingsManager.getGamesPlayed(); if (consecutiveRevivals > 0) { average += (float)(this.score - this.scoreBeforeRevive) / numGames; } else { average = (average * numGames + this.score) / (numGames + 1); DataAndSettingsManager.setGamesPlayed(numGames + 1); } this.averageScore = average; DataAndSettingsManager.setAverageScore(average); this.scoreBeforeRevive = this.score; DataAndSettingsManager.writeData(); }