Exemplo n.º 1
0
    public bool EquipInteraction(InteractionType.Type type, GameObject interactor)
    {
        bool success = false;

        if (type == InteractionType.Type.pickableWeapon)
        {
            WeaponItem item = interactor.GetComponent <WeaponItem>();
            if (item && playerController.weapon.Equip(item.type))
            {
                if (item.forbidSecond || playerController.secondHand.equipedItem.forbidWeapon)
                {
                    if (playerController.secondHand.equipedItem.type != SecondItem.Type.None)
                    {
                        inventory.AddItem(playerController.secondHand.equipedItem.Summarize(), 1);
                    }
                    playerController.secondHand.Equip(SecondItem.Type.None);
                }
                if (item.forbidShield)
                {
                    if (playerController.shield.equipedItem.type != ShieldItem.Type.None)
                    {
                        inventory.AddItem(playerController.shield.equipedItem.Summarize(), 1);
                    }
                    playerController.shield.Equip(ShieldItem.Type.None);
                }
                success = true;
                playerController.needEquipementAnimationUpdate = true;

                if (ToolDictionary.instance.tools.ContainsKey(playerController.weapon.equipedItem.toolFamily))
                {
                    List <AudioClip> sounds = ToolDictionary.instance.tools[playerController.weapon.equipedItem.toolFamily].collectionSound;
                    audiosource.clip = sounds[Random.Range(0, sounds.Count)];
                    audiosource.Play();
                }
            }
        }
        else if (type == InteractionType.Type.pickableBackpack)
        {
            BackpackItem item = interactor.GetComponent <BackpackItem>();
            if (item && playerController.backpack.Equip(item.type))
            {
                success = true;
            }
            playerController.needEquipementAnimationUpdate = true;

            if (success)
            {
                inventory.capacity = item.capacity;
            }

            if (ToolDictionary.instance.tools.ContainsKey(playerController.backpack.equipedItem.toolFamily))
            {
                List <AudioClip> sounds = ToolDictionary.instance.tools[playerController.backpack.equipedItem.toolFamily].collectionSound;
                audiosource.clip = sounds[Random.Range(0, sounds.Count)];
                audiosource.Play();
            }
        }
        else if (type == InteractionType.Type.pickableHead)
        {
            HeadItem item = interactor.GetComponent <HeadItem>();
            if (item && playerController.head.Equip(item.type))
            {
                success = true;
                playerController.needEquipementAnimationUpdate = true;
                int index = Mathf.Clamp((int)HeadItem.getCategory(playerController.head.equipedItem.type), 0, wearHead.Count - 1);
                audiosource.clip = wearHead[index];
                audiosource.Play();
            }
        }
        else if (type == InteractionType.Type.pickableSecond)
        {
            SecondItem item = interactor.GetComponent <SecondItem>();
            if (item && playerController.secondHand.Equip(item.type))
            {
                if (item.forbidWeapon || playerController.weapon.equipedItem.forbidSecond)
                {
                    if (playerController.weapon.equipedItem.type != WeaponItem.Type.None)
                    {
                        inventory.AddItem(playerController.weapon.equipedItem.Summarize(), 1);
                    }
                    playerController.weapon.Equip(WeaponItem.Type.None);
                }
                if (item.forbidShield)
                {
                    if (playerController.shield.equipedItem.type != ShieldItem.Type.None)
                    {
                        inventory.AddItem(playerController.shield.equipedItem.Summarize(), 1);
                    }
                    playerController.shield.Equip(ShieldItem.Type.None);
                }
                success = true;
                playerController.needEquipementAnimationUpdate = true;
            }
        }
        else if (type == InteractionType.Type.pickableShield)
        {
            ShieldItem item = interactor.GetComponent <ShieldItem>();
            if (item && playerController.shield.Equip(item.type))
            {
                if (playerController.weapon.equipedItem.forbidShield)
                {
                    if (playerController.weapon.equipedItem.type != WeaponItem.Type.None)
                    {
                        inventory.AddItem(playerController.weapon.equipedItem.Summarize(), 1);
                    }
                    playerController.weapon.Equip(WeaponItem.Type.None);
                }
                if (playerController.secondHand.equipedItem.forbidShield)
                {
                    if (playerController.secondHand.equipedItem.type != SecondItem.Type.None)
                    {
                        inventory.AddItem(playerController.secondHand.equipedItem.Summarize(), 1);
                    }
                    playerController.secondHand.Equip(SecondItem.Type.None);
                }
                success = true;
                playerController.needEquipementAnimationUpdate = true;
            }
        }
        else if (type == InteractionType.Type.pickableBody)
        {
            BodyItem item    = interactor.GetComponent <BodyItem>();
            bool     mounted = playerController.horse ? playerController.horse.equipedItem.type != HorseItem.Type.None : false;
            if (item && playerController.body.Equip(item.type, mounted))
            {
                success = true;
                playerController.needEquipementAnimationUpdate = true;
                int index = Mathf.Clamp((int)BodyItem.getCategory(playerController.body.equipedItem.type), 0, wearBody.Count - 1);
                audiosource.clip = wearBody[index];
                audiosource.Play();
            }
        }
        else if (type == InteractionType.Type.pickableHorse)
        {
            HorseItem item = interactor.GetComponent <HorseItem>();
            if ((playerController.horse && item.type == HorseItem.Type.None) || (!playerController.horse && item.type != HorseItem.Type.None))
            {
                // change player template
                PlayerController playerTemplate;
                if (item.type == HorseItem.Type.None)
                {
                    playerTemplate = Arsenal.Instance.playerTemplate;
                }
                else
                {
                    playerTemplate = Arsenal.Instance.mountedPlayerTemplate;
                }
                PlayerController destination = Instantiate <PlayerController>(playerTemplate);
                destination.gameObject.name = playerTemplate.gameObject.name;

                // copy
                PlayerController.MainInstance = destination;
                PlayerController.Copy(playerController, destination);
                MapStreaming.instance.focusAgent = destination.transform;
                ConstructionSystem.instance.tpsController.target = destination.transform.Find("CameraTarget");
                InteractionUI.instance.juicer = destination.interactionController.interactionJuicer;

                destination.interactionController.PickableInteraction(type, interactor);
                interactionJuicer.OnDelete();
                Destroy(gameObject);
            }
            else
            {
                if (playerController.horse && item && playerController.horse.Equip(item.type))
                {
                    success = true;
                    playerController.needEquipementAnimationUpdate = true;
                    int index = Mathf.Clamp((int)BodyItem.getCategory(playerController.body.equipedItem.type), 0, wearBody.Count - 1);
                    audiosource.clip = wearBody[index];
                    audiosource.Play();

                    bool mounted = playerController.horse ? playerController.horse.equipedItem.type != HorseItem.Type.None : false;
                    playerController.body.Equip(playerController.body.equipedItem.type, mounted, true);
                    inventory.onUpdateContent.Invoke();
                }
            }
        }

        if (success)
        {
            playerController.RecomputeLoadFactor();
        }
        return(success);
    }
Exemplo n.º 2
0
    public void OnIconClick(InventoryIcon icon)
    {
        Item item = GetItem(icon.item);

        if (!item)
        {
            return;
        }

        if (icon.item.itemType == Item.ItemType.Resource && interactionUI.resourceContainer != null)
        {
            ResourceItem resource = (ResourceItem)item;
            if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
            {
                string resourceName   = resource.resource.name;
                int    transfertCount = Input.GetMouseButtonUp(0) ? 1 : 10;
                transfertCount = Mathf.Min(transfertCount, inventory.inventory[icon.item]);
                transfertCount = interactionUI.resourceContainer.TryAddItem(resourceName, transfertCount);

                if (transfertCount > 0)
                {
                    inventory.RemoveItem(icon.item, transfertCount, true);
                }
                else if (interactionUI.resourceContainer.Accept(resourceName))
                {
                    PlayerController.MainInstance.interactionController.ThrowHelp("Container", "full");  // no icon for storage
                }
                else
                {
                    PlayerController.MainInstance.interactionController.ThrowHelp(resourceName, "nok");
                }
            }
        }
        else
        {
            if (item.usable && Input.GetMouseButtonUp(0))
            {
                Debug.Log("Use item " + icon.name);
                inventory.RemoveItem(icon.item, 1, true);
                PlayerController.MainInstance.RecomputeLoadFactor();
            }
            else if (item.equipable && Input.GetMouseButtonUp(1))
            {
                PlayerController player = PlayerController.MainInstance;

                if (IsEquipementIcon(icon)) // unequip
                {
                    if (icon.item.itemType == Item.ItemType.Weapon)
                    {
                        if (player.weapon.equipedItem.type != WeaponItem.Type.None)
                        {
                            Item old = Arsenal.Instance.Get(player.weapon.equipedItem.type, false);
                            if (old)
                            {
                                inventory.AddItem(old.Summarize(), 1);
                            }
                            if (ToolDictionary.instance.tools.ContainsKey(player.weapon.equipedItem.toolFamily))
                            {
                                FinishedUnequip(player, GetRandom(ToolDictionary.instance.tools[player.weapon.equipedItem.toolFamily].collectionSound));
                            }
                            player.weapon.Equip(WeaponItem.Type.None);
                        }
                    }
                    else if (icon.item.itemType == Item.ItemType.Backpack)
                    {
                        if (player.backpack.equipedItem.type != BackpackItem.Type.None)
                        {
                            Item old = Arsenal.Instance.Get(player.backpack.equipedItem.type, false);
                            if (old)
                            {
                                inventory.AddItem(old.Summarize(), 1);
                            }
                            if (ToolDictionary.instance.tools.ContainsKey(player.backpack.equipedItem.toolFamily))
                            {
                                FinishedUnequip(player, GetRandom(ToolDictionary.instance.tools[player.backpack.equipedItem.toolFamily].collectionSound));
                            }
                            player.backpack.Equip(BackpackItem.Type.None);
                            player.inventory.capacity = player.backpack.equipedItem.capacity;
                        }
                    }
                    else if (icon.item.itemType == Item.ItemType.Body)
                    {
                        if (!player.body.equipedItem.IsDefault())
                        {
                            Item old = Arsenal.Instance.Get(player.body.equipedItem.type, false, false);
                            if (old)
                            {
                                inventory.AddItem(old.Summarize(), 1);
                            }
                            FinishedUnequip(player, player.interactionController.wearBody[Mathf.Clamp((int)BodyItem.getCategory(player.body.equipedItem.type), 0, player.interactionController.wearBody.Count - 1)]);
                            player.body.Equip(BodyItem.defaultType, player.horse ? player.horse.equipedItem.type != HorseItem.Type.None : false);
                        }
                    }
                    else if (icon.item.itemType == Item.ItemType.Head)
                    {
                        if (!player.head.equipedItem.IsDefault())
                        {
                            Item old = Arsenal.Instance.Get(player.head.equipedItem.type, false);
                            if (old)
                            {
                                inventory.AddItem(old.Summarize(), 1);
                            }
                            FinishedUnequip(player, player.interactionController.wearHead[Mathf.Clamp((int)HeadItem.getCategory(player.head.equipedItem.type), 0, player.interactionController.wearHead.Count - 1)]);
                            player.head.Equip(HeadItem.defaultType);
                        }
                    }
                    else if (icon.item.itemType == Item.ItemType.Horse)
                    {
                        if (player.horse && player.horse.equipedItem.type != HorseItem.Type.None)
                        {
                            Item old = Arsenal.Instance.Get(player.horse.equipedItem.type, false);
                            if (old)
                            {
                                GameObject dropped = InstanciatePickable(icon.item);
                                dropped.transform.position = player.transform.position + Vector3.right;
                                MapModifier.instance.grid.AddGameObject(dropped, ConstructionLayer.LayerType.Decoration, false, false);
                            }
                            player.interactionController.Unmount();
                        }
                    }
                    else if (icon.item.itemType == Item.ItemType.Second)
                    {
                        if (player.secondHand.equipedItem.type != SecondItem.Type.None)
                        {
                            Item old = Arsenal.Instance.Get(player.secondHand.equipedItem.type, false);
                            if (old)
                            {
                                inventory.AddItem(old.Summarize(), 1);
                            }
                            FinishedUnequip(player, GetRandom(ToolDictionary.instance.tools["Hammer"].collectionSound));
                            player.secondHand.Equip(SecondItem.Type.None);
                        }
                    }
                    else if (icon.item.itemType == Item.ItemType.Shield)
                    {
                        if (player.shield.equipedItem.type != ShieldItem.Type.None)
                        {
                            Item old = Arsenal.Instance.Get(player.shield.equipedItem.type, false);
                            if (old)
                            {
                                inventory.AddItem(old.Summarize(), 1);
                            }
                            FinishedUnequip(player, player.interactionController.wearHead[Mathf.Clamp((int)HeadItem.Category.Medium, 0, player.interactionController.wearHead.Count - 1)]);
                            player.shield.Equip(ShieldItem.Type.None);
                        }
                    }
                    else
                    {
                        Debug.LogWarning("No equip methode for this type of object " + icon.item.itemType.ToString());
                    }
                }
                else // equip
                {
                    if (icon.item.itemType == Item.ItemType.Weapon)
                    {
                        Item old = Arsenal.Instance.Get(player.weapon.equipedItem.type, false);
                        if (old)
                        {
                            inventory.AddItem(old.Summarize(), 1);
                        }
                        player.interactionController.EquipInteraction(InteractionType.Type.pickableWeapon, item.gameObject);
                    }
                    else if (icon.item.itemType == Item.ItemType.Backpack)
                    {
                        Item old = Arsenal.Instance.Get(player.backpack.equipedItem.type, false);
                        if (old)
                        {
                            inventory.AddItem(old.Summarize(), 1);
                        }
                        player.interactionController.EquipInteraction(InteractionType.Type.pickableBackpack, item.gameObject);
                    }
                    else if (icon.item.itemType == Item.ItemType.Body)
                    {
                        Item old = Arsenal.Instance.Get(player.body.equipedItem.type, false, false);
                        if (old && !player.body.equipedItem.IsDefault())
                        {
                            inventory.AddItem(old.Summarize(), 1);
                        }
                        player.interactionController.EquipInteraction(InteractionType.Type.pickableBody, item.gameObject);
                    }
                    else if (icon.item.itemType == Item.ItemType.Head)
                    {
                        Item old = Arsenal.Instance.Get(player.head.equipedItem.type, false);
                        if (old && !player.head.equipedItem.IsDefault())
                        {
                            inventory.AddItem(old.Summarize(), 1);
                        }
                        player.interactionController.EquipInteraction(InteractionType.Type.pickableHead, item.gameObject);
                    }
                    else if (icon.item.itemType == Item.ItemType.Horse)
                    {
                        Item old = Arsenal.Instance.Get(player.horse.equipedItem.type, false);
                        if (old)
                        {
                            inventory.AddItem(old.Summarize(), 1);
                        }
                        player.interactionController.EquipInteraction(InteractionType.Type.pickableHorse, item.gameObject);
                    }
                    else if (icon.item.itemType == Item.ItemType.Second)
                    {
                        Item old = Arsenal.Instance.Get(player.secondHand.equipedItem.type, false);
                        if (old)
                        {
                            inventory.AddItem(old.Summarize(), 1);
                        }
                        player.interactionController.EquipInteraction(InteractionType.Type.pickableSecond, item.gameObject);
                    }
                    else if (icon.item.itemType == Item.ItemType.Shield)
                    {
                        Item old = Arsenal.Instance.Get(player.shield.equipedItem.type, false);
                        if (old)
                        {
                            inventory.AddItem(old.Summarize(), 1);
                        }
                        player.interactionController.EquipInteraction(InteractionType.Type.pickableShield, item.gameObject);
                    }
                    else
                    {
                        Debug.LogWarning("No equip methode for this type of object " + icon.item.itemType.ToString());
                    }
                    inventory.RemoveItem(icon.item, 1, true);
                }

                player.RecomputeLoadFactor();
            }
        }
    }