Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Race : MonoBehaviour {
- public List<GameObject> checkPoints;
- public List<GameObject> cars;
- public List<int> laps;
- public List<int> cur_checkpoint;
- public List<int> winners;
- public int lapsToWin;
- void start()
- {
- for (int x = 0; x < cars.Count; x ++) //init the winners list to have a slot for each car (set to -1 to show that the slot is not used yet)
- {
- winners.Add(-1);
- cur_checkpoint.Add(-1);
- cars[x].GetComponent<Dot_Truck_Controller>().ID = x; //set car IDs
- }
- }
- public bool addLap (int ID) //this method adds a lap, and returns true if the race is finished and adds the car to winners
- {
- if (laps[ID] + 1 < lapsToWin)
- {
- laps[ID]++;
- return false;
- }
- else
- {
- addToWinners(ID);
- return true;
- }
- }
- public bool addCheckPoint(int ID) //this method adds a checkpoint for a car, and if it makes a complete lap it adds a lap and ressets the checkpoint count. returns true if race is finished.
- {
- if (cur_checkpoint[ID] + 1 < checkPoints.Count - 1)
- {
- cur_checkpoint[ID]++;
- return false;
- }
- else
- {
- cur_checkpoint[ID] = 0;
- return addLap(ID);
- }
- }
- public bool testIfDone() //check if the race is complete
- {
- bool test = false;
- for (int x = 0; x < winners.Count; x ++)
- {
- if (winners[x] == -1)
- {
- test = true;
- }
- }
- return !test;
- }
- public void addToWinners(int ID) //this method adds the car to the winners list at the top
- {
- bool placed = false;
- for (int x = 0; x < winners.Count; x ++)
- {
- if (!placed && winners[x] == -1)
- {
- winners[x] = ID;
- placed = true;
- }
- }
- broadcast(ID, Broadcast.raceFinished);
- }
- public void addToLosers(int ID) //this method adds the car to the winners list at the bottom
- {
- bool placed = false;
- for (int x = winners.Count - 1; x >= 0; x--)
- {
- if (!placed && winners[x] == -1)
- {
- winners[x] = ID;
- placed = true;
- }
- }
- broadcast(ID, Broadcast.carWasted);
- }
- public List<bool> checkCheckPoint(int car_ID, int checkPoint_ID) //this checks if the passed checkpoint is the next one, and returns true if it is then adds it. If this ends the race, it sends a second bool of true, otherwise it is false.
- {
- List<bool> bools = new List<bool>();
- bools[0] = false;
- bools[1] = false;
- if (checkPoint_ID == cur_checkpoint[car_ID] + 1)
- {
- bools[1] = addCheckPoint(car_ID);
- bools[0] = true;
- }
- return bools;
- }
- public void broadcast(int ID, Broadcast message)
- {
- for (int x = 0; x < cars.Count; x ++)
- {
- cars[x].GetComponent<Dot_Truck_Controller>().Broadcast(ID, message);
- }
- }
- }
- public enum Broadcast
- {
- raceFinished,
- carWasted,
- checkPointCalled
- }
Advertisement
Add Comment
Please, Sign In to add comment