Example #1
0
    public HexCell GetCellFromPosition(Vector3 point)
    {
        HexCoordinate hexCoord = HexCoordinate.FromPosition(transform.InverseTransformPoint(point));
        int           index    = hexCoord.X + hexCoord.Z * mapWidth + hexCoord.Z / 2;

        return(cells[index]);
    }
    public HexCell GetCell(Vector3 position)
    {
        position = transform.InverseTransformPoint(position);
        HexCoordinate coordinate = HexCoordinate.FromPosition(position);
        int           index      = coordinate.X + coordinate.Z * sizeX + coordinate.Z / 2;

        return(cells[index]);
    }
Example #3
0
    public void ColorCell(Vector3 position, Color color)
    {
        position = transform.InverseTransformPoint(position);
        HexCoordinate coordinates = HexCoordinate.FromPosition(position);
        int           index       = coordinates.X + coordinates.Z * width + coordinates.Z / 2;
        HexCell       cell        = cells[index];

        cell.color = color;
        hexMesh.Triangulate(cells);
    }
Example #4
0
    public void SelectHex(Vector3 point)
    {
        UnselectHex();
        HexCoordinate hexCoord = HexCoordinate.FromPosition(transform.InverseTransformPoint(point));
        int           index    = hexCoord.X + hexCoord.Z * mapWidth + hexCoord.Z / 2;

        selectedCell = cells[index];
        selectedCell.indicator.gameObject.SetActive(true);
        selectedCell.indicator.SetColor(Indicator.StartColor);
    }
Example #5
0
    public static TileObject Get(Vector3 position)
    {
        HexCoordinate coordinate = HexCoordinate.FromPosition(position);

        for (int i = 0; i < instance.tiles.Length; i++)
        {
            if (instance.tiles[i].coordinate == coordinate)
            {
                return(instance.tiles[i]);
            }
        }

        return(null);
    }
Example #6
0
    private void HandleInput()
    {
        Ray        ray  = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit  = new RaycastHit();
        TileObject tile = null;

        if (Physics.Raycast(ray, out hit))
        {
            MousePosition = hit.point + Camera.main.transform.forward * 0.1f;

            HexCoordinate coordinates = HexCoordinate.FromPosition(MousePosition);
            int           index       = coordinates.X + coordinates.Z * Config.GameSave.width + coordinates.Z / 2;
            if (WorldManager.Tiles.Length > index && index >= 0)
            {
                tile = WorldManager.Tiles[index];
            }
            else
            {
                tile = null;
            }
        }
        else
        {
            MousePosition = Vector3.zero;
            tile          = null;
        }

        if (Input.GetMouseButtonUp(0) && !CameraManager.Dragging)
        {
            Config.Temporary.selectedTile = tile;
            CanvasLayerInGame.RefreshSelectedTile();
        }
        if (Config.Temporary.selectedUnit)
        {
            if (Input.GetMouseButtonDown(1))
            {
                path = WorldPathfinding.FindPath(Config.Temporary.selectedTile, tile).ToList();
            }

            if (Input.GetMouseButton(1))
            {
                if (tile)
                {
                    Gizmos.DrawTile(tile.transform.position + Vector3.up * 0.05f, WorldManager.InnerRadius, Color.white);
                }

                if (lastTile != tile && tile)
                {
                    var newPath = WorldPathfinding.FindPath(Config.Temporary.selectedTile, tile);
                    if (newPath != null)
                    {
                        path = newPath.ToList();
                    }
                }

                if (path != null && tile)
                {
                    for (int i = 0; i < path.Count; i++)
                    {
                        float size = (Mathf.Sin((Time.time + i * 0.2f) * 3f) + 5f) / 8f;

                        Color color = Color.yellow;
                        if (path.Count - i > Config.Temporary.selectedUnit.Moves + 1)
                        {
                            color = Color.red;
                        }

                        Gizmos.DrawTile(path[i].transform.position + Vector3.up * 0.05f, WorldManager.InnerRadius * size, color);
                    }
                }
            }
            if (Input.GetMouseButtonUp(1))
            {
                if (tile)
                {
                    int movesToSub = 0;
                    for (int i = 0; i < path.Count; i++)
                    {
                        if (i <= Config.Temporary.selectedUnit.Moves)
                        {
                            movesToSub++;
                            Config.Temporary.selectedUnit.Tile = path[(path.Count - 1) - i];
                            Config.Temporary.selectedTile      = path[(path.Count - 1) - i];
                        }
                    }

                    Config.Temporary.selectedUnit.Moves -= movesToSub - 1;
                    CanvasLayerInGame.RefreshSelectedTile();
                }
            }
        }

        lastTile = tile;
    }