Ticket #4270: fix-visualactor-save-reload.patch
File fix-visualactor-save-reload.patch, 6.2 KB (added by , 8 years ago) |
---|
-
source/simulation2/components/CCmpVisualActor.cpp
81 81 std::wstring m_SoundGroup; 82 82 fixed m_AnimDesync; 83 83 fixed m_AnimSyncRepeatTime; // 0.0 if not synced 84 fixed m_AnimSyncOffsetTime; 84 85 85 86 std::map<CStr, CStr> m_VariantSelections; 86 87 … … 205 206 206 207 InitModel(paramNode); 207 208 208 // We need to select animation even if graphics are disabled, as this modifies serialized state209 209 SelectAnimation("idle", false, fixed::FromInt(1), L""); 210 210 } 211 211 … … 232 232 serialize.String("sound group", m_SoundGroup, 0, 256); 233 233 serialize.NumberFixed_Unbounded("anim desync", m_AnimDesync); 234 234 serialize.NumberFixed_Unbounded("anim sync repeat time", m_AnimSyncRepeatTime); 235 serialize.NumberFixed_Unbounded("anim sync offset time", m_AnimSyncOffsetTime); 235 236 236 237 SerializeMap<SerializeString, SerializeString>()(serialize, "variation", m_VariantSelections); 237 238 … … 264 265 // If we serialized a different seed or different actor, reload actor 265 266 if (oldSeed != GetActorSeed() || m_BaseActorName != m_ActorName) 266 267 ReloadActor(); 267 else if (m_Unit)268 m_Unit->SetEntitySelection(m_VariantSelections);269 270 fixed repeattime = m_AnimSyncRepeatTime; // save because SelectAnimation overwrites it271 272 if (m_AnimRunThreshold.IsZero())273 SelectAnimation(m_AnimName, m_AnimOnce, m_AnimSpeed, m_SoundGroup);274 268 else 275 SelectMovementAnimation(m_AnimRunThreshold);269 ReloadUnitAnimation(); 276 270 277 SetAnimationSyncRepeat(repeattime);278 279 271 if (m_Unit) 280 272 { 281 273 CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle()); … … 287 279 virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) 288 280 { 289 281 // Quick exit for running in non-graphical mode 290 if ( m_Unit == NULL)282 if (!m_Unit) 291 283 return; 292 284 293 285 switch (msg.GetType()) … … 459 451 { 460 452 m_AnimRunThreshold = runThreshold; 461 453 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 468 455 } 469 456 470 457 virtual void SetAnimationSyncRepeat(fixed repeattime) … … 480 467 481 468 virtual void SetAnimationSyncOffset(fixed actiontime) 482 469 { 470 m_AnimSyncOffsetTime = actiontime; 471 483 472 if (m_Unit) 484 473 { 485 474 if (m_Unit->GetAnimation()) 486 m_Unit->GetAnimation()->SetAnimationSyncOffset( actiontime.ToFloat());475 m_Unit->GetAnimation()->SetAnimationSyncOffset(m_AnimSyncOffsetTime.ToFloat()); 487 476 } 488 477 } 489 478 … … 546 535 /// Helper method; initializes the model selection shape descriptor from XML. Factored out for readability of @ref Init. 547 536 void InitSelectionShapeDescriptor(const CParamNode& paramNode); 548 537 538 // ReloadActor is used when the actor or seed changes. 549 539 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(); 550 543 551 544 void Update(fixed turnLength); 552 545 }; … … 720 713 721 714 InitModel(node->GetChild("VisualActor")); 722 715 723 m_Unit->SetEntitySelection(m_VariantSelections);716 ReloadUnitAnimation(); 724 717 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 rate729 if (!m_AnimSyncRepeatTime.IsZero())730 if (m_Unit->GetAnimation())731 m_Unit->GetAnimation()->SetAnimationSyncRepeat(m_AnimSyncRepeatTime.ToFloat());732 733 718 m_Unit->GetModel().SetShadingColor(shading); 734 719 735 720 m_Unit->GetModel().SetPlayerID(playerID); … … 743 728 } 744 729 } 745 730 746 void CCmpVisualActor:: Update(fixed UNUSED(turnLength))731 void CCmpVisualActor::ReloadUnitAnimation() 747 732 { 748 733 if (!m_Unit) 749 734 return; 750 735 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 750 void CCmpVisualActor::Update(fixed UNUSED(turnLength)) 751 { 751 752 // 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; 757 755 758 CmpPtr<ICmpUnitMotion> cmpUnitMotion(GetEntityHandle());759 if (!cmpUnitMotion)760 756 CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle()); 757 if (!cmpPosition || !cmpPosition->IsInWorld()) 758 return; 761 759 762 float speed = cmpUnitMotion->GetCurrentSpeed().ToFloat(); 760 CmpPtr<ICmpUnitMotion> cmpUnitMotion(GetEntityHandle()); 761 if (!cmpUnitMotion) 762 return; 763 763 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; 769 766 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"; 773 774 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""); 783 780 }