Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- namespace InfiniteDiceBajaRacing
- {
- //InfiniteDice Kerbin Baja Racing - Written by InfiniteDice. Kudos to Raz for making Kerbtown, without him this wouldn't be possible.
- //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!
- //This is dedicated to all those kids who play Kerbal Space and to my son and daughter. Let the racing begin!
- //shared as an Attribution-NonCommercial-NoDerivs 3.0 Unported Licence.
- public class bajaRacing : MonoBehaviour
- {
- public bool doItOnce = true;
- //public bool shiftToBegin = true;
- //public int lapCounter;
- public int lastWaypointReached = 0;
- public float distToTarget;
- public float raceStartTime;
- public float lapStartTime;
- public bool raceStarted = false;
- public bool tripLastLeg = false;
- public Transform waypointOne, waypointTwo, waypointThree, waypointFour, waypointFive, waypointSix;
- public Transform trackDistToWaypointThis;
- public Transform billboardStartLoc;
- public int wayPointsOnTrack = 6;
- public int raceLaps = 3;//remaining
- public int remainingLaps;
- //[KSPField(isPersistant = true)] // I'd like this to be permanent but not sure if kerbtown supports it.
- public float timeToBeat;
- public float lastLapTime;
- public float fastestSpeed;
- public void Update()
- {
- if (!HighLogic.LoadedSceneIsFlight) return;
- {
- if (raceStarted == false && distToTarget < 6f && FlightGlobals.ActiveVessel.horizontalSrfSpeed <= 0.2f && Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift) || Input.GetKeyUp(KeyCode.RightApple))
- {
- remainingLaps = raceLaps;
- raceStarted = true;
- raceStartTime = Time.time;
- lapStartTime = Time.time;
- ScreenMessages.PostScreenMessage(new ScreenMessage("GO GO GO!", 3f, ScreenMessageStyle.UPPER_CENTER));
- }
- if (remainingLaps == 0 && raceStarted == true)
- {
- raceStarted = false;
- raceStartTime = 0f;
- lapStartTime = 0f;
- tripLastLeg = false;
- lastWaypointReached = 0;
- waypointOne.light.color = new Color(0f, 1f, 0f);
- waypointTwo.light.color = new Color(0f, 1f, 0f);
- waypointThree.light.color = new Color(0f, 1f, 0f);
- waypointFour.light.color = new Color(0f, 1f, 0f);
- waypointFive.light.color = new Color(0f, 1f, 0f);
- waypointSix.light.color = new Color(0f, 1f, 0f);
- ScreenMessages.PostScreenMessage(new ScreenMessage("Race Over! Track Resetting...", 4f, ScreenMessageStyle.LOWER_CENTER));
- }
- if (FlightGlobals.ActiveVessel.horizontalSrfSpeed > fastestSpeed && raceStarted == true)
- {
- fastestSpeed = Mathf.Round((float)FlightGlobals.ActiveVessel.horizontalSrfSpeed);
- }
- if (raceStarted == true && Input.GetKeyUp(KeyCode.End) || Input.GetKeyUp(KeyCode.RightApple))
- {
- raceStarted = false;
- raceStartTime = 0f;
- lapStartTime = 0f;
- tripLastLeg = false;
- lastWaypointReached = 0;
- waypointOne.light.color = new Color(0f, 1f, 0f);
- waypointTwo.light.color = new Color(0f, 1f, 0f);
- waypointThree.light.color = new Color(0f, 1f, 0f);
- waypointFour.light.color = new Color(0f, 1f, 0f);
- waypointFive.light.color = new Color(0f, 1f, 0f);
- waypointSix.light.color = new Color(0f, 1f, 0f);
- ScreenMessages.PostScreenMessage(new ScreenMessage("Race Aborted! Track Reset...", 4f, ScreenMessageStyle.UPPER_CENTER));
- }
- }
- }
- public void FixedUpdate()
- {
- if (!HighLogic.LoadedSceneIsFlight) return;
- {
- if (doItOnce == true)
- {
- try
- {
- GameObject fWGO = GameObject.Find("wayP1");
- waypointOne = fWGO.transform;
- //waypointOne = GameObject.Find("wayP1").transform;
- print("Found Waypoint 1 Transform!");
- print(fWGO.transform.position);
- }
- catch
- {
- print("failed to find waypoint 1 transform");
- }
- try
- {
- GameObject fWGO = GameObject.Find("wayP2");
- waypointTwo = fWGO.transform;
- //waypointOne = GameObject.Find("wayP1").transform;
- print("Found Waypoint 2 Transform!");
- print(fWGO.transform.position);
- }
- catch
- {
- print("failed to find waypoint 1 transform");
- }
- try
- {
- GameObject fWGO = GameObject.Find("wayP3");
- waypointThree = fWGO.transform;
- //waypointOne = GameObject.Find("wayP1").transform;
- print("Found Waypoint 3 Transform!");
- print(fWGO.transform.position);
- }
- catch
- {
- print("failed to find waypoint 3 transform");
- }
- try
- {
- GameObject fWGO = GameObject.Find("wayP4");
- waypointFour = fWGO.transform;
- //waypointOne = GameObject.Find("wayP1").transform;
- print("Found Waypoint 4 Transform!");
- print(fWGO.transform.position);
- }
- catch
- {
- print("failed to find waypoint 4 transform");
- }
- try
- {
- GameObject fWGO = GameObject.Find("wayP5");
- waypointFive = fWGO.transform;
- //waypointOne = GameObject.Find("wayP1").transform;
- print("Found Waypoint 5 Transform!");
- print(fWGO.transform.position);
- }
- catch
- {
- print("failed to find waypoint 5 transform");
- }
- try
- {
- GameObject fWGO = GameObject.Find("wayP6");
- waypointSix = fWGO.transform;
- //waypointOne = GameObject.Find("wayP1").transform;
- print("Found Waypoint 6 Transform!");
- print(fWGO.transform.position);
- }
- catch
- {
- print("failed to find waypoint 6 transform");
- }
- try
- {
- 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! :)
- billboardStartLoc = fWGO.transform;
- print("Found BillBoard Startline Transform!");
- print(fWGO.transform.position);
- }
- catch
- {
- print("failed to find BillBoard Startline transform");
- }
- ScreenMessages.PostScreenMessage(new ScreenMessage("Head over to the track and stop on the start/finish line to begin!", 8f, ScreenMessageStyle.UPPER_CENTER));
- doItOnce = false;
- }
- if (lastWaypointReached == 0)
- {
- trackDistToWaypointThis = waypointOne;
- waypointOne.light.color = new Color(0f, 1f, 0f);
- }
- else if (lastWaypointReached == 1)
- {
- trackDistToWaypointThis = waypointTwo;
- waypointOne.light.color = new Color(1f, 0f, 0f);
- }
- else if (lastWaypointReached == 2)
- {
- trackDistToWaypointThis = waypointThree;
- waypointTwo.light.color = new Color(1f, 0f, 0f);
- }
- else if (lastWaypointReached == 3)
- {
- trackDistToWaypointThis = waypointFour;
- waypointThree.light.color = new Color(1f, 0f, 0f);
- }
- else if (lastWaypointReached == 4)
- {
- trackDistToWaypointThis = waypointFive;
- waypointFour.light.color = new Color(1f, 0f, 0f);
- }
- else if (lastWaypointReached == 5)
- {
- trackDistToWaypointThis = waypointSix;
- waypointFive.light.color = new Color(1f, 0f, 0f);
- }
- else if (lastWaypointReached == 6)
- {
- trackDistToWaypointThis = waypointOne;
- waypointSix.light.color = new Color(1f, 0f, 0f);
- tripLastLeg = true;
- }
- distToTarget = (trackDistToWaypointThis.position - FlightGlobals.ActiveVessel.transform.position).magnitude;
- //print(distToTarget);
- if (raceStarted == true && distToTarget < 6f)
- {
- lastWaypointReached += 1;
- ScreenMessages.PostScreenMessage(new ScreenMessage("Checkpoint Passed!", 2f, ScreenMessageStyle.UPPER_CENTER));
- if (lastWaypointReached > wayPointsOnTrack)
- {
- lastWaypointReached = 1;
- }
- }
- if (raceStarted == false && distToTarget < 6f && FlightGlobals.ActiveVessel.horizontalSrfSpeed <= 0.2f)
- {
- ScreenMessages.PostScreenMessage(new ScreenMessage("Press SHIFT to Begin!", 1f, ScreenMessageStyle.UPPER_CENTER));
- }
- if (raceStarted == true && tripLastLeg == true && lastWaypointReached == 1) //Current lap is over
- {
- remainingLaps -= 1;
- lastLapTime = Time.time - lapStartTime;
- print("Last Laptime was: " + lastLapTime);
- ScreenMessages.PostScreenMessage(new ScreenMessage("Lap Time: " + lastLapTime, 4f, ScreenMessageStyle.UPPER_CENTER));
- if (lastLapTime < timeToBeat)
- {
- timeToBeat = lastLapTime;
- ScreenMessages.PostScreenMessage(new ScreenMessage("Your Best Lap ever! " + timeToBeat, 8f, ScreenMessageStyle.UPPER_CENTER));
- print("Best Laptime was: " + timeToBeat);
- }
- tripLastLeg = false; //Reset the last leg checker.
- if (remainingLaps == 0) //Race is over
- {
- float raceTotalTime = Time.time - raceStartTime;
- print("Total " + raceLaps + " Lap Race Time: " + raceTotalTime);
- ScreenMessages.PostScreenMessage(new ScreenMessage("The Race is Over! Total Time: " + raceTotalTime, 12f, ScreenMessageStyle.UPPER_CENTER));
- print("Top Recorded Speed: " + fastestSpeed);
- ScreenMessages.PostScreenMessage(new ScreenMessage("Top Speed: " + fastestSpeed, 12f, ScreenMessageStyle.UPPER_CENTER));
- }
- else if (remainingLaps > 0)
- {
- lapStartTime = Time.time;
- //waypointOne.light.color = new Color(0f, 1f, 0f);
- waypointTwo.light.color = new Color(0f, 1f, 0f);
- waypointThree.light.color = new Color(0f, 1f, 0f);
- waypointFour.light.color = new Color(0f, 1f, 0f);
- waypointFive.light.color = new Color(0f, 1f, 0f);
- waypointSix.light.color = new Color(0f, 1f, 0f);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement