SHARE
TWEET

KerbinBajaRacingforKerbTown

a guest Sep 16th, 2013 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7.  
  8. namespace InfiniteDiceBajaRacing
  9. {
  10.     //InfiniteDice Kerbin Baja Racing - Written by InfiniteDice. Kudos to Raz for making Kerbtown, without him this wouldn't be possible.
  11.     //Thanks to Snjo for finding some sharp edges here and there, I think I have smoothed those out now. Please enjoy and be a good sportsman!
  12.     //This is dedicated to all those kids who play Kerbal Space and to my son and daughter. Let the racing begin!
  13. //shared as an Attribution-NonCommercial-NoDerivs 3.0 Unported Licence.
  14.     public class bajaRacing : MonoBehaviour
  15.     {
  16.         public bool doItOnce = true;
  17.         //public bool shiftToBegin = true;
  18.  
  19.         //public int lapCounter;
  20.  
  21.         public int lastWaypointReached = 0;
  22.  
  23.         public float distToTarget;
  24.  
  25.         public float raceStartTime;
  26.         public float lapStartTime;
  27.  
  28.         public bool raceStarted = false;
  29.  
  30.         public bool tripLastLeg = false;
  31.  
  32.         public Transform waypointOne, waypointTwo, waypointThree, waypointFour, waypointFive, waypointSix;
  33.         public Transform trackDistToWaypointThis;
  34.  
  35.         public Transform billboardStartLoc;
  36.  
  37.         public int wayPointsOnTrack = 6;
  38.  
  39.         public int raceLaps = 3;//remaining
  40.  
  41.         public int remainingLaps;
  42.  
  43.         //[KSPField(isPersistant = true)] // I'd like this to be permanent but not sure if kerbtown supports it.
  44.         public float timeToBeat;
  45.  
  46.         public float lastLapTime;
  47.  
  48.         public float fastestSpeed;
  49.  
  50.  
  51.  
  52.  
  53.         public void Update()
  54.         {
  55.             if (!HighLogic.LoadedSceneIsFlight) return;
  56.             {
  57.                 if (raceStarted == false && distToTarget < 6f && FlightGlobals.ActiveVessel.horizontalSrfSpeed <= 0.2f && Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift) || Input.GetKeyUp(KeyCode.RightApple))
  58.                 {
  59.                     remainingLaps = raceLaps;
  60.                     raceStarted = true;
  61.                     raceStartTime = Time.time;
  62.                     lapStartTime = Time.time;
  63.                     ScreenMessages.PostScreenMessage(new ScreenMessage("GO GO GO!", 3f, ScreenMessageStyle.UPPER_CENTER));
  64.                 }
  65.  
  66.                 if (remainingLaps == 0 && raceStarted == true)
  67.                 {
  68.                     raceStarted = false;
  69.                     raceStartTime = 0f;
  70.                     lapStartTime = 0f;
  71.                     tripLastLeg = false;
  72.                     lastWaypointReached = 0;
  73.                     waypointOne.light.color = new Color(0f, 1f, 0f);
  74.                     waypointTwo.light.color = new Color(0f, 1f, 0f);
  75.                     waypointThree.light.color = new Color(0f, 1f, 0f);
  76.                     waypointFour.light.color = new Color(0f, 1f, 0f);
  77.                     waypointFive.light.color = new Color(0f, 1f, 0f);
  78.                     waypointSix.light.color = new Color(0f, 1f, 0f);
  79.                     ScreenMessages.PostScreenMessage(new ScreenMessage("Race Over! Track Resetting...", 4f, ScreenMessageStyle.LOWER_CENTER));
  80.                 }
  81.  
  82.                 if (FlightGlobals.ActiveVessel.horizontalSrfSpeed > fastestSpeed && raceStarted == true)
  83.                 {
  84.                     fastestSpeed = Mathf.Round((float)FlightGlobals.ActiveVessel.horizontalSrfSpeed);
  85.                 }
  86.  
  87.                 if (raceStarted == true && Input.GetKeyUp(KeyCode.End) || Input.GetKeyUp(KeyCode.RightApple))
  88.                 {
  89.                     raceStarted = false;
  90.                     raceStartTime = 0f;
  91.                     lapStartTime = 0f;
  92.                     tripLastLeg = false;
  93.                     lastWaypointReached = 0;
  94.                     waypointOne.light.color = new Color(0f, 1f, 0f);
  95.                     waypointTwo.light.color = new Color(0f, 1f, 0f);
  96.                     waypointThree.light.color = new Color(0f, 1f, 0f);
  97.                     waypointFour.light.color = new Color(0f, 1f, 0f);
  98.                     waypointFive.light.color = new Color(0f, 1f, 0f);
  99.                     waypointSix.light.color = new Color(0f, 1f, 0f);
  100.                     ScreenMessages.PostScreenMessage(new ScreenMessage("Race Aborted! Track Reset...", 4f, ScreenMessageStyle.UPPER_CENTER));
  101.                 }
  102.             }
  103.  
  104.         }
  105.  
  106.         public void FixedUpdate()
  107.         {
  108.             if (!HighLogic.LoadedSceneIsFlight) return;
  109.             {
  110.                 if (doItOnce == true)
  111.                 {
  112.                     try
  113.                     {
  114.                         GameObject fWGO = GameObject.Find("wayP1");
  115.                         waypointOne = fWGO.transform;
  116.                         //waypointOne = GameObject.Find("wayP1").transform;
  117.                         print("Found Waypoint 1 Transform!");
  118.                         print(fWGO.transform.position);
  119.                     }
  120.                     catch
  121.                     {
  122.                         print("failed to find waypoint 1 transform");
  123.                     }
  124.                     try
  125.                     {
  126.                         GameObject fWGO = GameObject.Find("wayP2");
  127.                         waypointTwo = fWGO.transform;
  128.                         //waypointOne = GameObject.Find("wayP1").transform;
  129.                         print("Found Waypoint 2 Transform!");
  130.                         print(fWGO.transform.position);
  131.                     }
  132.                     catch
  133.                     {
  134.                         print("failed to find waypoint 1 transform");
  135.                     }
  136.                     try
  137.                     {
  138.                         GameObject fWGO = GameObject.Find("wayP3");
  139.                         waypointThree = fWGO.transform;
  140.                         //waypointOne = GameObject.Find("wayP1").transform;
  141.                         print("Found Waypoint 3 Transform!");
  142.                         print(fWGO.transform.position);
  143.                     }
  144.                     catch
  145.                     {
  146.                         print("failed to find waypoint 3 transform");
  147.                     }
  148.                     try
  149.                     {
  150.                         GameObject fWGO = GameObject.Find("wayP4");
  151.                         waypointFour = fWGO.transform;
  152.                         //waypointOne = GameObject.Find("wayP1").transform;
  153.                         print("Found Waypoint 4 Transform!");
  154.                         print(fWGO.transform.position);
  155.                     }
  156.                     catch
  157.                     {
  158.                         print("failed to find waypoint 4 transform");
  159.                     }
  160.                     try
  161.                     {
  162.                         GameObject fWGO = GameObject.Find("wayP5");
  163.                         waypointFive = fWGO.transform;
  164.                         //waypointOne = GameObject.Find("wayP1").transform;
  165.                         print("Found Waypoint 5 Transform!");
  166.                         print(fWGO.transform.position);
  167.                     }
  168.                     catch
  169.                     {
  170.                         print("failed to find waypoint 5 transform");
  171.                     }
  172.                     try
  173.                     {
  174.                         GameObject fWGO = GameObject.Find("wayP6");
  175.                         waypointSix = fWGO.transform;
  176.                         //waypointOne = GameObject.Find("wayP1").transform;
  177.                         print("Found Waypoint 6 Transform!");
  178.                         print(fWGO.transform.position);
  179.                     }
  180.                     catch
  181.                     {
  182.                         print("failed to find waypoint 6 transform");
  183.                     }
  184.                     try
  185.                     {
  186.                         GameObject fWGO = GameObject.Find("BillboardStart");//this does nothing now! but will perhaps be used to position an explosion or other effect for the end of the race! :)
  187.                         billboardStartLoc = fWGO.transform;
  188.                         print("Found BillBoard Startline Transform!");
  189.                         print(fWGO.transform.position);
  190.                     }
  191.                     catch
  192.                     {
  193.                         print("failed to find BillBoard Startline transform");
  194.                     }
  195.  
  196.                    
  197.                     ScreenMessages.PostScreenMessage(new ScreenMessage("Head over to the track and stop on the start/finish line to begin!", 8f, ScreenMessageStyle.UPPER_CENTER));
  198.                     doItOnce = false;
  199.                 }
  200.  
  201.  
  202.                 if (lastWaypointReached == 0)
  203.                 {
  204.                     trackDistToWaypointThis = waypointOne;
  205.                     waypointOne.light.color = new Color(0f, 1f, 0f);
  206.                 }
  207.                 else if (lastWaypointReached == 1)
  208.                 {
  209.                     trackDistToWaypointThis = waypointTwo;
  210.                     waypointOne.light.color = new Color(1f, 0f, 0f);
  211.                 }
  212.                 else if (lastWaypointReached == 2)
  213.                 {
  214.                     trackDistToWaypointThis = waypointThree;
  215.                     waypointTwo.light.color = new Color(1f, 0f, 0f);
  216.                 }
  217.                 else if (lastWaypointReached == 3)
  218.                 {
  219.                     trackDistToWaypointThis = waypointFour;
  220.                     waypointThree.light.color = new Color(1f, 0f, 0f);
  221.                 }
  222.                 else if (lastWaypointReached == 4)
  223.                 {
  224.                     trackDistToWaypointThis = waypointFive;
  225.                     waypointFour.light.color = new Color(1f, 0f, 0f);
  226.                 }
  227.                 else if (lastWaypointReached == 5)
  228.                 {
  229.                     trackDistToWaypointThis = waypointSix;
  230.                     waypointFive.light.color = new Color(1f, 0f, 0f);
  231.  
  232.                 }
  233.                 else if (lastWaypointReached == 6)
  234.                 {
  235.                     trackDistToWaypointThis = waypointOne;
  236.                     waypointSix.light.color = new Color(1f, 0f, 0f);
  237.                     tripLastLeg = true;
  238.                 }
  239.  
  240.  
  241.                 distToTarget = (trackDistToWaypointThis.position - FlightGlobals.ActiveVessel.transform.position).magnitude;
  242.                 //print(distToTarget);
  243.  
  244.                 if (raceStarted == true && distToTarget < 6f)
  245.                 {
  246.                     lastWaypointReached += 1;
  247.                     ScreenMessages.PostScreenMessage(new ScreenMessage("Checkpoint Passed!", 2f, ScreenMessageStyle.UPPER_CENTER));
  248.                     if (lastWaypointReached > wayPointsOnTrack)
  249.                     {
  250.                         lastWaypointReached = 1;
  251.                     }
  252.                 }
  253.  
  254.                 if (raceStarted == false && distToTarget < 6f && FlightGlobals.ActiveVessel.horizontalSrfSpeed <= 0.2f)
  255.                 {
  256.                     ScreenMessages.PostScreenMessage(new ScreenMessage("Press SHIFT to Begin!", 1f, ScreenMessageStyle.UPPER_CENTER));
  257.  
  258.                 }
  259.  
  260.                 if (raceStarted == true && tripLastLeg == true && lastWaypointReached == 1) //Current lap is over
  261.                 {
  262.                     remainingLaps -= 1;
  263.                     lastLapTime = Time.time - lapStartTime;
  264.                     print("Last Laptime was: " + lastLapTime);
  265.                     ScreenMessages.PostScreenMessage(new ScreenMessage("Lap Time: " + lastLapTime, 4f, ScreenMessageStyle.UPPER_CENTER));
  266.                     if (lastLapTime < timeToBeat)
  267.                     {
  268.                         timeToBeat = lastLapTime;
  269.                         ScreenMessages.PostScreenMessage(new ScreenMessage("Your Best Lap ever! " + timeToBeat, 8f, ScreenMessageStyle.UPPER_CENTER));
  270.                         print("Best Laptime was: " + timeToBeat);
  271.                     }
  272.  
  273.                     tripLastLeg = false; //Reset the last leg checker.
  274.  
  275.                     if (remainingLaps == 0) //Race is over
  276.                     {
  277.                         float raceTotalTime = Time.time - raceStartTime;
  278.                         print("Total " + raceLaps + " Lap Race Time: " + raceTotalTime);
  279.                         ScreenMessages.PostScreenMessage(new ScreenMessage("The Race is Over! Total Time: " + raceTotalTime, 12f, ScreenMessageStyle.UPPER_CENTER));
  280.                         print("Top Recorded Speed: " + fastestSpeed);
  281.                         ScreenMessages.PostScreenMessage(new ScreenMessage("Top Speed: " + fastestSpeed, 12f, ScreenMessageStyle.UPPER_CENTER));
  282.                     }
  283.                     else if (remainingLaps > 0)
  284.                     {
  285.                         lapStartTime = Time.time;
  286.                         //waypointOne.light.color = new Color(0f, 1f, 0f);
  287.                         waypointTwo.light.color = new Color(0f, 1f, 0f);
  288.                         waypointThree.light.color = new Color(0f, 1f, 0f);
  289.                         waypointFour.light.color = new Color(0f, 1f, 0f);
  290.                         waypointFive.light.color = new Color(0f, 1f, 0f);
  291.                         waypointSix.light.color = new Color(0f, 1f, 0f);
  292.                     }
  293.  
  294.                 }
  295.  
  296.  
  297.  
  298.             }
  299.         }
  300.     }
  301. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top