4 using Microsoft.Xna.Framework;
9 internal readonly record
struct NetCircuitBoxCursorInfo(Vector2[] RecordedPositions, Option<Vector2> DragStart, Option<Identifier> HeldItem, ushort CharacterID = 0) : INetSerializableStruct;
11 internal sealed
class CircuitBoxCursor
13 public NetCircuitBoxCursorInfo Info;
15 public CircuitBoxCursor(NetCircuitBoxCursorInfo info)
17 if (Entity.FindEntityByID(info.CharacterID) is Character c)
19 Color = GenerateColor(c.Name);
25 public void UpdateInfo(NetCircuitBoxCursorInfo newInfo)
29 newInfo.HeldItem.Match(
30 some: newIdentifier =>
34 if (oldPrefab.Identifier == newIdentifier) { return; }
35 SetHeldPrefab(newIdentifier);
37 none: () => SetHeldPrefab(newIdentifier)
39 none: () => HeldPrefab = Option.None);
41 prevPosition = DrawPosition;
43 void SetHeldPrefab(Identifier identifier)
45 ItemPrefab? prefab = ItemPrefab.Prefabs.Find(prefab => prefab.Identifier.Equals(identifier));
46 HeldPrefab = prefab is
null ? Option.None : Option.Some(prefab);
50 public Option<ItemPrefab> HeldPrefab {
get;
private set; } = Option.None;
52 public Color Color = Color.White;
54 public static Color GenerateColor(
string name)
57 return ToolBoxCore.HSVToRGB(random.NextSingle() * 360f, 1f, 1f);
60 private const float UpdateTimeout = 5f;
62 private float updateTimer;
63 private float positionTimer;
64 private Vector2 prevPosition;
65 public Vector2 DrawPosition;
67 public bool IsActive => updateTimer < UpdateTimeout;
69 public void Update(
float deltaTime)
71 updateTimer += deltaTime;
73 Vector2 finalPosition = Info.RecordedPositions[^1];
75 if (positionTimer > 1f)
77 DrawPosition = finalPosition;
78 prevPosition = Vector2.Zero;
82 positionTimer += deltaTime;
84 float stepTimer = positionTimer * 10f;
85 int targetPositonIndex = (int)MathF.Floor(stepTimer);
86 int prevPosIndex = targetPositonIndex - 1;
88 Vector2 targetPosition = IsInRange(targetPositonIndex, Info.RecordedPositions.Length)
89 ? Info.RecordedPositions[targetPositonIndex]
92 Vector2 prevTargetPosition = IsInRange(prevPosIndex, Info.RecordedPositions.Length)
93 ? Info.RecordedPositions[prevPosIndex]
96 DrawPosition = Vector2.Lerp(prevTargetPosition, targetPosition, MathHelper.Clamp(stepTimer % 1f, 0f, 1f));
99 static bool IsInRange(
int index,
int length)
100 => index >= 0 && index < length;
103 public void ResetTimers()