private void CheckDiagonalPositions(List <Edge> directNeighbourhood, Edge edge)
        {
            if (edge == Edge.Right || edge == Edge.Left)
            {
                throw new ArgumentException();
            }

            var pos          = _tile.mapPosition;
            var edgePos      = MapUtils.FromEdgeToVec2(edge);
            var edgeLeftPos  = MapUtils.FromEdgeToVec2(Edge.Left);
            var edgeRightPos = MapUtils.FromEdgeToVec2(Edge.Right);

            if (directNeighbourhood.Contains(edge))
            {
                if (edge == Edge.Down)
                {
                    _obstacles.Add(ObstacleType.down);
                }
                else
                {
                    _obstacles.Add(ObstacleType.up);
                }

                if (directNeighbourhood.Contains(Edge.Left))
                {
                    if (_map[pos + edgePos + edgeLeftPos].state == TileState.Free)
                    {
                        if (edge == Edge.Down)
                        {
                            _obstacles.Add(ObstacleType.left_down);
                        }
                        else
                        {
                            _obstacles.Add(ObstacleType.left_up);
                        }
                    }
                }

                if (directNeighbourhood.Contains(Edge.Right))
                {
                    if (_map[pos + edgePos + edgeRightPos].state == TileState.Free)
                    {
                        if (edge == Edge.Down)
                        {
                            _obstacles.Add(ObstacleType.right_down);
                        }
                        else
                        {
                            _obstacles.Add(ObstacleType.right_up);
                        }
                    }
                }
            }
        }
Пример #2
0
        public void GeneratePathway()
        {
            var currentTile = endPosition;

            while (currentTile != Vector2.zero)
            {
                map[currentTile].state = TileState.Path;
                var nextTile = MapUtils.GetPreviusTile(currentTile, map);
                currentTile = nextTile;
            }
            InitializeMap();
        }
Пример #3
0
        public override void CalculatePath()
        {
            cameFrom  = new Dictionary <Vector2, Vector2>();
            costSoFar = new Dictionary <Vector2, double>();

            var startPos = mapManager.startPosition;
            var endPos   = mapManager.endPosition;

            var first = new SimplePriorityQueue <Vector2, double>();

            first.Enqueue(startPos, 0);


            cameFrom[startPos]  = startPos;
            costSoFar[startPos] = 0;

            while (first.Count > 0)
            {
                var current = first.Dequeue();

                if (current.Equals(endPos))
                {
                    break;
                }

                foreach (var next in MapUtils.GetNeighbourTiles(current, mapManager.map))
                {
                    double newCost = costSoFar[current] + 1;                     //changeable later - currently costs always 1

                    if (mapManager.map[next].state == TileState.Occupied)        //if occupied by obstacle
                    {
                        continue;
                    }

                    if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next])
                    {
                        var edge = MapUtils.FromVec2ToEdge(current - next);
                        mapManager.map[next].edge = MapUtils.GetOppositeEdge(edge);

                        costSoFar[next] = newCost;
                        double priority = newCost + Heuristic(next, endPos);
                        first.Enqueue(next, priority);
                        cameFrom[next] = current;
                    }
                }
            }
            mapManager.GeneratePathway();
        }
Пример #4
0
        public static List <Vector2> GetNeighbourTiles(Vector2 position, Dictionary <Vector2, TileData> map)
        {
            var neighbours = new List <Vector2>();

            foreach (Edge edge in Edge.GetValues(typeof(Edge)))
            {
                var pos = MapUtils.FromEdgeToVec2(edge) + position;

                if (!map.ContainsKey(pos))
                {
                    continue;
                }

                neighbours.Add(pos);
            }

            return(neighbours);
        }
Пример #5
0
        public override void CalculatePath()
        {
            var map = mapManager.map;

            _first = new Queue <Vector2>();

            _first.Enqueue(mapManager.startPosition);

            _visited = new HashSet <Vector2>();

            _visited.Add(mapManager.startPosition);

            while (_first.Count > 0)
            {
                var current = _first.Dequeue();

                if (current == mapManager.endPosition)                 //end when reaching end position
                {
                    break;
                }

                foreach (var next in MapUtils.GetNeighbourTiles(current, mapManager.map))
                {
                    if (map[next].state == TileState.Occupied)                     //if occupied by obstacle
                    {
                        continue;
                    }

                    if (_visited.Contains(next))                     //if already checked
                    {
                        continue;
                    }

                    var edge = MapUtils.FromVec2ToEdge(current - next);
                    map[next].edge = MapUtils.GetOppositeEdge(edge);

                    _first.Enqueue(next);
                    _visited.Add(next);
                }
            }

            mapManager.GeneratePathway();
        }
        private void SetObstacle(ObstacleType type)
        {
            var tiles = new List <Vector2>();

            var mainTilePos = _tile.mapPosition;

            tiles.Add(_tile.mapPosition);

            var pos          = _tile.mapPosition;
            var edgeUpPos    = MapUtils.FromEdgeToVec2(Edge.Up);
            var edgeDownPos  = MapUtils.FromEdgeToVec2(Edge.Down);
            var edgeLeftPos  = MapUtils.FromEdgeToVec2(Edge.Left);
            var edgeRightPos = MapUtils.FromEdgeToVec2(Edge.Right);

            switch (type)
            {
            case ObstacleType.down:
                tiles.Add(mainTilePos + edgeDownPos);
                break;

            case ObstacleType.up:
                tiles.Add(mainTilePos + edgeUpPos);
                break;

            case ObstacleType.left:
                tiles.Add(mainTilePos + edgeLeftPos);
                break;

            case ObstacleType.right:
                tiles.Add(mainTilePos + edgeRightPos);
                break;

            case ObstacleType.left_down:
                tiles.Add(mainTilePos + edgeDownPos);
                tiles.Add(mainTilePos + edgeLeftPos);
                tiles.Add(mainTilePos + edgeDownPos + edgeLeftPos);
                break;

            case ObstacleType.left_up:
                tiles.Add(mainTilePos + edgeUpPos);
                tiles.Add(mainTilePos + edgeLeftPos);
                tiles.Add(mainTilePos + edgeUpPos + edgeLeftPos);
                break;

            case ObstacleType.right_down:
                tiles.Add(mainTilePos + edgeDownPos);
                tiles.Add(mainTilePos + edgeRightPos);
                tiles.Add(mainTilePos + edgeDownPos + edgeRightPos);
                break;

            case ObstacleType.right_up:
                tiles.Add(mainTilePos + edgeUpPos);
                tiles.Add(mainTilePos + edgeRightPos);
                tiles.Add(mainTilePos + edgeUpPos + edgeRightPos);
                break;
            }

            foreach (Vector2 tile in tiles)
            {
                _map[tile].state = TileState.Occupied;
                _map[tile].edge  = Edge.Zero;
            }
        }