private void AddPermanentSystem(GameObject current, GameObject target) //Connects the two systems { int thisSystem = MasterScript.systemListConstructor.RefreshCurrentSystemA(current); //Get integer location of system in systemList int nearestSystem = MasterScript.systemListConstructor.RefreshCurrentSystemA(target); //Same for target MasterScript.systemListConstructor.systemList[thisSystem].permanentConnections.Add(MasterScript.systemListConstructor.systemList[nearestSystem].systemObject); //Add target system to current systems' permanent connections MasterScript.systemListConstructor.systemList [nearestSystem].permanentConnections.Add(MasterScript.systemListConstructor.systemList [thisSystem].systemObject); //Add current system to target systems' permanent connections ConnectionCoordinates connection = new ConnectionCoordinates(); //Create new connection object connection.systemOne = MasterScript.systemListConstructor.systemList [thisSystem].systemObject; //Assign the relevant data connection.systemTwo = MasterScript.systemListConstructor.systemList [nearestSystem].systemObject; coordinateList.Add(connection); //Add the object to the list of connections for (int i = 0; i < MasterScript.systemListConstructor.systemList[thisSystem].tempConnections.Count; ++i) //For all temporary connections in the current system { if (MasterScript.systemListConstructor.systemList[thisSystem].tempConnections[i].targetSystem == MasterScript.systemListConstructor.systemList[nearestSystem].systemObject) //If the target system is found in the current systems temporary connections { MasterScript.systemListConstructor.systemList[thisSystem].tempConnections.RemoveAt(i); //Remove it break; //And break the loop } } for (int i = 0; i < MasterScript.systemListConstructor.systemList[nearestSystem].tempConnections.Count; ++i) //Same as above for the target system { if (MasterScript.systemListConstructor.systemList[nearestSystem].tempConnections[i].targetSystem == MasterScript.systemListConstructor.systemList[thisSystem].systemObject) { MasterScript.systemListConstructor.systemList[nearestSystem].tempConnections.RemoveAt(i); break; } } }
private void AddInitialConnections() //This creates connections for the galactic centre so that nothing intersects with it { Vector3[] centreCorners = new Vector3[4] { new Vector3(55f, 55f, 0f), new Vector3(55f, 65f, 0f), new Vector3(65f, 65f, 0f), new Vector3(65f, 55f, 0f) }; for (int i = 0; i < 4; ++i) { int prev = i - 1; if (prev < 0) { prev = 3; } ConnectionCoordinates connection = new ConnectionCoordinates(); //Create new connection object GameObject sysOne = new GameObject(); sysOne.transform.position = centreCorners[i]; GameObject sysTwo = new GameObject(); sysTwo.transform.position = centreCorners[prev]; connection.systemOne = sysOne; //Assign the relevant data connection.systemTwo = sysTwo; coordinateList.Add(connection); //Add the object to the list of connections } }
public bool ReconnectSystems(GameObject systemA, GameObject systemB) { int thisSystem = MasterScript.RefreshCurrentSystem(systemA); //Get this system reconnected = true; List <GameObject> potentialConnections = new List <GameObject>(); float distance = 100f; //Set max distance int newConnection = -1; for (int i = 0; i < MasterScript.systemListConstructor.systemList[thisSystem].permanentConnections.Count; ++i) //For all permanent connections in this system { if (MasterScript.systemListConstructor.systemList[thisSystem].permanentConnections[i] == systemB) //If connection equals target { for (int j = 0; j < MasterScript.systemListConstructor.systemList.Count; ++j) //For all other systems { if (MasterScript.systemListConstructor.systemList[j].systemObject == systemA || MasterScript.systemListConstructor.systemList[j].systemObject == systemB) //If system equals either of original systems, continue { continue; } float tempDistance = Vector3.Distance(systemA.transform.position, MasterScript.systemListConstructor.systemList[j].systemObject.transform.position); if (tempDistance < distance) //Test if distance is less than current max distance { potentialConnections.Add(MasterScript.systemListConstructor.systemList[j].systemObject); } } GameObject tempObject = new GameObject(); for (int k = potentialConnections.Count - 1; k >= 0; --k) { bool swaps = false; for (int l = 1; l <= k; ++l) //Sort smallest to largest { float lMinus1Dist = Vector3.Distance(systemA.transform.position, potentialConnections[l - 1].transform.position); float lDist = Vector3.Distance(systemA.transform.position, potentialConnections[l].transform.position); if (lMinus1Dist > lDist) { tempObject = potentialConnections[l - 1]; potentialConnections[l - 1] = potentialConnections[l]; potentialConnections[l] = tempObject; swaps = true; } } if (swaps == false) { break; } } for (int j = 0; j < potentialConnections.Count; ++j) { if (MasterScript.mapConstructor.IsValidAngle(systemA, potentialConnections[j])) //If the connection would be within the valid angle parameters { if (MasterScript.mapConstructor.IsValidConnection(systemA, potentialConnections[j])) //If there is no intersection between this system and other system { newConnection = MasterScript.RefreshCurrentSystem(potentialConnections[j]); break; } } } if (newConnection != -1) //If the new connection is not -1 { MasterScript.systemListConstructor.systemList[thisSystem].permanentConnections[i] = MasterScript.systemListConstructor.systemList[newConnection].systemObject; //The connection that was system b is now the new connection MasterScript.systemListConstructor.systemList[newConnection].permanentConnections.Add(MasterScript.systemListConstructor.systemList[thisSystem].systemObject); //The new connection is now connected to this system ConnectionCoordinates temp = new ConnectionCoordinates(); temp.systemOne = MasterScript.systemListConstructor.systemList[thisSystem].systemObject; temp.systemTwo = MasterScript.systemListConstructor.systemList[newConnection].systemObject; MasterScript.mapConstructor.coordinateList.Add(temp); MasterScript.systemListConstructor.systemList[thisSystem].numberOfConnections = MasterScript.systemListConstructor.systemList[thisSystem].permanentConnections.Count; MasterScript.systemListConstructor.systemList[newConnection].numberOfConnections = MasterScript.systemListConstructor.systemList[newConnection].permanentConnections.Count; return(true); } break; } } return(false); }