Пример #1
0
        void OnDrawGizmosSelected()
        {
            if (grid == null)
            {
                return;
            }
            if (grid.DontDraw)
            {
                return;
            }
            if (Camera.main == null)
            {
                return;
            }

            var mousePos = Input.mousePosition;
            var pos      = Camera.main.ScreenToWorldPoint(mousePos);
            var node     = grid.NodeFromWorldPoint(pos);

            Gizmos.color = Color.green;
            Gizmos.DrawCube(grid.PosFromNode(node), Vector3.one * 0.5f);
            if (node.walkable)
            {
                Gizmos.color = Color.magenta;
                Gizmos.DrawCube(grid.PosFromNode(node), Vector3.one * 0.45f);
            }
            else
            {
                var closestNode = NodeHelper.ClosestWalkable(node, resolution);
                Gizmos.color = Color.magenta;
                Gizmos.DrawCube(grid.PosFromNode(closestNode), Vector3.one * 0.45f);
            }
        }
Пример #2
0
        IEnumerator <float> WalkTo(Vector3 Position, float StartingSpeedPercentage, float MaxSpeed, System.Action Callback)
        {
            Position = new Vector3(Position.x, Position.y, 1);
            PlayerAnimationHelper.StopPlayer();
            //RequestPath.GetPath(rb.position, Position, path, Resolution.High);
            RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col);
            //IgnoreAllCollisionExcept(true, 31, 9, 10);
            col.isTrigger = true;

            float   minSqrDist        = 0;
            Vector3 pathFindingCenter = Vector3.zero;

            if (currentTarget != null)
            {
                minSqrDist        = Mathf.Pow(currentTarget.interactionOptions.InteractionRange, 2);
                pathFindingCenter = currentTarget.gameObject.transform.position + (Vector3)currentTarget.interactionOptions.InteractionCenterOffset;
            }

            yield return(0f);

            if (path.Count != 0)
            {
                anim.SetBool("Walking", true);
            }
            MaxSpeed *= Stats.MoveSpeed;

            float _spd  = StartingSpeedPercentage * MaxSpeed;
            int   index = 0;

            while (this)
            {
                if (index >= path.Count)
                {
                    break;
                }

                if (!CanWalkAt(path[index]))
                {
                    yield return(0f);

                    //RequestPath.GetPath(rb.position, Position, path, Resolution.High);
                    RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col);
                    index = 0;
                    continue;
                }

                Vector3 currentPos = rb.position;
                Vector3 targetPos  = grid.PosFromNode(path[index]);

                var offset = new Vector3(targetPos.x - currentPos.x, targetPos.y - currentPos.y, 1);
                if (_spd != MaxSpeed)
                {
                    _spd = Mathf.MoveTowards(_spd, MaxSpeed, 10 * MaxSpeed * GameTime.Time.fixedDeltaTime);
                }
                float currentSpeed = _spd * GameTime.Time.fixedDeltaTime;

                var posAfterMovement = Vector3.MoveTowards(currentPos, targetPos, currentSpeed);

                // Update the index and increase the movement point without losing a frame of movement.
                if (posAfterMovement == targetPos)
                {
                    index++;

                    if (index >= path.Count)
                    {
                    }
                    else
                    {
                        targetPos = new Vector3(grid.PosFromNode(path[index]).x, grid.PosFromNode(path[index]).y, 1);
                        offset    = new Vector3(targetPos.x - currentPos.x, targetPos.y - currentPos.y, 1);

                        posAfterMovement = Vector3.MoveTowards(currentPos, targetPos, currentSpeed);
                    }
                }

                /// Check if we've reached the <see cref="Objects.Functionality.InteractionOptions.InteractionRange"/>.
                if (currentTarget != null)
                {
                    float sqrDistanceToTarget = Vector2.SqrMagnitude(posAfterMovement - pathFindingCenter);
                    if (sqrDistanceToTarget <= minSqrDist)
                    {
                        break;
                    }
                }

                rb.MovePosition(posAfterMovement);

                if (index >= path.Count)
                {
                    break;
                }

                CalculateFacing(offset);

                yield return(0f);
            }

            if (!this)
            {
                yield break;
            }

            anim.SetBool("Walking", false);
            yield return(0f);

            col.isTrigger = false;
            PlayerAnimationHelper.ResetPlayer();
            currentHandle = null;
            currentTarget = null;

            if (Callback != null)
            {
                Callback();
            }
        }
        IEnumerator <float> WalkTo(Vector3 Position, float StartingSpeedPercentage, float MaxSpeed, System.Action Callback)
        {
            Position = new Vector3(Position.x, Position.y, 1);
            PlayerAnimationHelper.StopPlayer();
            //RequestPath.GetPath(rb.position, Position, path, Resolution.High);
            RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col);
            //IgnoreAllCollisionExcept(true, 31, 9, 10);
            col.isTrigger = true;

            yield return(0f);

            if (path.Count != 0)
            {
                anim.SetBool("Walking", true);
            }
            MaxSpeed *= Stats.MoveSpeed;

            float _spd  = StartingSpeedPercentage * MaxSpeed;
            int   index = 0;

            while (this)
            {
                if (index >= path.Count)
                {
                    break;
                }

                if (!CanWalkAt(path[index]))
                {
                    yield return(0f);

                    //RequestPath.GetPath(rb.position, Position, path, Resolution.High);
                    RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col);
                    index = 0;
                    continue;
                }

                Vector3 targetPos = grid.PosFromNode(path[index]);

                Vector3 offset = new Vector3(targetPos.x - rb.position.x, targetPos.y - rb.position.y, 1);
                if (_spd != MaxSpeed)
                {
                    _spd = Mathf.MoveTowards(_spd, MaxSpeed, 10 * MaxSpeed * Time.fixedDeltaTime);
                }
                float currentSpeed = _spd * Time.fixedDeltaTime;

                var posAfterMovement = Vector3.MoveTowards(rb.position, targetPos, currentSpeed);

                if (posAfterMovement == targetPos)
                {
                    index++;

                    if (index >= path.Count)
                    {
                    }
                    else
                    {
                        targetPos = new Vector3(grid.PosFromNode(path[index]).x, grid.PosFromNode(path[index]).y, 1);
                        offset    = new Vector3(targetPos.x - rb.position.x, targetPos.y - rb.position.y, 1);

                        posAfterMovement = Vector3.MoveTowards(rb.position, targetPos, currentSpeed);
                    }
                }

                rb.MovePosition(posAfterMovement);

                if (index >= path.Count)
                {
                    break;
                }

                CalculateFacing(offset);

                yield return(0f);
            }

            if (!this)
            {
                yield break;
            }

            anim.SetBool("Walking", false);
            yield return(0f);

            GameLibOfMethods.player.GetComponent <Collider2D>().isTrigger = false;
            PlayerAnimationHelper.ResetPlayer();
            currentHandle = null;

            if (Callback != null)
            {
                Callback();
            }
        }
Пример #4
0
        public static Node ClosestWalkable(Node closestTo, Resolution resolution)
        {
            grid     = NodeGridManager.GetGrid(resolution);
            nodeGrid = grid.nodeGrid;

            // First, check if any of the neighbors are walkable
            BaseNode[] neighbors = closestTo.neighbours.directionNeighbours;

            X = closestTo.X;
            Y = closestTo.Y;

            maxX = nodeGrid.GetLength(0) - 1;
            maxY = nodeGrid.GetLength(1) - 1;

            helperList.Clear();


            // Find Closest Left
            helperList.Add(ClosestToOrigin(decrease, stay));

            // Find Closest Right
            helperList.Add(ClosestToOrigin(increase, stay));

            // Find Closest Top
            helperList.Add(ClosestToOrigin(stay, increase));

            // Find Closest Bot
            helperList.Add(ClosestToOrigin(stay, decrease));

            // Find Closest Diags
            helperList.Add(ClosestToOrigin(decrease, decrease));
            helperList.Add(ClosestToOrigin(decrease, increase));
            helperList.Add(ClosestToOrigin(increase, decrease));
            helperList.Add(ClosestToOrigin(increase, increase));

            // Find Most efficient Node
            int  minDist     = int.MaxValue;
            Node closestNode = null;

            foreach (Node n in helperList)
            {
                if (n == null)
                {
                    continue;
                }

                int dist = heuristic(n, closestTo);
                if (dist >= minDist)
                {
                    continue;
                }

                minDist     = dist;
                closestNode = n;
            }

            if (closestNode == null)
            {
                Debug.LogError("Couldn't find valid node on " + grid.PosFromNode(closestTo));
                return(closestTo);
            }
            else
            {
                return(closestNode);
            }
        }