Example #1
0
    public HexLoc MoveTo(HexDir dir)
    {
        dir = MoveDirFix(dir);
        HexLoc moveTo = new HexLoc(this.x, this.y);

        if ((int)dir == 0)
        {
            moveTo.x++;
            moveTo.y--;
        }
        if ((int)dir == 1)
        {
            moveTo.x++;
        }
        if ((int)dir == 2)
        {
            moveTo.y++;
        }
        if ((int)dir == 3)
        {
            moveTo.x--;
            moveTo.y++;
        }
        if ((int)dir == 4)
        {
            moveTo.x--;
        }
        if ((int)dir == 5)
        {
            moveTo.y--;
        }
        moveTo.z = (int)(0 - (moveTo.x + moveTo.y));
        return(moveTo);
    }
Example #2
0
        //public Coordinates coord;

        public Neighbor(int i)
        {
            direction = string.Empty;
            index     = i;
            hexDir    = HexDir.N;
            //coord = null;
        }
Example #3
0
        protected static HexDir GetOppositeDir(HexDir dir)
        {
            switch (dir)
            {
            case HexDir.Left:
                return(HexDir.Right);

            case HexDir.LeftForward:
                return(HexDir.RightBack);

            case HexDir.RightForward:
                return(HexDir.LeftBack);

            case HexDir.Right:
                return(HexDir.Left);

            case HexDir.RightBack:
                return(HexDir.LeftForward);

            case HexDir.LeftBack:
                return(HexDir.RightForward);

            default:
                return(0);
            }
        }
Example #4
0
    public void SetNeighbor(HexDir direction, List <Coordinates> coords, Coordinates cell)
    {
        _neighborIndexes[(int)direction] = new Neighbor(direction.ToString(), coords.IndexOf(cell));  //, cell);
        var opposite = direction.Opposite();

        cell._neighborIndexes[(int)opposite] = new Neighbor(opposite.ToString(), coords.IndexOf(this));  //, this);
    }
Example #5
0
    void TriangulateConnection(HexDir dir, HexCell cell, Vector3 v1, Vector3 v2)
    {
        HexCell Neighbor = cell.GetHexNeighbour(dir);

        if (null == Neighbor)
        {
            return;
        }
        //add rectangle
        Vector3 v3 = v1 + HexMetrics.GetBridge(dir);
        Vector3 v4 = v2 + HexMetrics.GetBridge(dir);

        AddQuad(v1, v2, v3, v4);


        AddQuadColor(cell.color, Neighbor.color);

        HexCell nextNeighbor = cell.GetHexNeighbour(HexDirectionExtensions.GetNextDir(dir));

        if (null != nextNeighbor)
        {
            AddTriangle(v2, v4, v2 + HexMetrics.GetBridge(HexDirectionExtensions.GetNextDir(dir)));
            AddTriangleColor(cell.color, Neighbor.color, nextNeighbor.color);
        }
    }
Example #6
0
    /// <summary>
    /// applies the correct textures, should only call this once
    /// </summary>
    public void SetupTextures()
    {
        string       textureBase  = "Images/Texture/Piece/" + colour.ToString().ToLower() + "-";
        Vector2      textureScale = new Vector2(6f, 10f);
        MeshRenderer meshRender   = obj.GetComponent <MeshRenderer>();

        // normal
        Material matNormColour = meshRender.materials[0];

        matNormColour.mainTexture      = Resources.Load <Texture>(textureBase + HexDir.GetShortName(normalDir) + normalRan);
        matNormColour.mainTextureScale = textureScale;

        // alt
        Material matAltColour = meshRender.materials[1];

        if (type != Type.Hook)
        {
            matAltColour.mainTexture = Resources.Load <Texture>(textureBase + HexDir.GetShortName(normalDir) + normalRan + "f");
        }
        else
        {
            matAltColour.mainTexture = Resources.Load <Texture>(textureBase + HexDir.GetShortName(normalDir) + normalRan);
        }
        matAltColour.mainTextureScale = textureScale;

        // piece colour
        meshRender.materials[2].color = GetMaterialColor();
    }
Example #7
0
        public Vector3 GetSiblingPos(HexDir dir)
        {
            switch (dir)
            {
            case HexDir.Left:
                return(Obj.transform.position + Vector3.left);

            case HexDir.LeftForward:
                return(Obj.transform.position + Vector3.left * 0.5f + Vector3.forward * Constants.c_sqrt_3 * 0.5f);

            case HexDir.RightForward:
                return(Obj.transform.position - Vector3.left * 0.5f + Vector3.forward * Constants.c_sqrt_3 * 0.5f);

            case HexDir.Right:
                return(Obj.transform.position - Vector3.left);

            case HexDir.RightBack:
                return(Obj.transform.position - Vector3.left * 0.5f - Vector3.forward * Constants.c_sqrt_3 * 0.5f);

            case HexDir.LeftBack:
                return(Obj.transform.position + Vector3.left * 0.5f - Vector3.forward * Constants.c_sqrt_3 * 0.5f);

            default:
                return(Vector3.zero);
            }
        }
Example #8
0
    public static Vector2 MoveInDirection(Vector2 startingPoint, HexDir dir)
    {
        switch (dir)
        {
        case HexDir.Up:
            return(startingPoint + new Vector2(0, 1));

        case HexDir.UpRight:
            return(startingPoint + new Vector2(1, 0));

        case HexDir.DownRight:
            return(startingPoint + new Vector2(1, -1));

        case HexDir.Down:
            return(startingPoint + new Vector2(0, -1));

        case HexDir.DownLeft:
            return(startingPoint + new Vector2(-1, 0));

        case HexDir.UpLeft:
            return(startingPoint + new Vector2(-1, 1));

        default:
            Debug.Log("Wat");
            return(startingPoint);
        }
    }
Example #9
0
    public void Load()
    {
        int radius = 7;

        if (radius > 0)
        {
            AddLoc(new HexLoc(0, 0));
            for (int fRadius = 1; fRadius <= radius; fRadius++)
            {
                //Set initial hex grid location
                HexLoc loc = new HexLoc(fRadius, -fRadius);
                HexDir dir = (HexDir)2;
                //Find data for each hex in the ring (each ring has 6 more hexes than the last)
                for (int fHex = 0; fHex < 6 * fRadius; fHex++)
                {
                    AddLoc(loc);
                    //Finds next hex in ring
                    loc = loc.MoveTo(dir);
                    if (loc.x == 0 || loc.y == 0 || loc.x == -loc.y)
                    {
                        dir++;
                    }
                }
            }
        }
        foreach (HexTile t in locs.Values)
        {
            t.FindConnections();
        }
    }
        private static void GetHexPos(ref int x, ref int y, HexDir dir)
        {
            switch (dir)
            {
            case HexDir.E:
                x++;
                return;

            case HexDir.W:
                x--;
                return;

            case HexDir.NW:
                y--;
                return;

            case HexDir.SE:
                y++;
                return;

            case HexDir.NE:
                x++;
                y--;
                return;

            case HexDir.SW:
                x--;
                y++;
                return;
            }
        }
Example #11
0
        public Vector3 GetSiblingPos(HexDir dir)
        {
            switch (dir)
            {
            case HexDir.Left:
                return(m_originPos + Vector3.left);

            case HexDir.LeftForward:
                return(m_originPos + Vector3.left * 0.5f + Vector3.forward * Constants.c_sqrt_3 * 0.5f);

            case HexDir.RightForward:
                return(m_originPos - Vector3.left * 0.5f + Vector3.forward * Constants.c_sqrt_3 * 0.5f);

            case HexDir.Right:
                return(m_originPos - Vector3.left);

            case HexDir.RightBack:
                return(m_originPos - Vector3.left * 0.5f - Vector3.forward * Constants.c_sqrt_3 * 0.5f);

            case HexDir.LeftBack:
                return(m_originPos + Vector3.left * 0.5f - Vector3.forward * Constants.c_sqrt_3 * 0.5f);

            default:
                return(Vector3.zero);
            }
        }
Example #12
0
        public virtual Hexagon CreateSibling <T>(HexDir dir, string index_name, System.Func <System.Type, Vector3, T> newFunc)
            where T : Hexagon, new()
        {
            T t = newFunc(typeof(T), GetSiblingPos(dir));

            m_siblingHexes[dir] = t;
            return(t);
        }
Example #13
0
 public Hexagon(Vector3 pos)
 {
     m_originPos = pos;
     Obj         = GameObject.Instantiate(Resources.Load(m_resPath) as GameObject, pos, Quaternion.identity);
     Model       = Obj.transform.Find("hexagon/Cylinder").gameObject;
     // friction
     for (HexDir i = HexDir.Left; i <= HexDir.LeftBack; i++)
     {
         m_frictions.Add(i, Random.Range(0.1f, 0.15f));
     }
 }
Example #14
0
 private HexDir MoveDirFix(HexDir dir)
 {
     while ((int)dir > 5)
     {
         dir -= 6;
     }
     while (dir < 0)
     {
         dir += 6;
     }
     return(dir);
 }
        internal static long Part2(string input)
        {
            string[] lines = input.Split('\n');
            bool[,] hexGrid = new bool[200, 200];

            int refx = 100;
            int refy = 100;

            foreach (string line in lines)
            {
                int x = refx;
                int y = refy;
                for (int c = 0; c < line.Length; c++)
                {
                    HexDir dir  = HexDir.E;
                    string next = "";
                    if (line[c] == 's' || line[c] == 'n')
                    {
                        next = line[c].ToString() + line[c + 1];
                        c++;
                    }
                    else
                    {
                        next = line[c].ToString();
                    }
                    dir = (HexDir)Enum.Parse(typeof(HexDir), next.ToUpper());
                    GetHexPos(ref x, ref y, dir);
                }
                hexGrid[x, y] = !hexGrid[x, y];
            }
            for (int i = 0; i < 100; i++)
            {
                MutateGrid(ref hexGrid);
            }
            int  count  = 0;
            bool offset = false;

            for (int py = 0; py < 200; py++)
            {
                for (int px = 0; px < 200; px++)
                {
                    if (hexGrid[px, py])
                    {
                        count++;
                    }
                    //Console.Write((hexGrid[px, py] ? "##" : "  "));
                }
                offset = !offset;
                //Console.Write("\n" + (offset ? " " : ""));
            }
            return(count);
        }
Example #16
0
        public Hexagon(Vector3 pos, int index_x, int index_y)
        {
            m_rand = new Random(DateTime.UtcNow.Second);

            m_originPos = pos;
            m_index_x   = index_x;
            m_index_y   = index_y;

            // friction
            for (HexDir i = HexDir.Left; i <= HexDir.LeftBack; i++)
            {
                m_frictions.Add(i, m_rand.Next(10, 15) / 100.0f);
            }
        }
Example #17
0
    void Triangulate(HexCell cell)
    {
        Vector3 center = cell.transform.localPosition;

        for (HexDir i = 0; i < HexDir.Size; i++)
        {
            Vector3 v1 = center + HexMetrics.GetFirstSolidDirCorner(i);
            Vector3 v2 = center + HexMetrics.GetSecSolidDirCorner(i);

            AddTriangle(
                center,
                v1,
                v2
                );

            AddTriangleColor(cell.color);

            if (i <= HexDir.SE)
            {
                TriangulateConnection(i, cell, v1, v2);
            }
            //Vector3 v3 = center + HexMetrics.GetFirstDirCorner(i);
            //Vector3 v4 = center + HexMetrics.GetSecDirCorner(i);

            //Vector3 v3 = v1 + HexMetrics.GetBridge(i);
            //Vector3 v4 = v2 + HexMetrics.GetBridge(i);


            //AddQuad(v1, v2, v3, v4);

            ////get cell neighbor
            //HexCell neighbor = cell.GetHexNeighbour(i) ?? cell;// neighbor == null ? cell : neighbor;;

            ////set triangle color
            ////AddQuadColor(cell.color, cell.color, (neighbor.color + cell.color + preneighbor.color) / 3f, (neighbor.color + cell.color + nexneighbor.color) / 3f);
            //AddQuadColor(cell.color, (cell.color + neighbor.color) * 0.5f);


            ////----------------------------Filling the gaps
            //HexCell preneighbor = cell.GetHexNeighbour(HexDirectionExtensions.GetPreviousDir(i)) ?? cell;
            //HexCell nexneighbor = cell.GetHexNeighbour(HexDirectionExtensions.GetNextDir(i)) ?? cell;
            //AddTriangle(v1, center + HexMetrics.GetFirstDirCorner(i), v3);
            //AddTriangleColor(cell.color, (cell.color + preneighbor.color + neighbor.color) / 3f, (cell.color + neighbor.color) * 0.5f);

            //AddTriangle(v2, v4, center + HexMetrics.GetSecDirCorner(i));
            //AddTriangleColor(cell.color, (cell.color + neighbor.color) * 0.5f, (cell.color + nexneighbor.color + neighbor.color) / 3f);
            ////----------------------------Filling the gaps
        }
    }
Example #18
0
        public static Hexagon CreateHexagon(HexType type, Hexagon sibling, HexDir dirToSibling, int index_x, int index_y)
        {
            Hexagon result = null;

            if (type == HexType.Game)
            {
                result = new GameHexagon(sibling.GetSiblingPos(dirToSibling), index_x, index_y);
            }
            else if (type == HexType.Solid)
            {
                result = new SolidHexagon(sibling.GetSiblingPos(dirToSibling), index_x, index_y);
            }

            return(result);
        }
Example #19
0
 protected void AddOrReplaceSibling(HexDir dir, Hexagon hex, bool setOpposite = true, bool searchAround = true)
 {
     if (m_siblingHexes.ContainsKey(dir))
     {
         if (null != m_siblingHexes[dir])
         {
             m_siblingHexes[dir].Destroy();
         }
         m_siblingHexes[dir] = hex;
     }
     else
     {
         m_siblingHexes.Add(dir, hex);
     }
 }
Example #20
0
        public static Hexagon CreateHexagon(HexType type, Hexagon sibling, HexDir dirToSibling, string name)
        {
            Hexagon result = null;

            if (type == HexType.Game)
            {
                result          = new GameHexagon(sibling.GetSiblingPos(dirToSibling));
                result.Obj.name = name;
            }
            else if (type == HexType.Solid)
            {
                result          = new SolidHexagon(sibling.GetSiblingPos(dirToSibling));
                result.Obj.name = name;
            }

            return(result);
        }
Example #21
0
    public bool CheckIfNeighbor(int index, out HexDir dir)
    {
        dir = HexDir.N;

        foreach (var neighbor in _neighborIndexes)
        {
            if (neighbor.index != index)
            {
                continue;
            }

            dir = neighbor.hexDir;
            return(true);
        }

        return(false);
    }
Example #22
0
	public GameObject getNeighbour(HexDir direction) {
		switch(direction) {
		case HexDir.E:
			return transform.GetComponentInParent<MapRow> ().getHexagon (hexaID + 1);
		case HexDir.W:
			return transform.GetComponentInParent<MapRow> ().getHexagon (hexaID - 1);
		case HexDir.NW:
			return getNWNeighbour ();
		case HexDir.NE:
			return getNENeighbour ();
		case HexDir.SE:
			return getSENeighbour ();
		case HexDir.SW:
			return getSWNeighbour ();
		default:
			break;
		}
		return null;
	}
Example #23
0
        /// <summary>
        /// Get next direction, default is clockwise
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        static HexDir GetNextDir(HexDir dir, bool isClockWise = true)
        {
            HexDir result = dir;

            if (isClockWise)
            {
                result += 1;
                if (result > HexDir.RightBack)
                {
                    result = HexDir.Left;
                }
            }
            else
            {
                result -= 1;
                if (result < HexDir.Left)
                {
                    result = HexDir.LeftBack;
                }
            }

            return(result);
        }
Example #24
0
    private static Coordinates GetSequentialNeighbors(Coordinates startingPoint, HexDir direction,
                                                      List <Coordinates> coords,
                                                      int dist, bool shouldAdd = true)
    {
        var         neighbor      = startingPoint.GetNeighbor((int)direction);
        Coordinates neighborCoord = null;

        for (var i = 0; i < dist; i++)
        {
            neighborCoord = HexMaker.GetCoord(neighbor.index);
            if (neighborCoord != null)
            {
                if (neighborCoord.IsWalkable)
                {
                    coords.Add(neighborCoord);
                }

                neighbor = neighborCoord.GetNeighbor((int)direction);
            }
        }

        return(neighborCoord);
    }
Example #25
0
 public static HexDir Opposite(this HexDir dir)
 {
     return((int)dir < 3 ? (dir + 3) : (dir - 3));
 }
Example #26
0
 public static HexDir Opposite(this HexDir direction)
 {
     return((int)direction < 3 ? (direction + 3) : (direction - 3));
 }
Example #27
0
 public static HexDir GetNextDir(HexDir dir)
 {
     return(dir == HexDir.NW ? HexDir.NE : dir + 1);
 }
Example #28
0
 public HexCell GetHexNeighbour(HexDir dir)
 {
     return(Neighbours[(int)dir]);
 }
Example #29
0
 public void SetHexNeighbour(HexDir dir, HexCell cell)
 {
     Neighbours[(int)dir] = cell;
     cell.Neighbours[(int)dir.Opposite()] = this;
 }
Example #30
0
 public void SetSibling(HexDir dir, Hexagon hex)
 {
     m_siblingHexes[dir] = hex;
 }
Example #31
0
 public void RemoveSibling(HexDir dir)
 {
     m_siblingHexes.Remove(dir);
 }