Example #1
0
    /// <summary>
    /// Sets up the ghost at the node specified
    /// </summary>
    /// <param name="ghostHouse">The ghost house for this ghost</param>
    /// <param name="homeNode">The home node for this ghost</param>
    /// <param name="timings">The state timings</param>
    /// <param name="aStarPathing">If this ghost is using astar or not for pathing</param>
    public void Init(Node ghostHouse, Node homeNode, GhostStateTiming[] timings, bool aStarPathing = false)
    {
        trans          = transform;
        rb             = GetComponent <Rigidbody2D>();
        anim           = GetComponent <Animator>();
        spriteRenderer = GetComponent <SpriteRenderer>();

        this.homeNode     = homeNode;
        this.ghostHouse   = ghostHouse;
        this.aStarPathing = aStarPathing;
        currentNode       = ghostHouse;
        targetNode        = ghostHouse;

        currentPos       = currentNode.pos;
        currentDirection = Vector2.zero;

        frightenedModeDuration = GameSettings.instance.GhostFrightenedDuration;

        rb.MovePosition(currentPos);

        if (aStarPathing)
        {
            aStarGraph = new AStarGraph(GameLogic.instance.CurrentLevel);
        }
        StateInit(timings);
    }
        private static Point GetTileNextToBuilding(int tileX, int tileY, AStarGraph graph)
        {
            AStarNode tileNode = graph.GetNode(tileX, tileY);

            if (tileNode is not null)
            {
                tileNode.FakeTileClear = true;

                AStarPath path = graph.FindPathWithBubbleCheck(graph.FarmerNodeOffset, tileNode);

                if (path is not null && path.Count > 0)
                {
                    return(new Point(tileX, tileY));
                }

                tileNode.FakeTileClear = false;
            }

            return(Point.Zero);
        }
        public static Point GetNearestTileNextToBuilding(AStarGraph graph, Building building)
        {
            int buildingTileX       = building.tileX.Value;
            int buildingTileY       = building.tileY.Value;
            int buildingTilesWidth  = building.tilesWide.Value;
            int buildingTilesHeight = building.tilesHigh.Value;

            int tileX;
            int tileY;

            if (Game1.player.getTileX() < buildingTileX)
            {
                tileX = buildingTileX;
            }
            else if (Game1.player.getTileX() > buildingTileX + buildingTilesWidth - 1)
            {
                tileX = buildingTileX + buildingTilesWidth - 1;
            }
            else
            {
                tileX = Game1.player.getTileX();
            }

            if (Game1.player.getTileY() < buildingTileY)
            {
                tileY = buildingTileY;
            }
            else if (Game1.player.getTileY() > buildingTileY + buildingTilesHeight)
            {
                tileY = buildingTileY + buildingTilesHeight - 1;
            }
            else
            {
                tileY = Game1.player.getTileY();
            }

            Point tile = ClickToMoveHelper.GetTileNextToBuilding(tileX, tileY, graph);

            if (tile != Point.Zero)
            {
                return(tile);
            }

            // There is no direct path to the nearest tile, let's search for an alternative around it.

            List <Point> tilesAroundBuilding = ClickToMoveHelper.ListOfTilesSurroundingBuilding(building);

            int tileIndex = 0;

            for (int i = 0; i < tilesAroundBuilding.Count; i++)
            {
                if (tilesAroundBuilding[i].X == tileX && tilesAroundBuilding[i].Y == tileY)
                {
                    tileIndex = i;
                    break;
                }
            }

            for (int i = 1, previousIndex = tileIndex - 1, nextIndex = tileIndex + 1;
                 i < tilesAroundBuilding.Count / 2;
                 i++, previousIndex--, nextIndex++)
            {
                if (previousIndex < 0)
                {
                    previousIndex += tilesAroundBuilding.Count;
                }

                tile = ClickToMoveHelper.GetTileNextToBuilding(
                    tilesAroundBuilding[previousIndex].X,
                    tilesAroundBuilding[previousIndex].Y,
                    graph);

                if (tile != Point.Zero)
                {
                    return(tile);
                }

                if (nextIndex > tilesAroundBuilding.Count - 1)
                {
                    nextIndex -= tilesAroundBuilding.Count;
                }

                tile = ClickToMoveHelper.GetTileNextToBuilding(
                    tilesAroundBuilding[nextIndex].X,
                    tilesAroundBuilding[nextIndex].Y,
                    graph);

                if (tile != Point.Zero)
                {
                    return(tile);
                }
            }

            return(new Point(Game1.player.getTileX(), Game1.player.getTileY()));
        }
Example #4
0
 public void WalkTo(Vector3 vPosition)
 {
     mPath            = AStarGraph.GetPath(transform.position, vPosition);
     mCurrentWaypoint = 0;
 }
Example #5
0
    protected AStarGraph _graph; // Estructura con los nodos del grafo que recorreremos

    // Use this for initialization
    void Start()
    {
        _graph = FindObjectOfType <AStarGraph>();
    }
Example #6
0
    public List <AStarConnection> AStarPathFind(AStarGraph graph, AStarNode start, AStarNode end)
    {
        List <AStarConnection> path = new List <AStarConnection>();

        AStarNodeRecord current = new AStarNodeRecord();

        List <AStarConnection> currentConnections = new List <AStarConnection>();

        AStarNodeRecord endNode       = new AStarNodeRecord();
        AStarNodeRecord endNodeRecord = new AStarNodeRecord();

        float endNodeHeuristic = 0;



        AStarNodeRecord startRecord = new AStarNodeRecord();

        startRecord.node               = start;
        startRecord.connection         = null;
        startRecord.costSoFar          = 0;
        startRecord.estimatedTotalCost = Heuristic(start, end);

        openList = new List <AStarNodeRecord>();
        openList.Add(startRecord);

        closedList = new List <AStarNodeRecord>();

        while (openList.Count > 0)
        {
            current = SmallestElement(openList);

            if (current.node == end)
            {
                break;
            }

            currentConnections = current.node.GetConnections();

            foreach (AStarConnection conect in currentConnections)
            {
                endNode.node       = conect.ToNode;
                endNode.connection = conect;
                endNode.costSoFar  = current.costSoFar + conect.Cost;

                if (closedList.Contains(endNode))
                {
                    endNodeRecord = closedList.Find(x => closedList.Contains(endNode));

                    if (endNodeRecord.costSoFar <= endNode.costSoFar)
                    {
                        //continue
                    }
                    else
                    {
                        closedList.Remove(endNodeRecord);
                        endNodeHeuristic = endNodeRecord.estimatedTotalCost - endNodeRecord.costSoFar;
                    }
                }
                else if (openList.Contains(endNode))
                {
                    endNodeRecord = openList.Find(x => openList.Contains(endNode));

                    if (endNodeRecord.costSoFar <= endNode.costSoFar)
                    {
                        //continue
                    }
                    else
                    {
                        endNodeHeuristic = endNodeRecord.estimatedTotalCost - endNodeRecord.costSoFar;
                    }
                }
                else
                {
                    endNodeRecord    = new AStarNodeRecord(endNode);
                    endNodeHeuristic = Heuristic(endNode.node, end);
                }

                endNodeRecord.costSoFar          = endNode.costSoFar;
                endNodeRecord.connection         = endNode.connection;
                endNodeRecord.estimatedTotalCost = endNodeRecord.costSoFar + endNodeHeuristic;

                if (!openList.Contains(endNode))
                {
                    openList.Add(endNodeRecord);
                }
            }

            openList.Remove(current);
            closedList.Add(current);
        }

        if (current.node != end)
        {
            //return none
        }
        else
        {
            AStarNode temp = new AStarNode();


            while (current.node != start)
            {
                path.Add(current.connection);
                temp = current.connection.FromNode;

                for (int i = closedList.Count - 1; i >= 0; i--)
                {
                    if (closedList[i].node == temp)
                    {
                        current = closedList[i];
                    }
                }
            }
        }
        path.Reverse();

        return(path);
    }
Example #7
0
 // Use this for initialization
 void Start()
 {
     AStarGraph.AddNode(this);
 }
Example #8
0
    public bool SetDestination(Vector3 pos, float speedFraction = 1f, float updateInterval = 0f, float navmeshSampleDistance = 0f)
    {
        if (!AI.move)
        {
            return(false);
        }
        if (!AI.navthink)
        {
            return(false);
        }
        paused = false;
        if (!CanUseNavMesh)
        {
            if (CanUseAStar && AStarGraph != null)
            {
                return(SetDestination(AStarGraph, AStarGraph.GetClosestToPoint(pos), speedFraction));
            }
            return(false);
        }
        if (AiManager.nav_disable)
        {
            return(false);
        }
        if (updateInterval > 0f && !UpdateIntervalElapsed(updateInterval))
        {
            return(true);
        }
        lastSetDestinationTime = UnityEngine.Time.time;
        currentSpeedFraction   = speedFraction;
        if (ReachedPosition(pos))
        {
            return(true);
        }
        if (navmeshSampleDistance > 0f && AI.setdestinationsamplenavmesh)
        {
            NavMeshHit hit;
            if (!NavMesh.SamplePosition(pos, out hit, navmeshSampleDistance, defaultAreaMask))
            {
                return(false);
            }
            pos = hit.position;
        }
        SetCurrentNavigationType(NavigationType.NavMesh);
        Destination = pos;
        bool flag;

        if (AI.usecalculatepath)
        {
            flag = NavMesh.CalculatePath(base.transform.position, Destination, navMeshQueryFilter, path);
            if (flag)
            {
                Agent.SetPath(path);
            }
            else if (AI.usesetdestinationfallback)
            {
                flag = Agent.SetDestination(Destination);
            }
        }
        else
        {
            flag = Agent.SetDestination(Destination);
        }
        if (flag && SpeedBasedAvoidancePriority)
        {
            Agent.avoidancePriority = Random.Range(0, 21) + Mathf.FloorToInt(speedFraction * 80f);
        }
        return(flag);
    }