예제 #1
0
    /// <summary>
    /// Called when the mouse moves (in logical space)
    /// </summary>
    /// <param name="mouseLogicalSpace">The mouse coordinates in logical space</param>
    /// <param name="offGrid">True if the coordinates are valid</param>
    /// <param name="cellGraph">The current scene cell graph</param>
    /// <param name="tilemap">The map</param>
    public void OnMouseMove(Vector3Int mouseLogicalSpace, bool offGrid, LogicalCellGraph cellGraph, IMap tilemap)
    {
        pendingTargetActive = false;
        pendingPath         = null;

        lastMouseLocation        = mouseLogicalSpace;
        lastMouseLocationOffGrid = offGrid;

        if (playerWeightGraph == null)
        {
            RebuildGraph(cellGraph);
        }

        if (playerWeightGraph == null || offGrid)
        {
            return;
        }

        var targetCell = cellGraph.LookupCell(mouseLogicalSpace.x, mouseLogicalSpace.y);

        var(accessible, path) = playerWeightGraph.LookupShortestPath(targetCell);

        if (accessible)
        {
            pendingTarget       = targetCell.Loc;
            pendingTargetActive = true;
            pendingPath         = path;
        }
    }
예제 #2
0
    /// <summary>
    /// Ticks the AI's brain. It will move one square each tick currently.
    /// </summary>
    /// <param name="graph">The scene graph</param>
    /// <param name="state">The state of the level</param>
    public void Tick(LogicalCellGraph graph, ILevelState state)
    {
        // So on a bigger game I probably wouldn't do this,
        // But it seems Djikstra's is performant enough to
        // Recompute each tick!! (keep in mind, the AI's tick
        // is only approximately once every second). If this
        // weren't the case, I would probably go with A*.
        RebuildGraph(graph);

        if (enemyPawn == null || enemyWeightGraph == null)
        {
            return;
        }
        var goal = findAIGoal(graph, state);

        if (goal == null)
        {
            return;
        }

        var(accessible, path) = enemyWeightGraph.LookupShortestPath(goal);
        if (!accessible)
        {
            return;
        }

        if (!path.Path.Any())
        {
            return;
        }

        var nextCell = path.Path.Skip(1).Take(1).FirstOrDefault();

        if (nextCell != null)
        {
            var truncatedPath = new LogicalPath();
            truncatedPath.PrependPath(nextCell);
            enemyPawn.PushMotionPath(truncatedPath);
        }
    }