A* Pathfinding寻路2D

一、下载插件引入

地址


二、创建寻路的网格,配置好格子的大小,应用于2D的配置和障碍物的层

image.png


三、配置障碍物

image.png


四、寻路的主角

image.png


五、代码控制,结合状态机和动画

using Pathfinding;
using Res;
using RK_Runtime;
using RK_Runtime.RK_Space_Entity;
using RK_Runtime.RK_Space_Fsm;
using RK_Runtime.RK_Space_UI;
using RK_Runtime.Utility;
using Spine;
using Spine.Unity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security;
using UnityEngine;
using UnityEngine.Rendering;

public class VC_PersonBase : RK_Entity
{
    public SceneName SceneName;

    protected float Scale = 1.0f;
    protected int Dir = 1;
    protected GameObject m_Circle;
    protected RK_UISpriteRender m_Tip;
    protected FSM<RK_Entity> m_Fsm;
    protected AILerp m_AILerp;
    protected SortingGroup m_SortGroup;

    protected Queue<Type> m_StateQueue = new Queue<Type>();

    protected SkeletonAnimation m_SkeletonAnimation;
    protected MeshRenderer m_MeshRenderer;
    protected CapsuleCollider2D m_MeshCollider;
    private VC_Talk m_Talk;

    public override void OnCreated()
    {
        base.OnCreated();

        m_AILerp = GetComponent<AILerp>();
        m_AILerp.enabled = false;
        m_SortGroup = GetComponent<SortingGroup>();
        m_Fsm = RK.Fsm.GetFsm(this);
        m_MeshCollider = GetComponent<CapsuleCollider2D>();
        m_Circle = GetComponent<Transform>("Circle").gameObject;
        m_Tip = GetViewComponent<RK_UISpriteRender>("Tip");

        m_Fsm.AddState<EmptyState>();
        m_Fsm.AddState<LazeState>();
    }

    public void PlayAni(string ani, bool loop = true, Action endAction = null)
    {
        try
        {
            var entry = m_SkeletonAnimation.AnimationState.SetAnimation(0, ani, loop);
            if (endAction != null)
            {
                entry.Complete += (e) => { 
                    endAction();
                };
            }
        }
        catch (Exception e)
        {
            RK.Log.LogError(e);
            RK.Log.LogError(this.GetType().ToString());
        }
    }

    public void MoveTo(Vector3 pos)
    {
        EnableAStar(true);
        EnableSortingGroup(true);
        m_AILerp.destination = pos;
        UpdateMoveDir();
    }

    public void UpdateMoveDir()
    {
        if(Mathf.Abs(m_AILerp.velocity.x) > 0.25f)
            SetDir(-m_AILerp.velocity.x);
    }

    public bool IsMoveEnd()
    {
        return m_AILerp.reachedDestination || m_AILerp.reachedEndOfPath;
    }

    public void EnableClick(bool enable)
    { 
        m_MeshCollider.enabled = enable;
    }

    public void EnableAStar(bool enable)
    {
        m_AILerp.enabled = enable;
    }

    public void EnableSortingGroup(bool enable)
    {
        //m_SortGroup.enabled = enable;
    }

    //1正方向 -1反方向
    public void SetDir(float dir)
    {
        transform.localScale = new Vector3((dir < 0 ? -Scale : Scale) * Dir, Scale, Scale);
    }

    public void SetMaxSpeed(float speed)
    { 
        m_AILerp.speed = speed;
    }

    public void SetSortingLayout(int sortingOrder)
    {
        if (m_MeshRenderer == null)
        { 
            m_MeshRenderer = GetComponent<MeshRenderer>();
        }

        m_MeshRenderer.sortingOrder = sortingOrder;
    }

    public void GoToState<S>(object data = null) where S : FSMState<RK_Entity>
    {
        GoToState(typeof(S), data);
    }

    public void GoToState(Type tp, object data = null)
    {
        m_Fsm.GoToState(tp, data);
    }

    public Type GetCurState()
    {
        if (m_Fsm != null && m_Fsm.currentstate != null)
        {
            return m_Fsm.currentstate.GetType();
        }
        return null;
    }

    public FSM<RK_Entity> GetFsm()
    {
        return m_Fsm;
    }

    public bool IsReady()
    { 
        return m_SkeletonAnimation != null;
    }

    public SkeletonAnimation GetSkeleton()
    {
        return m_SkeletonAnimation;
    }

    public void ShowTalk(string content, float time = 2.0f, int emoj = 0)
    {
        if (m_Talk != null)
        {
            m_Talk.Destroy();
            m_Talk = null;
        }

        if (emoj != 0)
        {
            m_Talk = RK.Entity.CreateEntity<VC_Talk>("Prefabs/Unit/TalkEmoj.prefab", content, transform, RK_ValueRef<float>.Pack(1.0f), RK_ValueRef<float>.Pack(1.5f), RK_ValueRef<float>.Pack(time), RK_ValueRef<int>.Pack(emoj));
        }
        else
        {
            m_Talk = RK.Entity.CreateEntity<VC_Talk>("Prefabs/Unit/Talk.prefab", content, transform, RK_ValueRef<float>.Pack(1.0f), RK_ValueRef<float>.Pack(1.5f), RK_ValueRef<float>.Pack(time), RK_ValueRef<int>.Pack(emoj));
        }
    }

    public void ShowOrderTalk(string content, float time = 2.0f, int emoj = 0)
    {
        if (m_Talk != null)
        {
            m_Talk.Destroy();
            m_Talk = null;
        }
        m_Talk = RK.Entity.CreateEntity<VC_Talk>("Prefabs/Unit/OrderTalk.prefab", content, transform, RK_ValueRef<float>.Pack(1.0f), RK_ValueRef<float>.Pack(1.5f), RK_ValueRef<float>.Pack(time), RK_ValueRef<int>.Pack(emoj));
    }

    public void GetHandNode()
    {
    }

    public void ShowTip(bool show, string path = null)
    {
        if (show)
        {
            m_Tip.SetActive(true);
            if (!string.IsNullOrEmpty(path))
            { 
                m_Tip.SetSpritePath(path);
            }
        }
        else
        {
            m_Tip.SetActive(false);
        }
    }

    public void ShowCircle(bool show)
    { 
        m_Circle.SetActive(show);
    }
}



首页 我的博客
粤ICP备17103704号