/// <summary> /// Create a patch of resources, all with NavMeshObstacles attached. /// </summary> /// <param name="number">The number of resources to create.</param> /// <param name="startId">The ID of the first resource in the patch.</param> /// <param name="center">The patch center.</param> /// <param name="radius">The patch radius.</param> /// <param name="sideLength">The resource size length.</param> /// <param name="resourceMaterial">The resource material.</param> /// <param name="patchMaterial">The material used to create a patch indicator on the ground.</param> /// <returns>The ID of the next resource to be created.</returns> public uint createResourcePatch(uint patchId, uint number, uint startId, Vector2 center, float radius, float sideLength, Material resourceMaterial, Material patchMaterial = null) { Transform resourcePatchTransform = null; uint numResourcesRoot = (uint)Mathf.Sqrt(number); if (patchMaterial != null) { Rect quadRect = new Rect(center - new Vector2(radius + sideLength, radius + sideLength), new Vector2(2 * (radius + sideLength), 2 * (radius + sideLength))); GameObject resourcePatch = WorldspaceUIFactory.createQuad("Resource Patch " + patchId, quadRect, patchMaterial); resourcePatchTransform = resourcePatch.transform; resourcePatchTransform.SetParent(resourceHeader.transform); } if (numResourcesRoot == 1) { createResource(startId++, center, sideLength, resourceMaterial, resourcePatchTransform); } else { float resourceSpacing = radius * 2.0f / (numResourcesRoot - 1); for (uint i = 0; i < numResourcesRoot; ++i) { for (uint j = 0; j < numResourcesRoot; ++j) { float x = center.x - radius + i * resourceSpacing; float z = center.y - radius + j * resourceSpacing; Vector2 position = new Vector3(x, z); createResource(startId++, position, sideLength, resourceMaterial, resourcePatchTransform); } } } return(startId); }
/// <summary> /// Create the environment, including initializing and scaling the ground if necessary. /// </summary> /// <param name="config">The current Config.</param> /// <returns>Whether the environment was initialized successfully.</returns> private bool generateEnvironment(Config config) { float scaledGroundLength = config.GroundLength / 10.0f; // plane's dimensions are scaled up bool result = true; if (scaledGroundLength <= 0f) { result = false; } else { // Create environment header object if (EnvironmentObjects == null) { EnvironmentObjects = GameObject.Find("Environment"); if (EnvironmentObjects != null) { Log.d(LogTag.MAIN, "Located Environment header object in scene"); } else { Log.d(LogTag.MAIN, "Created Environment header object"); EnvironmentObjects = new GameObject("Environment"); EnvironmentObjects.transform.position = Vector3.zero; EnvironmentObjects.transform.rotation = new Quaternion(0f, 0f, 0f, 0f); } } // Create ground if (Ground == null) { Ground = GameObject.Find("Ground"); if (Ground != null) { Log.d(LogTag.MAIN, "Located Ground in scene"); } else { Log.d(LogTag.MAIN, "Created Ground"); Ground = GameObject.CreatePrimitive(PrimitiveType.Plane); Ground.name = "Ground"; } } // Set Ground tag to Ground Ground.tag = "Ground"; // Position and scale ground according to configuration Ground.transform.localScale = new Vector3(scaledGroundLength, 1.0f, scaledGroundLength); Ground.transform.position = Vector3.zero; Ground.transform.SetParent(EnvironmentObjects.transform); // Add barriers around the ground, assuming it's rectangular GameObject positiveX = GameObject.CreatePrimitive(PrimitiveType.Cube); positiveX.name = "Positive X"; positiveX.transform.localScale = new Vector3(0.1f, 0.25f, config.GroundLength + 0.2f); positiveX.transform.position = new Vector3(config.GroundLength / 2.0f + 0.05f, 0.125f, 0.0f); positiveX.transform.parent = Ground.transform; GameObject negativeX = GameObject.CreatePrimitive(PrimitiveType.Cube); negativeX.name = "Negative X"; negativeX.transform.localScale = new Vector3(0.1f, 0.25f, config.GroundLength + 0.2f); negativeX.transform.position = new Vector3(config.GroundLength / -2.0f - 0.05f, 0.125f, 0.0f); negativeX.transform.parent = Ground.transform; GameObject positiveZ = GameObject.CreatePrimitive(PrimitiveType.Cube); positiveZ.name = "Positive Z"; positiveZ.transform.localScale = new Vector3(config.GroundLength + 0.2f, 0.25f, 0.1f); positiveZ.transform.position = new Vector3(0.0f, 0.125f, config.GroundLength / 2.0f + 0.05f); positiveZ.transform.parent = Ground.transform; GameObject negativeZ = GameObject.CreatePrimitive(PrimitiveType.Cube); negativeZ.name = "Negative Z"; negativeZ.transform.localScale = new Vector3(config.GroundLength + 0.2f, 0.25f, 0.1f); negativeZ.transform.position = new Vector3(0.0f, 0.125f, config.GroundLength / -2.0f - 0.05f); negativeZ.transform.parent = Ground.transform; // Add material to barriers if (sceneMaterials.groundBorder != null) { negativeX.GetComponent <Renderer>().material = sceneMaterials.groundBorder; negativeZ.GetComponent <Renderer>().material = sceneMaterials.groundBorder; positiveX.GetComponent <Renderer>().material = sceneMaterials.groundBorder; positiveZ.GetComponent <Renderer>().material = sceneMaterials.groundBorder; } // Create satellite GameObject satelliteBody; if (SatellitePrefab == null) { satelliteBody = GameObject.CreatePrimitive(PrimitiveType.Sphere); satelliteBody.transform.position = new Vector3(0, 15, 0); } else { satelliteBody = GameObject.Instantiate(SatellitePrefab); satelliteBody.GetComponent <Rigidbody>().AddTorque(Vector3.up * 20); } satelliteBody.tag = "Satellite"; satelliteBody.name = "Satellite"; satelliteBody.transform.parent = EnvironmentObjects.transform; Satellite = new Satellite(satelliteBody, this); // Place resources resourceFactory = new ResourceFactory(); if (currentConfig.ScatterResources) { nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 4, 0, new Vector2(-20, -20), 1f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 4, 4, new Vector2(-15, 10), 1f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 4, 8, new Vector2(26, -14), 1f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 4, 12, new Vector2(18, 8), 1f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 4, 16, new Vector2(13, -18), 1f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 1, 20, new Vector2(6, -4), 0.5f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 1, 21, new Vector2(-24, -3), 0.5f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 1, 22, new Vector2(-7, -11), 0.5f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 1, 23, new Vector2(8, 23), 0.5f, 1, sceneMaterials.resource, sceneMaterials.resourcePatch); } else { if (sceneMaterials.resource != null) { nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 25, 0, new Vector2(25, 25), 4, 1, sceneMaterials.resource); } else { nextResourceId = resourceFactory.createResourcePatch(nextPatchId++, 25, 0, new Vector2(25, 25), 4, 1, Color.blue); } } WorldspaceUIFactory.createQuad("Resource Home", config.ResourceHomeRect, sceneMaterials.resourceHome); } return(result); }
/// <summary> /// Create the environment, including initializing and scaling the ground if necessary. /// </summary> /// <param name="config">The current Config.</param> /// <returns>Whether the environment was initialized successfully.</returns> private bool generateEnvironment(Config config) { float scaledGroundLength = config.GroundLength / 10.0f; // plane's dimensions are scaled up bool result = true; if (scaledGroundLength <= 0f) { result = false; } else { // Create environment header object if (EnvironmentObjects == null) { EnvironmentObjects = GameObject.Find("Environment"); if (EnvironmentObjects != null) { Log.d(LogTag.MAIN, "Located Environment header object in scene"); } else { Log.d(LogTag.MAIN, "Created Environment header object"); EnvironmentObjects = new GameObject("Environment"); EnvironmentObjects.transform.position = Vector3.zero; EnvironmentObjects.transform.rotation = new Quaternion(0f, 0f, 0f, 0f); } } // Create ground if (Ground == null) { Ground = GameObject.Find("Ground"); if (Ground != null) { Log.d(LogTag.MAIN, "Located Ground in scene"); } else { Log.d(LogTag.MAIN, "Created Ground"); Ground = GameObject.CreatePrimitive(PrimitiveType.Plane); Ground.name = "Ground"; } } // Set Ground tag to Ground Ground.tag = "Ground"; // Position and scale ground according to configuration Ground.transform.localScale = new Vector3(scaledGroundLength, 1.0f, scaledGroundLength); Ground.transform.position = Vector3.zero; Ground.transform.SetParent(EnvironmentObjects.transform); // Add barriers around the ground, assuming it's rectangular GameObject positiveX = GameObject.CreatePrimitive(PrimitiveType.Cube); positiveX.name = "Positive X"; positiveX.transform.localScale = new Vector3(0.1f, 0.25f, config.GroundLength + 0.2f); positiveX.transform.position = new Vector3(config.GroundLength / 2.0f + 0.05f, 0.125f, 0.0f); positiveX.transform.parent = EnvironmentObjects.transform; GameObject negativeX = GameObject.CreatePrimitive(PrimitiveType.Cube); negativeX.name = "Negative X"; negativeX.transform.localScale = new Vector3(0.1f, 0.25f, config.GroundLength + 0.2f); negativeX.transform.position = new Vector3(config.GroundLength / -2.0f - 0.05f, 0.125f, 0.0f); negativeX.transform.parent = EnvironmentObjects.transform; GameObject positiveZ = GameObject.CreatePrimitive(PrimitiveType.Cube); positiveZ.name = "Positive Z"; positiveZ.transform.localScale = new Vector3(config.GroundLength + 0.2f, 0.25f, 0.1f); positiveZ.transform.position = new Vector3(0.0f, 0.125f, config.GroundLength / 2.0f + 0.05f); positiveZ.transform.parent = EnvironmentObjects.transform; GameObject negativeZ = GameObject.CreatePrimitive(PrimitiveType.Cube); negativeZ.name = "Negative Z"; negativeZ.transform.localScale = new Vector3(config.GroundLength + 0.2f, 0.25f, 0.1f); negativeZ.transform.position = new Vector3(0.0f, 0.125f, config.GroundLength / -2.0f - 0.05f); negativeZ.transform.parent = EnvironmentObjects.transform; // Create satellite GameObject satelliteBody = GameObject.CreatePrimitive(PrimitiveType.Sphere); satelliteBody.tag = "Satellite"; satelliteBody.name = "Satellite"; satelliteBody.transform.parent = EnvironmentObjects.transform; satelliteBody.transform.position = new Vector3(0, 15, 0); Satellite = new Satellite(satelliteBody, this); // Create resources resourceFactory = new ResourceFactory(); uint startId = 0; for (uint i = 0; i < 3; i++) { // random resource generator code //avoid the center be in the center of the Robots home //uint sideLength = (uint)(Random.Range(1, 4)); //startId = resourceFactory.createResourcePatch(i, sideLength * sideLength, startId, center, 1.5f, 1.0f, Color.blue, sceneMaterials.resourcePatch); // only adds 3 resource batches Vector2 center = new Vector2(Random.Range(-25.0f, 25.0f), Random.Range(-25.0f, 25.0f)); uint sideLength = 3; startId = resourceFactory.createResourcePatch(i, sideLength * sideLength, startId, center, 1.5f, 1.0f, Color.blue, sceneMaterials.resourcePatch); } // Create Resource Home WorldspaceUIFactory.createQuad("Resource Home", new Rect(new Vector2(17, 17), new Vector2(10, 10)), sceneMaterials.resourceHome); } return(result); }