- using MyCollections.DesignPatterns.Visitor;
- using MyCollections.Stats;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- [CreateAssetMenu(menuName = "Item/Potion")]
- public class Potion : Item, IVisitor
- {
- public enum OperatorType { Add, Multiply }
- [SerializeField] private StatType statType = StatType.Attack;
- [SerializeField] private OperatorType operatorType = OperatorType.Add;
- [SerializeField] private int value = 10;
- [SerializeField] private float duration = 5f;
- public void Visit<T>(T visitable) where T : Component, IVisitable
- {
- if (visitable is PlayerController controller)
- {
- if(CanApply(statType, controller))
- ApplyStatsEffect(controller);
- }
- }
- private void ApplyStatsEffect(PlayerController controller)
- {
- StatModifier modifier = operatorType switch
- {
- OperatorType.Add => new BasicStatModifier(statType, duration, v => v + value),
- OperatorType.Multiply => new BasicStatModifier(statType, duration, v => v * value),
- _ => throw new ArgumentOutOfRangeException()
- };
- controller.Stats.Mediator.AddModifier(modifier);
- Debug.Log($"Added modifier: {modifier}");
- }
- private bool CanApply(StatType statType, PlayerController controller)
- {
- switch (statType)
- {
- case StatType.Attack:
- return true;
- case StatType.Health:
- return controller.Health != controller.MaxHealth;
- case StatType.Mana:
- return true;
- }
- return true;
- }
- }