Exemplo n.º 1
0
    /* * * * Item-specific methods * * * */

    ///<summary>Returns whether interstitial ads should be shown, based on whether the temporary no-ads item is active.
    /// Should also check whether the user has bought the permanent no-ads IAP.</summary>
    public static bool shouldShowAds()
    {
        DateTime expiration = DataAndSettingsManager.getExpirationDateForStoreItem(ITEM_KEY_NO_ADS_TEMPORARY);
        DateTime now        = DateTime.Now;

        return(expiration.CompareTo(now) < 0);
    }
Exemplo n.º 2
0
    ///<summary>The main game coroutine - repeatedly moves the snake and checks for apples until the snake can't move.</summary>
    private IEnumerator runGame()
    {
        int[] next = this.snake.nextMove();
        while (this.validMove(next))
        {
            float timeToMove = this.timeInterval();
            if (this.aboutToEatApple(next) && this.shouldGrow())   // after a certain point, the snake shouldn't grow every time
            {
                this.snake.grow(timeToMove, DataAndSettingsManager.getSmoothMovementState());
            }
            else
            {
                int[] old = this.snake.move(timeToMove, DataAndSettingsManager.getSmoothMovementState());
                this.setValueAtCoordinates(old, SPACE_EMPTY);
            }
            yield return(StartCoroutine(this.pausableWait(timeToMove))); // the player may pause during this time

            this.lookForApples(next);                                    // by now, the snake has finished moving for the turn
            this.setValueAtCoordinates(next, SPACE_SNAKE);
            next = this.snake.nextMove();
        }

        if (this.isTutorial)
        {
            this.stopTutorial();
        }
        else
        {
            this.stopGame();
        }
        yield break;
    }
Exemplo n.º 3
0
    /* * * * 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);
    }
Exemplo n.º 4
0
    /* * * * Lifecycle methods * * * */

    void Awake()
    {
        #if UNITY_IOS
        System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
        #endif
        DataAndSettingsManager.loadData();
    }
Exemplo n.º 5
0
    private void showResetAverageButtonIfNecessary()
    {
        int resetsLeft = DataAndSettingsManager.getNumBoughtForStoreItem(StoreManager.ITEM_KEY_RESET_AVERAGE);

        this.resetAverageButtonLabel.text = "Reset average (" + resetsLeft + ")";
        this.resetAverageButton.SetActive(resetsLeft > 0);
    }
Exemplo n.º 6
0
    ///<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);
    }
Exemplo n.º 7
0
 void OnApplicationPause(bool pauseStatus)
 {
     // this method is called when the app is soft-closed on iOS and Android
     if (pauseStatus)
     {
         DataAndSettingsManager.writeData();
     }
 }
Exemplo n.º 8
0
 void OnEnable()
 {
     this.closeColorSchemeListAction(); // switch to the correct panel
     this.colorblindModeToggle.isOn = DataAndSettingsManager.getColorblindModeState();
     this.smoothMovementToggle.isOn = DataAndSettingsManager.getSmoothMovementState();
     this.musicToggle.isOn          = DataAndSettingsManager.getMusicEnabledState();
     this.soundsToggle.isOn         = DataAndSettingsManager.getSoundsEnabledState();
 }
Exemplo n.º 9
0
 ///<summary>Updates the statistics display.</summary>
 private void updateStatsMenu()
 {
     this.statsGoldLabel.text        = "" + DataAndSettingsManager.getGoldAmount();
     this.statsHighscoreLabel.text   = "Highscore: " + DataAndSettingsManager.getHighscore();
     this.statsAverageLabel.text     = "Average: " + DataAndSettingsManager.getAverageScore().ToString("F2"); // 2 decimal places
     this.statsGamesPlayedLabel.text = "Games Played: " + DataAndSettingsManager.getGamesPlayed();
     this.showResetAverageButtonIfNecessary();
 }
Exemplo n.º 10
0
 void Start()
 {
     gameStarter = GetComponent <GameStarter>();
     gameRunner  = GetComponent <GameRunner>();
     gameEnder   = GetComponent <GameEnder>();
     ColorSchemesManager.setColorScheme(DataAndSettingsManager.getColorSchemeID());
     gameEnder.loadAds();
     onInitialize();
 }
Exemplo n.º 11
0
 void Awake()
 {
     foreach (Sound s in sounds)
     {
         s.source        = this.gameObject.AddComponent <AudioSource>();
         s.source.clip   = s.clip;
         s.source.volume = s.volume;
         s.source.loop   = s.loop;
     }
     this.musicEnabled  = DataAndSettingsManager.getMusicEnabledState();
     this.soundsEnabled = DataAndSettingsManager.getSoundsEnabledState();
 }
Exemplo n.º 12
0
    /* * * * UI setup * * * */

    private void showHardModeToggleIfNecessary()
    {
        this.hardModeRect.SetActive(DataAndSettingsManager.getNumBoughtForStoreItem(StoreManager.ITEM_KEY_HARD_MODE) > 0);
        this.hardModeToggle.isOn = DataAndSettingsManager.getHardModeState();
        if (this.hardModeRect.activeSelf)   // need to adjust text position to make room for the toggle
        {
            this.helpText.anchoredPosition = new Vector2(0f, 400f);
        }
        else
        {
            this.helpText.anchoredPosition = new Vector2(0f, 300f);
        }
    }
    /* * * * Lifecycle methods * * * */

    void OnEnable()
    {
        OnSelectColorScheme += this.respondToOtherItemSelected;
        if (this.isLocked)
        {
            // check whether it should still be locked
            this.isLocked = (this.colorSchemeID > 1 && DataAndSettingsManager.getNumBoughtForStoreItem(this.packName) == 0);
            if (!this.isLocked)
            {
                this.nameLabel.text = this.colorSchemeName;
            }
        }
    }
Exemplo n.º 14
0
    ///<summary>Populates the list of color schemes with `ColorSchemePickerListItem`s.</summary>
    private void setupList()
    {
        int selectedID = DataAndSettingsManager.getColorSchemeID();

        for (int i = 0; i < ColorSchemesManager.getNumColorSchemes(); i++)
        {
            // instantiate a list item in the scroll view for each color scheme
            ColorSchemePickerListItem item     = Instantiate(this.listItemPrefab, new Vector2(0f, -120f * i - 60f), Quaternion.identity) as ColorSchemePickerListItem;
            RectTransform             itemRect = item.gameObject.GetComponent <RectTransform>();
            itemRect.SetParent(this.listContentRect.transform, false);
            item.setup(i, (i == selectedID));
        }
    }
Exemplo n.º 15
0
 ///<summary>Removes gold and bads, updates gold amount, and restarts the game.</summary>
 public void reviveGame()
 {
     //Debug.Log("revive game");
     this.removeGold();
     foreach (Cube bad in this.bads)
     {
         this.removeBad(bad);
     }
     this.goldAmount = DataAndSettingsManager.getGoldAmount();
     this.updateGoldLabel();
     this.isReviving = true;
     this.actuallyStartGameAction();
     this.isReviving = false;
 }
 public void selectAction()
 {
     if (this.isLocked)
     {
         return;
     }
     if (OnSelectColorScheme != null)
     {
         OnSelectColorScheme(); // deselect all list items
     }
     this.setSelected(true);
     ColorSchemesManager.setColorScheme(this.colorSchemeID);
     DataAndSettingsManager.setColorSchemeID(this.colorSchemeID);
     FindObjectOfType <AudioManager>().playButtonSound();
 }
Exemplo n.º 17
0
    private void showReviveButtonsIfNecessary()
    {
        bool canRevive = (this.consecutiveRevivals < MAX_CONSECUTIVE_REVIVALS && GameStateManager.canRevive());

        if (canRevive)
        {
            int revivesLeft = DataAndSettingsManager.getNumBoughtForStoreItem(StoreManager.ITEM_KEY_EXTRA_LIFE);
            this.reviveButtonLabel.text = "Revive (" + revivesLeft + ")";
            this.reviveButton.SetActive(revivesLeft > 0);
        }
        else
        {
            this.reviveButton.SetActive(false);
        }
        this.reviveWithAdButton.SetActive(canRevive);
    }
    /* * * * Public methods * * * */

    public void setup(int colorSchemeID, bool selected)
    {
        this.colorSchemeID = colorSchemeID;
        this.setSelected(selected);
        ColorScheme cs = ColorSchemesManager.getColorSchemeWithID(colorSchemeID);

        this.setColors(cs);
        this.colorSchemeName = cs.getName();
        this.nameLabel.text  = this.colorSchemeName;
        this.packName        = cs.getPackName();
        this.isLocked        = (colorSchemeID > 1 && DataAndSettingsManager.getNumBoughtForStoreItem(this.packName) == 0);
        if (this.isLocked)
        {
            this.nameLabel.text += " (Locked)";
        }
    }
Exemplo n.º 19
0
    ///<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();
    }
Exemplo n.º 20
0
    /* * * * Game pathway steps * * * */

    ///<summary>Initializes objects, the game space array, and counters, flags, and labels.</summary>
    private void setupGame()
    {
        //Debug.Log("setup game");
        this.destroyObjects();
        this.initializeObjects();

        this.space           = new int[SIZE, SIZE, SIZE];
        this.space[2, 2, 2]  = SPACE_SNAKE;
        this.space[3, 2, 2]  = SPACE_SNAKE;
        this.space[4, 2, 2]  = SPACE_SNAKE;
        this.score           = 0;
        this.applesCollected = 0;
        this.goldAmount      = DataAndSettingsManager.getGoldAmount();
        this.isHardMode      = DataAndSettingsManager.getHardModeState();
        this.isPaused        = false;

        this.generateApple();

        this.updateScoreLabel();
        this.updateGoldLabel();
        DataAndSettingsManager.updateColorblindModeListeners();
    }
Exemplo n.º 21
0
 ///<summary>Sets the counts of items with lifespans according to how many lifespans are left before their expiration dates.</summary>
 public static void updateLifespanItemCounts()
 {
     foreach (StoreItem item in STORE_ITEMS)
     {
         string key       = item.getKey();
         int    numBought = DataAndSettingsManager.getNumBoughtForStoreItem(key);
         if (numBought > 0 && item.hasLifespan())
         {
             DateTime expiration = DataAndSettingsManager.getExpirationDateForStoreItem(key);
             DateTime now        = DateTime.Now;
             int      difference = expiration.Subtract(now).Hours;
             if (difference > 0)
             {
                 DataAndSettingsManager.setNumBoughtForStoreItem(key, difference / item.getLifespanHours());
             }
             else
             {
                 DataAndSettingsManager.setNumBoughtForStoreItem(key, 0);
             }
         }
     }
 }
Exemplo n.º 22
0
 void OnDestroy()
 {
     DataAndSettingsManager.writeData();
 }
Exemplo n.º 23
0
    /* * * * UI actions * * * */

    public void toggleColorblindModeAction()
    {
        DataAndSettingsManager.setColorblindModeState(this.colorblindModeToggle.isOn);
    }
Exemplo n.º 24
0
 public void toggleSmoothMovementAction()
 {
     DataAndSettingsManager.setSmoothMovementState(this.smoothMovementToggle.isOn);
 }
Exemplo n.º 25
0
    public GameObject colorblindIndicators; // should be set in the editor

    void OnEnable()
    {
        DataAndSettingsManager.OnToggleColorblindMode += this.setColorblindMode;
        this.setColorblindMode(DataAndSettingsManager.getColorblindModeState());
    }
Exemplo n.º 26
0
    ///<summary>Decrements the item count by 1. For now, actual item functionality should be handled where the item is expended.</summary>
    public static void expendItem(string key)   // TODO: consider handling item functionality in here
    {
        int numBought = DataAndSettingsManager.getNumBoughtForStoreItem(key);

        DataAndSettingsManager.setNumBoughtForStoreItem(key, numBought - 1);
    }
Exemplo n.º 27
0
 public void toggleMusicAction()
 {
     DataAndSettingsManager.setMusicEnabledState(this.musicToggle.isOn);
     FindObjectOfType <AudioManager>().setMusicEnabled(this.musicToggle.isOn);
 }
Exemplo n.º 28
0
 ///<summary>Returns the number of this item owned by the user (not counting expended ones).</summary>
 public int getNumBought()
 {
     return(DataAndSettingsManager.getNumBoughtForStoreItem(this.key));
 }
Exemplo n.º 29
0
 public void toggleSoundEffectsAction()
 {
     DataAndSettingsManager.setSoundsEnabledState(this.soundsToggle.isOn);
     FindObjectOfType <AudioManager>().setSoundsEnabled(this.soundsToggle.isOn);
 }
Exemplo n.º 30
0
    /* * * * Helper methods * * * */

    private void updateColorSchemeLabel()
    {
        int id = DataAndSettingsManager.getColorSchemeID();

        this.colorSchemeLabel.text = ColorSchemesManager.getColorSchemeWithID(id).getName();
    }