Ejemplo n.º 1
0
        public void GraphWithoutTeleportRule()
        {
            var cellGraph    = generateTestGraph();
            var startingCell = cellGraph.LookupCell(0, 0);
            var targetCell   = cellGraph.LookupCell(2, 2);

            var graph = DijkstraWeightGraph.BuildDijkstraWeightGraph(cellGraph, startingCell, maxDistance: 0, allowNeighborTeleportation: false);

            var expectedPath = new Vector3Int[]
            {
                new Vector3Int(0, 0, 0),
                new Vector3Int(0, 1, 0),
                new Vector3Int(0, 2, 0),
                new Vector3Int(0, 3, 0),
                new Vector3Int(1, 3, 0),
                new Vector3Int(2, 3, 0),
                new Vector3Int(2, 2, 0),
            };
            var expectedDistance = expectedPath.Length - 1;

            var(accessible, shortestPath) = graph.LookupShortestPath(targetCell);
            Assert.True(accessible);
            Assert.True(PathsMatch(shortestPath.Path, expectedPath));

            var(accessible2, distance) = graph.LookupDistance(targetCell);
            Assert.True(accessible2);
            Assert.True(distance == expectedDistance);
        }
Ejemplo n.º 2
0
        public void GraphWithTeleportRule()
        {
            var cellGraph    = generateTestGraph();
            var startingCell = cellGraph.LookupCell(0, 0);
            var targetCell   = cellGraph.LookupCell(2, 2);

            var graph = DijkstraWeightGraph.BuildDijkstraWeightGraph(cellGraph, startingCell, maxDistance: 0, allowNeighborTeleportation: true);

            // Either of these is acceptable...
            var expectedPathOne = new Vector3Int[]
            {
                new Vector3Int(0, 0, 0),
                new Vector3Int(0, 1, 0),
                new Vector3Int(0, 2, 0),
                new Vector3Int(2, 2, 0),
            };
            var expectedPathTwo = new Vector3Int[] // Hop the gate allowed, contrary to actual rules in original
            {                                      // I might adjust that later, but for now it's expected
                new Vector3Int(0, 0, 0),
                new Vector3Int(1, 0, 0),
                new Vector3Int(2, 0, 0),
                new Vector3Int(2, 2, 0),
            };
            var expectedDistance = expectedPathOne.Length - 1;

            var(accessible, shortestPath) = graph.LookupShortestPath(targetCell);
            Assert.True(accessible);
            Assert.True(PathsMatch(shortestPath.Path, expectedPathOne) ||
                        PathsMatch(shortestPath.Path, expectedPathTwo));

            var(accessible2, distance) = graph.LookupDistance(targetCell);
            Assert.True(accessible2);
            Assert.True(distance == expectedDistance);
        }
Ejemplo n.º 3
0
    /// <summary>
    /// Call this when something changes in the environment
    /// (Gate unlocked, for example)
    /// </summary>
    /// <param name="cellGraph">The current scene cell graph</param>
    public void RebuildGraph(LogicalCellGraph cellGraph)
    {
        if (playerPawn == null)
        {
            return;
        }

        LogicalCell commandPosition;

        if (hasCommitedCell)
        {
            commandPosition = cellGraph.LookupCell(lastCommittedCell.x, lastCommittedCell.y);
        }
        else
        {
            var playerPos  = playerPawn.LogicalLocation;
            var playerCell = cellGraph.LookupCell(playerPos.x, playerPos.y);
            commandPosition = playerCell;
        }
        playerWeightGraph = DijkstraWeightGraph.BuildDijkstraWeightGraph(
            cellGraph,
            commandPosition,
            maxDistance: 6,
            allowNeighborTeleportation: true);
    }
Ejemplo n.º 4
0
        public void GraphWithLimitedDistance()
        {
            var cellGraph    = generateTestGraph();
            var startingCell = cellGraph.LookupCell(0, 0);
            var targetCell   = cellGraph.LookupCell(2, 2);

            var graph = DijkstraWeightGraph.BuildDijkstraWeightGraph(cellGraph, startingCell, maxDistance: 2, allowNeighborTeleportation: true);

            var(accessible, _) = graph.LookupShortestPath(targetCell);
            Assert.False(accessible);

            var(accessible2, _) = graph.LookupDistance(targetCell);
            Assert.False(accessible2);
        }
Ejemplo n.º 5
0
    /// <summary>
    /// Should be called when the scene's graph changes
    /// (Ie. a locked gate has been lifted)
    /// </summary>
    /// <param name="cellGraph">The scene graph</param>
    public void RebuildGraph(LogicalCellGraph cellGraph)
    {
        if (enemyPawn == null)
        {
            return;
        }

        var enemyPos  = enemyPawn.LogicalLocation;
        var enemyCell = cellGraph.LookupCell(enemyPos.x, enemyPos.y);

        enemyWeightGraph = DijkstraWeightGraph.BuildDijkstraWeightGraph(
            cellGraph,
            enemyCell,
            maxDistance: 0,
            allowNeighborTeleportation: true);
    }