Eonirr

Untitled

Apr 7th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using UnityEngine.Serialization;
  7. using UnityEditor;
  8.  
  9. [CreateAssetMenu(fileName = "NewSkill", menuName = "Skills/Skill")]
  10. public class SkillSO : ScriptableObject
  11. {
  12. public bool dontHighlightOnHover;
  13. public Sprite skillIcon;
  14. public Sprite baseSprite;
  15. public Sprite baseHoverSprite;
  16. public Sprite levelingSprite;
  17. public Sprite levelingHoverSprite;
  18. public Sprite maxedSprite;
  19. public Sprite maxedHoverSprite;
  20.  
  21. public CharacterSO characterSo;
  22. public WeaponSO unlockedWeapon;
  23.  
  24. [Header("SKILL INFORMATION")]
  25. public bool isUnlockableByPoint;
  26. public int minimumPoinToUnlock;
  27. public bool isSpaceship;
  28. public bool startUnlock;
  29. public bool skillUnlock;
  30. public bool inverseDecreaseAndIncrease;
  31. public bool isPercentage;
  32. public string affectedStat;
  33. public float affectedAmount;
  34.  
  35. public BuffSO buffSo;
  36. public List<StatFromBuffToModify> statToModifyList = new List<StatFromBuffToModify>();
  37.  
  38. [TextArea]
  39. public string skillDescription;
  40. public string skillDetail;
  41.  
  42. public List<int> costsPerLevel;
  43.  
  44. public List<SkillSO> skillNeeded;
  45.  
  46. public int currentLevel = 0;
  47.  
  48. public int MaxLevel => costsPerLevel.Count;
  49.  
  50. public bool IsMaxed => currentLevel >= MaxLevel;
  51.  
  52. public List<SkillSO> skillIUnlock = new List<SkillSO>();
  53.  
  54. float baseStatValue;
  55. bool hasKeepBaseStatValue;
  56.  
  57. bool hasKeepSecondBaseStatValue;
  58. float secondStartValue;
  59.  
  60. public void OnEnable()
  61. {
  62. currentLevel = 0;
  63. hasKeepBaseStatValue = false;
  64. hasKeepSecondBaseStatValue = false;
  65. }
  66.  
  67.  
  68. public void IncreaseCharacterStat()
  69. {
  70. if(CheckIfContainsVariable(characterSo, affectedStat))
  71. {
  72. if(inverseDecreaseAndIncrease)
  73. {
  74. DecreaseStat(characterSo, affectedStat, affectedAmount);
  75. }
  76. else
  77. {
  78. IncreaseStat(characterSo, affectedStat, affectedAmount);
  79. }
  80. }
  81.  
  82. if (buffSo)
  83. {
  84. for (int i = 0; i < statToModifyList.Count; i++)
  85. {
  86. if (statToModifyList[i].amountToModify > 0)
  87. {
  88. if (CheckIfContainsVariable(buffSo, statToModifyList[i].statName))
  89. {
  90. IncreaseBuffStat(buffSo, statToModifyList[i].statName, statToModifyList[i].amountToModify);
  91. }
  92. }
  93. }
  94. }
  95. }
  96.  
  97. public void DecreaseCharacterStat()
  98. {
  99. if (CheckIfContainsVariable(characterSo, affectedStat))
  100. {
  101. if (inverseDecreaseAndIncrease)
  102. {
  103. IncreaseStat(characterSo, affectedStat, affectedAmount);
  104. }
  105. else
  106. {
  107. DecreaseStat(characterSo, affectedStat, affectedAmount);
  108. }
  109. }
  110.  
  111. if (!buffSo) return;
  112.  
  113. for (int i = 0; i < statToModifyList.Count; i++)
  114. {
  115. if (CheckIfContainsVariable(buffSo, statToModifyList[i].statName))
  116. {
  117. DecreaseBuffStat(buffSo, statToModifyList[i].statName, statToModifyList[i].amountToModify);
  118. }
  119. }
  120. }
  121.  
  122.  
  123. public int GetCost()
  124. {
  125. return currentLevel < costsPerLevel.Count ? costsPerLevel[currentLevel] : costsPerLevel[costsPerLevel.Count - 1];
  126. }
  127.  
  128. public bool CanUpgrade()
  129. {
  130. if (IsMaxed) return false;
  131.  
  132. return GameManager.instance.GetVoidShard() >= GetCost();
  133. }
  134.  
  135. public void UpgradeSkill()
  136. {
  137. if (!IsMaxed)
  138. {
  139. IncreaseCharacterStat();
  140. GameManager.instance.SubstractVoidShard(GetCost());
  141. currentLevel++;
  142. }
  143. skillUnlock = true;
  144.  
  145. if(buffSo && !characterSo.buffSo.Contains(buffSo)) characterSo.buffSo.Add(buffSo);
  146. }
  147.  
  148. public void DowngradeSkill()
  149. {
  150. if (currentLevel > 0)
  151. {
  152. currentLevel--;
  153. DecreaseCharacterStat();
  154. GameManager.instance.AddVoidShard(GetCost());
  155. }
  156. else
  157. {
  158. //skillUnlock = false;
  159. if (characterSo.buffSo.Contains(buffSo)) characterSo.buffSo.Remove(buffSo);
  160. }
  161. }
  162.  
  163. public void AutoFillBuffValue()
  164. {
  165. statToModifyList.Clear();
  166. FillStatFromBuffSO(buffSo, statToModifyList);
  167. }
  168.  
  169. public void IncreaseBuffStat<T>(T scriptableObject, string variableName, float amount) where T : ScriptableObject
  170. {
  171. if (scriptableObject == null) return;
  172.  
  173. Type type = scriptableObject.GetType();
  174. FieldInfo field = type.GetField(variableName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  175.  
  176. if (field != null && field.FieldType == typeof(float))
  177. {
  178. float currentValue = (float)field.GetValue(scriptableObject);
  179.  
  180. field.SetValue(scriptableObject, currentValue + amount);
  181. }
  182. }
  183.  
  184. public void DecreaseBuffStat<T>(T scriptableObject, string variableName, float amount) where T : ScriptableObject
  185. {
  186. if (scriptableObject == null) return;
  187.  
  188. Type type = scriptableObject.GetType();
  189. FieldInfo field = type.GetField(variableName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  190.  
  191. if (field != null && field.FieldType == typeof(float))
  192. {
  193. float currentValue = (float)field.GetValue(scriptableObject);
  194.  
  195. field.SetValue(scriptableObject, currentValue - amount);
  196. }
  197. else
  198. {
  199. //Debug.Log($"No variable {variableName}");
  200. }
  201. }
  202.  
  203. public void IncreaseStat<T>(T scriptableObject, string variableName, float amount) where T : ScriptableObject
  204. {
  205. if (scriptableObject == null) return;
  206.  
  207. Type type = scriptableObject.GetType();
  208. FieldInfo field = type.GetField(variableName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  209.  
  210. if (field != null && field.FieldType == typeof(float))
  211. {
  212. float currentValue = (float)field.GetValue(scriptableObject);
  213.  
  214. if(!hasKeepBaseStatValue)
  215. {
  216. baseStatValue = Mathf.Abs(currentValue);
  217. hasKeepBaseStatValue = true;
  218. }
  219.  
  220. var pourcentageAmount = 0f;
  221. if (isPercentage)
  222. {
  223. pourcentageAmount = amount / 100;
  224.  
  225. if(currentLevel == 0)
  226. {
  227. if(baseStatValue == 0)
  228. {
  229. if (!hasKeepSecondBaseStatValue)
  230. {
  231. secondStartValue = Mathf.Abs(currentValue) + amount;
  232. hasKeepSecondBaseStatValue = true;
  233. }
  234.  
  235. field.SetValue(scriptableObject, currentValue + amount);
  236. }
  237. else
  238. {
  239. field.SetValue(scriptableObject, currentValue + (baseStatValue * pourcentageAmount));
  240. }
  241. }
  242. else
  243. {
  244. if (baseStatValue == 0)
  245. {
  246. field.SetValue(scriptableObject, currentValue + (secondStartValue * pourcentageAmount));
  247. }
  248. else
  249. {
  250. field.SetValue(scriptableObject, currentValue + (baseStatValue * pourcentageAmount));
  251. }
  252. }
  253. }
  254. else
  255. {
  256. field.SetValue(scriptableObject, currentValue + amount);
  257. }
  258. }
  259. else
  260. {
  261. //Debug.Log($"No variable {variableName}");
  262. }
  263. }
  264.  
  265. public void DecreaseStat<T>(T scriptableObject, string variableName, float amount) where T : ScriptableObject
  266. {
  267. if (scriptableObject == null) return;
  268.  
  269. Type type = scriptableObject.GetType();
  270. FieldInfo field = type.GetField(variableName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  271.  
  272. if (field != null && field.FieldType == typeof(float))
  273. {
  274. float currentValue = (float)field.GetValue(scriptableObject);
  275.  
  276. var percentageAmount = 0f;
  277. if (isPercentage)
  278. {
  279. if(inverseDecreaseAndIncrease)
  280. {
  281. if (!hasKeepBaseStatValue && currentValue != 0)
  282. {
  283. baseStatValue = Mathf.Abs(currentValue);
  284. hasKeepBaseStatValue = true;
  285. }
  286. }
  287. percentageAmount = amount / 100;
  288.  
  289. if (currentLevel < 1)
  290. {
  291. if(baseStatValue == 0)
  292. {
  293. field.SetValue(scriptableObject, 0f);
  294. }
  295. else
  296. {
  297. field.SetValue(scriptableObject, currentValue - (baseStatValue * percentageAmount));
  298. }
  299. }
  300. else
  301. {
  302. if (baseStatValue == 0)
  303. {
  304. field.SetValue(scriptableObject, currentValue - (secondStartValue * percentageAmount));
  305. }
  306. else
  307. {
  308. field.SetValue(scriptableObject, currentValue - (baseStatValue * percentageAmount));
  309. }
  310. }
  311. }
  312. else
  313. {
  314. field.SetValue(scriptableObject, currentValue - amount);
  315. }
  316. }
  317. else
  318. {
  319. //Debug.Log($"No variable {variableName}");
  320. }
  321. }
  322.  
  323. public void FillStatFromBuffSO<T>(T scriptableObject, List<StatFromBuffToModify> statList) where T : ScriptableObject
  324. {
  325. if (scriptableObject == null) return;
  326.  
  327. Type type = scriptableObject.GetType();
  328. FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  329.  
  330. foreach (var field in fields)
  331. {
  332. if (field != null && field.FieldType == typeof(float))
  333. {
  334. statList.Add(new StatFromBuffToModify
  335. {
  336. statName = field.Name,
  337. });
  338. }
  339. }
  340. }
  341.  
  342. public bool CheckIfContainsVariable<T>(T scriptableObject, string variableName) where T : ScriptableObject
  343. {
  344. if (scriptableObject == null) return false;
  345.  
  346. Type type = scriptableObject.GetType();
  347. FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  348.  
  349. return fields.Any(field => field.Name == variableName);
  350. }
  351. }
  352.  
  353. [System.Serializable]
  354. public class StatFromBuffToModify
  355. {
  356. public string statName;
  357. public float amountToModify;
  358. }
  359.  
  360.  
Advertisement
Add Comment
Please, Sign In to add comment