Ramaraunt1

race

Jan 9th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Race : MonoBehaviour {
  6.  
  7. public List<GameObject> checkPoints;
  8. public List<GameObject> cars;
  9. public List<int> laps;
  10. public List<int> cur_checkpoint;
  11. public List<int> winners;
  12. public int lapsToWin;
  13.  
  14. void start()
  15. {
  16. 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)
  17. {
  18. winners.Add(-1);
  19. cur_checkpoint.Add(-1);
  20. cars[x].GetComponent<Dot_Truck_Controller>().ID = x; //set car IDs
  21. }
  22. }
  23.  
  24. public bool addLap (int ID) //this method adds a lap, and returns true if the race is finished and adds the car to winners
  25. {
  26. if (laps[ID] + 1 < lapsToWin)
  27. {
  28. laps[ID]++;
  29. return false;
  30. }
  31. else
  32. {
  33. addToWinners(ID);
  34. return true;
  35. }
  36. }
  37.  
  38. 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.
  39. {
  40. if (cur_checkpoint[ID] + 1 < checkPoints.Count - 1)
  41. {
  42. cur_checkpoint[ID]++;
  43. return false;
  44. }
  45. else
  46. {
  47. cur_checkpoint[ID] = 0;
  48. return addLap(ID);
  49. }
  50. }
  51.  
  52. public bool testIfDone() //check if the race is complete
  53. {
  54. bool test = false;
  55. for (int x = 0; x < winners.Count; x ++)
  56. {
  57. if (winners[x] == -1)
  58. {
  59. test = true;
  60. }
  61. }
  62. return !test;
  63. }
  64.  
  65. public void addToWinners(int ID) //this method adds the car to the winners list at the top
  66. {
  67. bool placed = false;
  68. for (int x = 0; x < winners.Count; x ++)
  69. {
  70. if (!placed && winners[x] == -1)
  71. {
  72. winners[x] = ID;
  73. placed = true;
  74. }
  75. }
  76. broadcast(ID, Broadcast.raceFinished);
  77. }
  78.  
  79. public void addToLosers(int ID) //this method adds the car to the winners list at the bottom
  80. {
  81. bool placed = false;
  82. for (int x = winners.Count - 1; x >= 0; x--)
  83. {
  84. if (!placed && winners[x] == -1)
  85. {
  86. winners[x] = ID;
  87. placed = true;
  88. }
  89. }
  90. broadcast(ID, Broadcast.carWasted);
  91. }
  92.  
  93. 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.
  94. {
  95. List<bool> bools = new List<bool>();
  96. bools[0] = false;
  97. bools[1] = false;
  98. if (checkPoint_ID == cur_checkpoint[car_ID] + 1)
  99. {
  100. bools[1] = addCheckPoint(car_ID);
  101. bools[0] = true;
  102. }
  103. return bools;
  104. }
  105.  
  106. public void broadcast(int ID, Broadcast message)
  107. {
  108. for (int x = 0; x < cars.Count; x ++)
  109. {
  110. cars[x].GetComponent<Dot_Truck_Controller>().Broadcast(ID, message);
  111. }
  112. }
  113.  
  114.  
  115. }
  116.  
  117. public enum Broadcast
  118. {
  119. raceFinished,
  120. carWasted,
  121. checkPointCalled
  122. }
Advertisement
Add Comment
Please, Sign In to add comment