Exemple #1
0
 private void Draw_Grid(SpriteBatch spriteBatch)
 {
     for (int y = (int)grid.Size.Y - 1; y > -1; y--)
     {
         for (int x = (int)grid.Size.X - 1; x > -1; x--)
         {
             //draw a gray tile if the tile in question is in the opponents half
             if (pickedUpTileData.Item2 && (!turnPlayer.IsPointInHalf(grid.Tiles[x, y].OffsetCoordinate, false)))
             {
                 spriteBatch.Draw(isoTile, position: grid.Tiles[x, y].PixelCoordinate, scale: boardScaleVector, color: Color.Gray);
             }
             //draw a red tile if there is a picked up tile from the bench and the
             //  unit cap has been reached, and there is no unit on the hovered tile.
             //  (unit cannot be placed on hovered tile).
             else if (pickedUpTileData.Item2 && pickedUpTileData.Item3 && grid.CountPlayerUnitsOnGrid(turnPlayer.Id) >= turnPlayer.UnitCap &&
                      ((grid.GetUnitFromCoordinate(CoordConverter.OffsetToCube(new Vector2(x, y))) == null) ?
                       true : !(grid.GetUnitFromCoordinate(CoordConverter.OffsetToCube(new Vector2(x, y))).OwnerId == turnPlayer.Id)))
             {
                 spriteBatch.Draw(isoTile, position: grid.Tiles[x, y].PixelCoordinate, scale: boardScaleVector, color: Color.DarkRed);
             }
             //otherwise draw an outline around the tile if its hovered.
             else if (hoveredTileData.Item2 && !hoveredTileData.Item3 && grid.Tiles[x, y].OffsetCoordinate == hoveredTileData.Item1)
             {
                 //note the position must be decremented by (1,1)*boardScale as the sprite is (1,1) larger.
                 spriteBatch.Draw(hoveredIsoTile, position: grid.Tiles[x, y].PixelCoordinate - boardScaleVector, scale: boardScaleVector);
             }
             else
             {
                 spriteBatch.Draw(isoTile, position: grid.Tiles[x, y].PixelCoordinate, scale: boardScaleVector);
             }
         }
     }
 }
Exemple #2
0
        public Vector3 GetRandomPositionInHalf()
        {
            Random  rand = new Random();
            Vector2 pos  = new Vector2(rand.Next((int)gridHalfStart.X, (int)gridHalfEnd.X + 1), rand.Next((int)gridHalfStart.Y, (int)gridHalfEnd.Y + 1));

            return(CoordConverter.OffsetToCube(pos));
        }
Exemple #3
0
        private void GenerateTiles(int offsetSizeX, int offsetSizeY, Vector2 screenBoardOrigin, float boardScale)
        {
            tiles = new HexTile[offsetSizeX, offsetSizeY];
            //generate offset grid and convert to cube coords
            for (int x = 0; x < offsetSizeX; x++)
            {
                for (int y = 0; y < offsetSizeY; y++)
                {
                    Vector2 verticalRowOffset = Vector2.Zero;
                    //calculate the rows offset from the grid draw origin. use this to calculate each tile's pixel position.
                    // (position to draw on screen so they tessalate)
                    for (int i = 0; i < y; i++)
                    {
                        if ((i & 1) == 1)   //if current row is odd
                        {
                            verticalRowOffset += oddToEvenRowOffset;
                        }
                        else
                        {
                            verticalRowOffset += evenToOddRowOffset;
                        }
                    }
                    //pixel position:
                    Vector2 pixelCoordinate = screenBoardOrigin + (((x * horizontalRowOffset) + verticalRowOffset) * boardScale);

                    tiles[x, y] = new HexTile(CoordConverter.OffsetToCube(new Vector2(x, y)), pixelCoordinate);
                }
            }
        }
Exemple #4
0
 private void Draw_Grid(SpriteBatch spriteBatch)
 {
     //strange iteration over the grid is used to layer the textures and create
     // the isometric perspective. (from back right to bottom left)
     for (int y = (int)grid.Size.Y - 1; y > -1; y--)
     {
         for (int x = (int)grid.Size.X - 1; x > -1; x--)
         {
             spriteBatch.Draw(isoTile, position: grid.Tiles[x, y].PixelCoordinate, scale: boardScaleVector);
             //Draw action textures here to prevent having to iterate over the grid again.
             Draw_StepActionMove(spriteBatch, CoordConverter.OffsetToCube(new Vector2(x, y)));
         }
     }
 }
Exemple #5
0
 private Unit GetUnitFromTileData(Tuple <Vector2, bool, bool> data)
 {
     //get unit from bench or grid
     return(data.Item3? turnPlayer.GetBenchUnits()[(int)data.Item1.X] : grid.GetUnitFromCoordinate(CoordConverter.OffsetToCube(data.Item1)));
 }
Exemple #6
0
        private void ProcessTilePicked()
        {
            //if left click && hovering a tile
            if (InputManager.Instance.IsLeftMouseToggled() && hoveredTileData.Item2)
            {
                //if another tile has already been picked
                if (pickedUpTileData.Item2)
                {
                    //if tile is in player's half
                    if (turnPlayer.IsPointInHalf(hoveredTileData.Item1, hoveredTileData.Item3))
                    {
                        //if pending tile has a unit
                        bool hasUnit = DoesUnitExistAtDataPoint(hoveredTileData);

                        if (hasUnit)
                        {
                            Unit unitA = GetUnitFromTileData(hoveredTileData);
                            Unit unitB = GetUnitFromTileData(pickedUpTileData);

                            //if both units are on grid
                            if (!pickedUpTileData.Item3 && !hoveredTileData.Item3)
                            {
                                //swap unit positions
                                var temp = unitA.CubeCoordinate;
                                unitA.CubeCoordinate = unitB.CubeCoordinate;
                                unitB.CubeCoordinate = temp;
                            }
                            //if both units are on the board
                            else if (pickedUpTileData.Item3 && hoveredTileData.Item3)
                            {
                                //swap both units on the bench
                                turnPlayer.SwapTwoBenchUnits(unitA, unitB);
                            }
                            else if (pickedUpTileData.Item3 ^ hoveredTileData.Item3)
                            {
                                //if tiles are bench/grid (xor) swap them.
                                //get which unit is on which grid/bench
                                Unit benchUnit = hoveredTileData.Item3 ? unitA : unitB;
                                Unit gridUnit  = !hoveredTileData.Item3 ? unitA : unitB;
                                //remove units from current position
                                gridUnit.RemoveFromGrid(grid);
                                benchUnit.RemoveFromBench(turnPlayer);
                                //add units back in swapped position
                                benchUnit.AddToGrid(grid, gridUnit.CubeCoordinate);
                                gridUnit.AddToBench(turnPlayer, benchUnit.BenchPosition);
                            }
                        }
                        //if there is no unit on the pending tile
                        else
                        {
                            Unit unit = GetUnitFromTileData(pickedUpTileData);
                            //if selected unit is on the grid
                            if (!pickedUpTileData.Item3)
                            {
                                unit.RemoveFromGrid(grid);

                                //if destination is bench
                                if (hoveredTileData.Item3)
                                {
                                    unit.AddToBench(turnPlayer, (int)hoveredTileData.Item1.X);
                                }

                                //if destination is grid
                                else
                                {
                                    unit.AddToGrid(grid, CoordConverter.OffsetToCube(hoveredTileData.Item1));
                                }
                            }
                            //if selected unit in on the bench
                            else
                            {
                                //if destination is bench
                                if (hoveredTileData.Item3)
                                {
                                    unit.RemoveFromBench(turnPlayer);
                                    unit.AddToBench(turnPlayer, (int)hoveredTileData.Item1.X);
                                }
                                //if destination is grid
                                else
                                {
                                    //only move a unit from the bench to the grid if the player does not have too many units
                                    if (grid.CountPlayerUnitsOnGrid(turnPlayer.Id) < turnPlayer.UnitCap)
                                    {
                                        unit.RemoveFromBench(turnPlayer);
                                        unit.AddToGrid(grid, CoordConverter.OffsetToCube(hoveredTileData.Item1));
                                    }
                                }
                            }
                        }
                        //un-pick tile (set false flag)
                        pickedUpTileData = new Tuple <Vector2, bool, bool>(Vector2.Zero, false, false);
                    }
                }
                else
                {
                    //if the sell hotkey is held and eligible unit to sell is on tile, sell it.
                    // (unit cannot be last unit a player owns)
                    if (InputManager.Instance.IsKeyPressed(Keybinds.Actions.SellUnit) &&
                        grid.CountPlayerUnitsOnGrid(turnPlayer.Id) + turnPlayer.GetBenchCount() > 1 &&
                        DoesUnitExistAtDataPoint(hoveredTileData) && turnPlayer.IsPointInHalf(hoveredTileData.Item1, hoveredTileData.Item3))
                    {
                        Unit unit = GetUnitFromTileData(hoveredTileData);
                        turnPlayer.Money += (int)unit.GetType().GetField("Cost").GetRawConstantValue();
                        unit.Delete(grid, turnPlayer);
                    }

                    //pick up tile clicked if there is a unit
                    pickedUpTileData = new Tuple <Vector2, bool, bool>(hoveredTileData.Item1, DoesUnitExistAtDataPoint(hoveredTileData) && turnPlayer.IsPointInHalf(hoveredTileData.Item1, hoveredTileData.Item3), hoveredTileData.Item3);
                }
            }

            //if right mouse clicked un pick the tile.
            if (InputManager.Instance.IsRightMouseToggled())
            {
                //un-pick tile (set false flag)
                pickedUpTileData = new Tuple <Vector2, bool, bool>(Vector2.Zero, false, false);
            }
        }