Example #1
0
    void BuildStructures()
    {
        // Loop through each pixel of the structures map
        // if we find pixels that aren't transparent (or whatever our criteria is)
        // then we will spawn a structure based on the color code.

        Color32[] pixels = StructureMapTexture.GetPixels32();

        Color32 c32 = new Color32(255, 0, 0, 255);


        for (int x = 0; x < StructureMapTexture.width; x++)
        {
            for (int y = 0; y < StructureMapTexture.height; y++)
            {
                Color32 p = pixels[x + y * StructureMapTexture.width];
                if (p.a < 128)
                {
                    // transparent pixel, ignore.
                    continue;
                }

                Debug.Log("Not transparent!: " + p.ToString());

                foreach (StructureColor sc in StructureColors)
                {
                    if (sc.Color.r == p.r && sc.Color.g == p.g && sc.Color.b == p.b)
                    {
                        Debug.Log("Color match!");
                        // What is the position of the building?
                        Quaternion quaLatLon = CoordHelper.UVToRotation(new Vector2((float)x / StructureMapTexture.width, (float)y / StructureMapTexture.height));

                        // Are we within DegreesPerChunk/2 of the center of the chunk,
                        // along both direction?

                        float xDiff = quaLatLon.eulerAngles.x - ChunkRotation.eulerAngles.x;
                        if (xDiff > 180)
                        {
                            xDiff = xDiff - 360;
                        }
                        float yDiff = quaLatLon.eulerAngles.y - ChunkRotation.eulerAngles.y;
                        if (yDiff > 180)
                        {
                            yDiff = yDiff - 360;
                        }

                        if (Mathf.Abs(xDiff) > DegreesPerChunk / 2f || Mathf.Abs(yDiff) > DegreesPerChunk / 2f)
                        {
                            // Not in our chunk!
                            continue;
                        }

                        // Spawn the correct building.
                        Vector3    pos          = Vector3.zero;
                        Quaternion rot          = Quaternion.identity;
                        GameObject theStructure = (GameObject)Instantiate(sc.StructurePrefab, pos, rot, this.transform);

                        // Stop looping through structure colors
                        break; // foreach
                    }
                }
            }
        }
    }