OpCode Changes
This commit is contained in:
@@ -953,12 +953,114 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
{
|
||||
if (session.PlayerInstance == null) return;
|
||||
|
||||
var input = _ser.Deserialize<MoveInput>(payload);
|
||||
session.PlayerInstance.InputX = input.Dx;
|
||||
session.PlayerInstance.InputY = input.Dy;
|
||||
session.PlayerInstance.Rotation = input.AimAngle;
|
||||
var (dx, dy, aimAngle) = ParseMoveInput(payload, session.PlayerInstance.X, session.PlayerInstance.Y);
|
||||
session.PlayerInstance.InputX = dx;
|
||||
session.PlayerInstance.InputY = dy;
|
||||
session.PlayerInstance.Rotation = aimAngle;
|
||||
}
|
||||
|
||||
private static (float Dx, float Dy, float AimAngle) ParseMoveInput(ReadOnlyMemory<byte> payload, float currentX, float currentY)
|
||||
{
|
||||
if (payload.IsEmpty) return (0f, 0f, 0f);
|
||||
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(System.Text.Encoding.UTF8.GetString(payload.Span));
|
||||
var root = doc.RootElement;
|
||||
if (root.ValueKind != JsonValueKind.Object) return (0f, 0f, 0f);
|
||||
|
||||
var source = root;
|
||||
if (TryGetJsonProperty(root, "input", out var nested) && nested.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
source = nested;
|
||||
}
|
||||
|
||||
var hasDx = TryGetJsonFloat(source, out var dx, "dx", "moveX", "inputX", "horizontal");
|
||||
var hasDy = TryGetJsonFloat(source, out var dy, "dy", "moveY", "inputY", "vertical");
|
||||
var hasRawX = TryGetJsonFloat(source, out var rawX, "x");
|
||||
var hasRawY = TryGetJsonFloat(source, out var rawY, "y");
|
||||
|
||||
if (!hasDx && hasRawX && IsDirectionalValue(rawX))
|
||||
{
|
||||
dx = rawX;
|
||||
hasDx = true;
|
||||
}
|
||||
if (!hasDy && hasRawY && IsDirectionalValue(rawY))
|
||||
{
|
||||
dy = rawY;
|
||||
hasDy = true;
|
||||
}
|
||||
|
||||
// Compatibility: absolute target coordinates from location-style clients.
|
||||
if (!hasDx && !hasDy && hasRawX && hasRawY && (!IsDirectionalValue(rawX) || !IsDirectionalValue(rawY)))
|
||||
{
|
||||
var tx = rawX - currentX;
|
||||
var ty = rawY - currentY;
|
||||
var targetLen = MathF.Sqrt((tx * tx) + (ty * ty));
|
||||
if (targetLen > 0.001f)
|
||||
{
|
||||
dx = tx / targetLen;
|
||||
dy = ty / targetLen;
|
||||
hasDx = true;
|
||||
hasDy = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasDx) dx = 0f;
|
||||
if (!hasDy) dy = 0f;
|
||||
|
||||
var len = MathF.Sqrt((dx * dx) + (dy * dy));
|
||||
if (len > 1f)
|
||||
{
|
||||
dx /= len;
|
||||
dy /= len;
|
||||
}
|
||||
else
|
||||
{
|
||||
dx = Math.Clamp(dx, -1f, 1f);
|
||||
dy = Math.Clamp(dy, -1f, 1f);
|
||||
}
|
||||
|
||||
TryGetJsonFloat(source, out var aimAngle, "aimAngle", "rotation", "angle");
|
||||
return (dx, dy, aimAngle);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return (0f, 0f, 0f);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryGetJsonProperty(JsonElement source, string name, out JsonElement value)
|
||||
{
|
||||
foreach (var p in source.EnumerateObject())
|
||||
{
|
||||
if (string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
value = p.Value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool TryGetJsonFloat(JsonElement source, out float value, params string[] names)
|
||||
{
|
||||
foreach (var n in names)
|
||||
{
|
||||
if (!TryGetJsonProperty(source, n, out var e)) continue;
|
||||
if (e.ValueKind == JsonValueKind.Number && e.TryGetSingle(out value)) return true;
|
||||
if (e.ValueKind == JsonValueKind.String && float.TryParse(e.GetString(), out value)) return true;
|
||||
}
|
||||
|
||||
value = 0f;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsDirectionalValue(float value)
|
||||
=> float.IsFinite(value) && value >= -1.05f && value <= 1.05f;
|
||||
|
||||
private void HandleInputAttack(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
||||
{
|
||||
if (session.PlayerInstance == null) return;
|
||||
|
||||
Reference in New Issue
Block a user