Advertisement
Rakshalpha

Character

Oct 14th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.12 KB | None | 0 0
  1. using UnityEngine;
  2. using Assets.Scripts.Stats;
  3. using UnityEngine.UI;
  4. using System.Collections;
  5. using System;
  6.  
  7. public class Character : MonoBehaviour
  8. {
  9.     #region Stat Parameters
  10.  
  11.     public int Health = 100;
  12.  
  13.     /// <summary>
  14.     /// Character Attributes
  15.     /// </summary>
  16.     public Stat Strength;
  17.     public Stat Agility;
  18.     public Stat Intelligence;
  19.     public Stat Vitality;
  20.  
  21.     /// <summary>
  22.     /// Defensive Scores
  23.     /// </summary>
  24.     public Stat Defense;
  25.     public Stat Evasion;
  26.  
  27.     /// <summary>
  28.     /// Offensive Scores
  29.     /// </summary>
  30.     public Stat Damage;
  31.     public Stat CriticalChance;
  32.  
  33.     #endregion
  34.  
  35.     [SerializeField] GameObject characterPanel;
  36.     [SerializeField] Inventory inventory;
  37.     [SerializeField] EquipmentManager equipmentPanel;
  38.     [SerializeField] CraftingWindow craftingWindow;
  39.     [SerializeField] StatPanel statPanel;
  40.     [SerializeField] ItemTooltip itemTooltip;
  41.     [SerializeField] Image draggableItem;
  42.  
  43.     [SerializeField] GameObject playerObject;
  44.  
  45.     public AbilitySystemHelper _ash;
  46.  
  47.     private BaseItemSlot dragItemSlot;
  48.  
  49.     private void OnValidate()
  50.     {
  51.         if (itemTooltip == null)
  52.             itemTooltip = FindObjectOfType<ItemTooltip>();
  53.     }
  54.  
  55.     private void Awake()
  56.     {
  57.         statPanel.SetStats(Strength, Agility, Intelligence, Vitality, Damage, CriticalChance, Defense, Evasion);
  58.         Initialize();
  59.         statPanel.UpdateStatValues();
  60.         _ash = GetComponentInChildren<AbilitySystemHelper>();
  61.  
  62.         // Setup Events:
  63.         // Right Click
  64.         inventory.OnRightClickEvent += InventoryRightClick;
  65.         equipmentPanel.OnRightClickEvent += EquipmentPanelRightClick;
  66.         // Pointer Enter
  67.         inventory.OnPointerEnterEvent += ShowTooltip;
  68.         equipmentPanel.OnPointerEnterEvent += ShowTooltip;
  69.         craftingWindow.OnPointerEnterEvent += ShowTooltip;
  70.         // Pointer Exit
  71.         inventory.OnPointerExitEvent += HideTooltip;
  72.         equipmentPanel.OnPointerExitEvent += HideTooltip;
  73.         craftingWindow.OnPointerExitEvent += HideTooltip;
  74.         // Begin Drag
  75.         inventory.OnBeginDragEvent += BeginDrag;
  76.         equipmentPanel.OnBeginDragEvent += BeginDrag;
  77.         // End Drag
  78.         inventory.OnEndDragEvent += EndDrag;
  79.         equipmentPanel.OnEndDragEvent += EndDrag;
  80.         // Drag
  81.         inventory.OnDragEvent += Drag;
  82.         equipmentPanel.OnDragEvent += Drag;
  83.         //Drop
  84.         inventory.OnDropEvent += Drop;
  85.         equipmentPanel.OnDropEvent += Drop;
  86.     }
  87.  
  88.     private void Update()
  89.     {
  90.         CheckCharacterPanelStatus();
  91.     }
  92.  
  93.     private void CheckCharacterPanelStatus()
  94.     {
  95.         CharacterController cntrl = playerObject.GetComponent<CharacterController>();
  96.         if (characterPanel.activeSelf)
  97.         {
  98.             cntrl.enabled = false;
  99.         }
  100.         else if (!characterPanel.activeSelf)
  101.         {
  102.             cntrl.enabled = true;
  103.         }
  104.     }
  105.  
  106.     private void InventoryRightClick(BaseItemSlot itemSlot)
  107.     {        
  108.         if (itemSlot.Item is EquippableItem)
  109.         {
  110.             var equipmentType = itemSlot.Item as EquippableItem;
  111.  
  112.             // Handle swapping of equipment here if the piece of equipment
  113.             // is the same type as an already equipped item in said slot
  114.  
  115.  
  116.             Equip((EquippableItem)itemSlot.Item);
  117.         }
  118.         else if (itemSlot.Item is UsableItem)
  119.         {
  120.             UsableItem usableItem = (UsableItem)itemSlot.Item;
  121.             usableItem.Use(this);
  122.  
  123.             if (usableItem.IsConsumable)
  124.             {
  125.                 inventory.RemoveItem(usableItem);
  126.                 usableItem.Destroy();
  127.             }
  128.         }
  129.     }
  130.  
  131.     private void EquipmentPanelRightClick(BaseItemSlot itemSlot)
  132.     {
  133.         if (itemSlot.Item is EquippableItem)
  134.         {
  135.             Unequip((EquippableItem)itemSlot.Item);
  136.         }
  137.     }
  138.  
  139.     private void ShowTooltip(BaseItemSlot itemSlot)
  140.     {        
  141.         if (itemSlot.Item != null)
  142.         {
  143.             itemTooltip.ShowTooltip(itemSlot.Item);
  144.         }
  145.     }
  146.  
  147.     private void HideTooltip(BaseItemSlot itemSlot)
  148.     {
  149.         itemTooltip.HideTooltip();
  150.     }
  151.  
  152.     private void BeginDrag(BaseItemSlot itemSlot)
  153.     {
  154.         if (itemSlot.Item != null)
  155.         {
  156.             dragItemSlot = itemSlot;
  157.             draggableItem.sprite = itemSlot.Item.Icon;
  158.             draggableItem.transform.position = Input.mousePosition;
  159.             draggableItem.gameObject.SetActive(true);
  160.         }
  161.     }
  162.  
  163.     private void Drag(BaseItemSlot itemSlot)
  164.     {        
  165.         draggableItem.transform.position = Input.mousePosition;
  166.     }
  167.  
  168.     private void EndDrag(BaseItemSlot itemSlot)
  169.     {
  170.         dragItemSlot = null;
  171.         draggableItem.gameObject.SetActive(false);
  172.     }
  173.  
  174.     private void Drop(BaseItemSlot dropItemSlot)
  175.     {
  176.         if (dragItemSlot == null) return;
  177.  
  178.         if (dropItemSlot is EquipmentSlot)
  179.         {
  180.             EquipByDragAndDrop(dropItemSlot);
  181.         }
  182.         else if (dropItemSlot is InventorySlot)
  183.         {
  184.             if (dragItemSlot is EquipmentSlot)
  185.                 UnEquipByDragAndDrop(dropItemSlot);
  186.         }
  187.  
  188.         try
  189.         {
  190.             if (dropItemSlot.CanAddStack(dragItemSlot.Item))
  191.             {
  192.                 AddStacks(dropItemSlot);
  193.             }
  194.             else if (!dropItemSlot.CanReceiveItem(dragItemSlot.Item) && !dragItemSlot.CanReceiveItem(dropItemSlot.Item))
  195.             {
  196.                 SwapItems(dropItemSlot);
  197.             }
  198.         }
  199.         catch (NullReferenceException ex)
  200.         {
  201.             Debug.Log($"Technical Debt issue: {ex.ToString()}");
  202.             // This occurs because of the if "if (dropItemSlot.CanAddStack(dragItemSlot.Item))"
  203.             // Need to make it so it doesn't need to check if dropItemSlot can add stacks
  204.             // Not all instances of dropItemSlot apply to adding stacks
  205.         }
  206.  
  207.     }
  208.  
  209.     private void EquipByDragAndDrop(BaseItemSlot dropItemSlot)
  210.     {
  211.         EquippableItem dragItem = dragItemSlot.Item as EquippableItem;
  212.         EquipmentSlot dropSlot = dropItemSlot as EquipmentSlot;
  213.  
  214.         //Debug.Log($"item being dragged is: {dragItem}, item being dropped in a: {dropSlot.EquipmentType} slot");
  215.         if (dragItem.EquipmentType == dropSlot.EquipmentType)
  216.         {
  217.             //Debug.Log($"Equipping {dragItem} to {dropSlot.EquipmentType} slot");
  218.             Equip(dragItem);
  219.         }
  220.     }
  221.  
  222.     private void UnEquipByDragAndDrop(BaseItemSlot dropItemSlot)
  223.     {
  224.         EquippableItem dragItem = dragItemSlot.Item as EquippableItem;
  225.         InventorySlot dropSlot = dropItemSlot as InventorySlot;
  226.         var slotIndex = equipmentPanel.EquipmentSlotIndex((EquipmentSlot)dragItemSlot);
  227.         var itemToDispose = equipmentPanel.equipmentHelper.equipmentSlotMeshObject[slotIndex];
  228.  
  229.         //Debug.Log($"item being dragged is: {dragItem}, item being dropped in a: {dropSlot} slot");
  230.         //Debug.Log(equipmentPanel.EquipmentSlotIndex((EquipmentSlot)dragItemSlot));
  231.  
  232.         if (dropSlot.Item == null)
  233.         {
  234.             //Debug.Log($"Removing {dragItem} from {dragItemSlot}, adding {dragItem} back to {dropSlot} slot");
  235.             Unequip(dragItem);
  236.         }
  237.     }
  238.  
  239.     private void SwapItems(BaseItemSlot dropItemSlot)
  240.     {
  241.         EquippableItem dragItem = dragItemSlot.Item as EquippableItem;
  242.         EquippableItem dropItem = dropItemSlot.Item as EquippableItem;
  243.  
  244.         if (dropItemSlot is EquipmentSlot)
  245.         {
  246.             if (dragItem != null) dragItem.Equip(this);
  247.             if (dropItem != null) dropItem.Unequip(this);
  248.         }
  249.         if (dragItemSlot is EquipmentSlot)
  250.         {
  251.             if (dragItem != null) dragItem.Unequip(this);
  252.             if (dropItem != null) dropItem.Equip(this);
  253.         }
  254.         statPanel.UpdateStatValues();
  255.  
  256.         Item draggedItem = dragItemSlot.Item;
  257.         int draggedItemAmount = dragItemSlot.Amount;
  258.  
  259.         dragItemSlot.Item = dropItemSlot.Item;
  260.         dragItemSlot.Amount = dropItemSlot.Amount;
  261.  
  262.         dropItemSlot.Item = draggedItem;
  263.         dropItemSlot.Amount = draggedItemAmount;
  264.     }
  265.  
  266.     private void AddStacks(BaseItemSlot dropItemSlot)
  267.     {
  268.         int numAddableStacks = dropItemSlot.Item.MaximumStacks - dropItemSlot.Amount;
  269.         int stacksToAdd = Mathf.Min(numAddableStacks, dragItemSlot.Amount);
  270.  
  271.         dropItemSlot.Amount += stacksToAdd;
  272.         dragItemSlot.Amount -= stacksToAdd;
  273.     }
  274.  
  275.     private void Initialize()
  276.     {
  277.         Strength.BaseValue = 5.0f;
  278.         Agility.BaseValue = 5.0f;
  279.         Intelligence.BaseValue = 5.0f;
  280.         Vitality.BaseValue = 5.0f;
  281.  
  282.         Damage.BaseValue = 15.0f;
  283.         CriticalChance.BaseValue = 10.0f;
  284.         Defense.BaseValue = 35.0f;
  285.         Evasion.BaseValue = 15.0f;
  286.     }
  287.  
  288.     public void Equip(EquippableItem item)
  289.     {
  290.         if (inventory.RemoveItem(item))
  291.         {
  292.             EquippableItem previousItem;
  293.             if (equipmentPanel.AddItem(item, out previousItem))
  294.             {
  295.                 if (previousItem != null)
  296.                 {
  297.                     inventory.AddItem(previousItem);                    
  298.                     previousItem.Unequip(this);
  299.                     statPanel.UpdateStatValues();
  300.                     if (equipmentPanel.equipmentHelper.CheckEquippedItemName(previousItem.ItemName))
  301.                     {
  302.                         equipmentPanel.equipmentHelper.RemoveUnequippedItem(previousItem.name);
  303.                     }
  304.                 }
  305.                 item.Equip(this);
  306.                 statPanel.UpdateStatValues();
  307.                 equipmentPanel.equipmentHelper.isDirty = true;
  308.             }
  309.             else
  310.             {
  311.                 inventory.AddItem(item);
  312.             }
  313.         }
  314.     }
  315.  
  316.     public void Unequip(EquippableItem item)
  317.     {
  318.         if (inventory.CanAddItem(item))
  319.         {
  320.             equipmentPanel.RemoveItem(item);
  321.             item.Unequip(this);
  322.             statPanel.UpdateStatValues();
  323.             inventory.AddItem(item);            
  324.         }
  325.     }
  326.  
  327.     public void UpdateStatValues()
  328.     {
  329.         statPanel.UpdateStatValues();
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement