data.money = data.money - AllCharacters[i].GetComponent<Item>().priceCharacter;
public class GameCore : MonoBehaviour
{
[SerializeField] private ViewOne _view1;
[SerializeField] private ViewTwo _view2;
[SerializeField] private Player _playerPrefab;
private Controller _controller;
private void Awake()
{
InitializeUI();
InitializePlayer(_controller);
}
private void InitializePlayer(Controller controller)
{
var player = Instantiate(_playerPrefab);
player.Initialize(controller);
}
private void InitializeUI()
{
_controller = new Controller(_view1, _view2);
}
}
using System;
using System.Collections.Generic;
public class Model
{
public event Action<int[]> ModelUpdated;
private List<int> _integers = new List<int>();
public void Add(int integer)
{
_integers.Add(integer);
ModelUpdated?.Invoke(_integers.ToArray());
}
public void RemoveRandom()
{
var random = new Random();
RemoveAt(random.Next(0, _integers.Count));
}
private void RemoveAt(int integer)
{
_integers.RemoveAt(integer);
ModelUpdated?.Invoke(_integers.ToArray());
}
}
public interface IIntegersView
{
void UpdateView(int[] integers);
}
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class ViewOne : MonoBehaviour, IIntegersView
{
private Text _text;
private void Start()
{
_text = GetComponent<Text>();
}
public void UpdateView(int[] integers)
{
var text = " ";
foreach (var i in integers)
{
text += $"{i}, ";
}
_text.text = text;
}
}
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class ViewTwo : MonoBehaviour, IIntegersView
{
private Text _text;
private void Start()
{
_text = GetComponent<Text>();
}
public void UpdateView(int[] integers)
{
var group = integers.Where(x => x % 3 == 0).ToArray();
var text = " ";
foreach(var i in group)
{
text += $"{i}, ";
}
_text.text = text;
}
}
public class Controller
{
private Model _model;
private IIntegersView _view1;
private IIntegersView _view2;
public Controller(IIntegersView view1, IIntegersView view2)
{
_view1 = view1;
_view2 = view2;
_model = new Model();
_model.ModelUpdated += _view1.UpdateView;
_model.ModelUpdated += _view2.UpdateView;
}
public void AddInteger(int integer) => _model.Add(integer);
public void RemoveInteger() => _model.RemoveRandom();
}
using UnityEngine;
using Random = UnityEngine.Random;
public class Player : MonoBehaviour
{
[SerializeField] private ViewOne _view1;
[SerializeField] private ViewTwo _view2;
private Controller _controller;
private void Start()
{
_controller = new Controller(_view1, _view2);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
_controller.AddInteger(Random.Range(1, 100));
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
_controller.RemoveInteger();
}
}
}