Minecraft-Console-Client/MinecraftClient/Pathing/Execution/Templates/WalkTemplate.cs

52 lines
1.5 KiB
C#
Raw Normal View History

using System;
using MinecraftClient.Mapping;
using MinecraftClient.Physics;
namespace MinecraftClient.Pathing.Execution.Templates
{
/// <summary>
/// Walk/sprint toward a destination on the same Y level.
/// Used for Traverse and Diagonal moves.
/// </summary>
public sealed class WalkTemplate : IActionTemplate
{
public Location ExpectedStart { get; }
public Location ExpectedEnd { get; }
private int _tickCount;
private Location _lastPos;
private int _stuckTicks;
public WalkTemplate(Location start, Location end)
{
ExpectedStart = start;
ExpectedEnd = end;
_lastPos = start;
}
public TemplateState Tick(Location pos, PlayerPhysics physics, MovementInput input)
{
_tickCount++;
if (TemplateHelper.IsNear(pos, ExpectedEnd, horizThresholdSq: 0.20))
return TemplateState.Complete;
double movedSq = TemplateHelper.HorizontalDistanceSq(pos, _lastPos);
_stuckTicks = movedSq < 0.0005 ? _stuckTicks + 1 : 0;
_lastPos = pos;
if (_stuckTicks > 40 || _tickCount > 100)
return TemplateState.Failed;
double dx = ExpectedEnd.X - pos.X;
double dz = ExpectedEnd.Z - pos.Z;
physics.Yaw = TemplateHelper.CalculateYaw(dx, dz);
input.Forward = true;
input.Sprint = true;
return TemplateState.InProgress;
}
}
}