Esempio n. 1
0
    ///<summary>
    ///Create a hex on the given coordinates
    ///</summary>
    ///<param name="x">The x Coord of the hex</param>
    ///<param name="z">The z Coord of the hex</param>
    ///<param name="c">The character on the map, used to create the units</param>
    ///<param name="i">The loop index</param>
    ///<returns>Returns the created HexObject</returns>
    HexObject CreateHex(int x, int z, char c, int i)
    {
        float   xPos = x * (HexObject.innerRadius * 0.95f);
        float   zPos = (z + x * 0.5f - x / 2) * (HexObject.innerRadius * 1.1f);
        Vector3 pos  = new Vector3(xPos, 0f, zPos);

        //Instantiate a hex from the prefab
        HexObject hexCell = Instantiate <HexObject>(hexPrefab);

        hexCell.Coords = new Vector2(x, z);
        hexCell.transform.SetParent(transform, false);
        hexCell.transform.localPosition = pos;
        hexCell.hexType = HexObject.GetTypeFromChar(c);

        //If the given character is a unit character
        if (c == 'k' || c == 'c' || c == 'b')
        {
            Unit unit = Instantiate <Unit>(unitPrefab, hexCell.transform);
            unit.transform.localPosition = hexCell.transform.localPosition;
            hexCell.unit = unit;

            Unit.UnitType type;
            switch (c)
            {
            case 'c': type = Unit.UnitType.CAVELRY; break;

            case 'b': type = Unit.UnitType.BOWMAN; break;

            default: type = Unit.UnitType.KNIGHT; break;
            }
            unit.SetUnitType(type);

            unit.unitTeam = i < width * 2 ? Unit.UnitTeamType.PLAYER : Unit.UnitTeamType.ENEMY;

            Color unitColor = unit.unitTeam == Unit.UnitTeamType.PLAYER ? new Color(0.306f, 0.736f, 0.764f, 1) : new Color(0.773f, 0.412f, 0.353f, 1);
            unit.transform.GetComponentInChildren <Renderer>().material.SetColor("_Color", unitColor);

            units.Add(unit);
        }
        return(hexCell);
    }