// Use this for initialization void Start() { //get game manager gameManager = GameObject.Find("GameManager").GetComponent <GameStateManagerScript>(); //Obtain the player manager playerManager = GameObject.Find("Player").GetComponent <PlayerManager>(); //get entity entity = GameObject.FindGameObjectWithTag("Player").transform.GetComponent <PlayerManager>(); //get rankScript rankScript = GameObject.Find("GameManager").GetComponent <RankScript>(); //get check point script from entities checkpointScript = GameObject.FindGameObjectWithTag("Player").transform.GetComponent <CarCheckpointScript>(); //load sprites for sprites into arrays we created above from Resource folder lapSpriteList = Resources.LoadAll <Sprite>("LapSprites"); rankSpriteList = Resources.LoadAll <Sprite>("RankSprites"); itemSpriteList = Resources.LoadAll <Sprite>("ItemIconSprites"); //get image slots and link to the variables we set up lapCountSprite = gameObject.transform.Find("lapCount").GetComponent <Image>(); rankSprite = gameObject.transform.Find("rank").GetComponent <Image>(); itemSprite = gameObject.transform.Find("itemIcon").GetComponent <Image>(); }
// Use this for initialization void Awake() { //Obtain the script properties gameManager = GameObject.Find("GameManager").GetComponent <GameStateManagerScript>(); rank = GameObject.Find("GameManager").GetComponent <RankScript>(); carCheckpoint = GameObject.FindGameObjectWithTag("Player2").transform.GetComponent <CarCheckpointScript>(); drive = GameObject.FindGameObjectWithTag("Player2").transform.GetComponent <WheelDrive>(); spriteRenderer = GameObject.FindGameObjectWithTag("Player2").transform.GetChild(16).transform.GetComponent <SpriteRenderer>(); //Don't render the game object displayRank.SetActive(false); }
//Getter function for other object to call and get a car's current rank public int GetRank(CarCheckpointScript a_car) { int rank; for (int i = 0; i < rankList.Count; i++) { if (a_car == rankList[i]) { rank = i + 1; return(rank); } } return(0); }
//implement CompareRank function by using C# sorting //Should only compare 2 cars (1, 0, -1) //Because C# sorting is minimum start => [1 is smallest] [0 is tie] [-1 is biggest] private int CompareRank(CarCheckpointScript car1, CarCheckpointScript car2) { //Set Current Lap count as the first check condition if (car1.currLap > car2.currLap) { return(-1); } else if (car1.currLap < car2.currLap) { return(1); } else //if both cars in on same lap { //compare current check point if (car1.currCheckpointCount > car2.currCheckpointCount) { return(-1); } else if (car1.currCheckpointCount < car2.currCheckpointCount) { return(1); } else // if both having same checkpoint { //var to define which two checkpoints are two car's next checkpoint transform //(use this to check if it's the last check point or not) Transform car1NextCheckpoint; Transform car2NextCheckpoint; //if car1 has NOT traversed the last checkpoint (the one before finish/start line) if (car1.currCheckpointCount < checkpointList.Count - 1) { //car1's next check point is the the current one + 1 of the checkpointList car1NextCheckpoint = checkpointList[(car1.currCheckpointCount) + 1]; } else //if car1 HAS traversed the last checkpoint (the one before finish line) { //car1's next check point should be the first one in the checkpointList, which is the finish/start line car1NextCheckpoint = checkpointList[0]; } //if car2 has NOT traversed the last checkpoint (the one before finish/start line) if (car2.currCheckpointCount < checkpointList.Count - 1) { //car2's next check point is the the current one + 1 of the checkpointList car2NextCheckpoint = checkpointList[(car2.currCheckpointCount) + 1]; } else //if car2 HAS traversed the last checkpoint (the one before finish line) { //car2's next check point should be the first one in the checkpointList, which is the finish/start line car2NextCheckpoint = checkpointList[0]; } //With both cars' next checkpoints well defined, start to compare the distance to the next checkpoint they are going if ((car1.transform.position - car1NextCheckpoint.position).sqrMagnitude < (car2.transform.position - car2NextCheckpoint.position).sqrMagnitude) { return(-1); } if ((car1.transform.position - car1NextCheckpoint.position).sqrMagnitude > (car2.transform.position - car2NextCheckpoint.position).sqrMagnitude) { return(1); } } } // if all conditions are the same, they are tie return(0); }