Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
3.9 kB
1
Indexable
Never
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EquipmentUI : MonoBehaviour {

    public static EquipmentUI _instance;
    [HideInInspector] public Canvas canvas;
    private EquipmentPanelElement equipmentPanel;
    private InventoryPanelElement inventoryPanel;


    public Color acceptableSlotHighlightColor;
    public Color grantedSlotHighlightColor;

    public Color deniedSlotHighlightColor;
    public Color grantedButClassRestrictedSlotHighlightColor;

    public GameObject itemElementPrefab;

    private ItemElement selectedItem;
    private SlotElement selectedSlot;
    private ItemElement draggedItem;

    private Character character;

    private void Awake() {
        _instance = this;
        canvas = GetComponent<Canvas>();
        equipmentPanel = GetComponentInChildren<EquipmentPanelElement>();
        inventoryPanel = GetComponentInChildren<InventoryPanelElement>();
    }


    public void SetupForCharacter(Character character) {
        this.character = character;
        equipmentPanel.UpdateSlotsForCharacter(character);
    }

    internal void SetSlotSelection(SlotElement slotElement) {
        selectedSlot = slotElement;
    }

    internal void ClearSlotSelection() {
        selectedSlot = null;
    }

    internal void BeginDragOnItem(ItemElement itemElement) {
        if (itemElement.transform.parent.GetComponent<SlotElement>()) {
            equipmentPanel.HighlightSlotsForItem(itemElement.data, character);
        }
        draggedItem = itemElement;
    }

    // TODO: Chop this abomination up
    internal void EndDragOnItem(ItemElement itemElement) {
        if (selectedSlot == null || !selectedSlot.AcceptsItem(itemElement.data)) {
            character.UnequipItem(itemElement.data);
            inventoryPanel.MoveItemElementToInventory(itemElement);
        } else if (selectedSlot.AcceptsItem(itemElement.data)) {
            if (selectedSlot.HasItemElement()) {
                ItemElement ie = selectedSlot.GetItemElement();
                if (ie == selectedItem) {
                    selectedSlot.PlaceItemElementInSlot(ie);
                    return;
                }
                character.UnequipItem(ie.data);
                ie.transform.SetParent(inventoryPanel.transform);
            }
            selectedSlot.PlaceItemElementInSlot(itemElement);
            character.EquipItem(itemElement.data);
            ClearSlotSelection();
        }
        draggedItem = null;
        selectedItem = null;
        equipmentPanel.UpdateSlotsForCharacter(character);
        inventoryPanel.MoveItemElementsToInventory(equipmentPanel.ItemElementsInDisabledSlots());
        equipmentPanel.RemoveSlotHighlights();
    }

    internal void PointerEnterOnItem(ItemElement itemElement) {
        if (draggedItem != null) return;
        selectedItem = itemElement;
        equipmentPanel.HighlightSlotsForItem(itemElement.data, character);
    }

    internal void PointerExitOnItem(ItemElement itemElement) {
        if (draggedItem != null) return;
        equipmentPanel.RemoveSlotHighlights();
        equipmentPanel.UpdateSlotsForCharacter(character);
    }

    private ItemElement InstantiateItemElement(ItemData itemData) {
        GameObject o = Instantiate(itemElementPrefab);
        ItemElement ie = o.GetComponent<ItemElement>();
        ie.data = itemData;
        return ie;
    }

    public void AddItemToInventory(ItemData itemData) {
        ItemElement ie = InstantiateItemElement(itemData);
        inventoryPanel.MoveItemElementToInventory(ie);
    }

    internal void AddItemsToInventory(ItemData[] startingItems) {
        foreach(ItemData id in startingItems) {
            AddItemToInventory(id);
        }
    }
}