Ejemplo n.º 1
0
    /// <summary>
    /// Get the corners of a QR-space rectangle containing every cell touching an XY-space rectangle.
    /// </summary>
    public static HexCoord[] CartesianRectangleBounds(Vector2 cornerA, Vector2 cornerB)
    {
        Vector2 min = new Vector2(Math.Min(cornerA.x, cornerB.x), Math.Min(cornerA.y, cornerB.y));
        Vector2 max = new Vector2(Math.Max(cornerA.x, cornerB.x), Math.Max(cornerA.y, cornerB.y));

        HexCoord[] results =
        {
            HexCoord.AtPosition(min),
            HexCoord.AtPosition(max)
        };
        Vector2 pos = results[0].Position();

        if (pos.y - 0.5f >= min.y)
        {
            results[0] += m_AxialDirections[4];
        }
        else if (pos.x >= min.x)
        {
            results[0] += m_AxialDirections[3];
        }
        pos = results[1].Position();
        if (pos.y + 0.5f <= max.y)
        {
            results[1] += m_AxialDirections[1];
        }
        else if (pos.x <= max.x)
        {
            results[1] += m_AxialDirections[0];
        }
        return(results);
    }
Ejemplo n.º 2
0
    void OnDrop()
    {
        ZIndex = oldZIndex;
        puzzle.HideCursor();

        var coord   = HexCoord.AtPosition(Position);
        var mapCell = puzzle.Map.TryGetCell(coord);

        if (!IsValidDrop(mapCell))
        {
            puzzle.SoundPlayerWhoosh.Play(0);
            puzzle.ResetTile(this);
            return;
        }

        puzzle.SoundPlayerDrop.Play(0);

        MakeFixed();

        puzzle.Map.SetCell(new CellInfo(coord, this));
        puzzle.SnapTileToCell(this, coord);
        puzzle.SpawnTile();

        CalculatePaths(puzzle, coord);

        if (puzzle.Map.TileCount == puzzle.Map.CellCount)
        {
            OnFail();
        }
    }
Ejemplo n.º 3
0
    //Used for the very first placement of the game. Only hex 0,0 is eligible;
    public void GetEligibleFirstPlacement()
    {
        eligibleList.Clear();
        HexCoord originHex = HexCoord.AtPosition(new Vector2(0, 0));

        eligibleList.Add(originHex);   //Add hexcoord 0,0
        hexController.HighlightEligible(eligibleList);
    }
Ejemplo n.º 4
0
    public Vector2 FindNearest(Vector2 pos)
    {
        // Find the hex that overlaps a certain x,y point
        //Debug.Log(pos);
        nearestHex = HexCoord.AtPosition(pos);
        Vector2 hexPos = nearestHex.Position();

        return(hexPos);
    }
Ejemplo n.º 5
0
    //Used for the second placement of the game. Hexes neighboring 0,0 are eligible;
    public void GetEligibleSecondPlacement()
    {
        eligibleList.Clear();
        HexCoord originHex = HexCoord.AtPosition(new Vector2(0, 0));

        foreach (HexCoord nh in originHex.Neighbors())
        {
            eligibleList.Add(nh);   //Add all neighbors
        }

        hexController.HighlightEligible(eligibleList);
    }
Ejemplo n.º 6
0
    public void HighlightNearest(Vector2 pos)
    {
        nearestHex = HexCoord.AtPosition(pos);
        Vector2 hexPos = nearestHex.Position();

        if (nearestHex != prevHighlightedHex)
        {
            if (prevHighlight != null)
            {
                Destroy(prevHighlight);
            }
            GameObject highlight = (GameObject)Instantiate(highlightPrefab);
            highlight.GetComponent <HighlightController>().IneligibleHex();
            highlight.transform.position = hexPos;
            prevHighlight      = highlight;
            prevHighlightedHex = nearestHex;
        }
    }
Ejemplo n.º 7
0
    void HandleDrag()
    {
        if (!isDragging)
        {
            if (!isUnderMouse)
            {
                return;
            }

            isDragging = Input.IsActionPressed("left_click");
            if (!isDragging)
            {
                return;
            }

            mouseOffset = Position - GetViewport().GetMousePosition() / GlobalScale;
            oldZIndex   = ZIndex;
            ZIndex      = (int)ZLayers.DragTile;

            OnStartDrag();
        }

        if (!Input.IsActionPressed("left_click"))
        {
            isDragging = false;
            OnDrop();
            return;
        }

        Position = GetViewport().GetMousePosition() / GlobalScale + mouseOffset;
        var coord = HexCoord.AtPosition(Position);

        var mapCell = puzzle.Map.TryGetCell(coord);

        if (!IsValidDrop(mapCell))
        {
            puzzle.HideCursor();
            return;
        }

        puzzle.ShowCursor(coord);
    }
Ejemplo n.º 8
0
    //NOT USED
    public void HighlightNearestDeprecated(Vector2 pos)
    {
        // Find the hex that overlaps a certain x,y point
        //Debug.Log(pos);
        nearestHex = HexCoord.AtPosition(pos);
        Vector2    hexPos  = nearestHex.Position();
        GameObject hexDisp = null;

        //Brute force method to get hex display associated with a certain HexCoord
        int i = 0;
        //int ind = 0;
        float smallestDist = Mathf.Infinity;

        foreach (Transform child in HexDisplays.transform)
        {
            float dist = Vector2.SqrMagnitude(hexPos - (Vector2)child.position);

            if (dist < smallestDist)
            {
                smallestDist = dist;
                //ind = i;
                hexDisp = child.gameObject;
            }
            i++;
        }

        //Highlight the hex
        if (hexDisp != null && hexDisp != prevHexDisp)
        {
            if (prevHexDisp != null)
            {
                prevHexDisp.GetComponent <HexDisplayControl>().Highlight(false);
            }
            hexDisp.GetComponent <HexDisplayControl>().Highlight(true);

            prevHexDisp = hexDisp;
        }
    }
Ejemplo n.º 9
0
 public HexCoord GetNearestHex(Vector2 pos)
 {
     nearestHex = HexCoord.AtPosition(pos);
     return(nearestHex);
 }