Exemplo n.º 1
0
 void Awake()
 {
     _itemMixture         = ItemMixture.Instance();
     _inv                 = InventoryHandler.Instance();
     _tooltip             = Tooltip.Instance();
     _characterManager    = CharacterManager.Instance();
     _messagePanelHandler = MessagePanelHandler.Instance();
 }
Exemplo n.º 2
0
 public static ItemMixture Instance()
 {
     if (!_itemMixture)
     {
         _itemMixture = FindObjectOfType(typeof(ItemMixture)) as ItemMixture;
         if (!_itemMixture)
         {
             Debug.LogError("There needs to be one active ItemMixture script on a GameObject in your scene.");
         }
     }
     return(_itemMixture);
 }
Exemplo n.º 3
0
 public void GoToRecipeScene()
 {
     _itemMixture = ItemMixture.Instance();
     if (!_itemMixture.ItemLocked)
     {
         if (_itemMixture.IsEmpty())
         {
             BuildTrainStarter();
             SceneManager.LoadScene(SceneSettings.SceneIdForRecipes);
         }
     }
 }
Exemplo n.º 4
0
    public void OnDrop(PointerEventData eventData)
    {
        ItemData      draggedItem = eventData.pointerDrag.GetComponent <ItemData>();
        ItemMixture   mixedItem   = eventData.pointerDrag.GetComponent <ItemMixture>();
        ItemEquipment equipedItem = eventData.pointerDrag.GetComponent <ItemEquipment>();

        if (mixedItem == null && draggedItem == null && equipedItem == null)
        {
            return;
        }
        if (mixedItem != null)
        {
            if (mixedItem.ItemIns == null)
            {
                return;
            }
            if (!mixedItem.ReadyToUse())
            {
                return;
            }
        }
        if (draggedItem != null)
        {
            if (draggedItem.ItemIns == null)
            {
                return;
            }
        }
        if (equipedItem != null)
        {
            if (equipedItem.ItemIns == null)
            {
                return;
            }
        }
        ItemData existingItem = this.transform.GetComponentInChildren <ItemData>();

        if (existingItem == null)
        {
            return;
        }
        //Item from Mixture Area
        if (mixedItem != null)
        {
            if (mixedItem.ItemIns != null)
            {
                //#################################### not empty slot
                if (existingItem.ItemIns != null && existingItem.ItemIns.Item.Id != mixedItem.ItemIns.Item.Id)
                {
                    _inv.PrintMessage("New item should be placed in an empty inventory slot", Color.yellow);
                }
                //#################################### empty slot
                else
                {
                    if (_inv.AddItemToInventory(mixedItem.ItemIns.Item, mixedItem.ItemIns.UserItem.StackCnt, existingItem.SlotIndex))
                    {
                        //Delete mixedItem Item
                        mixedItem.LoadEmpty();
                        _inv.UpdateInventory();
                    }
                }
                return;
            }
        }
        //Item from equipments Area
        if (equipedItem != null)
        {
            if (equipedItem.ItemIns != null)
            {
                //#################################### not empty slot
                if (existingItem.ItemIns != null)
                {
                    _inv.PrintMessage("New item should be placed in an empty inventory slot", Color.yellow);
                }
                //#################################### empty slot
                else
                {
                    //Set the slot Item
                    existingItem.ItemIns = equipedItem.ItemIns;
                    existingItem.transform.parent.name         = existingItem.transform.name = existingItem.ItemIns.Item.Name;
                    existingItem.GetComponent <Image>().sprite = existingItem.ItemIns.Item.GetSprite();
                    var stackCntText = existingItem.transform.GetComponentInChildren <TextMeshProUGUI>();
                    stackCntText.text = existingItem.ItemIns.UserItem.StackCnt > 1 ? existingItem.ItemIns.UserItem.StackCnt.ToString() : "";
                    existingItem.ItemIns.UserItem.Order    = existingItem.SlotIndex;
                    existingItem.ItemIns.UserItem.Equipped = false;
                    //unload new Item to equipments
                    equipedItem.LoadItem(null);
                    _inv.UpdateInventory();
                }
                return;
            }
        }
        if (draggedItem != null)
        {
            if (SlotIndex != draggedItem.SlotIndex)
            {
                //#################################### Empty slot logic swap
                if (existingItem.ItemIns == null)
                {
                    //Swap Parent
                    draggedItem.transform.SetParent(existingItem.transform.parent);
                    existingItem.transform.SetParent(draggedItem.Parent);
                    draggedItem.Parent = null;
                    //Swap positions
                    existingItem.transform.position = existingItem.transform.parent.position;
                    draggedItem.transform.position  = draggedItem.transform.parent.position;
                    //Swap SlotIndex
                    var temp2 = existingItem.SlotIndex;
                    existingItem.SlotIndex             = draggedItem.SlotIndex;
                    draggedItem.ItemIns.UserItem.Order = draggedItem.SlotIndex = temp2;
                    //Swapping names
                    existingItem.transform.parent.name = "Empty";
                    draggedItem.transform.parent.name  = draggedItem.ItemIns.Item.Name;
                    _inv.UpdateInventory();
                }
                //#################################### Stacking: Same items Stack them together
                else if (existingItem.ItemIns.Item.Id == draggedItem.ItemIns.Item.Id)
                {
                    if (existingItem.ItemIns.UserItem.StackCnt + draggedItem.ItemIns.UserItem.StackCnt > existingItem.ItemIns.Item.MaxStackCnt)
                    {
                        draggedItem.ItemIns.UserItem.StackCnt  = draggedItem.ItemIns.UserItem.StackCnt - (existingItem.ItemIns.Item.MaxStackCnt - existingItem.ItemIns.UserItem.StackCnt);
                        existingItem.ItemIns.UserItem.StackCnt = existingItem.ItemIns.Item.MaxStackCnt;
                    }
                    else
                    {
                        existingItem.ItemIns.UserItem.StackCnt = draggedItem.ItemIns.UserItem.StackCnt + existingItem.ItemIns.UserItem.StackCnt;
                        draggedItem.ItemIns.UserItem.StackCnt  = 0;
                    }
                    var stackCntText = existingItem.transform.GetComponentInChildren <TextMeshProUGUI>();
                    if (existingItem.ItemIns.UserItem.StackCnt > 1)
                    {
                        stackCntText.text = existingItem.ItemIns.UserItem.StackCnt.ToString();
                    }
                    _inv.UpdateInventory();
                }
                else
                {
                    Recipe newRecipe = _inv.CheckRecipes(existingItem.ItemIns.Item.Id, draggedItem.ItemIns.Item.Id);
                    //#################################### Mixing
                    if (newRecipe != null)
                    {
                        if (newRecipe.FirstItemCnt <= existingItem.ItemIns.UserItem.StackCnt)
                        {
                            if (newRecipe.SecondItemCnt <= draggedItem.ItemIns.UserItem.StackCnt)
                            {
                                if (_itemMixture.ItemIns != null)
                                {
                                    _inv.PrintMessage("You are already making an Item", Color.yellow);
                                }
                                else
                                {
                                    //Mixing items Logic (Lambda) can also be 2 func in it :  () => { InstantiateObject(thingToSpawn); InstantiateObject(thingToSpawn, thingToSpawn); }
                                    _messagePanelHandler.ShowMessage(newRecipe.GetDescription(), MessagePanel.PanelType.YesCancel, () => { MixItemData(ref existingItem, ref draggedItem, newRecipe); });
                                }
                            }
                            else //Not enough materials
                            {
                                _inv.PrintMessage("Not enough " + draggedItem.ItemIns.Item.Name +
                                                  " in the inventory, You need " +
                                                  (newRecipe.SecondItemCnt - draggedItem.ItemIns.UserItem.StackCnt) + " more", Color.yellow);
                            }
                        }
                        else //Not enough materials
                        {
                            _inv.PrintMessage("Not enough " + existingItem.ItemIns.Item.Name + " in the inventory, You need " +
                                              (newRecipe.FirstItemCnt - existingItem.ItemIns.UserItem.StackCnt) + " more", Color.yellow);
                        }
                    }
                    //#################################### Swaping: Unmixable items Stack them together
                    else
                    {
                        //Swap Parent
                        draggedItem.transform.SetParent(existingItem.transform.parent);
                        existingItem.transform.SetParent(draggedItem.Parent);
                        draggedItem.Parent = null;
                        //Swap positions
                        existingItem.transform.position = existingItem.transform.parent.position;
                        draggedItem.transform.position  = draggedItem.transform.parent.position;
                        //Swap SlotIndex
                        var temp2 = existingItem.SlotIndex;
                        existingItem.ItemIns.UserItem.Order = existingItem.SlotIndex = draggedItem.SlotIndex;
                        draggedItem.ItemIns.UserItem.Order  = draggedItem.SlotIndex = temp2;
                        //Swapping names
                        existingItem.transform.parent.name = existingItem.ItemIns.Item.Name;
                        draggedItem.transform.parent.name  = draggedItem.ItemIns.Item.Name;

                        _inv.UpdateInventory();
                    }
                }
            }
        }
    }
Exemplo n.º 5
0
 void Start()
 {
     _inv                 = InventoryHandler.Instance();
     _itemMixture         = ItemMixture.Instance();
     _messagePanelHandler = MessagePanelHandler.Instance();
 }
Exemplo n.º 6
0
    // Use this for initialization
    void Start()
    {
        Debug.Log("***IH*** Start!");
        //Instance
        _inv              = InventoryHandler.Instance();
        _itemDatabase     = ItemDatabase.Instance();
        _characterManager = CharacterManager.Instance();
        _inventoryManager = InventoryManager.Instance();
        _itemMixture      = ItemMixture.Instance();
        _researchingSlot  = ResearchSlot.Instance();
        _GUIManager       = GUIManager.Instance();

        _inventoryPanel = GameObject.Find("Inventory Panel");
        _slotPanel      = _inventoryPanel.transform.Find("Slot Panel").gameObject;

        _inventorySlotBroken = Resources.Load <GameObject>("Prefabs/SlotInventoryBroken");
        _inventorySlot       = Resources.Load <GameObject>("Prefabs/SlotInventory");
        _inventoryItem       = Resources.Load <GameObject>("Prefabs/Item");

        //Disable All buttons inside building
        var insideBuilding = GameObject.Find("Building Interior");

        if (insideBuilding != null)
        {
            _inTerrain = false;
            GameObject.Find("ButtonShop").GetComponent <Button>().interactable    = false;
            GameObject.Find("ButtonSetting").GetComponent <Button>().interactable = false;
            GameObject.Find("ButtonAbout").GetComponent <Button>().interactable   = false;
            GameObject.Find("Item Mixture").GetComponent <Button>().interactable  = false;
            GameObject.Find("Research Slot").GetComponent <Button>().interactable = false;
            GameObject.Find("PlayerPic").GetComponent <Button>().interactable     = false;
            GameObject.Find("CharacterPic").GetComponent <Button>().interactable  = false;
        }
        _playerSlots   = _characterManager.CharacterSetting.CarryCnt;
        _userInventory = _inventoryManager.UserInvItems;
        //Debug.Log("IH-UserInventory.Count = " + _userInventory.Count);
        //foreach (var item in _userInventory) item.Print();
        //Equipment
        EquiSlots = _inventoryPanel.GetComponentsInChildren <SlotEquipment>();
        for (int i = 0; i < EquiSlots.Length; i++)
        {
            EquiSlots[i].name = "Slot " + EquiSlots[i].EquType;
            EquiSlots[i].GetComponentInChildren <TextMeshProUGUI>().text = EquiSlots[i].EquType.ToString();
            ItemEquipment equipmentItem = EquiSlots[i].GetComponentInChildren <ItemEquipment>();
            equipmentItem.ItemIns = null;
            equipmentItem.name    = "Empty";
            foreach (var equipmentIns in _userInventory)
            {
                if (!equipmentIns.UserItem.Equipped)
                {
                    continue;
                }
                if (((equipmentIns.Item.Type == ItemContainer.ItemType.Weapon || equipmentIns.Item.Type == ItemContainer.ItemType.Tool)
                     &&
                     ((EquiSlots[i].EquType == ItemContainer.PlaceType.Right && equipmentIns.UserItem.Order == (int)ItemContainer.PlaceType.Right) ||
                      (EquiSlots[i].EquType == ItemContainer.PlaceType.Left && equipmentIns.UserItem.Order == (int)ItemContainer.PlaceType.Left)))
                    ||
                    (equipmentIns.Item.PlaceHolder == EquiSlots[i].EquType && equipmentIns.Item.Type == ItemContainer.ItemType.Equipment)
                    )
                {
                    equipmentItem.ItemIns = equipmentIns;
                    equipmentItem.name    = equipmentIns.Item.Name;
                    equipmentItem.GetComponent <Image>().sprite = equipmentIns.Item.GetSprite();
                    break;
                }
            }
        }
        //Item Mixture
        InitMixture(_characterManager.CharacterMixture);
        //Researching
        InitResearching(_characterManager.CharacterResearching);
        //Inventory
        for (int i = 0; i < _slotAmount; i++)
        {
            if (i < _playerSlots)
            {
                InvSlots.Add(Instantiate(_inventorySlot));
                InvSlots[i].GetComponent <SlotData>().SlotIndex = i;
            }
            else
            {
                InvSlots.Add(Instantiate(_inventorySlotBroken));
            }
            InvSlots[i].transform.SetParent(_slotPanel.transform);
            if (i < _playerSlots)
            {
                GameObject itemObject = Instantiate(_inventoryItem);
                ItemData   data       = itemObject.GetComponent <ItemData>();
                itemObject.transform.SetParent(InvSlots[i].transform);
                data.SlotIndex = i;

                foreach (var itemIns in _userInventory)
                {
                    if (itemIns.UserItem.Equipped || itemIns.UserItem.Stored)
                    {
                        continue;
                    }
                    if (itemIns.UserItem.Order == i)
                    {
                        data.ItemIns = itemIns;
                        itemObject.transform.position = Vector2.zero;
                        InvSlots[i].name = itemObject.name = itemIns.Item.Name;
                        itemObject.GetComponent <Image>().sprite = itemIns.Item.GetSprite();
                        itemObject.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = itemIns.UserItem.StackCnt > 1 ? itemIns.UserItem.ToString() : "";
                        break;
                    }
                }
            }
            else
            {
                if (i == _playerSlots)
                {
                    Button button = InvSlots[i].GetComponentInChildren <Button>();
                    button.GetComponent <Image>().sprite = LockSprite;
                    InvSlots[i].name = button.name = "Lock";
                    if (_inTerrain)
                    {
                        button.interactable = true;
                    }
                }
            }
            InvSlots[i].transform.localScale = Vector3.one;
        }
        _inventoryManager.PrintInventory();
    }