Example #1
0
    public void UseSelectedItem()
    {
        if (_lastSelectedItem == -1)
        {
            return;
        }

        GlobalEventText.AddMessage(string.Format("You used \"{0}\"", Inventory.Items[_lastSelectedItem].Name));
        Inventory.Items[_lastSelectedItem].Use();
        RefreshUI();
    }
Example #2
0
    protected IEnumerator SmoothMovement(int desiredIndex)
    {
        if (Scene._grid[desiredIndex].isWalkable /* && Scene._grid[desiredIndex].ItemValue != ItemValues.MiningBlock*/)
        {
            CurrentIndexPosition = desiredIndex;
            CurrentPosY          = CurrentIndexPosition / Scene.Height;
            CurrentPosX          = CurrentIndexPosition - CurrentPosY * Scene.Height;
            Vector3 end = Scene._grid[desiredIndex].position;
            if (PlayerBiome != Scene._grid[desiredIndex].Biome)
            {
                PlayerBiome = Scene._grid[desiredIndex].Biome;
                GlobalEventText.AddMessage(string.Format("You just entered in the biome {0}", PlayerBiome.ToString()));
            }
            //Calculate the remaining distance to move based on the square magnitude of the difference between current position and end parameter.
            //Square magnitude is used instead of magnitude because it's computationally cheaper.
            float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
            //While that distance is greater than a very small amount (Epsilon, almost zero):
            while (sqrRemainingDistance > float.Epsilon)
            {
                //Find a new position proportionally closer to the end, based on the moveTime
                Vector3 newPostion = Vector3.MoveTowards(_rb2D.position, end, 20.0f * Time.deltaTime);
                //Call MovePosition on attached Rigidbody2D and move it to the calculated position.
                _rb2D.MovePosition(newPostion);

                //Recalculate the remaining distance after moving.
                sqrRemainingDistance = (transform.position - end).sqrMagnitude;

                //Return and loop until sqrRemainingDistance is close enough to zero to end the function
                yield return(null);
            }
            Fog.UpdateFog(CurrentIndexPosition, TileFogState.Active);
        }
        else
        {
            if (Scene._grid[desiredIndex].TileItem != null)
            {
                if (!Scene._grid[desiredIndex].TileItem.GetComponent <MonoDestructibleTile>().thisItem.OnDamage(50, desiredIndex))
                {
                    yield return(new WaitForSeconds(0.5f)); //Mining time
                }
                else
                {
                    Destroy(Scene._grid[desiredIndex].TileItem);
                }
            }
        }

        _movementReady = true;
    }
Example #3
0
    public static bool AddItemToInventory(Item item)
    {
        //Looking for the same item
        if (item.isStackable) //If the item is stackable
        {
            for (int i = 0; i < Items.Length; i++)
            {
                if (Items[i] != null)
                {
                    if (Items[i].Name == item.Name)
                    {
                        Items[i].Count += item.Count;
                        GlobalEventText.AddMessage(string.Format("You picked up \"{0}\" (x{1})", item.Name, item.Count));
                        if (InventoryCaneva.panel.gameObject.activeInHierarchy)
                        {
                            InventoryCaneva.RefreshUI();
                        }
                        return(true);
                    }
                }
            }
        }

        //Trying to add the item to the inventory
        for (int i = 0; i < Items.Length; i++)
        {
            if (Items[i] == null)
            {
                Items[i] = (Item)item.Clone();
                GlobalEventText.AddMessage(string.Format("You picked up \"{0}\" (x{1})", item.Name, item.Count));
                if (InventoryCaneva.panel.gameObject.activeInHierarchy)
                {
                    InventoryCaneva.RefreshUI();
                }
                return(true);
            }
        }

        //Inventory full
        Debug.Log("Inventory full!");
        GlobalEventText.AddMessage(string.Format("You can't pickup \"{0}\", because your inventory is full", item.Name));
        return(false);
    }
Example #4
0
 public void Use()
 {
     //Open and get the stuff inside the bag
     Player.Stats.Eat        += _stats.Eat;
     Player.Stats.Drink      += _stats.Drink;
     Player.Stats.Health     += _stats.Health;
     Player.Stats.Mana       += _stats.Mana;
     Player.Stats.Stamina    += _stats.Stamina;
     Player.Stats.Experience += _stats.Experience;
     GlobalEventText.AddMessage(string.Format("You just ate a {0}", this.Name));
     if (this.Count == 1)
     {
         Inventory.RemoveItemFromInventory(this);
     }
     else
     {
         this.Count--;
         Inventory.RefreshUI();
     }
 }
Example #5
0
    public void SelectedItem(int index)
    {
        int contentIndex = _buttons.Length * _actualPage + index;

        if (_itemContainer.Content[contentIndex].Name != "Money")
        {
            Inventory.AddItemToInventory(_itemContainer.Content[contentIndex]);
        }
        else
        {
            Inventory.Money += _itemContainer.Content[contentIndex].Value;
            GlobalEventText.AddMessage(string.Format("You get {0} gold !", _itemContainer.Content[contentIndex].Value));
            Inventory.RefreshUI();
        }
        _itemContainer.Content[contentIndex] = null;

        _images[index].sprite = null;
        _buttons[index].SetActive(false);

        //Check if all the content of this page is gone
        for (int i = 0; i < _buttons.Length; i++)
        {
            if (_buttons[i].activeInHierarchy)
            {
                return;
            }
        }
        //Actual page empty !
        //If we aren't at the page 0
        if (_actualPage > 0)
        {
            do
            {
                _actualPage--;
                int start = _actualPage * _buttons.Length;
                int end   = start + _buttons.Length;
                //Check if the page -1 contains items:
                for (int i = start; i < end; i++)
                {
                    if (_itemContainer.Content[i] != null)
                    {
                        UpdateButtonAtPage(_actualPage);
                        if (_actualPage == 0)
                        {
                            _previousButton.SetActive(false);
                        }
                        return;
                    }
                }
            } while (_actualPage > 0);
        }
        //If we are at the page 0 => check if the content list is empty:
        //Looking for a page with some content
        for (int i = 0; i < _itemContainer.Content.Length; i++)
        {
            if (_itemContainer.Content[i] != null)
            {
                _actualPage = (int)(i / Buttons.Length);
                UpdateButtonAtPage(_actualPage);
                _previousButton.SetActive(false);
                //Checking if we can keep the next button on:
                if ((_actualPage + 1) * _buttons.Length >= _itemContainer.Content.Length)
                {
                    _nextButton.SetActive(false);
                }
                return;
            }
        }

        //Nothing left, we can destroy the object:
        Inventory.RemoveItemFromInventory(_itemContainer);
        InventoryUI.OnHoovering = false;
        InventoryPopupInfos.Hide();
        Exit();
    }
Example #6
0
 public void PickupItem()
 {
     Inventory.Money += Value;
     GlobalEventText.AddMessage(string.Format("You picked up {0} gold !", Value));
     MonoBehaviour.Destroy(gameObject);
 }
Example #7
0
    public void InventoryItemClick(int index)
    {
        if (Inventory.Items[index] == null)
        {
            ClearText();
            _lastSelectedItem = -1;
            UseButton.gameObject.SetActive(false);
            return;
        }

        //Removing item:
        if (Input.GetMouseButton(1))
        {
            GameObject item = null;
            if (Input.GetKey(KeyCode.LeftShift))
            {
                GlobalEventText.AddMessage(string.Format("You just throw away \"{0}\" (x1)", Inventory.Items[index].Name));
                item = MonoItem.CreateGameObjectFromItem(Inventory.Items[index]);
                item.GetComponent <MonoItem>().thisItem.Count = 1;
                Inventory.DecreaseCount(Inventory.Items[index], 1);
            }
            else
            {
                GlobalEventText.AddMessage(string.Format("You just throw away \"{0}\" (x{1})", Inventory.Items[index].Name, Inventory.Items[index].Count));
                item = MonoItem.CreateGameObjectFromItem(Inventory.Items[index]);
                Inventory.RemoveItemFromInventory(Inventory.Items[index]);
                //Hide the infos popup
                OnHoovering = false;
                InventoryPopupInfos.Hide();
            }


            //Check if the playe tile contains items
            if (Scene._grid[Player.CurrentIndexPosition].TileItem != null)
            {
                //Check if the existing item is not a container
                Item[] items;
                Item   existing = Scene._grid[Player.CurrentIndexPosition].TileItem.GetComponent <MonoItem>().thisItem;
                if (existing.GType != GlobalType.Container)
                {
                    Debug.Log("Wrap items");
                    //Wrap all items in a bag:
                    Bag01 bag = new Bag01(Scene._grid[Player.CurrentIndexPosition].TileItem);
                    items    = new Item[2];
                    items[0] = existing;
                    items[1] = item.GetComponent <MonoItem>().thisItem;
                    bag.SetContent(items);
                    Scene._grid[Player.CurrentIndexPosition].TileItem.GetComponent <MonoItem>().thisItem = bag;

                    Scene._grid[Player.CurrentIndexPosition].ItemValue = bag.ItemValue;

                    Scene._grid[Player.CurrentIndexPosition].TileItem.GetComponent <SpriteRenderer>().sprite = bag.InGameSprite;

                    Destroy(item);
                }
                else
                {
                    Debug.Log("Add content");
                    ItemContainer container = (ItemContainer)existing;
                    items = new Item[container.Content.Length + 1];
                    //Get the existing items
                    for (int i = 0; i < container.Content.Length; i++)
                    {
                        items[i] = container.Content[i];
                    }
                    items[container.Content.Length] = (Item)item.GetComponent <MonoItem>().thisItem.Clone(); //Add the new item
                    container.SetContent(items);                                                             //Set the new content

                    Destroy(item);
                }
            }
            else
            {
                //Create the object in the player tile
                item.transform.position = Scene._grid[Player.CurrentIndexPosition].position;
                item.transform.SetParent(Scene._grid[Player.CurrentIndexPosition].TileObject.transform);
                Scene._grid[Player.CurrentIndexPosition].TileItem  = item;
                Scene._grid[Player.CurrentIndexPosition].ItemValue = item.GetComponent <MonoItem>().thisItem.ItemValue;
            }
            return;
        }
        _lastSelectedItem    = index;
        NameText.text        = string.Format("Name: {0}", Inventory.Items[index].Name);
        MassText.text        = string.Format("Mass: {0}", Inventory.Items[index].Mass);
        CountText.text       = string.Format("Quantity: {0}", Inventory.Items[index].Count);
        DescriptionText.text = string.Format("Description: {0}", Inventory.Items[index].Description);

        if (Inventory.Items[index].isUsable)
        {
            switch (Inventory.Items[index].GType)
            {
            case GlobalType.Fruits:
            case GlobalType.Vegetables:
                UseButton.transform.GetChild(0).GetComponent <Text>().text = "Eat";
                break;

            case GlobalType.Container:
                UseButton.transform.GetChild(0).GetComponent <Text>().text = "Open";
                break;

            default:
                UseButton.transform.GetChild(0).GetComponent <Text>().text = "Use";
                break;
            }
            UseButton.gameObject.SetActive(true);
        }
        else
        {
            UseButton.gameObject.SetActive(false);
        }
    }
Example #8
0
    public void CraftTableBTN()
    {
        #region Find the recipe
        int    recipeIndex  = Craft.CheckIfCorrectRecipe(ItemsInCraftTable);
        Recipe targetRecipe = Craft.Recipes[recipeIndex];
        if (recipeIndex == -1)
        {
            RemoveItemFromInventory();
            GlobalEventText.AddMessage("Bad recipe!");
            return;
        }
        //Check for the random success
        bool success = true;
        if (targetRecipe.SuccessChance != 100)
        {
            int result = Random.Range(0, 100);
            if (result <= targetRecipe.SuccessChance)
            {
                success = true;
            }
            else
            {
                success = false;
                GlobalEventText.AddMessage("You failed...");
            }
        }
        if (!targetRecipe.isKnown && success)
        {
            targetRecipe.isKnown = true; //Set the selected recipe as "known"
            GlobalEventText.AddMessage(string.Format("Yay! You learned how to make {0}", targetRecipe.Name));
        }

        if (success)
        {
            #region RandomResult
            //Check if the result is random:
            if (targetRecipe.isRandomResult)
            {
                bool        isOK         = false;
                int         index        = 0;
                List <Item> tempItemList = new List <Item>(targetRecipe.RandomResults.Length);
                for (int i = 0; i < targetRecipe.RandomResults.Length; i++)
                {
                    tempItemList.Add(targetRecipe.RandomResults[i]);
                }
                for (int i = 0; i < targetRecipe.AmountOfRandomItemInResult; i++)
                {
                    isOK = false;
                    Item rndResult = null;
                    //Get the random result
                    do
                    {
                        index     = Random.Range(0, tempItemList.Count);
                        rndResult = (Item)tempItemList[index].Clone();
                        for (int j = 0; j < 3; j++)
                        {
                            if (ItemsInResult[j] == null)
                            {
                                isOK = true;
                            }
                            else if (ItemsInResult[j].Name != rndResult.Name)
                            {
                                isOK = true;
                            }
                        }
                    } while (!isOK);


                    if (ItemsInResult[i] != null) // Look if the item is different
                    {
                        if (ItemsInResult[i].Name == rndResult.Name)
                        {
                            ItemsInResult[i].Count += rndResult.Count;
                        }
                        else
                        {
                            Inventory.AddItemToInventory(ItemsInResult[i]);
                            ItemsInResult[i] = (Item)rndResult.Clone();
                        }
                    }
                    else
                    {
                        ItemsInResult[i] = (Item)rndResult.Clone();
                    }

                    //Update result sprite
                    _resultImages[i].gameObject.SetActive(true);
                    _resultImages[i].sprite = ItemsInResult[i].InventorySprite;

                    tempItemList.RemoveAt(index);
                }
            }
            #endregion
            else
            {
                //Get the results (put the results in the result panel)
                for (int i = 0; i < 3; i++)
                {
                    if (Craft.Recipes[recipeIndex].Results[i] != null)
                    {
                        //Compare the actual recipe to the old one:
                        if (_previousRecipe != null && Craft.Recipes[recipeIndex].Name == _previousRecipe.Name)
                        {
                            if (ItemsInResult[i] != null)
                            {
                                ItemsInResult[i].Count += Craft.Recipes[recipeIndex].Results[i].Count;
                            }
                        }
                        else
                        {
                            //Add the previous item to the inventory
                            if (ItemsInResult[i] != null)
                            {
                                Inventory.AddItemToInventory(ItemsInResult[i]);
                            }

                            ItemsInResult[i] = (Item)Craft.Recipes[recipeIndex].Results[i].Clone();

                            //Update result sprite
                            _resultImages[i].gameObject.SetActive(true);
                            _resultImages[i].sprite = ItemsInResult[i].InventorySprite;
                        }
                    }
                    else
                    {
                        ItemsInResult[i] = null;

                        //Update result sprite
                        _resultImages[i].gameObject.SetActive(false);
                        _resultImages[i].sprite = ResourcesManager.instance.EmptySprite;
                    }
                }
            }
            _previousRecipe = Craft.Recipes[recipeIndex];
        }
        else
        {
            _previousRecipe = null;
        }


        //Handle craft items:
        for (int i = 0; i < 9; i++)
        {
            if (Craft.Recipes[recipeIndex].Items[i] != null)                                                   //Check if not empty
            {
                Inventory.DecreaseCount(ItemsInCraftTable[i].Name, Craft.Recipes[recipeIndex].Items[i].Count); //Remove the recipe count of the item
                DecreaseCount(ItemsInCraftTable[i], Craft.Recipes[recipeIndex].Items[i].Count);
            }
        }
        #endregion

        //Update craftUI (keep the rest in the ui)
        RefreshUI();
        RefreshResults();
        RefreshRecipesList();
        //Update the inventory
        Inventory.RefreshUI();
    }
Example #9
0
    public static void OnRecipeClick(int index)
    {
        ResetUI();
        bool isOK = true;

        bool[] tmp = { false, false, false, false, false, false, false, false, false };

        int count = 0;

        //1- Check if the inventory have enough items:
        for (int i = 0; i < 9; i++)
        {
            if (!isOK)
            {
                GlobalEventText.AddMessage("You can't craft this recipe!");
                break;
            }


            if (Craft.Recipes[index].Items[i] != null)
            {
                count = 0;
                for (int j = 0; j < Inventory.Items.Length; j++)
                {
                    if (Inventory.Items[j] != null)
                    {
                        if (Inventory.Items[j].Name == Craft.Recipes[index].Items[i].Name)
                        {
                            count += Inventory.Items[j].Count;
                        }
                    }
                }
                //Check the count:
                if (count < Craft.Recipes[index].Items[i].Count)
                {
                    isOK = false;
                }
                else
                {
                    tmp[i] = true;
                }
            }
            else
            {
                tmp[i] = true;
            }
        }
        if (!isOK)
        {
            return;
        }
        foreach (bool b in tmp)
        {
            if (!b)
            {
                GlobalEventText.AddMessage("You can't craft this item!");
                return;
            }
        }
        //Put the ingredients in the list
        for (int i = 0; i < 9; i++)
        {
            if (Craft.Recipes[index].Items[i] != null)
            {
                _images[i].gameObject.SetActive(true);
                _images[i].sprite = Craft.Recipes[index].Items[i].InventorySprite;
                _images[i].transform.GetChild(0).GetComponent <Text>().text = Craft.Recipes[index].Items[i].Count.ToString();
                ItemsInCraftTable[i] = (Item)Craft.Recipes[index].Items[i].Clone();
            }
        }
    }