dmitryzenevich

StarsMementoSample

Oct 23rd, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using Core.Interfaces;
  3. using UnityEngine;
  4.  
  5. namespace Game
  6. {
  7.     public class Coins : ScriptableObject, IOriginator<IMemento>
  8.     {
  9.         [SerializeField] private int _value;
  10.  
  11.         public int Value
  12.         {
  13.             get => _value;
  14.             private set
  15.             {
  16.                 if (value == _value)
  17.                     return;
  18.  
  19.                 _value = value;
  20.             }
  21.         }
  22.  
  23.         public bool IsEmpty => Value <= 0;
  24.  
  25.         public void Add() => Value++;
  26.  
  27.         public void Add(int amount)
  28.         {
  29.             if (amount < 0)
  30.                 throw new ArgumentException("Argument less than zero.", nameof(amount));
  31.  
  32.             Value += amount;
  33.         }
  34.  
  35.         #region Memento
  36.  
  37.         public IMemento SaveState() => new Memento(_value);
  38.  
  39.         public void RestoreState(IMemento memento) => _value = (int)memento.State;
  40.  
  41.         public void ResetToDefaultState() => _value = 0;
  42.  
  43.         [Serializable]
  44.         public struct Memento : IMemento
  45.         {
  46.             private readonly int _value;
  47.  
  48.             public int Value => _value;
  49.  
  50.             public object State => Value;
  51.  
  52.             public Memento(int value) => _value = value;
  53.         }
  54.  
  55.         #endregion
  56.     }
  57. }
Add Comment
Please, Sign In to add comment