Untitled

 avatar
unknown
csharp
a year ago
14 kB
7
Indexable
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public enum SlotType
{
    Body = 0,
    Weapon = 1,
    All = 2,
    Skill = 3,
    Consumable = 4
}

public enum SlotItemPosition
{
    Left,
    Right
}

public class ItemSlot : MonoBehaviour, IDropHandler
{

    public SlotType slotType;
    public SlotItemPosition slotItemPosition;
    public BodyPart acceptedPart;

    public bool ItemCompatibleWithSlot(Item itemBeingDragged, Item itemInDroppedSlot, ItemSlot droppedSlot, ItemSlot originSlot, SlotType originSlotType)
    {
        var result = false;

        //If type of item being dragged is equals to the type of slot (Weapon / Weapon, Consumable / Consumable...)
        if(itemBeingDragged == null || (itemBeingDragged.GetType().ToString() == droppedSlot.slotType.ToString()))
        {
            result = true;

            if(itemBeingDragged is Body itemBeingDraggedBody)
            {
                result = false;
                if(itemBeingDraggedBody.bodyPart.ToString() == droppedSlot.acceptedPart.ToString())
                {
                    result = true;
                }
            }
        }
        else
        //If type of item being dragged is NOT equals to the type of slot, check rules (Weapon / Consumable, Weapon / All...)
        {
            var itemBeingDraggedType = itemBeingDragged.GetType().BaseType.ToString();
            var droppedSlotType = droppedSlot.slotType.ToString();
            var itemInDroppedSlotType = "";
            if (itemInDroppedSlot != null) itemInDroppedSlotType = itemInDroppedSlot.GetType().ToString();

            //item being dragged is weapon and dropped into consumable slot
            //item being dragged is consumable and dropped into  weapon slot
            //item in dropped slot is not null and item being dragged is consumable and item in dropped slot is weapon
            //item in dropped slot is not null and item being dragged is weapon and item in dropped slot is consumable
            //item in dropped slot is not null and item being dragged is equals to type of item in dropped slot
            //item in dropped slot is null and slot accepts everything
            //slot type is the same as item type


            //If there is no item on the dropped slot
            if(itemInDroppedSlot == null)
            {
                //If the dropped slot accepts all
                if(droppedSlot.slotType == SlotType.All)
                {
                    result = true;
                }

                //If we have a weapon and were trying to drop into a consumable slot
                if(itemBeingDragged is Weapon && droppedSlot.slotType == SlotType.Consumable)
                {
                    result = true;
                }

                //If the item being dragged and item on dropped slot is body
                if (itemBeingDragged is Body itemBeingDraggedBody && itemInDroppedSlot is Body itemInDroppedSlotBody)
                {
                    result = false;
                    //if its the same bodypart, accepts it
                    if (itemBeingDraggedBody.bodyPart.ToString() == itemInDroppedSlotBody.bodyPart.ToString())
                    {
                        result = true;
                    }
                }

                //If we have a consumable and were trying to drop into a consumable slot
                if (itemBeingDragged is Consumable itemBeingDraggedConsumable && droppedSlot.slotType == SlotType.Consumable)
                {
                    result = true;
                }
            }
            else
            //If there IS item on the dropped slot
            {
                //If we have a weapon and there is a weapon on the dropped slot
                if(itemBeingDragged is Weapon itemBeingDraggedWeapon && itemInDroppedSlot is Weapon itemInDroppedSlotWeapon)
                {
                    result = true;
                }

                //If we have a body and there is a body on the dropped slot
                if (itemBeingDragged is Body itemBeingDraggedBody && itemInDroppedSlot is Body itemInDroppedSlotBody)
                {
                    result = false;
                    //if its the same bodypart, accepts it
                    if (itemBeingDraggedBody.bodyPart.ToString() == itemInDroppedSlotBody.bodyPart.ToString())
                    {
                        result = true;
                    }
                }

                //If we have a consumable and there is a consumable on the dropped slot
                if(itemBeingDragged is Consumable itemBeingDraggedConsumable && itemInDroppedSlot is Consumable itemInDroppedSlotConsumable)
                {
                    result = true;
                }

                //If we have a consumable and there is a weapon on the dropped slot
                if(itemBeingDragged is Consumable itemBeingDraggedConsumable2 && itemInDroppedSlot is Weapon itemInDroppedSlotWeapon2)
                {
                    //If the dropped slot accepts consumable items
                    if(droppedSlot.slotType == SlotType.Consumable || droppedSlot.slotType == SlotType.All)
                    {
                        result = true;
                    }
                }

                //If we have a weapon and there is a consumable on the slot
                if(itemBeingDragged is Weapon itemBeingDraggedWeapon2 && itemInDroppedSlot is Consumable itemInDroppedSlotConsumable2)
                {
                    if(originSlotType == SlotType.Consumable || originSlotType == SlotType.All)
                    {
                        result = true;
                    }
                }
            }
        }

        //se o slot dropado for do gladiador e o jogador não tiver gladiador ou o gladiador não estiver possibilitado de receber itens(viajando...)
        var droppedSlotParentGameObject = droppedSlot.transform.parent.gameObject;
        if (GladiatorHUDManager.Instance.gladiatorEquippedSlot.Find(x => x == droppedSlotParentGameObject)
            || GladiatorHUDManager.Instance.gladiatorWeaponsSlot.Find(x => x == droppedSlotParentGameObject))
        {

            if(GladiatorManager.Instance.gladiatorList.gladiators.Count <= 0 || GladiatorManager.Instance.CurrentGladiator.travellingData.isTravelling || GladiatorManager.Instance.CurrentGladiator.availabilityData.isInQuest || GladiatorManager.Instance.CurrentGladiator.availabilityData.isInContract)
                result = false;
        }

        //se estiver puxando do slot do gladiador e não tiver gladiador ou o gladiador estiver impossibilitado de receber items(viajando...)
        var originSlotParentGameObject = originSlot.transform.parent.gameObject;
        if (GladiatorHUDManager.Instance.gladiatorEquippedSlot.Find(x => x == originSlotParentGameObject)
            || GladiatorHUDManager.Instance.gladiatorWeaponsSlot.Find(x => x == originSlotParentGameObject))
        {
            if (GladiatorManager.Instance.gladiatorList.gladiators.Count <= 0 || GladiatorManager.Instance.CurrentGladiator.travellingData.isTravelling || GladiatorManager.Instance.CurrentGladiator.availabilityData.isInQuest || GladiatorManager.Instance.CurrentGladiator.availabilityData.isInContract)
                result = false;
        }

        return result;
    }

    public void OnDrop(PointerEventData eventData)
    {
        var originDragDrop = eventData.pointerDrag.GetComponent<ItemDragDrop>();
        var droppedDragDrop = transform.parent.GetComponent<ItemDragDrop>();

        var itemBeingDragged = originDragDrop.itemInDrag;
        var itemBeingDraggedType = itemBeingDragged != null ? itemBeingDragged.GetType() : null;

        var itemInDroppedSlotType = droppedDragDrop.item != null ? droppedDragDrop.item.GetType() : null;
        var itemInOriginSlotType = originDragDrop.item != null ? originDragDrop.item.GetType() : null;

        var originSlotType = originDragDrop.inventorySlot.GetComponent<ItemSlot>().slotType;
        var droppedSlotType = slotType;

        var originSlot = originDragDrop.inventorySlot.GetComponent<ItemSlot>();
        var droppedSlot = this;

        //se estiver arrastando um item vazio, ou se a origem e o destino for iguais
        if(itemBeingDragged == null || originSlot == droppedSlot){
            return;
        }

        DropItemToSlot(itemBeingDragged, originSlotType, droppedSlotType, droppedDragDrop, originDragDrop);
        
    }

    public void DropItemToSlot(Item itemBeingDragged, SlotType originSlotType, SlotType droppedSlotType, ItemDragDrop droppedDragDrop, ItemDragDrop originDragDrop)
    {
        if(!ItemCompatibleWithSlot(itemBeingDragged, droppedDragDrop.item, this, originDragDrop.GetComponentInChildren<ItemSlot>(), originSlotType)) return;

        //se dropou o item no slot All, e o slot de origem não é all
        if(droppedSlotType == SlotType.All && originSlotType != SlotType.All)
        {
            //se o item no slot dropado for nulo só adiciona, se não adiciona e remove
            if(droppedDragDrop.item == null){
                LudusManager.Instance.inventory.items.Add(itemBeingDragged);
            }else{
                LudusManager.Instance.inventory.items.Add(itemBeingDragged);
                LudusManager.Instance.inventory.items.Remove(droppedDragDrop.item);
            }
        }

        //se dropou o item em algum slot que não seja do tipo All e o slot de origem é do tipo all
        if(droppedSlotType != SlotType.All && originSlotType == SlotType.All)
        {
            //se o item no slot dropado for nulo só remove, se não remove e adiciona
            if(droppedDragDrop.item == null)
            {
                LudusManager.Instance.inventory.items.Remove(itemBeingDragged);
            }else{
                LudusManager.Instance.inventory.items.Remove(itemBeingDragged);
                LudusManager.Instance.inventory.items.Add(droppedDragDrop.item);
            }
        }

        originDragDrop.item = droppedDragDrop.item;
        droppedDragDrop.item = itemBeingDragged;

        droppedDragDrop.itemInDrag = null;
        originDragDrop.itemInDrag = null;

        originDragDrop.droppedInSlot = true;

        

        //print($"{originDragDrop.item} - {droppedDragDrop.item} - {originSlotType} - {droppedSlotType}");

        droppedDragDrop.RefreshItemImage();
        originDragDrop.RefreshItemImage();


        //Refaz os items que o gladiador está usando baseando-se nos slots
        var currentGladiator = GladiatorManager.Instance.CurrentGladiator;
        if (currentGladiator != null)
        {
            currentGladiator.ClearBodies();
            currentGladiator.equippedBody.helmet = GladiatorHUDManager.Instance.gladiatorEquippedSlot[1].GetComponent<ItemDragDrop>().item == null ? null : GladiatorHUDManager.Instance.gladiatorEquippedSlot[1].GetComponent<ItemDragDrop>().item as Body;
            currentGladiator.equippedBody.leftShoulder = GladiatorHUDManager.Instance.gladiatorEquippedSlot[2].GetComponent<ItemDragDrop>().item == null ? null : GladiatorHUDManager.Instance.gladiatorEquippedSlot[2].GetComponent<ItemDragDrop>().item as Body;
            currentGladiator.equippedBody.rightShoulder = GladiatorHUDManager.Instance.gladiatorEquippedSlot[0].GetComponent<ItemDragDrop>().item == null ? null : GladiatorHUDManager.Instance.gladiatorEquippedSlot[0].GetComponent<ItemDragDrop>().item as Body;
            currentGladiator.equippedBody.armor = GladiatorHUDManager.Instance.gladiatorEquippedSlot[4].GetComponent<ItemDragDrop>().item == null ? null : GladiatorHUDManager.Instance.gladiatorEquippedSlot[4].GetComponent<ItemDragDrop>().item as Body;
            currentGladiator.equippedBody.leftFeet = GladiatorHUDManager.Instance.gladiatorEquippedSlot[5].GetComponent<ItemDragDrop>().item == null ? null : GladiatorHUDManager.Instance.gladiatorEquippedSlot[5].GetComponent<ItemDragDrop>().item as Body;
            currentGladiator.equippedBody.rightFeet = GladiatorHUDManager.Instance.gladiatorEquippedSlot[3].GetComponent<ItemDragDrop>().item == null ? null : GladiatorHUDManager.Instance.gladiatorEquippedSlot[3].GetComponent<ItemDragDrop>().item as Body;

            //loop pelos consumables e add no gladiador em si
            currentGladiator.equippedConsumables.items.Clear();
            currentGladiator.equippedWeapons.items.Clear();

            for (int i = 0; i < GladiatorHUDManager.Instance.gladiatorWeaponsSlot.Count; i++)
            {
                //if its the first weapon slot, add to the equipped weapons instead of consumables
                if (i == 0)
                {
                    if (GladiatorHUDManager.Instance.gladiatorWeaponsSlot[i].GetComponent<ItemDragDrop>().item is Weapon weaponToAdd)
                        currentGladiator.equippedWeapons.items.Add(weaponToAdd);
                }
                else
                {
                    if (GladiatorHUDManager.Instance.gladiatorWeaponsSlot[i].GetComponent<ItemDragDrop>().item is Weapon weaponToAdd)
                        currentGladiator.equippedConsumables.items.Add(weaponToAdd);

                    if (GladiatorHUDManager.Instance.gladiatorWeaponsSlot[i].GetComponent<ItemDragDrop>().item is Consumable consumableToAdd)
                        currentGladiator.equippedConsumables.items.Add(consumableToAdd);
                }
            }
        }


        
        LudusManager.Instance.GetComponent<InventoryLoader>().RefreshInventory();
        GladiatorHUDManager.Instance.RefreshTrainingEntities();
        GladiatorHUDManager.Instance.RefreshUIEntities();
        if (currentGladiator != null) GladiatorHUDManager.Instance.RefreshStatsTexts(currentGladiator);
    }
}
Editor is loading...
Leave a Comment