コード例 #1
0
        public void PlayerDrawRoute(int pixelX, int pixelY)
        {
            var startUnit = _units.FindSelectedUnit();

            if (startUnit != null)
            {
                var cube        = HexGridMath.HexToCube(HexGridMath.PixelToHex(pixelX, pixelY));
                var destination = HexGridMath.CubeToOffset(cube.X, cube.Y, cube.Z);

                if (startUnit.Col != destination.Col || startUnit.Row != destination.Row)
                {
                    _shortestPath.ComputeShortestPath((TerrainMap)_terrainMap, startUnit.Col, startUnit.Row, destination.Col, destination.Row, startUnit.UnitType);

                    if (_shortestPath.WayPoint.Count > 0)
                    {
                        _shortestPath.WayPoint.Insert(0, new Offset(startUnit.Col, startUnit.Row));
                    }

                    for (int i = 1; i < _shortestPath.WayPoint.Count; i++)
                    {
                        GameContent.Sb.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
                        GraphicHelpers.DrawLine(_shortestPath.WayPoint[i - 1].ToPixel, _shortestPath.WayPoint[i].ToPixel, Color.Red, 6);
                        GameContent.Sb.End();
                    }
                }
            }
        }
コード例 #2
0
        public void PlotRoad(int startCol, int startRow, int destCol, int destRow)
        {
            // find shortest path
            _shortestPath.ComputeShortestPath(this, startCol, startRow, destCol, destRow, -1);

            // add the starting cell to the path
            _shortestPath.WayPoint.Insert(0, new Offset(startCol, startRow));

            TerrainCell prevTerrain     = null;
            Cube        prevTerrainCube = null;

            foreach (var wayPointOffset in _shortestPath.WayPoint)
            {
                var terrainCube = HexGridMath.OffsetToCube(wayPointOffset.Col, wayPointOffset.Row);
                var terrain     = (TerrainCell)Map[FindMapHash(terrainCube.X, terrainCube.Y, terrainCube.Z)];

                // figure out which sides of the cell are touching
                if (prevTerrain != null)
                {
                    if (prevTerrainCube.Y == terrainCube.Y)                     // lower left to upper right
                    {
                        if (terrainCube.X > prevTerrainCube.X)
                        {
                            if (prevTerrain.BackgroundType != 40)
                            {
                                prevTerrain.Roads |= RoadType.Road3;
                            }
                            if (terrain.BackgroundType != 40)
                            {
                                terrain.Roads |= RoadType.Road6;
                            }
                        }
                        else
                        {
                            if (prevTerrain.BackgroundType != 40)
                            {
                                prevTerrain.Roads |= RoadType.Road6;
                            }
                            if (terrain.BackgroundType != 40)
                            {
                                terrain.Roads |= RoadType.Road3;
                            }
                        }
                    }
                    else if (prevTerrainCube.Z == terrainCube.Z)                     // upper left to lower right
                    {
                        if (terrainCube.Y > prevTerrainCube.Y)
                        {
                            if (prevTerrain.BackgroundType != 40)
                            {
                                prevTerrain.Roads |= RoadType.Road5;
                            }
                            if (terrain.BackgroundType != 40)
                            {
                                terrain.Roads |= RoadType.Road2;
                            }
                        }
                        else
                        {
                            if (prevTerrain.BackgroundType != 40)
                            {
                                prevTerrain.Roads |= RoadType.Road2;
                            }
                            if (terrain.BackgroundType != 40)
                            {
                                terrain.Roads |= RoadType.Road5;
                            }
                        }
                    }
                    else if (prevTerrainCube.X == terrainCube.X)                     // vertical line
                    {
                        if (terrainCube.Y > prevTerrainCube.Y)
                        {
                            if (prevTerrain.BackgroundType != 40)
                            {
                                prevTerrain.Roads |= RoadType.Road4;
                            }
                            if (terrain.BackgroundType != 40)
                            {
                                terrain.Roads |= RoadType.Road1;
                            }
                        }
                        else
                        {
                            if (prevTerrain.BackgroundType != 40)
                            {
                                prevTerrain.Roads |= RoadType.Road1;
                            }
                            if (terrain.BackgroundType != 40)
                            {
                                terrain.Roads |= RoadType.Road4;
                            }
                        }
                    }
                    else
                    {
                        // should never get here
                        System.Diagnostics.Debug.Assert(false);
                    }
                }

                prevTerrain     = terrain;
                prevTerrainCube = terrainCube;
            }
        }
コード例 #3
0
 public void FindShortestPath(ITerrainMap terrainMap, int unitCol, int unitRow, int endCol, int endRow, int unitUnitType)
 {
     _shortestPath.ComputeShortestPath(terrainMap, unitCol, unitRow, endCol, endRow, unitUnitType);
 }