107 lines
3.2 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
using System.Collections.Generic;
using DotRecast.Core.Numerics;
using DotRecast.Detour;
using DotRecast.Detour.Crowd;
using Game.Logic.System;
using JNGame.Map.DotRecast;
using JNGame.Math;
using JNGame.Sync.System;
using UnityEngine;
namespace Game.JNGFrame.Logic.System
{
/// <summary>
/// 地图
/// </summary>
public class DMapSystem : DGBasisSystem
{
public DotRecastRoot Root;
public override void OnSyncStart()
{
base.OnSyncStart();
Root = new DotRecastRoot(GetSystem<DDataSystem>().MapData2);
Debug.Log($"DemoMapService - 加载完成地图");
}
public override void OnSyncUpdate()
{
base.OnSyncUpdate();
Root.Update(new LFloat(null,Sync.DeltaTime));
}
public DtCrowdAgentParams GetAgentParams()
{
int updateFlags = DtCrowdAgentUpdateFlags.DT_CROWD_ANTICIPATE_TURNS |
DtCrowdAgentUpdateFlags.DT_CROWD_OPTIMIZE_VIS |
DtCrowdAgentUpdateFlags.DT_CROWD_OPTIMIZE_TOPO |
DtCrowdAgentUpdateFlags.DT_CROWD_OBSTACLE_AVOIDANCE;
DtCrowdAgentParams ap = new DtCrowdAgentParams();
ap.radius = (LFloat)0.6f;
ap.height = (LFloat)2f;
ap.maxAcceleration = (LFloat)8.0f;
ap.maxSpeed = (LFloat)6f;
ap.collisionQueryRange = ap.radius * (LFloat)12f;
ap.pathOptimizationRange = ap.radius * (LFloat)30f;
ap.updateFlags = updateFlags;
ap.obstacleAvoidanceType = 0;
ap.separationWeight = (LFloat)2f;
return ap;
}
/// <summary>
/// 返回最近点
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public LVector3 GetNearbyPoints(LVector3 pos)
{
return Root.GetNearbyPoints(pos);
}
/// <summary>
/// 查询导航
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public List<DtStraightPath> FindPath(LVector3 start,LVector3 end)
{
return Root.FindPath(start,end);
}
//添加避障
2024-08-23 10:48:19 +08:00
public void AddAgent(ulong id,LVector3 start)
2024-08-17 14:27:18 +08:00
{
Root.AddAgent(id,new RcVec3f(start.x,start.y,start.z));
}
2024-08-23 10:48:19 +08:00
public void AddAgent(ulong id,LVector3 start,DtCrowdAgentParams agentParams)
2024-08-17 14:27:18 +08:00
{
Root.AddAgent(id,new RcVec3f(start.x,start.y,start.z),agentParams);
}
//删除避障
2024-08-23 10:48:19 +08:00
public void DelAgent(ulong id)
2024-08-17 14:27:18 +08:00
{
Root.DelAgent(id);
}
//移动避障
2024-08-23 10:48:19 +08:00
public void MoveAgent(ulong id,LVector3 move)
2024-08-17 14:27:18 +08:00
{
Root.MoveAgent(id,new RcVec3f(move.x,move.y,move.z));
}
//向量移动避障
2024-08-23 10:48:19 +08:00
public void VectorMoveAgent(ulong id,LVector2 vector)
2024-08-17 14:27:18 +08:00
{
Root.VectorMoveAgent(id,new RcVec3f(vector.x,0,vector.y));
}
//获取避障
2024-08-23 10:48:19 +08:00
public DtCrowdAgent GetAgent(ulong id)
2024-08-17 14:27:18 +08:00
{
return Root.GetAgent(id);
}
}
}