Skip to content

Commit

Permalink
Handle glide_size being updated mid-glide (OpenDreamProject#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit authored Jan 10, 2024
1 parent 816e59c commit 5769bea
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
20 changes: 13 additions & 7 deletions OpenDreamClient/Rendering/AtomGlideSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace OpenDreamClient.Rendering;
/// Disables RobustToolbox's transform lerping and replaces it with our own gliding
/// </summary>
public sealed class AtomGlideSystem : EntitySystem {
private sealed class Glide(TransformComponent transform) {
private sealed class Glide(TransformComponent transform, DMISpriteComponent sprite) {
public readonly TransformComponent Transform = transform;
public readonly DMISpriteComponent Sprite = sprite;
public Vector2 EndPos;
public float MovementSpeed;
}

[Dependency] private readonly TransformSystem _transformSystem = default!;
Expand Down Expand Up @@ -42,9 +42,16 @@ public override void FrameUpdate(float frameTime) {

for (int i = 0; i < _currentGlides.Count; i++) {
var glide = _currentGlides[i];

if (glide.Sprite.Icon.Appearance == null) {
_currentGlides.RemoveSwap(i--);
continue;
}

var currentPos = glide.Transform.LocalPosition;
var newPos = currentPos;
var movement = glide.MovementSpeed * frameTime;
var movementSpeed = CalculateMovementSpeed(glide.Sprite.Icon.Appearance.GlideSize);
var movement = movementSpeed * frameTime;

// Move X towards the end position at a constant speed
if (!MathHelper.CloseTo(currentPos.X, glide.EndPos.X)) {
Expand Down Expand Up @@ -111,7 +118,7 @@ private void OnTransformMove(ref MoveEvent e) {
}

if (glide == null) {
glide = new(e.Component);
glide = new(e.Component, sprite);
_currentGlides.Add(glide);
}

Expand All @@ -120,16 +127,15 @@ private void OnTransformMove(ref MoveEvent e) {
_transformSystem.SetLocalPositionNoLerp(e.Sender, startingFrom, e.Component);

glide.EndPos = glidingTo;
glide.MovementSpeed = CalculateMovementSpeed(sprite.Icon.Appearance.GlideSize);
_ignoreMoveEvent = false;
}

private static float CalculateMovementSpeed(byte glideSize) {
private static float CalculateMovementSpeed(float glideSize) {
if (glideSize == 0)
glideSize = 4; // TODO: 0 gives us "automated control" over this value, not just setting it to 4

// Assume a 20 TPS server
// TODO: Support other TPS
return (float)glideSize / EyeManager.PixelsPerMeter * 20f;
return glideSize / EyeManager.PixelsPerMeter * 20f;
}
}
2 changes: 1 addition & 1 deletion OpenDreamRuntime/AtomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void SetAppearanceVar(IconAppearance appearance, string varName, DreamVal
break;
case "glide_size":
value.TryGetValueAsFloat(out float glideSize);
appearance.GlideSize = (byte) glideSize;
appearance.GlideSize = glideSize;
break;
case "render_source":
value.TryGetValueAsString(out appearance.RenderSource);
Expand Down
3 changes: 1 addition & 2 deletions OpenDreamRuntime/Procs/DMOpcodeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,14 +1697,13 @@ public static ProcStatus SwitchCaseRange(DMProcState state) {
public static ProcStatus Spawn(DMProcState state) {
int jumpTo = state.ReadInt();
state.Pop().TryGetValueAsFloat(out var delay);
int delayMilliseconds = (int)(delay * 100);

// TODO: It'd be nicer if we could use something such as DreamThread.Spawn here
// and have state.Spawn return a ProcState instead
DreamThread newContext = state.Spawn();

//Negative delays mean the spawned code runs immediately
if (delayMilliseconds < 0) {
if (delay < 0) {
newContext.Resume();
// TODO: Does the rest of the proc get scheduled?
// Does the value of the delay mean anything?
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamShared/Dream/IconAppearance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class IconAppearance : IEquatable<IconAppearance> {
[ViewVariables] public Vector2i PixelOffset;
[ViewVariables] public Color Color = Color.White;
[ViewVariables] public byte Alpha = 255;
[ViewVariables] public byte GlideSize;
[ViewVariables] public float GlideSize;
/// <summary>
/// An appearance can gain a color matrix filter by two possible forces: <br/>
/// 1. the /atom.color var is modified. <br/>
Expand Down

0 comments on commit 5769bea

Please sign in to comment.