Mobile Game Tutorial part 06 – singleton pattern & scores.

Hi, part 5 of mobile game is available, in this part i wil lteach you how to create game manager using singleton pattern and how to calculate distance travelled by rocket.

CODE:

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


public class GameManager : MonoBehaviour
{
    public static GameManager instance { get; private set; }
    public TextMeshProUGUI moneyTXT;
    public float moneyBonusMultiplier = 2;
    float money;
    public bool GamePaused;

    public void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else if( instance != null)
        {
            Destroy(gameObject);
        }
    }

    public void AddMoney(float _moneyToAdd)
    {
        _moneyToAdd = Mathf.Round(_moneyToAdd);
        money += _moneyToAdd * moneyBonusMultiplier;
        moneyTXT.text = money.ToString() + " $";
    }

    public void PauseGame()
    {
        Time.timeScale = 0;
        GamePaused = true;

    }
    // Start is called before the first frame update
    void Start()
    {
        GamePaused = false;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;


public class RocketController : MonoBehaviour
{
    public Image fuelBarMask;
    public float maxFuel = 10;
    public float fuelConsumptionSpeed = 0.1f;
    public float thrustForce = 10;
    public float strafeSpeed = 5;
    float currentFuel;
    Vector2 touchPos;
    Rigidbody2D rb2d;

    float startPos;
    float longestDstTravelled = 0;
    public TextMeshProUGUI dstTravelledTXT;
    // Start is called before the first frame update
    void Start()
    {
        currentFuel = maxFuel;
        rb2d = GetComponent<Rigidbody2D>();
        startPos = this.transform.position.y;

        longestDstTravelled = 0;
    }


    // Update is called once per frame
    void FixedUpdate()
    {
        if(!GameManager.instance.GamePaused)
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            switch (touch.phase)
            {
                case TouchPhase.Began:
                    touchPos = Camera.main.ScreenToWorldPoint(touch.position);
                    break;
                case TouchPhase.Moved:
                    touchPos = Camera.main.ScreenToWorldPoint(touch.position);
                    Strafe();
                    AddThrustForce();
                    break;
                case TouchPhase.Stationary:
                    AddThrustForce();
                    Strafe();
                    break;
                case TouchPhase.Ended:
                    break;

            }
        }
    }

    private void Update()
    {
        if(!GameManager.instance.GamePaused)
        DistanceTravelled();
    }
    public void DistanceTravelled()
    {
        float currentDstTravelled = this.transform.position.y - startPos;
        if (currentDstTravelled < 0)
        {
            currentDstTravelled = 0;
        }
        if (currentDstTravelled > longestDstTravelled)
        {
            longestDstTravelled = currentDstTravelled;
        }

        dstTravelledTXT.text = currentDstTravelled.ToString("F1") + " m";

        if(currentFuel<=0 && rb2d.velocity.y <= 0)
        {
            GameManager.instance.PauseGame();
            GameManager.instance.AddMoney(longestDstTravelled);
        }
    }
    public void AddThrustForce()
    {
        if (currentFuel > 0)
        {
            Vector2 _thrustForceVector = new Vector2(0, thrustForce);
            rb2d.AddForce(_thrustForceVector);
            ConsumeFuel();
        }
    }

    public void ConsumeFuel()
    {
        currentFuel -= fuelConsumptionSpeed; // currentFuel = currentFuel - fuelConsumptionSpeed;

        float fill = currentFuel / maxFuel;
        fuelBarMask.fillAmount = fill;
    }

    public void Strafe()
    {
        Vector2 _strafeVector = new Vector2(touchPos.x, transform.position.y);
        if (currentFuel > 0)
        {
            transform.position = Vector2.MoveTowards(transform.position, _strafeVector, strafeSpeed * Time.fixedDeltaTime);
        }
        if (currentFuel < 0)
        {
            transform.position = Vector2.MoveTowards(transform.position, _strafeVector, (strafeSpeed/3) * Time.fixedDeltaTime);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *