Ticket #131: blink.patch

File blink.patch, 6.9 KB (added by wraitii, 7 years ago)
  • binaries/data/mods/public/simulation/components/Capturable.js

    diff --git a/binaries/data/mods/public/simulation/components/Capturable.js b/binaries/data/mods/public/simulation/components/Capturable.js
    index 55fdde0..372a424 100644
    a b Capturable.prototype.RegisterCapturePointsChanged = function()  
    142142    Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp });
    143143
    144144    var owner = cmpOwnership.GetOwner();
    145     if (owner == -1 || this.cp[owner] > 0)
     145    if (owner == -1)
    146146        return;
    147147
     148    if (this.cp[owner] > 0)
     149    {
     150        this.UpdateBlinking();
     151        return;
     152    }
     153
    148154    // if all cp has been taken from the owner, convert it to the best player
    149155    var bestPlayer = 0;
    150156    for (let i in this.cp)
    Capturable.prototype.RegisterCapturePointsChanged = function()  
    160166    let cmpCapturedPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
    161167    if (cmpCapturedPlayerStatisticsTracker)
    162168        cmpCapturedPlayerStatisticsTracker.CapturedEntity(this.entity);
     169
     170    this.UpdateBlinking();
    163171};
    164172
     173Capturable.prototype.UpdateBlinking = function()
     174{
     175    let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     176    if (!cmpOwnership)
     177        return;
     178
     179    let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
     180    if (!cmpVisual)
     181        return;
     182
     183    let owner = cmpOwnership.GetOwner();
     184    if (this.cp[owner] == this.maxCp)
     185    {
     186        cmpVisual.ClearBlinking();
     187        return;
     188    }
     189
     190    let bestPlayer = 0;
     191    for (let i in this.cp)
     192        if (i != owner && this.cp[i] >= this.cp[bestPlayer])
     193            bestPlayer = +i;
     194
     195    let cmpPlayer = QueryPlayerIDInterface(bestPlayer);
     196    if (!cmpPlayer)
     197    {
     198        cmpVisual.ClearBlinking();
     199        return;
     200    }
     201    let color = cmpPlayer.GetColor();
     202    cmpVisual.SetBlinking(color.r, color.g, color.b, 0.4, true, 0.4 + this.cp[owner] / this.maxCp * 4.0);
     203}
     204
    165205Capturable.prototype.GetRegenRate = function()
    166206{
    167207    var regenRate = +this.template.RegenRate;
  • source/simulation2/components/CCmpVisualActor.cpp

    diff --git a/source/simulation2/components/CCmpVisualActor.cpp b/source/simulation2/components/CCmpVisualActor.cpp
    index f84dc51..fcfe2e2 100644
    a b private:  
    8383    fixed m_AnimSyncRepeatTime; // 0.0 if not synced
    8484    fixed m_AnimSyncOffsetTime;
    8585
     86    // blinking-related, can use float since this will be purely graphical.
     87    bool m_Blinking;
     88    bool m_BlinkingLoop;
     89    CColor m_BlinkingColor;
     90    float m_BlinkingTime;
     91    float m_BlinkingPeriod;
     92    double m_BlinkingStart;
     93
    8694    std::map<CStr, CStr> m_VariantSelections;
    8795
    8896    u32 m_Seed; // seed used for random variations
    public:  
    282290    {
    283291        switch (msg.GetType())
    284292        {
     293        case MT_Interpolate:
     294        {
     295            Interpolate();
     296            break;
     297        }
    285298        case MT_Update_Final:
    286299        {
    287300            const CMessageUpdate_Final& msgData = static_cast<const CMessageUpdate_Final&> (msg);
    public:  
    486499        }
    487500    }
    488501
     502    virtual void SetBlinking(float r, float g, float b, float blinkTime, bool loop, float blinkPeriod)
     503    {
     504        if (!m_Blinking)
     505            m_BlinkingStart = timer_Time();
     506
     507        m_BlinkingColor = CColor(r, g, b, 1.0f);
     508        m_Blinking = true;
     509        m_BlinkingTime = blinkTime;
     510        m_BlinkingLoop = loop;
     511        m_BlinkingPeriod = blinkPeriod;
     512
     513        // subscribe to interpolation.
     514        GetSimContext().GetComponentManager().DynamicSubscriptionNonsync(MT_Interpolate, this, true);
     515    }
     516
     517    virtual void ClearBlinking()
     518    {
     519        m_Blinking = false;
     520        GetSimContext().GetComponentManager().DynamicSubscriptionNonsync(MT_Interpolate, this, false);
     521    }
     522
    489523    virtual void SetVariable(const std::string& name, float value)
    490524    {
    491525        if (m_Unit)
    private:  
    536570    void ReloadUnitAnimation();
    537571
    538572    void Update(fixed turnLength);
     573
     574    void Interpolate();
    539575};
    540576
    541577REGISTER_COMPONENT_TYPE(VisualActor)
    void CCmpVisualActor::Update(fixed UNUSED(turnLength))  
    783819    SelectAnimation(name, false, speed, L"");
    784820    m_AnimRunThreshold = runThreshold;
    785821}
     822
     823void CCmpVisualActor::Interpolate()
     824{
     825    if (!m_Blinking)
     826    {
     827        ClearBlinking();
     828        return;
     829    }
     830    if (!m_Unit)
     831        return;
     832
     833    CModelAbstract& model = m_Unit->GetModel();
     834
     835    double time = fmod(timer_Time()-m_BlinkingStart, (double)(m_BlinkingTime+m_BlinkingPeriod));
     836
     837    if (time > m_BlinkingTime)
     838    {
     839        if (!m_BlinkingLoop)
     840        {
     841            ClearBlinking();
     842            return;
     843        }
     844        model.SetShadingColor(CColor(m_R.ToFloat(), m_G.ToFloat(), m_B.ToFloat(), 1.0f));
     845        return;
     846    }
     847
     848    CColor color(0.0f, 0.0f, 0.0f, 1.0f);
     849    float influence = (float)fabs(1.0f - time/(m_BlinkingTime/2.0f));
     850    color.r = m_R.ToFloat() * influence + m_BlinkingColor.r * (1.0f - influence);
     851    color.g = m_G.ToFloat() * influence + m_BlinkingColor.g * (1.0f - influence);
     852    color.b = m_B.ToFloat() * influence + m_BlinkingColor.b * (1.0f - influence);
     853    model.SetShadingColor(color);
     854}
     855
  • source/simulation2/components/ICmpVisual.cpp

    diff --git a/source/simulation2/components/ICmpVisual.cpp b/source/simulation2/components/ICmpVisual.cpp
    index e39942d..b9b9cd1 100644
    a b DEFINE_INTERFACE_METHOD_2("ReplaceMoveAnimation", void, ICmpVisual, ReplaceMoveA  
    3030DEFINE_INTERFACE_METHOD_1("SetAnimationSyncRepeat", void, ICmpVisual, SetAnimationSyncRepeat, fixed)
    3131DEFINE_INTERFACE_METHOD_1("SetAnimationSyncOffset", void, ICmpVisual, SetAnimationSyncOffset, fixed)
    3232DEFINE_INTERFACE_METHOD_4("SetShadingColor", void, ICmpVisual, SetShadingColor, fixed, fixed, fixed, fixed)
     33DEFINE_INTERFACE_METHOD_6("SetBlinking", void, ICmpVisual, SetBlinking, float, float, float, float, bool, float)
     34DEFINE_INTERFACE_METHOD_0("ClearBlinking", void, ICmpVisual, ClearBlinking)
    3335DEFINE_INTERFACE_METHOD_2("SetVariable", void, ICmpVisual, SetVariable, std::string, float)
    3436DEFINE_INTERFACE_METHOD_0("GetActorSeed", u32, ICmpVisual, GetActorSeed)
    3537DEFINE_INTERFACE_METHOD_1("SetActorSeed", void, ICmpVisual, SetActorSeed, u32)
  • source/simulation2/components/ICmpVisual.h

    diff --git a/source/simulation2/components/ICmpVisual.h b/source/simulation2/components/ICmpVisual.h
    index 6b11fdc..6a5b8b3 100644
    a b public:  
    143143    virtual void SetShadingColor(fixed r, fixed g, fixed b, fixed a) = 0;
    144144
    145145    /**
     146     * Set the actor to blink a certain color. Purely graphical.
     147     * @param r red component, expected range [0, 1]
     148     * @param g green component, expected range [0, 1]
     149     * @param b blue component, expected range [0, 1]
     150     * @param loop whether the blinking stops after one period
     151     * @param blinkTime how long should a "blink" take
     152     * @param blinkPeriod how long between two blinks. Irrelevant if not looping
     153     */
     154    virtual void SetBlinking(float r, float g, float b, float blinkTime, bool loop = false, float blinkPeriod = 0.0f) = 0;
     155
     156    /**
     157     * Stop blinking
     158     */
     159    virtual void ClearBlinking() = 0;
     160
     161    /**
    146162     * Set an arbitrarily-named variable that the model may use to alter its appearance
    147163     * (e.g. in particle emitter parameter computations).
    148164     */