} // end SetDimensions function // Generates tiles and adds them to the dictionary. public static void GenerateAndAddTiles() { // Get all the game objects tagged as resources. GameObject[] resourceObjects = GameObject.FindGameObjectsWithTag("Resource"); // Loop over the map. Width first. for (int width = 32; width < (int)MapSize.x; width += 64) { // Then height. for (int height = 32; height < (int)MapSize.y; height += 64) { // We are in the fourth quadrant so the y is negative. Vector3 key = new Vector3(width, height * -1, -1.0f); // Create an empty tile at the given position. Tile newTile = new Tile(key, ResourceType.NONE, null); // Add the tile to the dictionary. TileDictionary.AddEntry(key, newTile); } // end inner height for loop } // end outer width for loop // Now loop over the resource array and set the tiles to resources. for (int index = 0; index < resourceObjects.Length; index++) { Vector3 key = ToPixels(resourceObjects[index].transform.position); // Get the ResourceTile component. ResourceTile resTile = resourceObjects[index].GetComponent <ResourceTile>(); // Holds the tile's resource type. ResourceType resourceType = resTile.resourceType; // Update the tile at the given key. TileDictionary.UpdateTile(key, resourceType, resourceObjects[index]); } // end for loop } // end GenerateAndAddTiles function.
// Handle the custom properties. public void HandleCustomProperties (GameObject gameObject, IDictionary<string, string> customProperties) { // Does this game oject have a ResType property? if ( !customProperties.ContainsKey( "ResType" ) ) { // Simply return. return; } // Now we meed to determine which type of resource it is. ResourceType tmp; // Holds the results of parsing. GameObject instance = null; // Holds the game object instance. try { // Attempt to parse the string into the enum value. tmp = (ResourceType)Enum.Parse( typeof( ResourceType ), customProperties["ResType"] ); // Switch over the possible values. switch ( tmp ) { case ResourceType.WOOL: { // Instantiate the resource prefab. instance = GameObject.Instantiate( PrefabReference.prefabResource_Wool ) as GameObject; // Set the instances name to the name of the prefab. instance.name = PrefabReference.prefabResource_Wool.name; // Add the ResourceTile component to the instance. ResourceTile resTile = instance.AddComponent<ResourceTile>(); // Add the resource type to the script component. resTile.resourceType = ResourceType.WOOL; break; } // end case case ResourceType.WOOD: { // Instantiate the resource prefab. instance = GameObject.Instantiate( PrefabReference.prefabResource_Wood ) as GameObject; // Set the instances name to the name of the prefab. instance.name = PrefabReference.prefabResource_Wood.name; // Add the ResourceTile component to the instance. ResourceTile resTile = instance.AddComponent<ResourceTile>(); // Add the resource type to the script component. resTile.resourceType = ResourceType.WOOD; break; } // end case case ResourceType.FISH: { // Instantiate the resource prefab. instance = GameObject.Instantiate( PrefabReference.prefabResource_Fish ) as GameObject; // Set the instances name to the name of the prefab. instance.name = PrefabReference.prefabResource_Fish.name; // Add the ResourceTile component to the instance. ResourceTile resTile = instance.AddComponent<ResourceTile>(); // Add the resource type to the script component. resTile.resourceType = ResourceType.FISH; break; } // end case case ResourceType.ORE: { // Instantiate the resource prefab. instance = GameObject.Instantiate( PrefabReference.prefabResource_Ore ) as GameObject; // Set the instances name to the name of the prefab. instance.name = PrefabReference.prefabResource_Ore.name; // Add the ResourceTile component to the instance. ResourceTile resTile = instance.AddComponent<ResourceTile>(); // Add the resource type to the script component. resTile.resourceType = ResourceType.ORE; break; } // end case default: { // Couldn't parse correctly so set the instance to null. instance = null; break; } // end default case } // end switch statment } // end try statement catch (Exception ex) { // The parsing failed so set the instance to null. Debug.Log( "Something went wrong. Exception: " + ex.Message ); instance = null; } // end catch statement // Make sure instance exists. if ( instance == null ) { // Simply return. return; } // Tag the instance as a Resource. instance.tag = "Resource"; // Use the position of the game object we're attached to. instance.transform.parent = gameObject.transform; instance.transform.localPosition = new Vector3( 0.0f, 0.0f, -1.0f ); // Scale by a factor of 100. For some reason they're 1/100th the size and this make them big enought o be visible. instance.transform.localScale = new Vector3(100.0f, 100.0f, 0.0f); // Add to the parent transform's local position. This corrects the placement. instance.transform.parent.localPosition += new Vector3(32.0f, 32.0f, 0.0f); } // end HandleCustomProperties function