Programming the Time Manager

Velicous racing has a time trail mode(endurance) inside of it, for this game made to work a manger is needed that records the players lap information and time remaining. The lap information will be stored so it can be viewed later, while if the time remaining variable reaches zero the game is over.

The time manager script itself is completely self contained for safety and relatively simple. It contains a timer which is used to record both the players lap time and the time remaining, it also contains some other variables such as how far the player has driven, the number of laps they have done and a check to see if they have passed a checkpoint.

The script is illustrated below,

New-Mind-Map (12)

As shown the script is essentially a timer, all other variables contained within it are arbitrary variables that are used to control whether the game is over or record the players performance.

The timer script also contains two methods, one is activated every time the player passes a checkpoint, this ‘checkpoint’ method adds some time to the players total remaining time and checks to see if the player has passed all of the checkpoints, if he has it is presumed he has completed a lap. Finally the checkpoint method talks to the player’s UI to give some visual feedback when they pass through a checkpoint.

The other method contained on this script is activated when all of the checkpoints in the level have been passed and thus a lap completed. This ‘lap’ completed method checks the players lap time to see if it is a personal best and updates the player preferences accordingly. It also resets the lap timer and adds to the lap count.

The entire script is pasted below for reference.

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

[RequireComponent(typeof(Player_UI))]
[RequireComponent(typeof(Player_Motor))]
public class Player_TimeManager_Script : MonoBehaviour {

 public static bool GameOver = false;
 private float distanceDriven = 0;
 public float DistanceDriven { get { return distanceDriven; } }
 private float bestDistance = 0;
 public float BestDistanceDriven { get { return bestDistance; } }

 private float timeLeft = 80;
 public float TimeLeft { get { return timeLeft; } }

 private int lapCount = 1;
 public float LapCount { get { return lapCount; } }

 private float lapTimeSeconds = 0;
 private string formattedLapTime = "00 : 00";
 public string FormattedLapTime { get { return formattedLapTime; } }

 private float bestLapSeconds = 0;
 private string formattedBestLapTime = "00 : 00";
 public string FormattedBestLapTime { get { return formattedBestLapTime; } }

 private int numberOfCheckPointsPassed = 0;
 private int numberOfCheckPointsInLevel;

 private Player_UI playerUI;
 private Player_Motor playerMotor;

 void Start()
 {
  GameOver = false;
  playerUI = GetComponent<Player_UI>();
  playerMotor = GetComponent<Player_Motor>();
  numberOfCheckPointsInLevel =
  GameObject.FindGameObjectsWithTag("Checkpoint").Length;

  bestDistance = PlayerPrefs.GetFloat("BestDistanceDriven");
  bestLapSeconds = PlayerPrefs.GetFloat("BestLapSeconds");
 }

 void Update()
 {
  if(numberOfCheckPointsPassed != 0)
  {
    timeLeft -= Time.fixedDeltaTime;
    lapTimeSeconds += Time.fixedDeltaTime;
    FormatLapTime(lapTimeSeconds);
    if (GameOver == false)
    {
      int speed = Mathf.RoundToInt((playerMotor.ForwardSpeed() / 2));
      distanceDriven += speed * Time.fixedDeltaTime;
    }

    if(distanceDriven > bestDistance)
    {
      bestDistance = distanceDriven;
    }
  }

  if(timeLeft <= 0)
  {
    GameOver = true;
    if (bestLapSeconds != 0 && bestLapSeconds < PlayerPrefs.GetFloat("BestLapSeconds"))     {       PlayerPrefs.SetFloat("BestLapSeconds", bestLapSeconds);     }     if(bestDistance != 0 && bestDistance >
    PlayerPrefs.GetFloat("BestDistanceDriven"))
    {
      PlayerPrefs.SetFloat("BestDistanceDriven", bestDistance);
    }
  }
}

private void FormatLapTime(float _seconds)
{
  string minutes = Mathf.Floor(_seconds / 60).ToString("00");
  string seconds = Mathf.Floor(_seconds % 60).ToString("00");
  formattedLapTime = minutes + " : " + seconds;
}

public void CheckPointPassed()
{
  timeLeft += 10;
  numberOfCheckPointsPassed += 1;
  if(numberOfCheckPointsPassed > numberOfCheckPointsInLevel)
  {
    numberOfCheckPointsPassed = 1;
    LapComplete();
  }
  playerUI.StartCoroutine("FlashCheckPoint");
}

private void LapComplete()
{
  if(lapTimeSeconds < bestLapSeconds || bestLapSeconds == 0)
  {
    bestLapSeconds = lapTimeSeconds;
    string minutes = Mathf.Floor(lapTimeSeconds / 60).ToString("00");
    string seconds = Mathf.Floor(lapTimeSeconds % 60).ToString("00");
    formattedBestLapTime = minutes + " : " + seconds;
  }
  lapTimeSeconds = 0;
  lapCount += 1;
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: