Exemple #1
0
    // buy the currently selected item
    void BuySelectedItem()
    {
        // check if the currently selected item is an element (0) or an artifact (1)
        Item item = m_itemList[m_currentItemIndex];

        if (item.m_type == 0)
        {
            // it's an element - we need to know how much
            SwitchToBuyAmountState();
        }
        else
        {
            // get access to the game data
            GameData gameData = DataController.m_instance.m_gameData;

            // get access to the player data
            PlayerData playerData = DataController.m_instance.m_playerData;

            // get access to the artifact data
            GD_Artifact artifactGameData = gameData.m_artifactList[item.m_id];

            // it's an artifact - check if the player can afford it
            if (playerData.m_bank.m_currentBalance < artifactGameData.m_starportPrice)
            {
                // nope - show an error message
                SwitchToErrorMessageState("Insufficient funds");

                Debug.Log("playerData.m_bankPlayerData.m_currentBalance = " + playerData.m_bank.m_currentBalance);
                Debug.Log("artifactGameData.m_starportPrice = " + artifactGameData.m_starportPrice);
            }
            else if (artifactGameData.m_volume > playerData.m_playerShip.GetRemainingVolme())
            {
                // player's ship has no room for it - show an error message
                SwitchToErrorMessageState("Insufficient cargo space");

                Debug.Log("playerData.m_shipCargoPlayerData.m_volumeUsed = " + playerData.m_playerShip.m_volumeUsed);
                Debug.Log("artifactGameData.m_volume = " + artifactGameData.m_volume);
                Debug.Log("playerData.m_shipConfigurationPlayerData.m_volume = " + playerData.m_playerShip.m_volume);
            }
            else
            {
                // deduct the price of the artifact from the player's bank balance
                playerData.m_bank.m_currentBalance -= gameData.m_artifactList[item.m_id].m_starportPrice;

                // transfer the artifact from the starport to the ship
                playerData.m_starport.m_artifactStorage.Remove(item.m_id);
                playerData.m_playerShip.AddArtifact(item.m_id);

                // update the screen
                UpdateScreen();

                // play a ui sound
                SoundController.m_instance.PlaySound(SoundController.Sound.Update);
            }
        }
    }
Exemple #2
0
    // call this whenever we change state or do something that would result in something changing on the screen
    void UpdateScreen()
    {
        if (m_currentState == State.ErrorMessage)
        {
            // turn on the error message display
            m_errorPanel.SetActive(true);
            m_overlayPanel.SetActive(true);

            // turn off the other popup panels
            m_amountGameObject.SetActive(false);
            m_confirmAnalyzePanel.SetActive(false);
            m_analyzePanel.SetActive(false);
        }
        else
        {
            // turn off the error message display
            m_errorPanel.SetActive(false);
            m_overlayPanel.SetActive(false);

            if (m_currentState == State.MenuBar)
            {
                // all we need to do in this state is hide the trade screen and show the welcome screen
                m_welcomeGameObject.SetActive(true);
                m_tradeGameObject.SetActive(false);
                m_amountGameObject.SetActive(false);
                m_confirmAnalyzePanel.SetActive(false);
                m_analyzePanel.SetActive(false);
            }
            else
            {
                // hide the welcome screen and show the trade screen
                m_welcomeGameObject.SetActive(false);
                m_tradeGameObject.SetActive(true);

                // get access to the game data
                GameData gameData = DataController.m_instance.m_gameData;

                // get access to the player data
                PlayerData playerData = DataController.m_instance.m_playerData;

                // check if we are needing the buy or sell amount
                if ((m_currentState == State.BuyAmount) || (m_currentState == State.SellAmount))
                {
                    // display the amount input
                    m_amountGameObject.SetActive(true);
                    m_overlayPanel.SetActive(true);

                    // get the currently selected element id
                    int elementId = m_itemList[m_currentItemIndex].m_id;

                    // figure out the maximum amount the player can buy
                    int maximumAmount = 0;

                    if (m_currentState == State.BuyAmount)
                    {
                        maximumAmount = GetMaximumBuyAmountDueToCurrentBalance(elementId);

                        if (maximumAmount == 0)
                        {
                            // the player is broke and cannot buy anything - so immediately block the player
                            m_currentState = State.BuyItem;
                            SwitchToErrorMessageState("Insufficient funds");
                            return;
                        }

                        int remainingVolume = playerData.m_playerShip.GetRemainingVolme();

                        if (remainingVolume == 0)
                        {
                            // the cargo hold is full and the player cannot buy anything - so immediately block the player
                            m_currentState = State.BuyItem;
                            SwitchToErrorMessageState("Insufficient cargo space");
                            return;
                        }

                        // the maximum amount the player can buy is the lesser of the funds remaining or the cargo hold space remaining
                        if (remainingVolume < maximumAmount)
                        {
                            maximumAmount = remainingVolume;
                        }
                    }
                    else
                    {
                        // the maximum amount is however much the player has in the ships cargo hold
                        PD_ElementReference elementReference = playerData.m_playerShip.m_elementStorage.Find(elementId);

                        maximumAmount = elementReference.m_volume;
                    }

                    // update the amount label
                    m_amountLabelText.text = "Transfer how many cubic meters? (0.0 to " + (maximumAmount / 10) + "." + (maximumAmount % 10) + ")";
                }
                else
                {
                    // hide the amount input
                    m_amountGameObject.SetActive(false);
                }

                // check if we are confirming analysis of an artifact
                if (m_currentState == State.AnalyzeConfirm)
                {
                    // show the confirm analyze panel
                    m_confirmAnalyzePanel.SetActive(true);
                    m_overlayPanel.SetActive(true);
                }
                else
                {
                    // hide the confirm analyze panel
                    m_confirmAnalyzePanel.SetActive(false);
                }

                // check if we are showing the analysis of an artifact
                if (m_currentState == State.AnalyzeShow)
                {
                    // show the analyze panel
                    m_analyzePanel.SetActive(true);
                    m_overlayPanel.SetActive(true);
                }
                else
                {
                    // hide the analyze panel
                    m_analyzePanel.SetActive(false);
                }

                // reset the trade item list
                m_itemList = new List <Item>();

                // clear out the text
                m_itemListText.text      = "";
                m_volumeListText.text    = "";
                m_unitValueListText.text = "";

                m_rowCount = 0;

                if ((m_currentState != State.AnalyzeItem) && (m_currentState != State.AnalyzeConfirm) && (m_currentState != State.AnalyzeShow))
                {
                    // add elements heading
                    m_itemListText.text      += "<color=#A35514>Elements</color>" + Environment.NewLine;
                    m_volumeListText.text    += Environment.NewLine;
                    m_unitValueListText.text += Environment.NewLine;

                    m_rowCount++;

                    // get access to the ship cargo data for elements
                    PD_ElementStorage elementStorage = playerData.m_playerShip.m_elementStorage;

                    if ((m_currentState == State.BuyItem) || (m_currentState == State.BuyAmount))
                    {
                        // add all elements available to buy in starport
                        for (int elementId = 0; elementId < gameData.m_elementList.Length; elementId++)
                        {
                            GD_Element elementGameData = gameData.m_elementList[elementId];

                            if (elementGameData.m_availableInStarport)
                            {
                                m_itemListText.text += elementGameData.m_name + Environment.NewLine;

                                PD_ElementReference elementReference = elementStorage.Find(elementId);

                                if (elementReference == null)
                                {
                                    m_volumeListText.text += "0.0" + Environment.NewLine;
                                }
                                else
                                {
                                    m_volumeListText.text += (elementReference.m_volume / 10) + "." + (elementReference.m_volume % 10) + Environment.NewLine;
                                }

                                m_unitValueListText.text += elementGameData.m_starportPrice + Environment.NewLine;

                                m_itemList.Add(new Item(m_rowCount++, 0, elementId, 0));
                            }
                        }
                    }
                    else if ((m_currentState == State.SellItem) || (m_currentState == State.SellAmount))
                    {
                        // add all elements in the ship cargo hold
                        foreach (PD_ElementReference elementReference in elementStorage.m_elementList)
                        {
                            GD_Element elementGameData = elementReference.GetElementGameData();

                            m_itemListText.text      += elementGameData.m_name + Environment.NewLine;
                            m_volumeListText.text    += (elementReference.m_volume / 10) + "." + (elementReference.m_volume % 10) + Environment.NewLine;
                            m_unitValueListText.text += elementGameData.m_starportPrice + Environment.NewLine;

                            m_itemList.Add(new Item(m_rowCount++, 0, elementReference.m_elementId, 0));
                        }
                    }
                }

                if ((m_currentState == State.BuyItem) || (m_currentState == State.BuyAmount) || (m_currentState == State.AnalyzeItem) || (m_currentState == State.AnalyzeConfirm) || (m_currentState == State.AnalyzeShow))
                {
                    // add artifacts heading
                    m_itemListText.text      += "<color=#A35514>Artifacts</color>" + Environment.NewLine;
                    m_volumeListText.text    += Environment.NewLine;
                    m_unitValueListText.text += Environment.NewLine;

                    m_rowCount++;

                    // get access to the starport data for artifacts
                    PD_ArtifactStorage artifactStorage = playerData.m_starport.m_artifactStorage;

                    // add all artifacts available to buy in starport
                    foreach (PD_ArtifactReference artifactReference in artifactStorage.m_artifactList)
                    {
                        GD_Artifact artifactGameData = artifactReference.GetArtifactGameData();

                        m_itemListText.text      += artifactGameData.m_name + Environment.NewLine;
                        m_volumeListText.text    += (artifactGameData.m_volume / 10) + "." + (artifactGameData.m_volume % 10) + Environment.NewLine;
                        m_unitValueListText.text += artifactGameData.m_starportPrice + Environment.NewLine;

                        m_itemList.Add(new Item(m_rowCount++, 1, artifactReference.m_artifactId, 0));
                    }
                }

                if ((m_currentState == State.SellItem) || (m_currentState == State.SellAmount) || (m_currentState == State.AnalyzeItem) || (m_currentState == State.AnalyzeConfirm) || (m_currentState == State.AnalyzeShow))
                {
                    // add artifacts heading
                    m_itemListText.text      += "<color=#A35514>Artifacts</color>" + Environment.NewLine;
                    m_volumeListText.text    += Environment.NewLine;
                    m_unitValueListText.text += Environment.NewLine;

                    m_rowCount++;

                    // get access to the ship storage for artifacts
                    PD_ArtifactStorage artifactStorage = playerData.m_playerShip.m_artifactStorage;

                    // add all artifacts in the ship cargo hold
                    foreach (PD_ArtifactReference artifactReference in artifactStorage.m_artifactList)
                    {
                        GD_Artifact artifactGameData = artifactReference.GetArtifactGameData();

                        m_itemListText.text      += artifactGameData.m_name + Environment.NewLine;
                        m_volumeListText.text    += (artifactGameData.m_volume / 10) + "." + (artifactGameData.m_volume % 10) + Environment.NewLine;
                        m_unitValueListText.text += artifactGameData.m_actualValue + Environment.NewLine;

                        m_itemList.Add(new Item(m_rowCount++, 1, artifactReference.m_artifactId, 0));
                    }
                }

                // add end of list heading
                m_itemListText.text      += "<color=#A35514>End of List</color>" + Environment.NewLine;
                m_volumeListText.text    += Environment.NewLine;
                m_unitValueListText.text += Environment.NewLine;

                m_rowCount++;

                // make sure we have something in the list
                if (m_itemList.Count == 0)
                {
                    // there is nothing sell or analyze - so immediately block the player
                    SwitchToMenuBarState();
                    return;
                }

                // keep the current selection index within bounds
                if (m_currentItemIndex >= m_itemList.Count)
                {
                    m_currentItemIndex = m_itemList.Count - 1;
                }

                // force the text object to update (so we can get the correct height)
                m_itemListText.ForceMeshUpdate();

                // force the canvas to update
                Canvas.ForceUpdateCanvases();

                // show the up arrow only if the first item is not selected
                m_upArrowImage.gameObject.SetActive((m_currentItemIndex > 0));

                // show the down arrow only if the last part is not selected
                m_downArrowImage.gameObject.SetActive(m_currentItemIndex < (m_itemList.Count - 1));

                // get the row number of the currently selected item
                int row = m_itemList[m_currentItemIndex].m_row;

                // get the height of the item list viewport
                float viewportHeight = m_itemListMask.GetComponent <RectTransform>().rect.height;

                // calculate height of each text row
                float rowHeight = m_itemListText.renderedHeight / m_rowCount;

                // figure out the offset for the selection box
                float selectionBoxOffset;

                while (true)
                {
                    selectionBoxOffset = (row + m_currentRowOffset) * rowHeight;

                    if ((selectionBoxOffset + rowHeight * 2) >= viewportHeight)
                    {
                        m_currentRowOffset--;
                    }
                    else if (selectionBoxOffset < rowHeight)
                    {
                        m_currentRowOffset++;
                    }
                    else
                    {
                        break;
                    }
                }

                // put the item selection box in the right place
                RectTransform rectTransform = m_selectionXform.GetComponent <RectTransform>();
                rectTransform.offsetMin = m_baseSelectionOffsetMin + new Vector3(0.0f, -selectionBoxOffset, 0.0f);
                rectTransform.offsetMax = m_baseSelectionOffsetMax + new Vector3(0.0f, -selectionBoxOffset, 0.0f);

                // calculate the offset for the text
                float textOffset = m_currentRowOffset * rowHeight;

                // move the text in all 3 columns
                m_itemListText.rectTransform.offsetMax      = new Vector3(0.0f, -textOffset, 0.0f);
                m_volumeListText.rectTransform.offsetMax    = new Vector3(0.0f, -textOffset, 0.0f);
                m_unitValueListText.rectTransform.offsetMax = new Vector3(0.0f, -textOffset, 0.0f);

                // update bank balance amount
                m_currentBalanceText.text = "Your account balance is: " + playerData.m_bank.m_currentBalance + " M.U.";
            }
        }

        // enable or disable buttons now
        bool enableButtons = (m_currentState == State.MenuBar);

        m_buyButton.interactable     = enableButtons;
        m_sellButton.interactable    = enableButtons;
        m_analyzeButton.interactable = enableButtons;
        m_exitButton.interactable    = enableButtons;
    }