vchelaru / FlatRedBall

Cross-platform 2D game engine focused on ultimate productivity built in .NET
http://flatredball.com
MIT License
413 stars 66 forks source link

State interpolating logic omits variables #1625

Closed RychuP closed 1 week ago

RychuP commented 1 week ago

In my particular example I was interpolating to an FRB state using InterpolateToState method. The states contain three variables: two floats and a bool. One of the floats and the bool interpolated as expected. The Sprite Texture Scale remained unchanged during interpolation. Example can be found here.

scalenotscaling

vchelaru commented 1 week ago

Thanks for reporting this. The reason this is happening is InterpolateToState method relies on internal velocity variables to work. If a variable (such as TextureScale) doesn't have a velocity variable, then it won't apply in this method. This is a bad implementation because it requires understanding what does and doesn't have velocity variables instead of it "just working".

Instead, the TweenerManager provides an interpolation method which will work for all variables. Here's what your code might look like using that:

public void StartInvulnerability()
{
    _invulnerabilityStartTime = TimeManager.CurrentScreenTime;

    // disable damage receiving
    IsDamageReceivingEnabled = false;

    // create a start instruction
    CurrentInvulnerabilityState = Invulnerability.Visible;
    //var startInstr = InterpolateToState(Invulnerability.Hidden, InvulnerabilityTimeAfterDamage);

    _=TweenerManager.Self.TweenAsync(this, 
        (value) => InterpolateBetween(Invulnerability.Visible, Invulnerability.Hidden, value), 
        0, 
        1, 
        (float)InvulnerabilityTimeAfterDamage);

    // create a stop instruction
    var stopInstr = new DelegateInstruction(StopInvulnerability)
    {
        TimeToExecute = TimeManager.CurrentScreenTime + InvulnerabilityTimeAfterDamage
    };
    Instructions.Add(stopInstr);
}

To help address this for future users, here's the changes I've made:

  1. I've added an Obsolete attribute to the InterpolateToState so that users know that they should change approaches image
  2. I've modified the codegen to include info about which variables are and aren't included as velocity variables in the method itself. image

For more information on the alternative code using TweenerManager, see https://docs.flatredball.com/flatredball/api/stateinterpolationplugin/tweenermanager

The docs here are fairly basic, so if you would like any more info please let me know and I can add more.