Exemplo n.º 1
0
 private void AStarFindPathTo(Tile goal)
 {
     if (WorldController.Instance.IsWalkable(goal.x, goal.y) != 0)
     {
         path = ASPath.FindPath(CurrentTile(), goal);
     }
 }
Exemplo n.º 2
0
    protected void Move(Vector2 position)
    {
        state = EnemyState.MOVE;
        // transform.position = Vector2.MoveTowards(transform.position, position, moveSpeed * Time.deltaTime);

        Vector2 tilePos       = AStarPath.GetTilePosition(transform.position);
        Vector2 playerTilePos = AStarPath.GetTilePosition(player.transform.position);

        path = AStarPath.FindPath(tilePos, playerTilePos);

        if (path != null)
        {
            path.Reverse();

            List <Vector2> pathWorldPosition = new List <Vector2>();
            for (int i = 0; i < path.Count; i++)
            {
                pathWorldPosition.Add(AStarPath.GetTileWorldPosition(path[i].position));
            }

            if (followEnumerator != null)
            {
                StopCoroutine(followEnumerator);
            }

            followEnumerator = FollowPath(pathWorldPosition, player.transform.position);

            StartCoroutine(followEnumerator);
        }
    }
Exemplo n.º 3
0
    public void GenerateGrid()
    {
        map = CreateNodesFromTilemaps.CreateNodes(grid, floor, obstacleLayers, transform);

        SpawnNodes(map, false);
        AStarPath.SaveMap(map);

        path = AStarPath.FindPath(new Vector2(-7.9f, -.2f), new Vector2(-.1f, -.1f));
        StartCoroutine(DelayedPath());
    }
    public Stack <Coordinate> FindRandomPath(Vector3 objectPosition)
    {
        var start = Maze[(int)Math.Round(objectPosition.x) / mazeScale, (int)Math.Round(objectPosition.z) / mazeScale];

        var end = Maze[UnityEngine.Random.Range(1, MazeWidth - 2), UnityEngine.Random.Range(1, MazeWidth - 2)];

        while (end.type != MazeID.SPACE)
        {
            end = Maze[UnityEngine.Random.Range(1, MazeWidth - 2), UnityEngine.Random.Range(1, MazeWidth - 2)];
        }

        AStarPath pathfinder = new AStarPath(Maze, start, end, MazeWidth, MazeHeight);

        return(pathfinder.FindPath());
    }
Exemplo n.º 5
0
    public bool SetDestination(BasePath path, BasePathNode newTargetNode, float speedFraction)
    {
        if (!AI.move)
        {
            return(false);
        }
        if (!AI.navthink)
        {
            return(false);
        }
        paused = false;
        if (!CanUseAStar)
        {
            return(false);
        }
        if (newTargetNode == targetNode && HasPath)
        {
            return(true);
        }
        if (ReachedPosition(newTargetNode.transform.position))
        {
            return(true);
        }
        BasePathNode closestToPoint = path.GetClosestToPoint(base.transform.position);

        if (closestToPoint == null || closestToPoint.transform == null)
        {
            return(false);
        }
        float pathCost;

        if (AStarPath.FindPath(closestToPoint, newTargetNode, out currentAStarPath, out pathCost))
        {
            currentSpeedFraction = speedFraction;
            targetNode           = newTargetNode;
            SetCurrentNavigationType(NavigationType.AStar);
            Destination = newTargetNode.transform.position;
            return(true);
        }
        return(false);
    }
Exemplo n.º 6
0
        void GenerateRandomGrid()
        {
            var gridArea   = TestGrid.GetGridArea();
            var width      = gridArea.Width;
            var height     = gridArea.Height;
            var grid       = new List <GridTile> ();
            var gridModels = new List <GridModel> ();


            ClearGridParent();


            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    var gt = Instantiate(gridPrefab, GridParent);
                    gt.name = (x + "," + y);
                    gt.transform.localPosition = new Vector3(x, y, GridParent.transform.position.z);
                    grid.Add(gt);
                    gridModels.Add(new GridModel(gt));
                }
            }
            gridArea.SetGrids(gridModels);

            var cpu         = TestGrid.GetCpu();
            var cpuModel    = GridUtility.GetEmptyGrid(gridArea);
            var cpuLocation = gridArea.GetCellLocation(cpuModel.Cells.First()); //need PORTS and ENTRANCE

            cpuModel.GridComponents.Add(cpu);
            var componentScript = cpuModel.GridTile.gameObject.GetComponent <ComponentScript>();

            componentScript.CreateComponent(cpu);

            //empty.GridTile.SetState(empty);

            var boostChip         = TestGrid.GetBoostChip();
            var boostModel        = GridUtility.GetEmptyGrid(gridArea);
            var boostChipLocation = gridArea.GetCellLocation(boostModel.Cells.First());//need PORTS and ENTRANCE

            boostModel.AddComponent(boostChip);
            componentScript = boostModel.GridTile.gameObject.GetComponent <ComponentScript>();
            componentScript.CreateComponent(boostChip);
            boostModel.GridTile.SetState(boostModel);

            var componentCount = 4;
            var usedTiles      = new List <GridTile> ();
            var max            = grid.Count;
            int i = 0;

            List <GridAreaLocation> recvrLocations = new List <GridAreaLocation>();

            while (i < componentCount)
            {
                var receiverModel = GridUtility.GetEmptyGrid(gridArea);


                var gridComponent = new GridComponent();
                gridComponent.Color = GameUtility.GetRandomColor();
                var c     = new CellLocation(Random.Range(0, 3), Random.Range(0, 3));
                var rCell = receiverModel.GetCell(c);// testing, picking first cell of the grid
                recvrLocations.Add(gridArea.GetCellLocation(rCell));

                gridComponent.CellLocations.Add(c);
                receiverModel.AddComponent(gridComponent);

                componentScript = receiverModel.GridTile.gameObject.GetComponent <ComponentScript>();
                componentScript.CreateComponent(gridComponent);
                ++i;
                receiverModel.GridTile.SetState(receiverModel);
            }

            List <GridPath> paths = new List <GridPath>();

            foreach (var r in recvrLocations)
            {
                var gp = new GridPath();
                gp.StartEntrance  = r;
                gp.FinishEntrance = cpuLocation;

                AStarPath aStar = new AStarPath();
                var       path  = aStar.FindPath(r, cpuLocation, gridArea);
                gp.Path = path;
                //GridUtility.GeneratePath(gp, gridArea);

                paths.Add(gp);
            }


            GridUtility.SetCellStates(paths, gridArea);
            gridModels.ForEach(x => x.GridTile.SetState(x));
            //gridComponent.Cell.Location = new CellLocation { X = 0, Y = 1 };
            //componentScript.CreateComponent (gridComponent);


            //g.gameObject.AddComponent<ComponentScript> ();
        }
Exemplo n.º 7
0
    public void UpdateMovement_Patrol()
    {
        float single;
        Stack <BasePathNode> basePathNodes;

        if (this.patrolPath == null)
        {
            return;
        }
        if (UnityEngine.Time.time < this.nextPatrolTime)
        {
            return;
        }
        this.nextPatrolTime = UnityEngine.Time.time + 20f;
        if (this.HasPath() && !this.IsAtFinalDestination())
        {
            return;
        }
        if (Interface.CallHook("OnBradleyApcPatrol", this) != null)
        {
            return;
        }
        PathInterestNode    randomInterestNodeAwayFrom = this.patrolPath.GetRandomInterestNodeAwayFrom(base.transform.position, 10f);
        BasePathNode        closestToPoint             = this.patrolPath.GetClosestToPoint(randomInterestNodeAwayFrom.transform.position);
        BasePathNode        item = null;
        bool                flag = false;
        List <BasePathNode> list = Facepunch.Pool.GetList <BasePathNode>();

        if (!this.GetEngagementPath(ref list))
        {
            item = this.patrolPath.GetClosestToPoint(base.transform.position);
        }
        else
        {
            flag = true;
            item = list[list.Count - 1];
        }
        if (Vector3.Distance(this.finalDestination, closestToPoint.transform.position) > 2f)
        {
            if (closestToPoint == item)
            {
                this.currentPath.Clear();
                this.currentPath.Add(closestToPoint.transform.position);
                this.currentPathIndex = -1;
                this.pathLooping      = false;
                this.finalDestination = closestToPoint.transform.position;
                return;
            }
            if (AStarPath.FindPath(item, closestToPoint, out basePathNodes, out single))
            {
                this.currentPath.Clear();
                if (flag)
                {
                    for (int i = 0; i < list.Count - 1; i++)
                    {
                        this.currentPath.Add(list[i].transform.position);
                    }
                }
                foreach (BasePathNode basePathNode in basePathNodes)
                {
                    this.currentPath.Add(basePathNode.transform.position);
                }
                this.currentPathIndex = -1;
                this.pathLooping      = false;
                this.finalDestination = closestToPoint.transform.position;
            }
        }
    }
Exemplo n.º 8
0
    public void UpdateMovement_Hunt()
    {
        float single;

        if (Interface.CallHook("OnBradleyApcHunt", this) != null)
        {
            return;
        }
        if (this.patrolPath == null)
        {
            return;
        }
        BradleyAPC.TargetInfo item = this.targetList[0];
        if (item.IsValid())
        {
            if (this.HasPath() && item.IsVisible())
            {
                if (this.currentPath.Count > 1)
                {
                    Vector3 vector3 = this.currentPath[this.currentPathIndex];
                    this.ClearPath();
                    this.currentPath.Add(vector3);
                    this.finalDestination = vector3;
                    this.currentPathIndex = 0;
                    return;
                }
            }
            else if (UnityEngine.Time.time > this.nextEngagementPathTime && !this.HasPath() && !item.IsVisible())
            {
                bool                flag           = false;
                BasePathNode        closestToPoint = this.patrolPath.GetClosestToPoint(base.transform.position);
                List <BasePathNode> list           = Facepunch.Pool.GetList <BasePathNode>();
                if (this.GetEngagementPath(ref list))
                {
                    flag           = true;
                    closestToPoint = list[list.Count - 1];
                }
                BasePathNode        basePathNode  = null;
                List <BasePathNode> basePathNodes = Facepunch.Pool.GetList <BasePathNode>();
                this.patrolPath.GetNodesNear(item.lastSeenPosition, ref basePathNodes, 30f);
                Stack <BasePathNode> basePathNodes1 = null;
                float single1 = Single.PositiveInfinity;
                float single2 = this.mainTurretEyePos.localPosition.y;
                foreach (BasePathNode basePathNode1 in basePathNodes)
                {
                    Stack <BasePathNode> basePathNodes2 = new Stack <BasePathNode>();
                    if (!item.entity.IsVisible(basePathNode1.transform.position + new Vector3(0f, single2, 0f), Single.PositiveInfinity) || !AStarPath.FindPath(closestToPoint, basePathNode1, out basePathNodes2, out single) || single >= single1)
                    {
                        continue;
                    }
                    basePathNodes1 = basePathNodes2;
                    single1        = single;
                    basePathNode   = basePathNode1;
                }
                if (basePathNodes1 != null)
                {
                    this.currentPath.Clear();
                    if (flag)
                    {
                        for (int i = 0; i < list.Count - 1; i++)
                        {
                            this.currentPath.Add(list[i].transform.position);
                        }
                    }
                    foreach (BasePathNode basePathNode2 in basePathNodes1)
                    {
                        this.currentPath.Add(basePathNode2.transform.position);
                    }
                    this.currentPathIndex = -1;
                    this.pathLooping      = false;
                    this.finalDestination = basePathNode.transform.position;
                }
                Facepunch.Pool.FreeList <BasePathNode>(ref basePathNodes);
                Facepunch.Pool.FreeList <BasePathNode>(ref list);
                this.nextEngagementPathTime = UnityEngine.Time.time + 5f;
            }
        }
    }
Exemplo n.º 9
0
    public void UpdateMovement_Hunt()
    {
        if (Interface.CallHook("OnBradleyApcHunt", this) != null || patrolPath == null)
        {
            return;
        }
        TargetInfo targetInfo = targetList[0];

        if (!targetInfo.IsValid())
        {
            return;
        }
        if (HasPath() && targetInfo.IsVisible())
        {
            if (currentPath.Count > 1)
            {
                Vector3 item = currentPath[currentPathIndex];
                ClearPath();
                currentPath.Add(item);
                finalDestination = item;
                currentPathIndex = 0;
            }
        }
        else
        {
            if (!(UnityEngine.Time.time > nextEngagementPathTime) || HasPath() || targetInfo.IsVisible())
            {
                return;
            }
            bool                flag  = false;
            BasePathNode        start = patrolPath.GetClosestToPoint(base.transform.position);
            List <BasePathNode> nodes = Facepunch.Pool.GetList <BasePathNode>();
            if (GetEngagementPath(ref nodes))
            {
                flag  = true;
                start = nodes[nodes.Count - 1];
            }
            BasePathNode        basePathNode = null;
            List <BasePathNode> nearNodes    = Facepunch.Pool.GetList <BasePathNode>();
            patrolPath.GetNodesNear(targetInfo.lastSeenPosition, ref nearNodes, 30f);
            Stack <BasePathNode> stack = null;
            float num = float.PositiveInfinity;
            float y   = mainTurretEyePos.localPosition.y;
            foreach (BasePathNode item2 in nearNodes)
            {
                Stack <BasePathNode> path = new Stack <BasePathNode>();
                float pathCost;
                if (targetInfo.entity.IsVisible(item2.transform.position + new Vector3(0f, y, 0f)) && AStarPath.FindPath(start, item2, out path, out pathCost) && pathCost < num)
                {
                    stack        = path;
                    num          = pathCost;
                    basePathNode = item2;
                }
            }
            if (stack == null && nearNodes.Count > 0)
            {
                Stack <BasePathNode> path2         = new Stack <BasePathNode>();
                BasePathNode         basePathNode2 = nearNodes[UnityEngine.Random.Range(0, nearNodes.Count)];
                float pathCost2;
                if (AStarPath.FindPath(start, basePathNode2, out path2, out pathCost2) && pathCost2 < num)
                {
                    stack        = path2;
                    basePathNode = basePathNode2;
                }
            }
            if (stack != null)
            {
                currentPath.Clear();
                if (flag)
                {
                    for (int i = 0; i < nodes.Count - 1; i++)
                    {
                        currentPath.Add(nodes[i].transform.position);
                    }
                }
                foreach (BasePathNode item3 in stack)
                {
                    currentPath.Add(item3.transform.position);
                }
                currentPathIndex = -1;
                pathLooping      = false;
                finalDestination = basePathNode.transform.position;
            }
            Facepunch.Pool.FreeList(ref nearNodes);
            Facepunch.Pool.FreeList(ref nodes);
            nextEngagementPathTime = UnityEngine.Time.time + 5f;
        }
    }
Exemplo n.º 10
0
    public void UpdateMovement_Patrol()
    {
        if (patrolPath == null || UnityEngine.Time.time < nextPatrolTime)
        {
            return;
        }
        nextPatrolTime = UnityEngine.Time.time + 20f;
        if ((HasPath() && !IsAtFinalDestination()) || Interface.CallHook("OnBradleyApcPatrol", this) != null)
        {
            return;
        }
        PathInterestNode    randomInterestNodeAwayFrom = patrolPath.GetRandomInterestNodeAwayFrom(base.transform.position);
        BasePathNode        closestToPoint             = patrolPath.GetClosestToPoint(randomInterestNodeAwayFrom.transform.position);
        BasePathNode        basePathNode = null;
        bool                flag         = false;
        List <BasePathNode> nodes        = Facepunch.Pool.GetList <BasePathNode>();

        if (GetEngagementPath(ref nodes))
        {
            flag         = true;
            basePathNode = nodes[nodes.Count - 1];
        }
        else
        {
            basePathNode = patrolPath.GetClosestToPoint(base.transform.position);
        }
        if (!(Vector3.Distance(finalDestination, closestToPoint.transform.position) > 2f))
        {
            return;
        }
        if (closestToPoint == basePathNode)
        {
            currentPath.Clear();
            currentPath.Add(closestToPoint.transform.position);
            currentPathIndex = -1;
            pathLooping      = false;
            finalDestination = closestToPoint.transform.position;
        }
        else
        {
            Stack <BasePathNode> path;
            float pathCost;
            if (!AStarPath.FindPath(basePathNode, closestToPoint, out path, out pathCost))
            {
                return;
            }
            currentPath.Clear();
            if (flag)
            {
                for (int i = 0; i < nodes.Count - 1; i++)
                {
                    currentPath.Add(nodes[i].transform.position);
                }
            }
            foreach (BasePathNode item in path)
            {
                currentPath.Add(item.transform.position);
            }
            currentPathIndex = -1;
            pathLooping      = false;
            finalDestination = closestToPoint.transform.position;
        }
    }