motkot
@motkot
Программирование C#.

Как сделать этот код более приемлемым?

Есть несколько классов, которые хранятся на комплектующих компьютера. Я везде проверяю, что за компонент я вставляю в компьютер. В магазине проверяю, что за компонент хочет купить игрок. Как от этого избавится?

Код
using UnityEngine;
using TMPro;

public class Buy : MonoBehaviour
{
    public Money Money;

    [SerializeField] Motherboard _currentMotherboard;
    [SerializeField] Power _currentPower;
    [SerializeField] Hdd _currentHdd;
    [SerializeField] Processor _currentProcessor;
    [SerializeField] Cooler _currentCooler;
    [SerializeField] Ram _currentRam;

    [Header("Motherboard info")]
    [SerializeField] GameObject _motherboardCanvas;

    [SerializeField] TMP_Text _motherboardPriceText;
    [SerializeField] TMP_Text _motherboardSocket;
    [SerializeField] TMP_Text _motherboardName;

    [Header("Power info")]
    [SerializeField] GameObject _powerCanvas;

    [SerializeField] TMP_Text _powerPriceText;
    [SerializeField] TMP_Text _powerCharge;
    [SerializeField] TMP_Text _powerName;

    [Header("Hdd info")]
    [SerializeField] GameObject _hddCanvas;

    [SerializeField] TMP_Text _hddPriceText;
    [SerializeField] TMP_Text _hddMemoryAmount;
    [SerializeField] TMP_Text _hddName;

    [Header("Processor info")]
    [SerializeField] GameObject _processorCanvas;

    [SerializeField] TMP_Text _processorPriceText;
    [SerializeField] TMP_Text _processorCoresAmount;
    [SerializeField] TMP_Text _processorThreads;
    [SerializeField] TMP_Text _processorSocket;
    [SerializeField] TMP_Text _processorName;

    [Header("Cooler info")]
    [SerializeField] GameObject _coolerCanvas;

    [SerializeField] TMP_Text _coolerPriceText;
    [SerializeField] TMP_Text _coolerCold;
    [SerializeField] TMP_Text _coolerName;

    [Header("Ram info")]
    [SerializeField] GameObject _ramCanvas;

    [SerializeField] TMP_Text _ramPriceText;
    [SerializeField] TMP_Text _ramMemoryAmount;
    [SerializeField] TMP_Text _ramName;

    public void RemoveAll()
    {
        _currentMotherboard = null;
        _currentPower = null;
        _currentHdd = null;
        _currentProcessor = null;
        _currentCooler = null;
        _currentRam = null;
    }

    public void buy(RaycastHit  hit)
    {
        if (hit.collider.GetComponent<Motherboard>())
        {
            ShowInformation(hit.collider.GetComponent<Motherboard>());
        }

        if (hit.collider.GetComponent<Power>())
        {
            ShowInformation(hit.collider.GetComponent<Power>());
        }

        if (hit.collider.GetComponent<Hdd>())
        {
            ShowInformation(hit.collider.GetComponent<Hdd>());
        }

        if (hit.collider.GetComponent<Processor>())
        {
             ShowInformation(hit.collider.GetComponent<Processor>());
        }

        if (hit.collider.GetComponent<Cooler>())
        {
            ShowInformation(hit.collider.GetComponent<Cooler>());
        }

        if (hit.collider.GetComponent<Ram>())
        {
            ShowInformation(hit.collider.GetComponent<Ram>());
        }
    }

    public void CompleteBuy()
    {
        if(_currentMotherboard)
        {
            if(_currentMotherboard.Price <= Money.Amount)
            {
                Money.Amount -= _currentMotherboard.Price;
                Money.RefreshValue();
            }
        }
        else if (_currentPower)
        {
            if (_currentPower.Price <= Money.Amount)
            {
                Money.Amount -= _currentPower.Price;
                Money.RefreshValue();
            }
        }
        else if(_currentHdd)
        {
            if (_currentHdd.Price <= Money.Amount)
            {
                Money.Amount -= _currentHdd.Price;
                Money.RefreshValue();
            }
        }
        else if(_currentProcessor)
        {
            if (_currentProcessor.Price <= Money.Amount)
            {
                Money.Amount -= _currentProcessor.Price;
                Money.RefreshValue();
            }
        }
        else if (_currentCooler)
        {
            if (_currentCooler.Price <= Money.Amount)
            {
                Money.Amount -= _currentCooler.Price;
                Money.RefreshValue();
            }
        }
        else if (_currentRam)
        {
            if (_currentRam.Price <= Money.Amount)
            {
                Money.Amount -= _currentRam.Price;
                Money.RefreshValue();
            }
        }
    }

    private void ShowInformation(Motherboard motherboard)
    {
        _motherboardSocket.text = "Сокет: " + motherboard.SocketName;
        _motherboardName.text = "Название: " + motherboard.Name;
        _motherboardPriceText.text = "Цена: " + motherboard.Price + " руб.";

        _currentMotherboard = motherboard;

        _motherboardCanvas.SetActive(true);
    }

    private void ShowInformation(Power power)
    {
        _powerCharge.text = "Мощность: " + power.Charge + "W";
        _powerName.text = "Название: " + power.Name;
        _powerPriceText.text = "Цена: " + power.Price + " руб.";

        _currentPower = power;

        _powerCanvas.SetActive(true);
    }

    private void ShowInformation(Hdd hdd)
    {
        _hddMemoryAmount.text = "Количество памяти: " + hdd.MemoryAmount + "GB";
        _hddName.text = "Название: " + hdd.Name;
        _hddPriceText.text = "Цена: " + hdd.Price + " руб.";

        _currentHdd = hdd;

        _hddCanvas.SetActive(true);
    }

    private void ShowInformation(Processor processor)
    {
        _processorName.text = "Название: " + processor.Name;
        _processorCoresAmount.text = "Количество ядер: " + processor.CoresCount;
        _processorThreads.text = "Частота процессора: " + processor.Threads + "GHZ";
        _processorSocket.text = "Сокет процессора: " + processor.SocketName;
        _processorPriceText.text = "Цена: " + processor.Price + " руб.";

        _currentProcessor = processor;

        _processorCanvas.SetActive(true);
    }

    private void ShowInformation(Cooler cooler)
    {
        _coolerName.text = "Название: " + cooler.Name;
        _coolerCold.text = "Охлаждение: " + cooler.Cold + " градусов";
        _coolerPriceText.text = "Цена: " + cooler.Price + " руб.";

        _currentCooler = cooler;

        _coolerCanvas.SetActive(true);
    }

    private void ShowInformation(Ram ram)
    {
        _ramMemoryAmount.text = "Количество памяти: " + ram.MemoryAmount + " GB";
        _ramName.text = "Название: " + ram.Name;
        _ramPriceText.text = "Цена: " + ram.Price + " руб.";

        _currentRam = ram;

        _ramCanvas.SetActive(true);
    }
}
  • Вопрос задан
  • 189 просмотров
Решения вопроса 1
@Voland69
У классов Motherboard, Power и т.п. выделить общее поведение в родительский класс (например Component) и метод ShowInformation тоже сделать с параметром типа Component.
CheckItem аналогично.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы