Ticket #4270: fix-visualactor-save-reload.patch

File fix-visualactor-save-reload.patch, 6.2 KB (added by Itms, 8 years ago)
  • source/simulation2/components/CCmpVisualActor.cpp

     
    8181    std::wstring m_SoundGroup;
    8282    fixed m_AnimDesync;
    8383    fixed m_AnimSyncRepeatTime; // 0.0 if not synced
     84    fixed m_AnimSyncOffsetTime;
    8485
    8586    std::map<CStr, CStr> m_VariantSelections;
    8687
     
    205206       
    206207        InitModel(paramNode);
    207208
    208         // We need to select animation even if graphics are disabled, as this modifies serialized state
    209209        SelectAnimation("idle", false, fixed::FromInt(1), L"");
    210210    }
    211211
     
    232232        serialize.String("sound group", m_SoundGroup, 0, 256);
    233233        serialize.NumberFixed_Unbounded("anim desync", m_AnimDesync);
    234234        serialize.NumberFixed_Unbounded("anim sync repeat time", m_AnimSyncRepeatTime);
     235        serialize.NumberFixed_Unbounded("anim sync offset time", m_AnimSyncOffsetTime);
    235236
    236237        SerializeMap<SerializeString, SerializeString>()(serialize, "variation", m_VariantSelections);
    237238
     
    264265        // If we serialized a different seed or different actor, reload actor
    265266        if (oldSeed != GetActorSeed() || m_BaseActorName != m_ActorName)
    266267            ReloadActor();
    267         else if (m_Unit)
    268             m_Unit->SetEntitySelection(m_VariantSelections);
    269 
    270         fixed repeattime = m_AnimSyncRepeatTime; // save because SelectAnimation overwrites it
    271 
    272         if (m_AnimRunThreshold.IsZero())
    273             SelectAnimation(m_AnimName, m_AnimOnce, m_AnimSpeed, m_SoundGroup);
    274268        else
    275             SelectMovementAnimation(m_AnimRunThreshold);
     269            ReloadUnitAnimation();
    276270
    277         SetAnimationSyncRepeat(repeattime);
    278 
    279271        if (m_Unit)
    280272        {
    281273            CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle());
     
    287279    virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
    288280    {
    289281        // Quick exit for running in non-graphical mode
    290         if (m_Unit == NULL)
     282        if (!m_Unit)
    291283            return;
    292284
    293285        switch (msg.GetType())
     
    459451    {
    460452        m_AnimRunThreshold = runThreshold;
    461453
    462         if (m_Unit)
    463         {
    464             SetVariant("animation", "walk");
    465             if (m_Unit->GetAnimation())
    466                 m_Unit->GetAnimation()->SetAnimationState("walk", false, 1.f, 0.f, L"");
    467         }
     454        // TODO: Rename that function since it only sets the threshold
    468455    }
    469456
    470457    virtual void SetAnimationSyncRepeat(fixed repeattime)
     
    480467
    481468    virtual void SetAnimationSyncOffset(fixed actiontime)
    482469    {
     470        m_AnimSyncOffsetTime = actiontime;
     471
    483472        if (m_Unit)
    484473        {
    485474            if (m_Unit->GetAnimation())
    486                 m_Unit->GetAnimation()->SetAnimationSyncOffset(actiontime.ToFloat());
     475                m_Unit->GetAnimation()->SetAnimationSyncOffset(m_AnimSyncOffsetTime.ToFloat());
    487476        }
    488477    }
    489478
     
    546535    /// Helper method; initializes the model selection shape descriptor from XML. Factored out for readability of @ref Init.
    547536    void InitSelectionShapeDescriptor(const CParamNode& paramNode);
    548537
     538    // ReloadActor is used when the actor or seed changes.
    549539    void ReloadActor();
     540    // ReloadUnitAnimation is used for a minimal reloading upon deserialization, when the actor and seed are identical.
     541    // It is also used by ReloadActor.
     542    void ReloadUnitAnimation();
    550543
    551544    void Update(fixed turnLength);
    552545};
     
    720713
    721714    InitModel(node->GetChild("VisualActor"));
    722715
    723     m_Unit->SetEntitySelection(m_VariantSelections);
     716    ReloadUnitAnimation();
    724717
    725     if (m_Unit->GetAnimation())
    726         m_Unit->GetAnimation()->SetAnimationState(m_AnimName, m_AnimOnce, m_AnimSpeed.ToFloat(), m_AnimDesync.ToFloat(), m_SoundGroup.c_str());
    727 
    728     // We'll lose the exact synchronisation but we should at least make sure it's going at the correct rate
    729     if (!m_AnimSyncRepeatTime.IsZero())
    730         if (m_Unit->GetAnimation())
    731             m_Unit->GetAnimation()->SetAnimationSyncRepeat(m_AnimSyncRepeatTime.ToFloat());
    732 
    733718    m_Unit->GetModel().SetShadingColor(shading);
    734719
    735720    m_Unit->GetModel().SetPlayerID(playerID);
     
    743728    }
    744729}
    745730
    746 void CCmpVisualActor::Update(fixed UNUSED(turnLength))
     731void CCmpVisualActor::ReloadUnitAnimation()
    747732{
    748733    if (!m_Unit)
    749734        return;
    750735
     736    m_Unit->SetEntitySelection(m_VariantSelections);
     737
     738    if (!m_Unit->GetAnimation())
     739        return;
     740
     741    m_Unit->GetAnimation()->SetAnimationState(m_AnimName, m_AnimOnce, m_AnimSpeed.ToFloat(), m_AnimDesync.ToFloat(), m_SoundGroup.c_str());
     742
     743    // We'll lose the exact synchronisation but we should at least make sure it's going at the correct rate
     744    if (!m_AnimSyncRepeatTime.IsZero())
     745        m_Unit->GetAnimation()->SetAnimationSyncRepeat(m_AnimSyncRepeatTime.ToFloat());
     746    if (!m_AnimSyncOffsetTime.IsZero())
     747        m_Unit->GetAnimation()->SetAnimationSyncOffset(m_AnimSyncOffsetTime.ToFloat());
     748}
     749
     750void CCmpVisualActor::Update(fixed UNUSED(turnLength))
     751{
    751752    // If we're in the special movement mode, select an appropriate animation
    752     if (!m_AnimRunThreshold.IsZero())
    753     {
    754         CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle());
    755         if (!cmpPosition || !cmpPosition->IsInWorld())
    756             return;
     753    if (m_AnimRunThreshold.IsZero())
     754        return;
    757755
    758         CmpPtr<ICmpUnitMotion> cmpUnitMotion(GetEntityHandle());
    759         if (!cmpUnitMotion)
    760             return;
     756    CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle());
     757    if (!cmpPosition || !cmpPosition->IsInWorld())
     758        return;
    761759
    762         float speed = cmpUnitMotion->GetCurrentSpeed().ToFloat();
     760    CmpPtr<ICmpUnitMotion> cmpUnitMotion(GetEntityHandle());
     761    if (!cmpUnitMotion)
     762        return;
    763763
    764         std::string name;
    765         if (speed == 0.0f)
    766             name = "idle";
    767         else
    768             name = (speed < m_AnimRunThreshold.ToFloat()) ? "walk" : "run";
     764    fixed speed = cmpUnitMotion->GetCurrentSpeed();
     765    std::string name;
    769766
    770         std::map<std::string, std::string>::const_iterator it = m_AnimOverride.find(name);
    771         if (it != m_AnimOverride.end())
    772             name = it->second;
     767    if (speed.IsZero())
     768    {
     769        speed = fixed::FromFloat(1.f);
     770        name = "idle";
     771    }
     772    else
     773        name = (speed < m_AnimRunThreshold) ? "walk" : "run";
    773774
    774         SetVariant("animation", name);
    775         if (m_Unit->GetAnimation())
    776         {
    777             if (speed == 0.0f)
    778                 m_Unit->GetAnimation()->SetAnimationState(name, false, 1.f, 0.f, L"");
    779             else
    780                 m_Unit->GetAnimation()->SetAnimationState(name, false, speed, 0.f, L"");
    781         }
    782     }
     775    std::map<std::string, std::string>::const_iterator it = m_AnimOverride.find(name);
     776    if (it != m_AnimOverride.end())
     777        name = it->second;
     778
     779    SelectAnimation(name, false, speed, L"");
    783780}