Ejemplo n.º 1
0
        bool SetMovementTargetToSearchPointAroundSearchCenter()
        {
            List <PathfindingNode>    possibleSearchNodes = new List <PathfindingNode>();
            List <PathfindingNode>    neighborsToCheck    = new List <PathfindingNode>();
            HashSet <PathfindingNode> neighborsChecked    = new HashSet <PathfindingNode>();
            PathfindingNode           searchCenterNode    = grid.NodeAtWorldPosition(searchCenter);
            float searchRadius = this.searchRadius;

            if (Vector2.Distance(searchCenter, searchCenterNode.WorldPosition) > grid.GridScale)
            {
                searchCenter = searchCenterNode.WorldPosition;
                searchRadius = Vector2.Distance(searchCenter, rigidbody.position);
            }
            neighborsToCheck.Add(searchCenterNode);
            PathfindingNode neighborToCheck;

            while (neighborsToCheck.Count > 0)
            {
                neighborToCheck = neighborsToCheck[0];
                neighborsToCheck.RemoveAt(0);
                foreach (PathfindingNode neighbor in grid.GetNeighbors(neighborToCheck))
                {
                    if (!neighborsChecked.Contains(neighbor))
                    {
                        if (Vector2.Distance(searchCenter, neighbor.WorldPosition) <= searchRadius)
                        {
                            neighborsToCheck.Add(neighbor);
                            if (neighbor.IsWalkable)
                            {
                                possibleSearchNodes.Add(neighbor);
                            }
                        }
                        neighborsChecked.Add(neighbor);
                    }
                }
            }

            if (possibleSearchNodes.Count <= 0)
            {
                return(false);
            }

            PathfindingNode searchNode = possibleSearchNodes[Random.Range(0, possibleSearchNodes.Count - 1)];

            eventManager.TriggerEvent(EnemyEvents.OnSetMovementTarget, new ParamsObject(searchNode.WorldPosition));
            return(true);
        }
Ejemplo n.º 2
0
        bool SetCoverTargetFromPlayerLastKnownPosition()
        {
            Vector2 bestCoverPoint;
            List <PathfindingNode>  potentialCoverNodes = new List <PathfindingNode>();
            HashSet <Vector2>       inProcessQueue      = new HashSet <Vector2>();
            Queue <PathfindingNode> nodesToProcess      = new Queue <PathfindingNode>();

            grid.Reset();
            PathfindingNode node = grid.NodeAtWorldPosition(rigidbody.position);

            node.Parent = null;
            nodesToProcess.Enqueue(node);
            inProcessQueue.Add(node.WorldPosition);

            while (nodesToProcess.Count > 0 && Vector2.Distance(rigidbody.position, node.WorldPosition) < maxCoverSearchDistance)
            {
                node = nodesToProcess.Dequeue();

                Quaternion   left            = Quaternion.AngleAxis(-7, Vector3.forward);
                Quaternion   right           = Quaternion.AngleAxis(7, Vector3.forward);
                RaycastHit2D leftRaycastHit  = Physics2D.Linecast(playerLastKnownPosition, left * node.WorldPosition, sightBlockMask);
                RaycastHit2D rightRaycastHit = Physics2D.Linecast(playerLastKnownPosition, right * node.WorldPosition, sightBlockMask);
                if (leftRaycastHit && rightRaycastHit)
                {
                    potentialCoverNodes.Add(node);
                }

                foreach (PathfindingNode neighbor in grid.GetNeighbors(node))
                {
                    if (!inProcessQueue.Contains(neighbor.WorldPosition))
                    {
                        if (neighbor.IsWalkable)
                        {
                            neighbor.Parent = node;
                            nodesToProcess.Enqueue(neighbor);
                            inProcessQueue.Add(neighbor.WorldPosition);
                        }
                    }
                }
            }

            float           nodeDistance         = 0;
            float           smallestNodeDistance = float.MaxValue;
            PathfindingNode currNode;

            bestCoverPoint = NullVector;
            foreach (PathfindingNode coverNode in potentialCoverNodes)
            {
                currNode     = coverNode;
                nodeDistance = 0;
                while (currNode != null)
                {
                    if (currNode.Parent != null)
                    {
                        nodeDistance += Vector2.Distance(currNode.WorldPosition, currNode.Parent.WorldPosition);
                    }
                    currNode = currNode.Parent;
                }
                if (nodeDistance < 5)
                {
                    //print("NodePos: " + coverNode.WorldPosition + ", NodeDist: " + nodeDistance);
                }
                if (nodeDistance < smallestNodeDistance)
                {
                    smallestNodeDistance = nodeDistance;
                    bestCoverPoint       = coverNode.WorldPosition;
                }
            }

            if (bestCoverPoint != NullVector)
            {
                coverDistance = smallestNodeDistance;
                coverTarget   = bestCoverPoint;
                //print("BEST NodePos: " + coverTarget + ", NodeDist: " + coverDistance);
                Quaternion left  = Quaternion.AngleAxis(-sightAngle, Vector3.forward);
                Quaternion right = Quaternion.AngleAxis(sightAngle, Vector3.forward);
                leftP  = left * coverTarget;
                rightP = right * coverTarget;
                nodeP  = coverTarget;
                return(true);
            }

            return(false);
        }