Answer the question
In order to leave comments, you need to log in
How to make something normal out of such a system?
Created GridRepository:
public class StatisticRepository
{
public int Coins;
}
public class StatisticInteractor
{
StatisticRepository _repository = new StatisticRepository();
public int Coins => _repository.Coins;
public delegate void OnCoinsValue();
public event OnCoinsValue OnCoinsValueChanged;
public void AddCoins(int value)
{
_repository.Coins += value;
OnCoinsValueChanged?.Invoke();
}
public void SpentCoins(int value)
{
_repository.Coins -= value;
OnCoinsValueChanged?.Invoke();
}
}
using UnityEngine;
public class Coin : MonoBehaviour
{
[SerializeField] private int _value;
[SerializeField] private ParticleSystem _effect;
private StatisticInteractor _statisticInteractor = new StatisticInteractor();
private void OnTriggerEnter(Collider other)
{
if(other.GetComponent<PlayerMovement>())
{
_statisticInteractor.AddCoins(_value);
Instantiate(_effect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
using UnityEngine;
using TMPro;
public class CoinsRender : MonoBehaviour
{
[SerializeField] private TMP_Text _coinsText;
StatisticInteractor _statisticInteractor = new StatisticInteractor();
private void OnEnable()
{
_statisticInteractor.OnCoinsValueChanged += RenderText;
}
private void OnDisable()
{
_statisticInteractor.OnCoinsValueChanged -= RenderText;
}
public void RenderText()
{
_coinsText.text = "Монетки: " + _statisticInteractor.Coins;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question