示例#1
0
    public void GenerateHarbors(Transform parent)
    {
        List <Tuple <Intersection, Intersection> > harborIntersectionPairsList = new List <Tuple <Intersection, Intersection> > ();
        List <Intersection> shoreIntersections = new List <Intersection>();

        foreach (Intersection intersection in Intersections.Values)
        {
            if (intersection.isShoreIntersection() && intersection.isMainIslandIntersection())
            {
                shoreIntersections.Add(intersection);
            }
        }

        int numHarbors    = 14;
        int segmentLength = shoreIntersections.Count / numHarbors;
        int randNum       = Random.Range(0, 3);

        for (int i = 0; i < shoreIntersections.Count; i++)
        {
            if (i % segmentLength == 0 && shoreIntersections[i].harbor == null)
            {
                List <Intersection> neighbors = shoreIntersections [i].getNeighborIntersections();
                Intersection        neighbor  = null;

                foreach (Intersection neighborIntersection in neighbors)
                {
                    if (neighborIntersection.isShoreIntersection())
                    {
                        neighbor = neighborIntersection;
                    }
                }

                if (harborID == 1 || harborID == 5 || harborID == 6 || harborID == 8 || harborID == 9 || harborID == 12)
                {
                    harborID++;
                    continue;
                }

                int randomNum   = Random.Range(0, harborPrefabs.Length);
                int harborIndex = harborID % harborPrefabs.Length;

                if (harborID == 10)
                {
                    harborIndex = 5;
                }
                GameObject harborGO = Instantiate(harborPrefabs[harborIndex], parent);
                //GameObject harborGO = Instantiate(harborPrefabs[randomNum], parent);
                Harbor harbor = harborGO.GetComponent <Harbor> ();
                harbor.id = harborID++;

                shoreIntersections [i].harbor = harbor;
                neighbor.harbor = harbor;

                harborGO.transform.position = shoreIntersections [i].getCommonTileWith(neighbor, TileType.Ocean).transform.position
                                              + 0.01f * Vector3.up;
                harborGO.transform.localScale *= 1.25f;
                harborGO.name = "Harbor " + harbor.id;

                harbor.locations.Add(shoreIntersections [i]);
                harbor.locations.Add(neighbor);

                shoreIntersections [i].getCommonTileWith(neighbor, TileType.Ocean).hasHarbor = true;

                SpriteRenderer[] arrows = harbor.GetComponentsInChildren <SpriteRenderer> ();

                /*arrows [1].transform.LookAt (shoreIntersections [i].transform.position + Vector3.up * arrows[1].transform.position.y);
                 * arrows[1].transform.Translate(Vector3.up * 0.3f);
                 * arrows [1].transform.Translate (Vector3.forward * 0.2f, Space.Self);
                 * arrows[1].transform.Rotate(new Vector3(90f, 2700f, 0.0f));
                 * arrows [2].transform.LookAt (neighbor.transform.position + Vector3.up * arrows[2].transform.position.y);
                 * arrows [2].transform.Translate (Vector3.forward * 0.2f, Space.Self);
                 * arrows[2].transform.Translate(Vector3.up * 0.3f);
                 * arrows[2].transform.Rotate(new Vector3(90f, 270f, 0.0f));*/

                int cornerNum1 = shoreIntersections [i].getCommonTileWith(neighbor, TileType.Ocean).getCornerNumberOfIntersection(shoreIntersections [i]);
                arrows [1].transform.rotation = Quaternion.Euler(new Vector3(90.0f, 0.0f, 30.0f + 60.0f * cornerNum1));
                arrows[1].transform.Translate(Vector3.right * (float)(5 * hexRadius / 8), Space.Self);

                int cornerNum2 = shoreIntersections [i].getCommonTileWith(neighbor, TileType.Ocean).getCornerNumberOfIntersection(neighbor);
                arrows [2].transform.rotation = Quaternion.Euler(new Vector3(90.0f, 0.0f, 30.0f + 60.0f * cornerNum2));
                arrows[2].transform.Translate(Vector3.right * (float)(5 * hexRadius / 8), Space.Self);

                harbors.Add(harbor);
                harborsByIdDictionary.Add(harborID - 1, harbor);
            }
        }
    }