You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.0 KiB

1 month ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sog;
using Sog.Service;
using ProtoCSStruct;
namespace Game
{
public class MoveController
{
public const int PathMaxPoint = 16;
public static void Update(MapActor actor, long nowMs)
{
if(nowMs - actor.move.m_lastMoveTime < 500)
{
return;
}
//没有移动,调用路径生成
if(actor.move.IsMoving() == false)
{
IMovePathGenerator generator = MovePathGeneratorMgr.Instance.GetGenerator((int)actor.move.moveType);
if(generator != null)
{
generator.Update(actor, nowMs);
}
return;
}
if(actor.move.m_lastMoveTime == 0)
{
actor.move.m_lastMoveTime = GameServerUtils.GetTimeMs();
return;
}
long timeGap = GameServerUtils.GetTimeMs() - actor.move.m_lastMoveTime;
actor.move.m_lastMoveTime = GameServerUtils.GetTimeMs();
//在移动处理移动
int iSpeed = actor.prop.GetSpeed();
int iDistance = (int)(iSpeed * timeGap / 1000);
DBPosition curPos = actor.position;
DBPosition newPos = curPos;
if(iDistance > 0)
{
MoveByDistance(actor, iDistance, ref newPos);
}
if(MoveUtils.IsPosSame(curPos, newPos) == false)
{
DBPosition dir = new DBPosition();
MapSvc.ChangePosition(actor, actor.map, curPos, newPos, dir, true);
}
}
//添加路径点
public static void AddPathPoint(MapActor actor, DBPosition pos)
{
if(actor.move.m_stPathPoints.Count > PathMaxPoint)
{
TraceLog.Error("MoveController.AddPathPoint actor {0} path point full", actor.actorId);
return;
}
TraceLog.Trace("MoveController.AddPathPoint actor {0} add path point {1} {2} {3}"
, actor.actorId, pos.X, pos.Y, pos.Z);
actor.move.m_stPathPoints.Enqueue(pos);
}
public static void MoveByDistance(MapActor actor, int iDistance, ref DBPosition newPos)
{
TraceLog.Trace("MoveController.MoveByDistance actor {0} move distance {1}"
, actor.actorId, iDistance);
int remainMoveDis = iDistance;
DBPosition pos = actor.position;
CSMoveWalk moveWalk = new CSMoveWalk();
moveWalk.ActorId = actor.actorId;
moveWalk.Mapid = actor.map.mapDescId;
moveWalk.MoveSpeed = actor.prop.GetSpeed();
while (actor.move.m_stPathPoints.Count > 0)
{
DBPosition nextpos = actor.move.m_stPathPoints.Peek();
int pointDistance = (int)MoveUtils.GetDistance3D(ref pos, ref nextpos);
if(remainMoveDis > pointDistance)
{
pos = nextpos;
actor.move.m_stPathPoints.Dequeue();
moveWalk.Points.Add(ref nextpos);
}
else
{
pos.X = MoveUtils.ComputeCoordinate((nextpos.X - pos.X),
pos.X, remainMoveDis, pointDistance);
pos.Z = MoveUtils.ComputeCoordinate((nextpos.Z - pos.Z),
pos.Z, remainMoveDis, pointDistance);
moveWalk.Points.Add(ref pos);
}
remainMoveDis -= pointDistance;
}
newPos = pos;
//没路了,先直接停下来,长距离寻路再说(比如n个路径点)
if(actor.move.m_stPathPoints.Count == 0)
{
moveWalk.IsStop = true;
}
MapViewSvc.BroadCastToPlayerInView(actor, (int)CSGameMsgID.MoveWalk, ref moveWalk);
}
}
}