TrueGrime
TrueGrime

Reputation: 275

Summing Up the result of various numeric effects on a single variable

public class ComEffectColor : ComEffectBase<Ref<Color>>
{
    private readonly Color finalColor;
    private Color originalColor;

    public ComEffectColor(Ref<Color> effectTarget, Color finalColor, TimeSpan duration, TimeSpan startDelay) 
        : base(effectTarget, duration, startDelay)
    {
        this.finalColor = finalColor;

        StartDelayEnded += ComEffectColorStartDelayEnded;
    }

    protected override void UpdateCustom()
    {
        if (elapsedTime < duration)
        {
            float progress = (float)(elapsedTime.TotalMilliseconds / duration.TotalMilliseconds);

            effectTarget.Value = Color.Lerp(originalColor, finalColor, progress);
        }
        else
        {
            effectTarget.Value = finalColor;

            InvokeFinished();
        }
    }

    void ComEffectColorStartDelayEnded(object sender, EventArgs e)
    {
        originalColor = effectTarget.Value;
    }
}

This is the constructor of the ComEffectColor : IComEffect class. Its function is to take a pointer to a color variable that may belong to any other object, and change its value gradually towards the 'targetColor' during the duration specified.

after initializing such an effect class, it is added to an instance of ComEffectManager which handles their updating and removal after they are finished.

The problem I am facing is how to enable mixing of 2 or more ComEffectColor on the same color variable. So if I start with a color variable which is black (0, 0, 0) and I add 2 ComEffectColor with 1 sec duration each, where the first one's target color is (128, 128, 128) and the other is (0, 0, 0), after 1 second I should end up with (64, 64, 64).

I want the effects to be independent, so I dont want to first collect all the color offsets and find the 'summation of force' and apply that to the color variable. This is due to other features of IComEffect but I dont want make this a very long post.

Other effects can be added while these 2 in the above example are still running.

Any ideas how this can be done?

The code above is the naive implementation that overwrites the other effects running simultaneously. This is not the complete class as it inherits from ComEffectBase which handles all the common routines of all kinds of effects.

Upvotes: 3

Views: 152

Answers (1)

JamieB
JamieB

Reputation: 348

I do not think this is possible without collecting all your transformation outcomes and determining the "final transformation" target.

For example, if you eliminate the code and just say this in English (math really): your color function c starts at time t0 at color c(t0), and after some time t1, you could arrive at two "final" colors, c(t1) and/or c'(t1). That doesn't make sense, because you cannot take one variable through two paths to two different colors (without a lot of flashing). So instead you are really saying don't take two paths, take one path: the average path (or some other path).

The only way to do that deterministically is to have a function that can choose its next output. That is equivalent of applying a second function, say f, which takes as parameters, time and the possible paths (c and c') = f(t,c,c'). For an average path, f needs to know all paths to average among them.

Perhaps you could do a faster "choosing" function which just takes the first path or last path. But that did not match your requirements. (or if you have access to a quantum computer... ;-) )

Upvotes: 1

Related Questions