Ejemplo n.º 1
0
    public Vector2?GetVel(JPS_PathFinder mPathFinder, Vector2 ori, Vector2?target = null, float speed = 0f)
    {
        GridView.Inst[GM.CurMapName][1].GetJPS_Path();

        if (mMoveQueue.Count > 0)
        {
            var tpos = mMoveQueue.Peek();
            if (tpos == ori)
            {
                mMoveQueue.Dequeue();
                return((tpos - ori).normalized * Mathf.Clamp(speed, 0, Vector2.Distance(tpos, ori) / Time.fixedDeltaTime));
            }
            else
            {
                return((tpos - ori).normalized * Mathf.Clamp(speed, 0, Vector2.Distance(tpos, ori) / Time.fixedDeltaTime));
            }
        }
        else
        {
            if (target is null)
            {
                return(null);
            }
            var path = mPathFinder.Find(ori, target.GetValueOrDefault());
            foreach (var i in path)
            {
                mMoveQueue.Enqueue(i);
            }
            if (mMoveQueue.Count == 0)
            {
                return(Vector2.zero);
            }
            return((mMoveQueue.Peek() - ori).normalized * Mathf.Clamp(speed, 0, Vector2.Distance(mMoveQueue.Peek(), ori) / Time.fixedDeltaTime));
        }
    }
Ejemplo n.º 2
0
    private void OnGUI()
    {
        if (GUI.Button(new Rect(500, 10, 100, 20), "Start"))
        {
            GridView.Inst[GM.CurMapName][ObjectSize].GetJPS_Path();
            Vector2[] path = pathFinder.Find(transform.position, PlayerPos.position);
            UpdatePath(path);
        }

        if (GUI.Button(new Rect(500, 40, 100, 20), "path"))
        {
            foreach (var i in mPath)
            {
                Instantiate(prefab, i, Quaternion.identity);
            }
        }
    }
Ejemplo n.º 3
0
    public Vector2 GetVelIfUpdateTarget(JPS_PathFinder mPathFinder, Vector2 origin, Vector2 target, float speed)
    {
        var tposList = mPathFinder.Find(origin, target);

        if (tposList.Length == 0)
        {
            return(Vector2.zero);
        }

        var Goal = tposList[0];

        bool SetGoal(int n)
        {
            if (tposList.Length > n)
            {
                Goal = tposList[n];
            }
            else
            {
                return(false);
            }
            return(true);
        }

        if (CurGoal != null && CurGoal.Value == Goal)
        {
            if (!SetGoal(1))
            {
                return(Vector2.zero);
            }
        }
        if (origin == Goal)
        {
            CurGoal = origin;
            if (!SetGoal(1))
            {
                return(Vector2.zero);
            }
        }

        return((Goal - origin).normalized * Mathf.Clamp(speed, 0, Vector2.Distance(origin, Goal) / Time.fixedDeltaTime));
    }