// checks if resource is there private void OnTriggerStay(Collider other) { if (!resourceFound && other.tag == "Resource") // Checks if it is a resource, but only if a resource have not already been found { // Goes through all valid resources foreach (Resource type in neededResource) // Goes through all valid resources { // Checks if the type of the resource is correct if (other.gameObject.GetComponentInChildren <ResourceWorldObject>().ReturnType().ToString() == type.ToString()) { resource = other.gameObject.GetComponent <ResourceWorldObject>(); resourceFound = true; // Goes through resources of GameManager and finds which one is being harvested, before setting the resource index to that resource's idnex for (int i = 0; i < GameManager.resources.Length; i++) { if (GameManager.resources[i].resource.ToString() == resource.ReturnType().ToString()) { resourceIndex = i; } } } } } }
// Loads save from file public void Load() { // Ensures load only runs on the island scene if (SceneManager.GetActiveScene().name == "PrivateIsland") { // Load if (File.Exists(path)) { // Opens stream FileStream stream = new FileStream(path, FileMode.Open); try { // Loads resources int numResources = (int)formatter.Deserialize(stream); for (int i = 0; i < numResources; i++) { ResourceSave resourceData = formatter.Deserialize(stream) as ResourceSave; // Instantiate resource GameObject resourceObject = Resources.Load("Prefabs/WorldResources/Raw resources/" + RemoveCopyInName(resourceData.objectName)) as GameObject; GameObject worldObject = LoadObject(resourceObject, resourceData.position, resourceData.rotation); ResourceWorldObject resource = worldObject.GetComponentInChildren <ResourceWorldObject>(); // ERROR lies here, somehow resource.LoadFromSave(resourceData.resourceAmount); } // Load factories int numFactories = (int)formatter.Deserialize(stream); for (int i = 0; i < numFactories; i++) { FactorySave factoryData = formatter.Deserialize(stream) as FactorySave; // Instantiate factory GameObject factoryObject = Resources.Load("Prefabs/Buildings/Factory/Primary buildings/" + RemoveCopyInName(factoryData.objectName)) as GameObject; GameObject worldObject = LoadObject(factoryObject, factoryData.position, factoryData.rotation); // Updates position, makes sure the building finishes building if it is finished FactoryBuilding factory = worldObject.GetComponent <FactoryBuilding>(); factory.LoadFromSave(factoryData.presentHealth, factoryData.buildingFinished, factoryData.yOffset); factory.LoadFactory(factoryData.isWorking, factoryData.remainingTime, factoryData.timeRound, factoryData.index, factoryData.remainingRounds, factoryData.originalRounds); } // Load harvesters int numHarvesters = (int)formatter.Deserialize(stream); for (int i = 0; i < numHarvesters; i++) { BuildingSave harvesterData = formatter.Deserialize(stream) as BuildingSave; // Instantiate harvester GameObject resourceObject = Resources.Load("Prefabs/Buildings/ResourceGathering/" + RemoveCopyInName(harvesterData.objectName)) as GameObject; GameObject worldObject = LoadObject(resourceObject, harvesterData.position, harvesterData.rotation); // Updates position, makes sure the building finishes building if it is finished AbstractResourceHarvesting harvester = worldObject.GetComponent <AbstractResourceHarvesting>(); harvester.LoadFromSave(harvesterData.presentHealth, harvesterData.buildingFinished, harvesterData.yOffset); } // Load houses int numHouses = (int)formatter.Deserialize(stream); for (int i = 0; i < numHouses; i++) { BuildingSave houseData = formatter.Deserialize(stream) as BuildingSave; // Instantiate harvester // GameObject resourceObject = Resources.Load("Prefabs/Buildings/" + RemoveCopyInName(houseData.objectName)) as GameObject; GameObject resourceObject = Resources.Load("Prefabs/Buildings/HouseTemplate") as GameObject; GameObject worldObject = LoadObject(resourceObject, houseData.position, houseData.rotation); // Updates position, makes sure the building finishes building if it is finished AbstractHouse house = worldObject.GetComponent <AbstractHouse>(); house.LoadFromSave(houseData.presentHealth, houseData.buildingFinished, houseData.yOffset); } // Resource amounts for (int i = 0; i < GameManager.resources.Length; i++) { // Sends in amount of resource GameManager.resources[i].amount = (float)formatter.Deserialize(stream); } // Money GameManager.moneyAmount = (float)formatter.Deserialize(stream); // Roads int numRoads = (int)formatter.Deserialize(stream); roadPlacer.enabled = true; Debug.Log("NumberOfRoads: " + numRoads); for (int i = 0; i < numRoads; i++) { RoadSave roadeData = formatter.Deserialize(stream) as RoadSave; Vector3 startPos = new Vector3(roadeData.startPos_X, roadeData.startPos_Y, roadeData.startPos_Z); Vector3 controllNode1 = new Vector3(roadeData.controllNode1_X, roadeData.controllNode1_Y, roadeData.controllNode1_Z); Vector3 controllNode2 = new Vector3(roadeData.controllNode2_X, roadeData.controllNode2_Y, roadeData.controllNode2_Z); Vector3 endPos = new Vector3(roadeData.endPos_X, roadeData.endPos_Y, roadeData.endPos_Z); roadPlacer.GenerateRoad(startPos, controllNode1, controllNode2, endPos); Debug.Log("LoadingRoads"); } roadPlacer.enabled = false; } catch (System.Exception e) { Debug.Log("Error: " + e); } // Camera position Camera.main.transform.position = new Vector3((float)formatter.Deserialize(stream), (float)formatter.Deserialize(stream), (float)formatter.Deserialize(stream)); // Camera rotation if (cameraMovement != null) { cameraMovement.LoadAngles((float)formatter.Deserialize(stream), (float)formatter.Deserialize(stream)); } // Loads sun position sunManager.UpdatePosition((float[])formatter.Deserialize(stream)); // Time timeManager.UpdateTime((int[])formatter.Deserialize(stream)); // Camera mode GameManager.inputManager.frozenAngle = (bool)formatter.Deserialize(stream); Cursor.visible = /*GameManager.isPaused || */ GameManager.inputManager.frozenAngle; if (Cursor.visible) { Cursor.lockState = CursorLockMode.None; } else { Cursor.lockState = CursorLockMode.Locked; } // Close stream stream.Close(); } else { Debug.Log("No save file found"); } } }