Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / Items / Potion.cs
  1. using MyCollections.DesignPatterns.Visitor;
  2. using MyCollections.Stats;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using System;
  7. [CreateAssetMenu(menuName = "Item/Potion")]
  8. public class Potion : Item, IVisitor
  9. {
  10. public enum OperatorType { Add, Multiply }
  11. [SerializeField] private StatType statType = StatType.Attack;
  12. [SerializeField] private OperatorType operatorType = OperatorType.Add;
  13. [SerializeField] private int value = 10;
  14. [SerializeField] private float duration = 5f;
  15. public void Visit<T>(T visitable) where T : Component, IVisitable
  16. {
  17. if (visitable is PlayerController controller)
  18. {
  19. if(CanApply(statType, controller))
  20. ApplyStatsEffect(controller);
  21. }
  22. }
  23. private void ApplyStatsEffect(PlayerController controller)
  24. {
  25. StatModifier modifier = operatorType switch
  26. {
  27. OperatorType.Add => new BasicStatModifier(statType, duration, v => v + value),
  28. OperatorType.Multiply => new BasicStatModifier(statType, duration, v => v * value),
  29. _ => throw new ArgumentOutOfRangeException()
  30. };
  31. controller.Stats.Mediator.AddModifier(modifier);
  32. Debug.Log($"Added modifier: {modifier}");
  33. }
  34. private bool CanApply(StatType statType, PlayerController controller)
  35. {
  36. switch (statType)
  37. {
  38. case StatType.Attack:
  39. return true;
  40. case StatType.Health:
  41. return controller.Health != controller.MaxHealth;
  42. case StatType.Mana:
  43. return true;
  44. }
  45. return true;
  46. }
  47. }