Esempio n. 1
0
    // Creates a new honeycomb in the game, initialized with the given data.
    public void InitializeWithData(HoneycombData data)
    {
        // Destroy all old game objects.
        foreach (Strip strip in this.strips)
        {
            GameObject.Destroy(strip);
        }
        this.strips.Clear();

        // Compute the honeycomb properties.
        float stripWidth = Util.edgeLengthToStripWidth(this.edgeLength);

        // Compute the position of the first strip.
        // The center of each strip is the middle of the strip.
        // Each strip runs parallel to the y-axis. Their centers are in a line parallel to the x-axis.
        Vector3 nextPosition = this.transform.position;

        nextPosition.x -= (data.strips.Length - 1f) * 0.5f * stripWidth;
        // Create the strips.
        for (int i = 0; i < data.strips.Length; ++i)
        {
            StripData stripData = data.strips[i];
            Strip     strip     = Instantiate <Strip>(this.stripPrefab, nextPosition, Quaternion.identity);
            strip.stripNumber = i;
            this.strips.Add(strip);
            strip.InitializeWithData(stripData, this.edgeLength);
            // Set the next hex's position.
            nextPosition.x += stripWidth;
        }
    }