Example #1
0
    public List <GameBoardCell> GetCellsAround(GameBoardCell cell, int distance)
    {
        List <GameBoardCell> tiles     = new List <GameBoardCell>();
        List <Vector2>       tranforms = new List <Vector2>()
        {
            new Vector2(1, 1), new Vector2(-1, 1), new Vector2(1, -1), new Vector2(-1, -1)
        };

        for (int x = 0; x <= distance; x++)
        {
            for (int y = 0; y <= distance && x + y <= distance; y++)
            {
                if (x == 0 && y == 0)
                {
                    continue;
                }
                tranforms.ForEach(v =>
                {
                    Vector2 position = cell.Position + (new Vector2(x, y) * v);

                    if (position.x >= 0 && position.y >= 0)
                    {
                        if (tiles.Where(c => c.Position == position).FirstOrDefault() == null)
                        {
                            tiles.Add(CellAt(position));
                        }
                    }
                });
            }
        }


        return(tiles);
    }
Example #2
0
    private void MovePlayer(Player player)
    {
        int cellMoves = Random.Range(1, 7);

        Debug.Log($"Move player {cellMoves} cells forward");

        int           currentCellIndex  = playersOnBoard[player];
        GameBoardCell currentCell       = cells[currentCellIndex];
        Vector2Int    lastMoveDirection = Vector2Int.zero;

        for (int i = 0; i < cellMoves; i++)
        {
            int           nextCellIndex = currentCellIndex == cells.Count - 1 ? 0 : currentCellIndex + 1;
            GameBoardCell nextCell      = cells[nextCellIndex];

            if (currentCell.IsCorner || i == cellMoves - 1)
            {
                Vector3 endPosition = currentCell.IsCorner
                                        ? new Vector3(currentCell.X, player.transform.localPosition.y, currentCell.Z)
                                        : new Vector3(nextCell.X, player.transform.localPosition.y, nextCell.Z);
                playerMovementsQueue.Enqueue(MoveOverSpeed(player, endPosition, 3f));
            }

            currentCell      = nextCell;
            currentCellIndex = nextCellIndex;
        }

        playersOnBoard[player] = currentCellIndex;
        StartCoroutine(PlayerMovementsCoordinator());
    }
Example #3
0
    private void CreateCell(int x, int z, bool isCorner, Color color)
    {
        Debug.Log($"Creating cell ({x},{z})");
        GameBoardCell cell = Instantiate <GameBoardCell>(cellPrefab);

        cell.transform.SetParent(transform, false);
        cell.SetPosition(x, z);
        cell.IsCorner = isCorner;
        cell.Color    = color;
        cells.Add(cell);
    }
    protected override void OnStateStarted()
    {
        base.OnStateStarted();

        // state that we need
        tileToAttack = Manager.Data <GameBoardCell>("tileToAttack");

        GameBoardCell tileToMoveTo = Global.ActiveMap.Board.GetCellsAround(tileToAttack, 1)
                                     .Where(v => Slave.CanMoveTo(v) && v.HasUnit == false)
                                     .First();

        Attack(tileToMoveTo);
    }
Example #5
0
    private void ProcessActiveTile()
    {
        Vector2       mouse  = GetTree().Root.GetMousePosition();
        GameBoardCell active = Board.CellAtWorldPosition(mouse);

        if (active != null && active.TileIndex != -1)
        {
            debugLabel.Text = string.Format(@"
                Mouse at:   {0}
                TileId:     {1}
                Tile:       {2}
            ", active.Position, active.TileIndex, active.TileName);
        }
    }
Example #6
0
    private void Triangulate(GameBoardCell cell)
    {
        Vector3 center = cell.transform.localPosition;

        AddTriangle(
            center + GameBoardMetrics.CELL_CORNERS[2],
            center + GameBoardMetrics.CELL_CORNERS[1],
            center + GameBoardMetrics.CELL_CORNERS[0]);
        AddTriangle(
            center + GameBoardMetrics.CELL_CORNERS[2],
            center + GameBoardMetrics.CELL_CORNERS[0],
            center + GameBoardMetrics.CELL_CORNERS[3]);
        AddTriangleColor(cell.Color);
        AddTriangleColor(cell.Color);
    }
Example #7
0
    public Vector2[] FindPath(Vector2 from, Vector2 to, List <GameBoardCell> board, float limit)
    {
        FStar fstar = new FStar();

        // TODO: this probably isn't very efficent AT ALL
        GameBoardCell[,] map = new GameBoardCell[50, 50];

        // add all the points to fstar and map
        board.ForEach(cell => {
            int index = board.IndexOf(cell);

            // TODO: compute weights
            fstar.AddPoint(index, cell.Position);
            map[(int)cell.Position.x, (int)cell.Position.y] = cell;
        });

        // get from and to
        int fromIndex = board.IndexOf(GetCellFromMap(map, (int)from.x, (int)from.y));
        int toIndex   = board.IndexOf(GetCellFromMap(map, (int)to.x, (int)to.y));

        // make all the point connections
        board.ForEach(cell => {
            GameBoardCell up, down, left, right;
            int index, upIndex, downIndex, leftIndex, rightIndex, x, y;

            index = board.IndexOf(cell);
            x     = (int)cell.Position.x;
            y     = (int)cell.Position.y;

            up    = GetCellFromMap(map, x, y - 1);
            down  = GetCellFromMap(map, x, y + 1);
            left  = GetCellFromMap(map, x - 1, y);
            right = GetCellFromMap(map, x + 1, y);

            upIndex    = up == null ? -1 : board.IndexOf(up);
            downIndex  = up == null ? -1 : board.IndexOf(down);
            leftIndex  = up == null ? -1 : board.IndexOf(left);
            rightIndex = up == null ? -1 : board.IndexOf(right);

            ConnectPoints(index, upIndex, fstar);
            ConnectPoints(index, downIndex, fstar);
            ConnectPoints(index, leftIndex, fstar);
            ConnectPoints(index, rightIndex, fstar);
        });

        Vector2[] results = fstar.GetPointPath(fromIndex, toIndex);
        return(results);
    }
Example #8
0
    public void Search()
    {
        GameBoardCell startCell = board.CellAt(origin);

        frontier.Enqueue(startCell);
        cameFrom.Add(startCell.Position.ToString(), startCell.Position);

        int iters = 0;

        while (frontier.Count() > 0)
        {
            iters++;
            GameBoardCell current = frontier.Dequeue();
            current.Neighbors.ForEach(next => {
                if (!cameFrom.ContainsKey(next.Position.ToString()))
                {
                    frontier.Enqueue(next);
                    cameFrom.Add(next.Position.ToString(), current.Position);
                }
            });
        }
    }
    private async void Attack(GameBoardCell attackPositionTile)
    {
        // first, move into position
        if (tileToAttack.Position.BoardDistance(Slave.Cell.Position) > Slave.AttackRange)
        {
            Slave.MoveToAction(attackPositionTile.WorldPosition);

            // wait for movement to finish
            await ToSignal(Slave, nameof(Unit.FinishedMoving));
        }

        // then, do the combat
        Unit otherUnit = tileToAttack.Unit;

        Slave.FightAction(otherUnit);

        // wait for the combat to finish
        await ToSignal(Slave, nameof(Unit.FinishedFighting));

        Global.Log("Finished fighting");

        // unit is done with turn
        Manager.Change <BasicFinishedTurnState>();
    }
Example #10
0
 public bool IsSameTile(GameBoardCell otherTile)
 {
     return(otherTile.Position == Position);
 }
Example #11
0
 public bool IsWithin(GameBoardCell otherTile, int distance)
 {
     return(Position.DistanceTo(otherTile.Position) <= distance);
 }
 private void OnSelectedAttack(GameBoardCell actionedTile)
 {
     // move and attack
     // UnHighlightCells();
     // Attack(actionedTile);
 }
Example #13
0
 public virtual int ComputeMovementCost(GameBoardCell cell)
 {
     return(Cell.Position.BoardDistance(cell.Position) + cell.MovementCost);
 }
 private void Attack(GameBoardCell actionedTile)
 {
     // Manager.Mutate("tileToAttack", actionedTile);
     // Manager.Change<BasicAttackingState>();
 }
Example #15
0
 public virtual bool CanMoveTo(GameBoardCell cell)
 {
     return(cell.Position.BoardDistance(Cell.Position) <= Speed);
 }
 private void OnSelectedCancel(GameBoardCell actionedTile)
 {
     UnHighlightCells();
     Unit.All.ForEach(unit => unit.State.Revert());
 }
 private void OnSelectedWait(GameBoardCell actionedTile)
 {
     // move here
     UnHighlightCells();
     MoveUnit(actionedTile.WorldPosition);
 }
Example #18
0
 public void OpenAtMouse(Unit unit)
 {
     Position     = GetTree().Root.GetMousePosition();
     actionedTile = Global.ActiveMap.Board.CellAtWorldPosition(Position);
     Open(unit);
 }
Example #19
0
 public GameBoard( int width, int height )
 {
     GameBoardCells = new GameBoardCell[width, height];
 }