Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ReactiveProperty<T>
- {
- public event Action<T> OnChanged;
- private T _value;
- public T Value
- {
- get => _value;
- set
- {
- _value = value;
- OnChanged?.Invoke(_value);
- }
- }
- }
- [Serializable]
- public class StatsModel
- {
- public readonly ReactiveProperty<int> STR = new ReactiveProperty<int>();
- public readonly ReactiveProperty<int> INT = new ReactiveProperty<int>();
- public readonly ReactiveProperty<int> DEX = new ReactiveProperty<int>();
- public int StatsAvailable;
- public StatsModel()
- {
- STR.Value = 0;
- INT.Value = 0;
- DEX.Value = 0;
- StatsAvailable = 10;
- }
- }
- public class StatsView : MonoBehaviour
- {
- private StatsViewModel _statsViewModel;
- [SerializeField] private TextMeshProUGUI _STRValText;
- [SerializeField] private TextMeshProUGUI _INTValText;
- [SerializeField] private TextMeshProUGUI _DEXValText;
- [SerializeField] private TextMeshProUGUI _statsAvailableText;
- [SerializeField] private Button _addSTRButton;
- [SerializeField] private Button _addINTButton;
- [SerializeField] private Button _addDEXButton;
- [SerializeField] private Button _resetButton;
- [SerializeField] private Button _submitButton;
- [SerializeField] private Button _saveButton;
- [SerializeField] private Button _loadButton;
- public void Init(StatsViewModel statsViewModel)
- {
- _statsViewModel = statsViewModel;
- _statsViewModel.STRView.OnChanged += DisplaySTRView;
- _statsViewModel.INTView.OnChanged += DisplayINTView;
- _statsViewModel.DEXView.OnChanged += DisplayDEXView;
- _statsViewModel.StatsAvailableView.OnChanged += DisplayStatsAvailable;
- _statsViewModel.STRButtonEnabled.OnChanged += OnSTRButtonEnabled;
- _statsViewModel.INTButtonEnabled.OnChanged += OnINTButtonEnabled;
- _statsViewModel.DEXButtonEnabled.OnChanged += OnDEXButtonEnabled;
- // Обработка нажатия на кнопок "+"
- _addSTRButton.onClick.AddListener(_statsViewModel.OnIncreaseSTRBtnClicked);
- _addDEXButton.onClick.AddListener(_statsViewModel.OnIncreaseDEXBtnClicked);
- _addINTButton.onClick.AddListener(_statsViewModel.OnIncreaseINTBtnClicked);
- // Обработка нажатия на кнопки RESET и SUBMIT
- _resetButton.onClick.AddListener(_statsViewModel.OnResetButtonClicked);
- _submitButton.onClick.AddListener(_statsViewModel.OnSubmitBtnClicked);
- // Save & Load
- _saveButton.onClick.AddListener(_statsViewModel.OnButtonSave);
- _loadButton.onClick.AddListener(_statsViewModel.OnButtonLoad);
- _STRValText.SetText(_statsViewModel.STRView.Value.ToString());
- _DEXValText.SetText(_statsViewModel.DEXView.Value.ToString());
- _INTValText.SetText(_statsViewModel.INTView.Value.ToString());
- _statsAvailableText.SetText(_statsViewModel.StatsAvailableView.Value.ToString());
- }
- // Команды на отрисовку значений
- private void DisplaySTRView(int value)
- {
- _STRValText.SetText(value.ToString());
- }
- private void DisplayINTView(int value)
- {
- _INTValText.SetText(value.ToString());
- }
- private void DisplayDEXView(int value)
- {
- _DEXValText.SetText(value.ToString());
- }
- private void DisplayStatsAvailable(int value)
- {
- _statsAvailableText.SetText(value.ToString());
- }
- // Команды на отрисовку кнопку
- private void OnSTRButtonEnabled(bool value)
- {
- OnButtonEnabled(_addSTRButton, value);
- }
- private void OnINTButtonEnabled(bool value)
- {
- OnButtonEnabled(_addINTButton, value);
- }
- private void OnDEXButtonEnabled(bool value)
- {
- OnButtonEnabled(_addDEXButton, value);
- }
- private void OnButtonEnabled(Button button, bool value)
- {
- button.interactable = value;
- }
- // Відписка
- private void Dispose()
- {
- _statsViewModel.STRView.OnChanged -= DisplaySTRView;
- _statsViewModel.INTView.OnChanged -= DisplayINTView;
- _statsViewModel.DEXView.OnChanged -= DisplayDEXView;
- _statsViewModel.StatsAvailableView.OnChanged -= DisplayStatsAvailable;
- _statsViewModel.STRButtonEnabled.OnChanged -= OnSTRButtonEnabled;
- _statsViewModel.INTButtonEnabled.OnChanged -= OnINTButtonEnabled;
- _statsViewModel.DEXButtonEnabled.OnChanged -= OnDEXButtonEnabled;
- }
- private void OnDestroy()
- {
- Dispose();
- }
- }
- public class StatsViewModel
- {
- protected StatsModel _statsModel;
- // Максимальное значение STR, DEX и INT
- private const int MAX_STAT_VALUE = 7;
- // наш вид
- public ReactiveProperty<int> STRView = new();
- public ReactiveProperty<int> INTView = new();
- public ReactiveProperty<int> DEXView = new();
- public ReactiveProperty<int> StatsAvailableView = new();
- // Активність кнопок STR+, DEX+, INT+
- public ReactiveProperty<bool> STRButtonEnabled = new();
- public ReactiveProperty<bool> INTButtonEnabled = new();
- public ReactiveProperty<bool> DEXButtonEnabled = new();
- public StatsViewModel(StatsModel statsModel)
- {
- _statsModel = statsModel;
- _statsModel.STR.OnChanged += OnModelSTRChanged;
- _statsModel.INT.OnChanged += OnModelINTChanged;
- _statsModel.DEX.OnChanged += OnModelDEXChanged;
- StatsAvailableView.OnChanged += OnModelStatsToSpendViewChanged;
- OnResetButtonClicked();
- }
- public void OnIncreaseSTRBtnClicked()
- {
- IncreasePropertyValue(STRView);
- }
- public void OnIncreaseINTBtnClicked()
- {
- IncreasePropertyValue(INTView);
- }
- public void OnIncreaseDEXBtnClicked()
- {
- IncreasePropertyValue(DEXView);
- }
- /// Сброс до состояния модели
- public void OnResetButtonClicked()
- {
- STRView.Value = _statsModel.STR.Value;
- INTView.Value = _statsModel.INT.Value;
- DEXView.Value = _statsModel.DEX.Value;
- StatsAvailableView.Value = _statsModel.StatsAvailable;
- DefineButtonsStatus();
- }
- /// Подтверждение изменений
- public void OnSubmitBtnClicked()
- {
- _statsModel.STR.Value = STRView.Value;
- _statsModel.INT.Value = INTView.Value;
- _statsModel.DEX.Value = DEXView.Value;
- _statsModel.StatsAvailable = StatsAvailableView.Value;
- }
- public void OnButtonSave()
- {
- SaveManager.Save(nameof(StatsModel), _statsModel);
- }
- public void OnButtonLoad()
- {
- StatsModel statsModel = SaveManager.Load<StatsModel>(nameof(StatsModel));
- _statsModel.STR.Value = statsModel.STR.Value;
- _statsModel.INT.Value = statsModel.INT.Value;
- _statsModel.DEX.Value = statsModel.DEX.Value;
- _statsModel.StatsAvailable = statsModel.StatsAvailable;
- OnResetButtonClicked();
- }
- public void Dispose()
- {
- _statsModel.STR.OnChanged -= OnModelSTRChanged;
- _statsModel.INT.OnChanged -= OnModelINTChanged;
- _statsModel.DEX.OnChanged -= OnModelDEXChanged;
- StatsAvailableView.OnChanged -= OnModelStatsToSpendViewChanged;
- }
- private void OnModelSTRChanged(int value)
- {
- STRView.Value = value;
- }
- private void OnModelINTChanged(int value)
- {
- INTView.Value = value;
- }
- private void OnModelDEXChanged(int value)
- {
- DEXView.Value = value;
- }
- private void IncreasePropertyValue(ReactiveProperty<int> property)
- {
- property.Value += 1;
- StatsAvailableView.Value -= 1;
- }
- /// Проверяем, активны или неактивны кнопки "+"
- private void DefineButtonsStatus()
- {
- STRButtonEnabled.Value = STRView.Value < MAX_STAT_VALUE;
- INTButtonEnabled.Value = INTView.Value < MAX_STAT_VALUE;
- DEXButtonEnabled.Value = DEXView.Value < MAX_STAT_VALUE;
- if (StatsAvailableView.Value <= 0)
- {
- STRButtonEnabled.Value = false;
- INTButtonEnabled.Value = false;
- DEXButtonEnabled.Value = false;
- }
- }
- private void OnModelStatsToSpendViewChanged(int obj)
- {
- DefineButtonsStatus();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement