Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System;
- using System.Linq;
- using System.Reflection;
- using UnityEngine;
- using UnityEngine.Serialization;
- using UnityEditor;
- [CreateAssetMenu(fileName = "NewSkill", menuName = "Skills/Skill")]
- public class SkillSO : ScriptableObject
- {
- public bool dontHighlightOnHover;
- public Sprite skillIcon;
- public Sprite baseSprite;
- public Sprite baseHoverSprite;
- public Sprite levelingSprite;
- public Sprite levelingHoverSprite;
- public Sprite maxedSprite;
- public Sprite maxedHoverSprite;
- public CharacterSO characterSo;
- public WeaponSO unlockedWeapon;
- [Header("SKILL INFORMATION")]
- public bool isUnlockableByPoint;
- public int minimumPoinToUnlock;
- public bool isSpaceship;
- public bool startUnlock;
- public bool skillUnlock;
- public bool inverseDecreaseAndIncrease;
- public bool isPercentage;
- public string affectedStat;
- public float affectedAmount;
- public BuffSO buffSo;
- public List<StatFromBuffToModify> statToModifyList = new List<StatFromBuffToModify>();
- [TextArea]
- public string skillDescription;
- public string skillDetail;
- public List<int> costsPerLevel;
- public List<SkillSO> skillNeeded;
- public int currentLevel = 0;
- public int MaxLevel => costsPerLevel.Count;
- public bool IsMaxed => currentLevel >= MaxLevel;
- public List<SkillSO> skillIUnlock = new List<SkillSO>();
- float baseStatValue;
- bool hasKeepBaseStatValue;
- bool hasKeepSecondBaseStatValue;
- float secondStartValue;
- public void OnEnable()
- {
- currentLevel = 0;
- hasKeepBaseStatValue = false;
- hasKeepSecondBaseStatValue = false;
- }
- public void IncreaseCharacterStat()
- {
- if(CheckIfContainsVariable(characterSo, affectedStat))
- {
- if(inverseDecreaseAndIncrease)
- {
- DecreaseStat(characterSo, affectedStat, affectedAmount);
- }
- else
- {
- IncreaseStat(characterSo, affectedStat, affectedAmount);
- }
- }
- if (buffSo)
- {
- for (int i = 0; i < statToModifyList.Count; i++)
- {
- if (statToModifyList[i].amountToModify > 0)
- {
- if (CheckIfContainsVariable(buffSo, statToModifyList[i].statName))
- {
- IncreaseBuffStat(buffSo, statToModifyList[i].statName, statToModifyList[i].amountToModify);
- }
- }
- }
- }
- }
- public void DecreaseCharacterStat()
- {
- if (CheckIfContainsVariable(characterSo, affectedStat))
- {
- if (inverseDecreaseAndIncrease)
- {
- IncreaseStat(characterSo, affectedStat, affectedAmount);
- }
- else
- {
- DecreaseStat(characterSo, affectedStat, affectedAmount);
- }
- }
- if (!buffSo) return;
- for (int i = 0; i < statToModifyList.Count; i++)
- {
- if (CheckIfContainsVariable(buffSo, statToModifyList[i].statName))
- {
- DecreaseBuffStat(buffSo, statToModifyList[i].statName, statToModifyList[i].amountToModify);
- }
- }
- }
- public int GetCost()
- {
- return currentLevel < costsPerLevel.Count ? costsPerLevel[currentLevel] : costsPerLevel[costsPerLevel.Count - 1];
- }
- public bool CanUpgrade()
- {
- if (IsMaxed) return false;
- return GameManager.instance.GetVoidShard() >= GetCost();
- }
- public void UpgradeSkill()
- {
- if (!IsMaxed)
- {
- IncreaseCharacterStat();
- GameManager.instance.SubstractVoidShard(GetCost());
- currentLevel++;
- }
- skillUnlock = true;
- if(buffSo && !characterSo.buffSo.Contains(buffSo)) characterSo.buffSo.Add(buffSo);
- }
- public void DowngradeSkill()
- {
- if (currentLevel > 0)
- {
- currentLevel--;
- DecreaseCharacterStat();
- GameManager.instance.AddVoidShard(GetCost());
- }
- else
- {
- //skillUnlock = false;
- if (characterSo.buffSo.Contains(buffSo)) characterSo.buffSo.Remove(buffSo);
- }
- }
- public void AutoFillBuffValue()
- {
- statToModifyList.Clear();
- FillStatFromBuffSO(buffSo, statToModifyList);
- }
- public void IncreaseBuffStat<T>(T scriptableObject, string variableName, float amount) where T : ScriptableObject
- {
- if (scriptableObject == null) return;
- Type type = scriptableObject.GetType();
- FieldInfo field = type.GetField(variableName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- if (field != null && field.FieldType == typeof(float))
- {
- float currentValue = (float)field.GetValue(scriptableObject);
- field.SetValue(scriptableObject, currentValue + amount);
- }
- }
- public void DecreaseBuffStat<T>(T scriptableObject, string variableName, float amount) where T : ScriptableObject
- {
- if (scriptableObject == null) return;
- Type type = scriptableObject.GetType();
- FieldInfo field = type.GetField(variableName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- if (field != null && field.FieldType == typeof(float))
- {
- float currentValue = (float)field.GetValue(scriptableObject);
- field.SetValue(scriptableObject, currentValue - amount);
- }
- else
- {
- //Debug.Log($"No variable {variableName}");
- }
- }
- public void IncreaseStat<T>(T scriptableObject, string variableName, float amount) where T : ScriptableObject
- {
- if (scriptableObject == null) return;
- Type type = scriptableObject.GetType();
- FieldInfo field = type.GetField(variableName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- if (field != null && field.FieldType == typeof(float))
- {
- float currentValue = (float)field.GetValue(scriptableObject);
- if(!hasKeepBaseStatValue)
- {
- baseStatValue = Mathf.Abs(currentValue);
- hasKeepBaseStatValue = true;
- }
- var pourcentageAmount = 0f;
- if (isPercentage)
- {
- pourcentageAmount = amount / 100;
- if(currentLevel == 0)
- {
- if(baseStatValue == 0)
- {
- if (!hasKeepSecondBaseStatValue)
- {
- secondStartValue = Mathf.Abs(currentValue) + amount;
- hasKeepSecondBaseStatValue = true;
- }
- field.SetValue(scriptableObject, currentValue + amount);
- }
- else
- {
- field.SetValue(scriptableObject, currentValue + (baseStatValue * pourcentageAmount));
- }
- }
- else
- {
- if (baseStatValue == 0)
- {
- field.SetValue(scriptableObject, currentValue + (secondStartValue * pourcentageAmount));
- }
- else
- {
- field.SetValue(scriptableObject, currentValue + (baseStatValue * pourcentageAmount));
- }
- }
- }
- else
- {
- field.SetValue(scriptableObject, currentValue + amount);
- }
- }
- else
- {
- //Debug.Log($"No variable {variableName}");
- }
- }
- public void DecreaseStat<T>(T scriptableObject, string variableName, float amount) where T : ScriptableObject
- {
- if (scriptableObject == null) return;
- Type type = scriptableObject.GetType();
- FieldInfo field = type.GetField(variableName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- if (field != null && field.FieldType == typeof(float))
- {
- float currentValue = (float)field.GetValue(scriptableObject);
- var percentageAmount = 0f;
- if (isPercentage)
- {
- if(inverseDecreaseAndIncrease)
- {
- if (!hasKeepBaseStatValue && currentValue != 0)
- {
- baseStatValue = Mathf.Abs(currentValue);
- hasKeepBaseStatValue = true;
- }
- }
- percentageAmount = amount / 100;
- if (currentLevel < 1)
- {
- if(baseStatValue == 0)
- {
- field.SetValue(scriptableObject, 0f);
- }
- else
- {
- field.SetValue(scriptableObject, currentValue - (baseStatValue * percentageAmount));
- }
- }
- else
- {
- if (baseStatValue == 0)
- {
- field.SetValue(scriptableObject, currentValue - (secondStartValue * percentageAmount));
- }
- else
- {
- field.SetValue(scriptableObject, currentValue - (baseStatValue * percentageAmount));
- }
- }
- }
- else
- {
- field.SetValue(scriptableObject, currentValue - amount);
- }
- }
- else
- {
- //Debug.Log($"No variable {variableName}");
- }
- }
- public void FillStatFromBuffSO<T>(T scriptableObject, List<StatFromBuffToModify> statList) where T : ScriptableObject
- {
- if (scriptableObject == null) return;
- Type type = scriptableObject.GetType();
- FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- foreach (var field in fields)
- {
- if (field != null && field.FieldType == typeof(float))
- {
- statList.Add(new StatFromBuffToModify
- {
- statName = field.Name,
- });
- }
- }
- }
- public bool CheckIfContainsVariable<T>(T scriptableObject, string variableName) where T : ScriptableObject
- {
- if (scriptableObject == null) return false;
- Type type = scriptableObject.GetType();
- FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- return fields.Any(field => field.Name == variableName);
- }
- }
- [System.Serializable]
- public class StatFromBuffToModify
- {
- public string statName;
- public float amountToModify;
- }
Advertisement
Add Comment
Please, Sign In to add comment