Search Unity

Increase Stats on Level Up:

Discussion in 'Scripting' started by PimpPanda, Jul 15, 2021.

  1. PimpPanda

    PimpPanda

    Joined:
    Jun 4, 2021
    Posts:
    2
    Hello.
    Sorry if someone has asked this before, but I cant seem to find a solution anywhere. I am new to Game Development and have been struggling with creating a stat system that is affected by both level and gear. I have the gear part working and have also created an Exp and Leveling system, but I cant figure out how to increase the base value of my stats when my character levels up automatically. this is probably something very basic, but I cant seem to figure it out.

    This is the Code for my Character and his stats:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using Kryz.CharacterStats;
    7. using UnityEngine.SceneManagement;
    8.  
    9. public class Character : MonoBehaviour
    10. {
    11.     public int maxHealth;
    12.    
    13.     public Animator animator;
    14.     public HealthBar healthBar;
    15.    
    16.  
    17.  
    18.  
    19.  
    20.     public int currentHealth;
    21.     private PlayerHealth health;
    22.  
    23.     public CharacterStat Health;
    24.     public CharacterStat Stamina;
    25.     public CharacterStat Damage;
    26.     public CharacterStat LevelPoints;
    27.  
    28.     [SerializeField] Inventory2 inventory;
    29.     [SerializeField] EquipmentPanel equipmentPanel;
    30.     [SerializeField] StatPanel statPanel;
    31.     [SerializeField] ItemTooltip itemTooltip;
    32.     [SerializeField] Image draggableItem;
    33.  
    34.  
    35.     private InventorySlot2 draggedSlot;
    36.  
    37.  
    38.  
    39.     void Start()
    40.     {
    41.        
    42.         maxHealth = (int)Health.calculateFinalValue();
    43.         currentHealth = maxHealth;
    44.         healthBar.SetMaxHealth(maxHealth);
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.         if (currentHealth > maxHealth)
    50.         {
    51.             currentHealth = maxHealth;
    52.         }
    53.         maxHealth = (int)Health.calculateFinalValue();
    54.         healthBar.SetMaxHealth(maxHealth);
    55.         healthBar.SetHealth(currentHealth);
    56.     }
    57.  
    58.     private void OnValidate()
    59.     {
    60.         if (itemTooltip == null)
    61.         {
    62.             itemTooltip = FindObjectOfType<ItemTooltip>();
    63.         }
    64.     }
    65.  
    66.  
    67.     private void Awake()
    68.     {
    69.  
    70.  
    71.         statPanel.SetStats(Health, Stamina, Damage);
    72.         statPanel.UpdateStatValues();
    73.  
    74.         //Setup Events
    75.         //RIghtClick
    76.         inventory.OnRightClickEvent += InventoryRightClick;
    77.         equipmentPanel.OnRightClickEvent += EquipmentPanelRightClick;
    78.         //Pointer Enter
    79.         inventory.OnPointerEnterEvent += HideTooltip;
    80.         equipmentPanel.OnPointerEnterEvent += HideTooltip;
    81.         //Pointer Exit
    82.         inventory.OnPointerExitEvent += ShowTooltip;
    83.         equipmentPanel.OnPointerExitEvent += ShowTooltip;
    84.         //BeginDrag
    85.         inventory.OnBeginDragEvent += BeginDrag;
    86.         equipmentPanel.OnBeginDragEvent += BeginDrag;
    87.         //End Drag
    88.         inventory.OnEndDragEvent += EndDrag;
    89.         equipmentPanel.OnEndDragEvent += EndDrag;
    90.         //Drag
    91.         inventory.OnDragEvent += Drag;
    92.         equipmentPanel.OnDragEvent += Drag;
    93.         //Drop
    94.         inventory.OnDropEvent += Drop;
    95.         equipmentPanel.OnDropEvent += Drop;
    96.       }
    97.  
    98.  
    99.  
    100.  
    101.     public void TakeDamage(int damage)
    102.     {
    103.         animator.SetTrigger("Hurt");
    104.  
    105.         currentHealth -= damage;
    106.  
    107.         healthBar.SetHealth(currentHealth);
    108.  
    109.  
    110.  
    111.         if (currentHealth <= 0)
    112.         {
    113.             animator.SetBool("IsDead", true);
    114.             Die();
    115.  
    116.         }
    117.     }
    118.  
    119.     public void Die()
    120.     {
    121.         Physics2D.IgnoreLayerCollision(8, 3, true);
    122.         GetComponent<BoxCollider2D>().enabled = false;
    123.         GetComponent<CharacterController2D>().enabled = false;
    124.         GetComponent<PlayerCombat>().enabled = false;
    125.         FindObjectOfType<GameManager>().GameOver();
    126.  
    127.     }
    128.  
    129.  
    130.  
    131.  
    132.  
    133.  
    134.  
    135.     private void InventoryRightClick(InventorySlot2 itemSlot)
    136.     {
    137.        if(itemSlot.Item is EquipableItem)
    138.         {
    139.             Equip((EquipableItem)itemSlot.Item);
    140.         }
    141.        else if (itemSlot.Item is UsableItem)
    142.         {
    143.             UsableItem usableItem = (UsableItem)itemSlot.Item;
    144.             usableItem.Use(this);
    145.  
    146.             if (usableItem.IsConsumable)
    147.             {
    148.                 inventory.RemoveItem(usableItem);
    149.                 usableItem.Destroy();
    150.             }
    151.         }
    152.     }
    153.  
    154.     private void EquipmentPanelRightClick(InventorySlot2 itemSlot)
    155.     {
    156.         EquipableItem equipableItem = itemSlot.Item as EquipableItem;
    157.  
    158.         if (itemSlot.Item is EquipableItem)
    159.         {
    160.             Unequip((EquipableItem)itemSlot.Item);
    161.         }
    162.     }
    163.  
    164.     private void ShowTooltip(InventorySlot2 itemSlot)
    165.     {
    166.         EquipableItem equipableItem = itemSlot.Item as EquipableItem;
    167.  
    168.         if (equipableItem != null)
    169.         {
    170.             itemTooltip.ShowTooltip(equipableItem);
    171.         }
    172.     }
    173.  
    174.     private void HideTooltip(InventorySlot2 itemSlot)
    175.     {
    176.         itemTooltip.HideTooltip();
    177.     }
    178.  
    179.     private void BeginDrag(InventorySlot2 itemSlot)
    180.     {
    181.         if(itemSlot.Item != null)
    182.         {
    183.             draggedSlot = itemSlot;
    184.             draggableItem.sprite = itemSlot.Item.Icon;
    185.             draggableItem.transform.position = Input.mousePosition;
    186.             draggableItem.enabled = true;
    187.         }
    188.     }
    189.  
    190.     private void EndDrag(InventorySlot2 itemSlot)
    191.     {
    192.         draggedSlot = null;
    193.         draggableItem.enabled = false;
    194.     }
    195.  
    196.     private void Drag(InventorySlot2 itemSlot)
    197.     {
    198.         if (draggableItem.enabled)
    199.         {
    200.            draggableItem.transform.position = Input.mousePosition;
    201.         }
    202.     }
    203.  
    204.     private void Drop(InventorySlot2 dropItemSlot)
    205.     {
    206.         if (draggedSlot == null) return;
    207.  
    208.         if (dropItemSlot.CanAddStacks(draggedSlot.Item))
    209.         {
    210.             int numAddableStacks = dropItemSlot.Item.MaximunStacks - dropItemSlot.Amount;
    211.             int stacksToAdd = Mathf.Min(numAddableStacks, draggedSlot.Amount);
    212.  
    213.             dropItemSlot.Amount += stacksToAdd;
    214.             draggedSlot.Amount -= stacksToAdd;
    215.         }
    216.  
    217.         else if(dropItemSlot.CanReceiveItem(draggedSlot.Item)&& draggedSlot.CanReceiveItem(dropItemSlot.Item))
    218.         {
    219.             EquipableItem dragItem = draggedSlot.Item as EquipableItem;
    220.             EquipableItem dropItem = dropItemSlot.Item as EquipableItem;
    221.  
    222.             if(draggedSlot is EquipmentSlots)
    223.             {
    224.                 if (dragItem != null) dragItem.Unequip(this);
    225.                 if (dropItem != null) dropItem.Equip(this);
    226.             }
    227.             if(dropItemSlot is EquipmentSlots)
    228.             {
    229.                 if (dragItem != null) dragItem.Equip(this);
    230.                 if (dropItem != null) dropItem.Unequip(this);
    231.             }
    232.  
    233.             statPanel.UpdateStatValues();
    234.  
    235.             Item2 draggedItem = draggedSlot.Item;
    236.             int draggedItemAmount = draggedSlot.Amount;
    237.             draggedSlot.Item = dropItemSlot.Item;
    238.             draggedSlot.Amount = dropItemSlot.Amount;
    239.             dropItemSlot.Item = draggedItem;
    240.             dropItemSlot.Amount = draggedItemAmount;
    241.         }
    242.  
    243.     }
    244.  
    245.     public void Equip(EquipableItem item)
    246.     {
    247.         if (inventory.RemoveItem(item))
    248.         {
    249.             EquipableItem previousItem;
    250.             if(equipmentPanel.AddItem(item, out previousItem))
    251.             {
    252.                 if (previousItem != null)
    253.                 {
    254.                     inventory.AddItem(previousItem);
    255.                     previousItem.Unequip(this);
    256.                     statPanel.UpdateStatValues();
    257.                 }
    258.                 item.Equip(this);
    259.                 statPanel.UpdateStatValues();
    260.             }
    261.             else
    262.             {
    263.                 inventory.AddItem(item);
    264.             }
    265.         }
    266.     }
    267.  
    268.     public void Unequip(EquipableItem item)
    269.     {
    270.         if(!inventory.IsFull() && equipmentPanel.RemoveItem(item))
    271.         {
    272.             item.Unequip(this);
    273.             statPanel.UpdateStatValues();
    274.             inventory.AddItem(item);
    275.         }
    276.     }
    277.  
    278. }


    This is the code to calculate the Stats when I add the gear:


    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Collections.ObjectModel;
    4. using Kryz.CharacterStats;
    5.  
    6. namespace Kryz.CharacterStats
    7. {
    8.  
    9.  
    10.  
    11.  
    12.     [Serializable]
    13.     public class CharacterStat
    14.     {
    15.         public float BaseValue;
    16.  
    17.  
    18.         public virtual float Value
    19.         {
    20.             get
    21.             {
    22.                 if (isDirty || BaseValue != lastBaseValue)
    23.                 {
    24.                     lastBaseValue = BaseValue;
    25.                     _Value = calculateFinalValue();
    26.                     isDirty = false;
    27.                 }
    28.                 return _Value;
    29.             }
    30.         }
    31.  
    32.  
    33.         protected bool isDirty = true;
    34.         protected float _Value;
    35.         protected float lastBaseValue = float.MinValue;
    36.  
    37.         protected readonly List<StatModifier> statModifiers;
    38.         public readonly ReadOnlyCollection<StatModifier> StatModifiers;
    39.  
    40.         public CharacterStat()
    41.         {
    42.             statModifiers = new List<StatModifier>();
    43.             StatModifiers = statModifiers.AsReadOnly();
    44.         }
    45.  
    46.         public CharacterStat(float baseValue) : this()
    47.         {
    48.             BaseValue = baseValue;
    49.         }
    50.  
    51.  
    52.         public virtual void AddModifier(StatModifier mod)
    53.         {
    54.             isDirty = true;
    55.             statModifiers.Add(mod);
    56.             statModifiers.Sort(CompareModifierOrder);
    57.         }
    58.  
    59.         protected virtual int CompareModifierOrder(StatModifier a, StatModifier b)
    60.         {
    61.             if (a.Order < b.Order)
    62.             {
    63.                 return -1;
    64.             }
    65.             else if (a.Order > b.Order)
    66.             {
    67.                 return 1;
    68.             }
    69.             return 0;
    70.         }
    71.  
    72.  
    73.         public virtual bool RemoveModifier(StatModifier mod)
    74.         {
    75.  
    76.             if (statModifiers.Remove(mod))
    77.             {
    78.                 isDirty = true;
    79.                 return true;
    80.             }
    81.             return false;
    82.         }
    83.  
    84.         public virtual bool RemoveAllModifiersFromSource(object source)
    85.         {
    86.             bool didRemove = false;
    87.  
    88.             for (int i = statModifiers.Count - 1; i >= 0; i--)
    89.             {
    90.                 if (statModifiers[i].Source == source)
    91.                 {
    92.                     isDirty = true;
    93.                     didRemove = true;
    94.                     statModifiers.RemoveAt(i);
    95.                 }
    96.             }
    97.             return didRemove;
    98.         }
    99.  
    100.         public virtual float calculateFinalValue()
    101.         {
    102.             float finalValue = BaseValue;
    103.             float sumPercentAdd = 0;
    104.  
    105.             for (int i = 0; i < statModifiers.Count; i++)
    106.             {
    107.  
    108.                 StatModifier mod = statModifiers[i];
    109.  
    110.                 if (mod.Type == StatModType.Flat)
    111.                 {
    112.                     finalValue += mod.Value;
    113.                 }
    114.                 else if (mod.Type == StatModType.PercentAdd)
    115.                 {
    116.                     sumPercentAdd += mod.Value;
    117.  
    118.                     if (i + 1 >= statModifiers.Count || statModifiers[i + 1].Type != StatModType.PercentAdd)
    119.                     {
    120.                         finalValue *= 1 + sumPercentAdd;
    121.                         sumPercentAdd = 0;
    122.                     }
    123.                 }
    124.  
    125.                 else if (mod.Type == StatModType.PercentMult)
    126.                 {
    127.                     finalValue *= 1 + mod.Value;
    128.                 }
    129.  
    130.             }
    131.  
    132.             return (float)Math.Round(finalValue, 4);
    133.         }
    134.     }
    135. }
    136.