//Function to load faction data public static FactionData[] LoadFactions(FactionData[] factionData) { //Clears current faction data ClearFactions(factionData.Length); for (int i = 0; i < factionData.Length; i++) { //Faction variable assignment factions[i] = factionData[i]; factions[i].homeSystem = GalaxyGenerator.GetGalaxyNodeFromID(factionData[i].homeSystemID, GalaxyGenerator.GetAllGalaxySystems()); factions[i].exploredSystems = new List <GalaxyNode>(); factions[i].ownedSystems = new List <GalaxyNode>(); factions[i].ownedSystemIDs = new List <int>(); factions[i].exploredSystemIDs = new List <int>(); factions[i].resourceData = ResetFactionResourceData(factions[i].factionID); //Get all explored systems for (int j = 0; j < factionData[i].exploredSystemIDs.Count; j++) { GalaxyNode newNode = GalaxyGenerator.GetGalaxyNodeFromID(factionData[i].exploredSystemIDs[j], GalaxyGenerator.GetAllGalaxySystems()); AddExploredSystem(factions[i].factionID, newNode); } //Get all owned systems for (int j = 0; j < factionData[i].ownedSystemIDs.Count; j++) { GalaxyNode newNode = GalaxyGenerator.GetGalaxyNodeFromID(factionData[i].ownedSystemIDs[j], GalaxyGenerator.GetAllGalaxySystems()); AddControlledSystem(factions[i].factionID, newNode); } } SaveData.current.factions = factions; return(factions); }
//Adding actual nodes to list public void AddConnectingNode(GalaxyNode nodeToConnect) { if (!connectingNodes.Contains(nodeToConnect)) { connectingNodes.Add(nodeToConnect); } }
//Update the amount of a resource a faction collects for a period of time. private static void UpdateResourceInflux(int factionID, GalaxyNode resourceNode) { if (factionID < factions.Length && factionID >= 0) { GalaxyNodeResourceData[] resources = resourceNode.GetResourcesData(); //For every resource in the node for (int i = 0; i < resources.Length; i++) { //Get faction data FactionData data = GetFactionData(factionID); //For every resource the faction can collect for (int j = 0; j < data.resourceData.Length; j++) { if (data.resourceData[j].resourceType == resources[i].resourceType) { //If the producing resource node is enabled (has the faction built a mining rig? etc..) if (resources[i].isEnabled) { //Add to resource influx data.resourceData[j].resourceInflux += resources[i].productionRate; } } } } } }
//Adds an explored system to the list public static void AddExploredSystem(int factionID, GalaxyNode newExploredSystem) { if (factionID < factions.Length && factionID >= 0) { //Check the system is not already in the list bool alreadyInList = false; bool alreadyInIDList = false; FactionData data = factions[factionID]; for (int i = 0; i < data.exploredSystems.Count; i++) { if (data.exploredSystems.Contains(newExploredSystem)) { alreadyInList = true; } if (data.exploredSystemIDs.Contains(newExploredSystem.nodeID)) { alreadyInIDList = true; } } //If allowed to proceed if (alreadyInList == false) { factions[factionID].exploredSystems.Add(newExploredSystem); } if (alreadyInIDList == false) { factions[factionID].exploredSystemIDs.Add(newExploredSystem.nodeID); } } }
//Function to remove an explored system from the list public static void RemoveExploredSystem(int factionID, GalaxyNode systemToRemove) { if (factionID < factions.Length && factionID >= 0) { FactionData data = factions[factionID]; data.exploredSystems.Remove(systemToRemove); data.exploredSystemIDs.Remove(systemToRemove.nodeID); } }
//Function to remove a controlled system from the list public static void RemoveControlledSystem(int factionID, GalaxyNode systemToRemove) { if (factionID < factions.Length && factionID >= 0) { FactionData data = factions[factionID]; data.ownedSystems.Remove(systemToRemove); data.ownedSystemIDs.Remove(systemToRemove.nodeID); UpdateResourceInflux(factionID, systemToRemove); } }
public static GalaxyNode GetGalaxyNodeFromID(int nodeID, GameObject[] nodesToSearch) { for (int i = 0; i < nodesToSearch.Length; i++) { GalaxyNode node = nodesToSearch[i].GetComponent <GalaxyNode>(); if (nodeID == node.nodeID) { return(node); } } return(null); }
public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) { GalaxyNode node = (GalaxyNode)obj; info.AddValue("xPos", node.position.x); info.AddValue("yPos", node.position.y); info.AddValue("zPos", node.position.z); info.AddValue("id", node.nodeID); info.AddValue("ring", node.currentRing); info.AddValue("features", node.features); info.AddValue("resources", node.resources); info.AddValue("ships", node.ships); }
// Update is called once per frame void Update() { if (canInteract) { //If left mouse button clicked if (Input.GetMouseButtonDown(0)) { //Get mouse position Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y); //Raycast and return information RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero); //If something was hit if (hit.collider != null) { //Disable info panel on last selected node if (previouslySelectedNode != null) { previouslySelectedNode.DisableInfoPanel(); } //Get node data currentlySelectedNode = hit.transform.gameObject.GetComponent <GalaxyNode>(); if (currentlySelectedNode != null) { previouslySelectedNode = currentlySelectedNode; //Do something to node, e.g. get resource data. GalaxyNodeResourceData[] data = currentlySelectedNode.GetResourcesData(); for (int i = 0; i < data.Length; i++) { //Debug.Log(data[i].resourceType + " amount: " + data[i].totalResource + " at a rate of: " + data[i].productionRate); currentlySelectedNode.EnableResource(i); } Factions.RemoveControlledSystem(currentlySelectedNode.GetOwningFactionID(), currentlySelectedNode); Factions.AddControlledSystem(playerFactionID, currentlySelectedNode); Factions.CheckForCapitulation(); currentlySelectedNode.AddShip(Ships.CreateNewShip(currentlySelectedNode, Ships.ShipType.Exploration)); currentlySelectedNode.EnableInfoPanel(); } } else { if (previouslySelectedNode != null) { previouslySelectedNode.DisableInfoPanel(); } } } } }
public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { GalaxyNode node = (GalaxyNode)obj; node.position.x = (float)info.GetValue("xPos", typeof(float)); node.position.y = (float)info.GetValue("yPos", typeof(float)); node.position.z = (float)info.GetValue("zPos", typeof(float)); node.nodeID = (int)info.GetValue("id", typeof(int)); node.currentRing = (int)info.GetValue("ring", typeof(int)); node.features = (List <GalaxyNode.SystemFeatures>)info.GetValue("features", typeof(List <GalaxyNode.SystemFeatures>)); node.resources = (List <GalaxyNodeResourceData>)info.GetValue("resources", typeof(List <GalaxyNodeResourceData>)); node.ships = (List <ShipData>)info.GetValue("ships", typeof(List <ShipData>)); obj = node; return(obj); }
//Creates a new ship and adds it to the array public static ShipData CreateNewShip(GalaxyNode spawnSystem, ShipType type) { int owningID = spawnSystem.GetOwningFactionID(); ShipData ship = new ShipData { shipID = ships.Count, owningFactionID = owningID, shipType = type, shipHealth = 100, shipShield = 100, shipDamage = 10, shipSpeed = 100, }; ships.Add(ship); return(ship); }
//Determines which nodes should be resource nodes void GenerateResourceNodes(GalaxyGenerationResourceData data, int currentResource) { //Repeat for the number of nodes that are designated as the nodetype for (int i = 0; i < data.currentNodeCount; i++) { //Random node GalaxyNode node = systems[Random.Range(0, systems.Length)].GetComponent <GalaxyNode>(); //Null pointer check if (node != null) { //Node resource addition node.AddResource(data.nodeResourceType, Mathf.FloorToInt(data.resourceRichnessMultiplier * Random.Range(data.minResourceRichness, data.maxResourceRichness)), Random.Range(data.minProductionRate, data.maxProductionRate)); //Loading calculation resourceLoading = Mathf.Clamp01((float)(i * (currentResource + 1)) / (float)(systemResourcesData.Length * data.currentNodeCount)); } } }
//Adds an owned system to the list public static void AddControlledSystem(int factionID, GalaxyNode newControlledSystem) { if (factionID < factions.Length && factionID >= 0) { //Check the system is not already in the list bool alreadyInList = false; bool alreadyInIDList = false; FactionData data = factions[factionID]; for (int i = 0; i < data.ownedSystems.Count; i++) { if (data.ownedSystems.Contains(newControlledSystem)) { alreadyInList = true; } if (data.ownedSystemIDs.Contains(newControlledSystem.nodeID)) { alreadyInIDList = true; } } //If its new to the list if (alreadyInList == false) { //Add to explored systems, incase it wasnt added before AddExploredSystem(factionID, newControlledSystem); //Adding to controlled lists factions[factionID].ownedSystems.Add(newControlledSystem); newControlledSystem.SetOwningFaction(factionID); UpdateResourceInflux(factionID, newControlledSystem); } if (alreadyInIDList == false) { factions[factionID].ownedSystemIDs.Add(newControlledSystem.nodeID); } } }
//Create faction data array randomly. public static FactionData[] CreateFactions(int numberOfFactionsToCreate, GameObject[] systems) { numberOfFactions = numberOfFactionsToCreate; factions = new FactionData[numberOfFactions]; GameObject[] homeSystems = new GameObject[numberOfFactions]; //For every faction to create for (int i = 0; i < numberOfFactionsToCreate; i++) { //Faction identifiers factions[i].factionID = i; factions[i].factionName = "Faction" + Random.Range(0, 100); factions[i].factionColour = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f)); //Home / Starting system GalaxyNode homeSystem = systems[Random.Range(0, systems.Length)].GetComponent <GalaxyNode>(); //Validating faction system, prevents positioning multiple factions inside same system. bool valid = true; for (int j = 0; j < homeSystems.Length; j++) { //If the system is already in the array if (homeSystems[j] == homeSystem.gameObject) { //It is not valid valid = false; } } //If positioning is valid if (valid == true) { //Basic setup factions[i].homeSystem = homeSystem; homeSystem.AddSystemFeature(GalaxyNode.SystemFeatures.Planet); homeSystem.SetOwningFaction(i); factions[i].homeSystemID = homeSystem.nodeID; homeSystems[i] = homeSystem.gameObject; factions[i].hasCapitulated = false; //Assigning Galaxy node arrays factions[i].exploredSystems = new List <GalaxyNode>(); factions[i].ownedSystems = new List <GalaxyNode>(); factions[i].exploredSystemIDs = new List <int>(); factions[i].ownedSystemIDs = new List <int>(); //Resource assigning factions[i].resourceData = ResetFactionResourceData(factions[i].factionID); //Add homesystem to explored and owned systems AddExploredSystem(factions[i].factionID, factions[i].homeSystem); AddControlledSystem(factions[i].factionID, factions[i].homeSystem); //Update resources UpdateResourceInflux(i, homeSystem); } else { i--; } } return(factions); }
//Coroutine to spawn the galaxy, coroutine needed otherwise performance is awful. IEnumerator CreateGalaxyCoroutine() { for (int k = 0; k < numberOfTimesToGenerate; k++) { //Resetting loading bars positionLoading = 0.0f; collisionLoading = 0.0f; resourceLoading = 0.0f; connectLoading = 0.0f; totalLoading = 0.0f; numberOfSystemsGenerated = 0; startTime = Time.time; //If random generation if (randomGeneration == true) { systemDensity = Random.Range(0.10f, 0.12f); systemCenterRadius = Random.Range(0.0f, 25.0f); systemRingCount = Random.Range(4, 7); systemRingWidth = Random.Range(3.5f, 4.5f); } //Calculating other variables using randomly generated or selected values systemRadius = systemRingCount * ringIntervalMultiplier; generationCost = systemRingCount * systemRingWidth * systemDensity; //Generation cost warnings if (generationCost > 6.0f) { Debug.LogWarning("High generation cost"); } else if (generationCost > 4.0f) { Debug.LogWarning("Medium generation cost"); } else { Debug.Log("Low generation cost"); } //Array assignments densities[k] = systemDensity; centerRadii[k] = systemCenterRadius; ringCounts[k] = systemRingCount; ringWidths[k] = systemRingWidth; collisionDistances[k] = systemCollisionDistance; radii[k] = systemRadius; ringIntervals[k] = ringIntervalMultiplier; generationCosts[k] = generationCost; //For each system ring for (int i = 0; i < systemRingCount; i++) { //Calculate segment size and ring radius float segmentSize = (systemRadius - systemCenterRadius) / systemRingCount; float ringRadius = segmentSize * (i + 1); //Calculate ring radius min/max float minRingRadius = ringRadius - systemRingWidth; float maxRingRadius = ringRadius + systemRingWidth; //Calculate ring area float upperArea = maxRingRadius * maxRingRadius * Mathf.PI; float lowerArea = minRingRadius * minRingRadius * Mathf.PI; float ringArea = upperArea - lowerArea; //Calculate number of systems for each ring using formula: mass (number of systems) = density * volume (area). int numberOfSystemsForRing = Mathf.FloorToInt(systemDensity * ringArea); //Instantiate ring object for ring image, give gameobject ring name GameObject galaxyRing = Instantiate(ringPrefab, new Vector2(0, 0), Quaternion.identity, transform); galaxyRing.name = "GalaxyRing " + (i + 1); for (int j = 0; j < numberOfSystemsForRing; j++) { //Spawn systems randomly float angle = Random.Range(0.0f, 1.0f) * Mathf.PI * 2f; Vector2 newPos = new Vector2(Mathf.Cos(angle) * (ringRadius + systemCenterRadius) + Random.Range(-systemRingCount, systemRingWidth), Mathf.Sin(angle) * (ringRadius + systemCenterRadius) + Random.Range(-systemRingWidth, systemRingWidth)); //Calculate position GameObject node = Instantiate(systemPrefab, newPos, Quaternion.identity, transform.GetChild(i)); node.GetComponent <GalaxyNode>().currentRing = i + 1; if (j % coroutineGenerateYieldIntervals == 0) { yield return(null); } } //Loading calculation positionLoading = Mathf.Clamp01((float)i / (float)systemRingCount); } positionLoading = 1.0f; //Get all current systems within scene systems = GetAllGalaxySystems(); if (systemCollisions == true) { //Remove colliding systems (check if they are valid) for (int i = 0; i < systems.Length; i++) { //Get all systems within range of systems[i] GameObject[] systemsToDestroy = CheckRangeOfSystems(systems[i], systems, systemCollisionDistance, true); //Destroy those systems for (int j = 0; j < systemsToDestroy.Length; j++) { DestroyImmediate(systemsToDestroy[j]); } //Coroutine yield interval if (i % coroutineCollisionYieldIntervals == 0) { yield return(null); } //Loading calculation collisionLoading = Mathf.Clamp01((float)i / (float)systems.Length); } //Get all current systems within scene after some have been removed systems = GetAllGalaxySystems(); } collisionLoading = 1.0f; //Calculate number of systems numberOfSystemsGenerated = systems.Length; numberOfSystems[k] = numberOfSystemsGenerated; //If resources are enabled if (systemResources == true) { //For every resource in galaxy gen data for (int i = 0; i < systemResourcesData.Length; i++) { //Generating resource data systemResourcesData[i].currentNodeCount = Mathf.FloorToInt(systemResourcesData[i].resourcePercentage / 100 * systems.Length); GenerateResourceNodes(systemResourcesData[i], i); } } resourceLoading = 1.0f; //For every system SaveData.current.galaxyNodes = new List <GalaxyNode>(); for (int i = 0; i < systems.Length; i++) { if (systems[i] != null) { //Get node GalaxyNode node = systems[i].GetComponent <GalaxyNode>(); if (systemConnections) { //Generate connecting nodes for every node, used in AI pathfinding GameObject[] systemsToConnect = CheckRangeOfSystems(systems[i], systems, systemConnectRange, false); //Connect these nodes for (int j = 0; j < systemsToConnect.Length; j++) { //Add to connecting node list GalaxyNode currentConnectNode = systemsToConnect[j].GetComponent <GalaxyNode>(); node.AddConnectingNode(currentConnectNode); //Coroutine yield interval if (i % coroutineCollisionYieldIntervals == 0) { yield return(null); } } //Loading calculation connectLoading = Mathf.Clamp01((float)i / (float)systems.Length); } //Node setup node.name = "Node " + i; node.nodeID = i; node.CreateNodeUI(nodeUIPrefab, nodeResourceInfoUI); node.UpdateGalaxyNodeData(node.currentRing, node.nodeID, node.features, node.ships); //coroutine yield check if (i % coroutineResourceYieldIntervals == 0) { yield return(null); } SaveData.current.galaxyNodes.Add(node); } } connectLoading = 1.0f; //If factions are enabled. if (systemFactions == true) { //Create faction starting systems. factions = Factions.CreateFactions(numberOfFactions, systems); SaveData.current.factions = factions; float x = factions[0].homeSystem.transform.position.x; float y = factions[0].homeSystem.transform.position.y; playerCamera.GetComponent <CameraMovement>().InitializeCameraSettings(new Vector3(x, y, -10), systemRadius, 15.0f, systemRadius); } //Calculate time taken timings[k] = Time.time - startTime; if (debugMode == true) { RunDebugging(k); currentGeneration++; } } }
//Coroutine to load the galaxy map IEnumerator LoadGalaxyCoroutine(List <GalaxyNode> nodeData) { //Reset loading bars positionLoading = 0.0f; collisionLoading = 0.0f; resourceLoading = 0.0f; connectLoading = 0.0f; totalLoading = 0.0f; yield return(new WaitForSeconds(0.1f)); //Calculate number of rings int numberOfRings = 0; for (int i = 0; i < nodeData.Count; i++) { numberOfRings = nodeData[i].currentRing; } //Instantiate those rings for (int i = 0; i < numberOfRings; i++) { GameObject ring = Instantiate(ringPrefab, new Vector3(0, 0), Quaternion.identity, transform); ring.name = "GalaxyRing " + (i + 1); } //For every node to be created for (int i = 0; i < nodeData.Count; i++) { //Instantiate to a position int currentIteration = i; GameObject node = Instantiate(systemPrefab, nodeData[i].position, Quaternion.identity, transform.GetChild(nodeData[i].currentRing - 1)); node.name = "Node " + currentIteration; GalaxyNode galaxyNode = node.GetComponent <GalaxyNode>(); galaxyNode.UpdateGalaxyNodeData(nodeData[i].currentRing, nodeData[i].nodeID, nodeData[i].features, nodeData[i].ships); if (i % coroutineGenerateYieldIntervals == 0) { yield return(null); } positionLoading = Mathf.Clamp01((float)i / (float)systemRingCount); } positionLoading = 1.0f; collisionLoading = 1.0f; //Array creation GameObject[] nodeObjects = GetAllGalaxySystems(); for (int i = 0; i < nodeObjects.Length; i++) { GalaxyNode galaxyNode = nodeObjects[i].GetComponent <GalaxyNode>(); galaxyNode.resources = nodeData[i].resources; resourceLoading = Mathf.Clamp01((float)i / (float)nodeObjects.Length); } resourceLoading = 1.0f; for (int i = 0; i < nodeObjects.Length; i++) { if (nodeObjects[i] != null) { //Get node GalaxyNode node = nodeObjects[i].GetComponent <GalaxyNode>(); if (systemConnections) { //Generate connecting nodes for every node, used in AI pathfinding GameObject[] systemsToConnect = CheckRangeOfSystems(nodeObjects[i], nodeObjects, systemConnectRange, false); //Connect these nodes for (int j = 0; j < systemsToConnect.Length; j++) { //Add to connecting node list GalaxyNode currentConnectNode = systemsToConnect[j].GetComponent <GalaxyNode>();; node.AddConnectingNode(currentConnectNode); //Coroutine yield interval if (i % coroutineCollisionYieldIntervals == 0) { yield return(null); } } //Loading calculation connectLoading = Mathf.Clamp01((float)i / (float)nodeObjects.Length); } node.CreateNodeUI(nodeUIPrefab, nodeResourceInfoUI); //coroutine yield check if (i % coroutineResourceYieldIntervals == 0) { yield return(null); } } } connectLoading = 1.0f; factions = Factions.LoadFactions(SaveData.current.factions); }