Example #1
0
        private int DrawFromVertex3(IntVector2 topLeftCell, HexSprite currentSprite, IntVector2 currentCell, List <IntVector2> gridCells, List <Vector3> vertexList)
        {
            IntVector2 nextCell;

            // compare the coordinates of the next
            // if current x even, the next cell on the same y level is up; if odd, it's down
            // if even and x, y or if odd and x, y-1
            if (currentCell.x % 2 == 0)
            {
                nextCell = gridCells.FirstOrDefault(c => c.x == currentCell.x + 1 && c.y == currentCell.y + 1);
            }
            else
            {
                nextCell = gridCells.FirstOrDefault(c => c.x == currentCell.x + 1 && c.y == currentCell.y);
            }

            if (nextCell != null)
            {
                // we need 1->2 of the next cell
                HexSprite nextSprite = _cells[nextCell.x, nextCell.y].GetComponent <HexSprite>();
                vertexList.Add(nextSprite.FindVertex2());
                //gridCells.Remove(nextCell);
                return(DrawFromVertex2(topLeftCell, nextSprite, nextCell, gridCells, vertexList));
            }
            else
            {
                // vertex 3 should not touch the topLeftHex, so draw 3 -> 4 on this cell and continue
                vertexList.Add(currentSprite.FindVertex4());
                return(DrawFromVertex4(topLeftCell, currentSprite, currentCell, gridCells, vertexList));
            }
        }
Example #2
0
        private int DrawFromVertex1(IntVector2 topLeftCell, HexSprite currentSprite, IntVector2 currentCell, List <IntVector2> gridCells, List <Vector3> vertexList)
        {
            // see if there is a node directly above us
            IntVector2 nextCell = gridCells.FirstOrDefault(c => c.x == currentCell.x && c.y == currentCell.y - 1);

            if (nextCell != null)
            {
                // we need 5->6 of the next cell
                var nextSprite = _cells[nextCell.x, nextCell.y].GetComponent <HexSprite>();
                vertexList.Add(nextSprite.FindVertex6());
                //gridCells.Remove(nextCell);
                return(DrawFromVertex6(topLeftCell, nextSprite, nextCell, gridCells, vertexList));
            }
            else
            {
                // see if the topLeftCell is directly above this cell
                if (topLeftCell.x == currentCell.x && topLeftCell.y == currentCell.y - 1)
                {
                    return(5);
                }
                else
                {
                    // draw 1->2 and continue
                    vertexList.Add(currentSprite.FindVertex2());
                    return(DrawFromVertex2(topLeftCell, currentSprite, currentCell, gridCells, vertexList));
                }
            }
        }
Example #3
0
 private void OnHexSpriteEntered(HexSprite hexSprite)
 {
     HoverHex = hexSprite;
     if (OnHexEntered != null)
     {
         OnHexEntered(hexSprite);
     }
 }
Example #4
0
 private void OnHexSpriteExited(HexSprite hexSprite)
 {
     if (HoverHex == hexSprite)
     {
         HoverHex = null;
     }
     if (OnHexExited != null)
     {
         OnHexExited(hexSprite);
     }
 }
Example #5
0
 private void OnHexClick(HexSprite hexSprite)
 {
     if (SelectedHex != null)
     {
         SelectedHex.ShowBorder(false);
     }
     hexSprite.ShowBorder(true);
     SelectedHex = hexSprite;
     if (OnHexSelect != null)
     {
         OnHexSelect(hexSprite.GridPosition);
     }
 }
Example #6
0
        private int DrawFromVertex4(IntVector2 topLeftCell, HexSprite currentSprite, IntVector2 currentCell, List <IntVector2> gridCells, List <Vector3> vertexList)
        {
            // see if there is a node directly below us
            IntVector2 nextCell = gridCells.FirstOrDefault(c => c.x == currentCell.x && c.y == currentCell.y + 1);

            if (nextCell != null)
            {
                // we need 2->3 of the next cell
                HexSprite nextSprite = _cells[nextCell.x, nextCell.y].GetComponent <HexSprite>();
                vertexList.Add(nextSprite.FindVertex3());
                return(DrawFromVertex3(topLeftCell, nextSprite, nextCell, gridCells, vertexList));
            }
            else
            {
                //top left cannot be directly below us
                // draw 4->5 and continue
                vertexList.Add(currentSprite.FindVertex5());
                return(DrawFromVertex5(topLeftCell, currentSprite, currentCell, gridCells, vertexList));
            }
        }
Example #7
0
        private int DrawFromVertex6(IntVector2 topLeftCell, HexSprite currentSprite, IntVector2 currentCell, List <IntVector2> gridCells, List <Vector3> vertexList)
        {
            IntVector2 nextCell;

            // compare the coordinates of the next
            // if current x even, the next cell on the same y level is up; if odd, it's down
            // if even and x, y or if odd and x, y-1
            if (currentCell.x % 2 == 0)
            {
                nextCell = gridCells.FirstOrDefault(c => c.x == currentCell.x - 1 && c.y == currentCell.y);
            }
            else
            {
                nextCell = gridCells.FirstOrDefault(c => c.x == currentCell.x - 1 && c.y == currentCell.y - 1);
            }

            if (nextCell != null)
            {
                // we need 4->5 of the next cell
                HexSprite nextSprite = _cells[nextCell.x, nextCell.y].GetComponent <HexSprite>();
                vertexList.Add(nextSprite.FindVertex5());
                return(DrawFromVertex5(topLeftCell, nextSprite, nextCell, gridCells, vertexList));
            }
            else
            {
                // see if the top left hex is to our top left
                if ((currentCell.x % 2 == 0 && topLeftCell.x == currentCell.x - 1 && topLeftCell.y == currentCell.y) ||
                    (currentCell.x % 2 == 1 && topLeftCell.x == currentCell.x - 1 && topLeftCell.y == currentCell.y - 1))
                {
                    return(4);
                }
                else
                {
                    // draw 6->1 on this cell and continue
                    vertexList.Add(currentSprite.FindVertex1());
                    return(DrawFromVertex1(topLeftCell, currentSprite, currentCell, gridCells, vertexList));
                }
            }
        }
Example #8
0
        void CreateCell(int x, int y)
        {
            HexSprite cell = _cells[x, y] = Instantiate(HexCellPrefab).GetComponent <HexSprite>();

            cell.GridPosition = new IntVector2 {
                x = x, y = y
            };
            cell.OnClicked += OnHexClick;
            cell.OnEntered += OnHexSpriteEntered;
            cell.OnExited  += OnHexSpriteExited;
            VisualNode mapNode = null;

            if (x < _visualData.GetLength(0) && y < _visualData.GetLength(1))
            {
                mapNode = _visualData[x, y];
            }
            if (mapNode != null)
            {
                cell.Renderer.color = mapNode.Color;
                switch (mapNode.TopVisual)
                {
                case TopVisual.Starbase:
                    GameObject     starBase   = Instantiate(StarbasePrefab);
                    SpriteRenderer sbRenderer = starBase.GetComponent <SpriteRenderer>();
                    starBase.transform.SetParent(cell.transform, false);
                    sbRenderer.color = new Color(mapNode.TopVisualColor.r * 0.5f, mapNode.TopVisualColor.g * 0.5f, mapNode.TopVisualColor.b * 0.5f);
                    break;

                case TopVisual.Battlestation:
                    GameObject     battleStation = Instantiate(BattlestationPrefab);
                    SpriteRenderer bsRenderer    = battleStation.GetComponent <SpriteRenderer>();
                    battleStation.transform.SetParent(cell.transform, false);
                    bsRenderer.color = new Color(mapNode.TopVisualColor.r * 0.5f, mapNode.TopVisualColor.g * 0.5f, mapNode.TopVisualColor.b * 0.5f);
                    break;

                case TopVisual.MajorPlanet:
                    GameObject     major       = Instantiate(MajorPlanetPrefab);
                    SpriteRenderer majRenderer = major.GetComponent <SpriteRenderer>();
                    major.transform.SetParent(cell.transform, false);
                    majRenderer.color = new Color(mapNode.TopVisualColor.r * 0.5f, mapNode.TopVisualColor.g * 0.5f, mapNode.TopVisualColor.b * 0.5f);
                    break;

                case TopVisual.MinorPlanet:
                    GameObject     minor       = Instantiate(MinorPlanetPrefab);
                    SpriteRenderer minRenderer = minor.GetComponent <SpriteRenderer>();
                    minor.transform.SetParent(cell.transform, false);
                    minRenderer.color = new Color(mapNode.TopVisualColor.r * 0.5f, mapNode.TopVisualColor.g * 0.5f, mapNode.TopVisualColor.b * 0.5f);
                    break;

                case TopVisual.Capital:
                    GameObject     cap         = Instantiate(CapitalPrefab);
                    SpriteRenderer capRenderer = cap.GetComponent <SpriteRenderer>();
                    cap.transform.SetParent(cell.transform, false);
                    capRenderer.color = new Color(mapNode.TopVisualColor.r * 0.5f, mapNode.TopVisualColor.g * 0.5f, mapNode.TopVisualColor.b * 0.5f);
                    break;
                }
            }

            float height = cell.Renderer.sprite.bounds.size.y * cell.Renderer.transform.localScale.y;
            float width  = cell.Renderer.sprite.bounds.size.x * cell.Renderer.transform.localScale.x;

            Vector3 position;

            position.x = x * width * 0.75f;
            position.y = -(y + x * 0.5f - (x + 1) / 2) * height;
            position.z = 0;

            cell.transform.SetParent(transform, false);
            cell.transform.localPosition = position;

            TextMesh label = cell.GetComponentInChildren <TextMesh>();

            label.text = x.ToString("00") + y.ToString("00");
            if (x == 0 || y == 0)
            {
                cell.Renderer.enabled = false;
                label.text            = "";
            }
        }