Home > OS >  Proper method calls from a common class in Unity
Proper method calls from a common class in Unity

Time:12-15

I'm trying to write code nice and "right", the point is that I have a class Timer, which has two methods "time to start the game" and "time to end the game", both methods are public. Also I have a class that scene controller, in which I take into account the timer values and output them to the UI. I have a dilemma, where do I call the timer methods? In Update controller or in Update timer? It sounds silly of course, but I would like to understand how to do it better. Timer script is a general script, that is, it should not be tied to any scene or UI interface.

Timer:


`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Timer : MonoBehaviour
{
    public float waitForStartGame = 5;
    public float waitForEndGame = 300;

    private void Update() 
    {
        WaitForEndGame();
        WaitForStartGame();
    }

    public void WaitForEndGame()
    {
        if (waitForEndGame >= 0)
        {
            waitForEndGame -= Time.deltaTime;
        }
    }

    public void WaitForStartGame()
    {
        if (waitForStartGame >= 0)
        {
            waitForStartGame -= Time.deltaTime;
        }
    }
}

`


Controller:


`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SumoController : MonoBehaviour
{
    public bool isWin;
    public bool isGameOver;
    public bool isGameStart;
    public SumoTankController player;
    public SumoTankAI bot;
    public SumoUIManager sumoUIManager;
    public Timer timer;

    private void Start()
    {
        player.GetComponent<SumoTankController>();
        player.sumoController = this;
        player.GetComponentInChildren<SumoTankGun>().sumoController = this;
        bot.GetComponent<SumoTankAI>();
        bot.sumoController = this;
        bot.GetComponentInChildren<SumoTankAIGun>().sumoController = this;
        sumoUIManager.matchResultText.gameObject.SetActive(false);
        sumoUIManager.timeToStartGame.gameObject.SetActive(true);
    }

    private void Update()
    {
        WaitForStartGame();
        WaitForEndGame();
    }

    public void CheckMatchResult()
    {
        if (!isGameOver)
        {
            if (isWin)
            {
                sumoUIManager.matchResultText.text = "Вы победили!";
                sumoUIManager.matchResultText.gameObject.SetActive(true);
            }
            else
            {
                sumoUIManager.matchResultText.text = "Вы проиграли!";
                sumoUIManager.matchResultText.gameObject.SetActive(true);
            }
        }
    }

    public void WaitForStartGame()
    {
        if (timer.waitForStartGame >= 0)
        {
            sumoUIManager.timeToStartGame.text = $"{timer.waitForStartGame:f1}";
        }
        else
        {
            sumoUIManager.timeToStartGame.gameObject.SetActive(false);
            isGameStart = true;
        }
    }

    public void WaitForEndGame(){
        if (timer.waitForEndGame >= 0)
        {
            float minutes = Mathf.FloorToInt(timer.waitForEndGame / 60);
            float seconds = Mathf.FloorToInt(timer.waitForEndGame % 60);
            sumoUIManager.timeToEndGame.text = $"{minutes:00}:{seconds:00}";
        }
        else
        {
            sumoUIManager.backBtn.onClick.Invoke();
        }
    }
}

`

CodePudding user response:

you already called the method in your update in updates under your timer, so they are already counting but if you need a timer, instead of making a separate class only contain timer you can probably just calculate the time difference by saving the time in a variable then subtract it by Time.time or using coroutine

CodePudding user response:

When designing a class you need to understand what the class will/should do. In this case you create a class Timer. This class will update itself and should handle all time actions.

Consider this: When two people are in a contest of a chess game with a clock, why would it make sense to hit the King piece to stop the timer?

You can create a callback with a delegate in the Timer class which gets invoked when the conditions are met.

That last will make the calls in your SumoController.Update unneccessary.

This goes for every class and method. Responsibility, what will be the purpose of this class or method. That is in every application. In game development there is this thing called performance, optimising code for the reduction of execution time is very important. That's a consideration to keep in mind too.

  • Related