Browse Source

Remove ignore network update, add animated network attribute check.

Aster Jian 11 years ago
parent
commit
0de5de0678

+ 82 - 29
Source/Engine/Scene/Animatable.cpp

@@ -70,48 +70,89 @@ void Animatable::SetObjectAnimation(ObjectAnimation* objectAnimation)
 
 
 void Animatable::SetAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation)
 void Animatable::SetAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation)
 {
 {
-    const AttributeAnimation* currentAnimation = GetAttributeAnimation(name);
-    if (currentAnimation == attributeAnimation)
-        return;
+    const AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
 
 
     if (attributeAnimation)
     if (attributeAnimation)
     {
     {
-        const Vector<AttributeInfo>* attributes = GetAttributes();
-        if (!attributes)
-        {
-            LOGERROR(GetTypeName() + " has no attributes");
+        if (currentInstance && attributeAnimation == currentInstance->GetAttributeAnimation())
             return;
             return;
-        }
 
 
-        for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
+        // Get attribute info
+        const AttributeInfo* attributeInfo = 0;
+        if (currentInstance)
+            attributeInfo = &currentInstance->GetAttributeInfo();
+        else
         {
         {
-            const AttributeInfo& attributeInfo = *i;
-            if (!attributeInfo.name_.Compare(name, true))
+            const Vector<AttributeInfo>* attributes = GetAttributes();
+            if (!attributes)
             {
             {
-                if (attributeAnimation->GetValueType() == attributeInfo.type_)
-                {
-                    SharedPtr<AttributeAnimationInstance> attributeAnimationInstance(new AttributeAnimationInstance(this, attributeInfo, attributeAnimation));
-                    attributeAnimationInstances_[name] = attributeAnimationInstance;
+                LOGERROR(GetTypeName() + " has no attributes");
+                return;
+            }
 
 
-                    if (!currentAnimation)
-                        OnAttributeAnimationAdded();
-                }
-                else
+            for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
+            {
+                if (name == (*i).name_)
                 {
                 {
-                    LOGERROR("Error value type");
-                    return;
+                    attributeInfo = &(*i);
+                    break;
                 }
                 }
-
             }
             }
         }
         }
+
+        if (!attributeInfo)
+        {
+            LOGERROR("Invalid name: " + name);
+            return;
+        }
+
+        // Check value type is same with attribute type
+        if (attributeAnimation->GetValueType() != attributeInfo->type_)
+        {
+            LOGERROR("Invalid value type");
+            return;
+        }
+
+        // Add network attribute to set
+        if (attributeInfo->mode_ & AM_NET)
+        {
+            const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
+            for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
+            {
+                if (name == (*i).name_)
+                {
+                    animatedNetworkAttributes_.Insert(&(*i));
+                    break;
+                }
+            }            
+        }
+
+        attributeAnimationInstances_[name] = new AttributeAnimationInstance(this, *attributeInfo, attributeAnimation);
+
+        if (!currentInstance)
+            OnAttributeAnimationAdded();
     }
     }
     else
     else
     {
     {
-        if (currentAnimation)
+        if (!currentInstance)
+            return;
+
+        // Remove network attribute from set
+        if (currentInstance->GetAttributeInfo().mode_ & AM_NET)
         {
         {
-            attributeAnimationInstances_.Erase(name);
-            OnAttributeAnimationRemoved();
+            const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
+            for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
+            {
+                if (name == (*i).name_)
+                {
+                    animatedNetworkAttributes_.Erase(&(*i));
+                    break;
+                }
+            }            
         }
         }
+
+        attributeAnimationInstances_.Erase(name);
+        OnAttributeAnimationRemoved();
     }
     }
 }
 }
 
 
@@ -122,10 +163,8 @@ const ObjectAnimation* Animatable::GetObjectAnimation() const
 
 
 const AttributeAnimation* Animatable::GetAttributeAnimation(const String& name) const
 const AttributeAnimation* Animatable::GetAttributeAnimation(const String& name) const
 {
 {
-    HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Find(name);
-    if (i != attributeAnimationInstances_.End())
-        return i->second_->GetAttributeAnimation();
-    return 0;
+    const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
+    return instance ? instance->GetAttributeAnimation() : 0;
 }
 }
 
 
 void Animatable::SetObjectAnimationAttr(ResourceRef value)
 void Animatable::SetObjectAnimationAttr(ResourceRef value)
@@ -181,4 +220,18 @@ void Animatable::UpdateAttributeAnimations(float timeStep)
         i->second_->Update(timeStep);
         i->second_->Update(timeStep);
 }
 }
 
 
+bool Animatable::IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const
+{
+    return animatedNetworkAttributes_.Find(&attrInfo) != animatedNetworkAttributes_.End();
+}
+
+const AttributeAnimationInstance* Animatable::GetAttributeAnimationInstance(const String& name) const
+{
+    HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Find(name);
+    if (i != attributeAnimationInstances_.End())
+        return i->second_;
+
+    return 0;
+}
+
 }
 }

+ 7 - 0
Source/Engine/Scene/Animatable.h

@@ -22,6 +22,7 @@
 
 
 #pragma once
 #pragma once
 
 
+#include "HashSet.h"
 #include "Serializable.h"
 #include "Serializable.h"
 
 
 namespace Urho3D
 namespace Urho3D
@@ -74,11 +75,17 @@ protected:
     virtual void OnAttributeAnimationRemoved() = 0;
     virtual void OnAttributeAnimationRemoved() = 0;
     /// Update attribute animations.
     /// Update attribute animations.
     void UpdateAttributeAnimations(float timeStep);
     void UpdateAttributeAnimations(float timeStep);
+    /// Is animated network attribute.
+    bool IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const;
+    /// Return attribute animation instance.
+    const AttributeAnimationInstance* GetAttributeAnimationInstance(const String& name) const;
 
 
     /// Animation enabled.
     /// Animation enabled.
     bool animationEnabled_;
     bool animationEnabled_;
     /// Animation.
     /// Animation.
     SharedPtr<ObjectAnimation> objectAnimation_;
     SharedPtr<ObjectAnimation> objectAnimation_;
+    /// Animated network attribute set.
+    HashSet<const AttributeInfo*> animatedNetworkAttributes_;
     /// Attribute animation instances.
     /// Attribute animation instances.
     HashMap<String, SharedPtr<AttributeAnimationInstance> > attributeAnimationInstances_;
     HashMap<String, SharedPtr<AttributeAnimationInstance> > attributeAnimationInstances_;
 };
 };

+ 3 - 1
Source/Engine/Scene/AttributeAnimation.h

@@ -102,7 +102,7 @@ public:
     /// Return value type.
     /// Return value type.
     VariantType GetValueType() const { return valueType_; }
     VariantType GetValueType() const { return valueType_; }
     /// Is interpolatable.
     /// Is interpolatable.
-    bool IsInterpolatable() const;
+    bool IsInterpolatable() const { return isInterpolatable_; }
     /// Return begin time.
     /// Return begin time.
     float GetBeginTime() const { return beginTime_; }
     float GetBeginTime() const { return beginTime_; }
     /// Return end time.
     /// Return end time.
@@ -111,6 +111,8 @@ public:
     float CalculateScaledTime(float currentTime) const;
     float CalculateScaledTime(float currentTime) const;
     /// Return all key frames.
     /// Return all key frames.
     const Vector<AttributeKeyFrame>& GetKeyFrames() const { return keyframes_; }
     const Vector<AttributeKeyFrame>& GetKeyFrames() const { return keyframes_; }
+    /// Has event frames.
+    bool HasEventFrames() const { return eventFrames_.Size() != 0; }
     /// Return all event frames between time.
     /// Return all event frames between time.
     void GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const;
     void GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const;
 
 

+ 27 - 21
Source/Engine/Scene/AttributeAnimationInstance.cpp

@@ -59,9 +59,12 @@ void AttributeAnimationInstance::Update(float timeStep)
         return;
         return;
     
     
     currentTime_ += timeStep;
     currentTime_ += timeStep;
-    float scaledTime = attributeAnimation_->CalculateScaledTime(currentTime_);
-
+    
     const Vector<AttributeKeyFrame>& keyFrames = attributeAnimation_->GetKeyFrames();
     const Vector<AttributeKeyFrame>& keyFrames = attributeAnimation_->GetKeyFrames();
+    if (keyFrames.Size() < 2)
+        return;
+
+    float scaledTime = attributeAnimation_->CalculateScaledTime(currentTime_);
     for (unsigned i = 1; i < keyFrames.Size(); ++i)
     for (unsigned i = 1; i < keyFrames.Size(); ++i)
     {
     {
         const AttributeKeyFrame& currKeyFrame = keyFrames[i];
         const AttributeKeyFrame& currKeyFrame = keyFrames[i];
@@ -76,29 +79,32 @@ void AttributeAnimationInstance::Update(float timeStep)
         }
         }
     }
     }
 
 
-    Vector<const AttributeEventFrame*> eventFrames;
-    switch (attributeAnimation_->GetCycleMode())
+    if (attributeAnimation_->HasEventFrames())
     {
     {
-    case CM_LOOP:
-        if (lastScaledTime_ < scaledTime)
-            attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
-        else
+        Vector<const AttributeEventFrame*> eventFrames;
+        switch (attributeAnimation_->GetCycleMode())
         {
         {
-            attributeAnimation_->GetEventFrames(lastScaledTime_, attributeAnimation_->GetEndTime(), eventFrames);
-            attributeAnimation_->GetEventFrames(attributeAnimation_->GetBeginTime(), scaledTime, eventFrames);
+        case CM_LOOP:
+            if (lastScaledTime_ < scaledTime)
+                attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
+            else
+            {
+                attributeAnimation_->GetEventFrames(lastScaledTime_, attributeAnimation_->GetEndTime(), eventFrames);
+                attributeAnimation_->GetEventFrames(attributeAnimation_->GetBeginTime(), scaledTime, eventFrames);
+            }
+            break;
+        case CM_CLAMP:
+            attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
+            break;
+
+        case CM_PINGPONG:
+            // Not implement
+            break;
         }
         }
-        break;
-    case CM_CLAMP:
-        attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
-        break;
-
-    case CM_PINGPONG:
-        // Not implement
-        break;
-    }
 
 
-    for (unsigned i = 0; i < eventFrames.Size(); ++i)
-        animatable_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
+        for (unsigned i = 0; i < eventFrames.Size(); ++i)
+            animatable_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
+    }
 
 
     lastScaledTime_ = scaledTime;
     lastScaledTime_ = scaledTime;
 }
 }

+ 6 - 5
Source/Engine/Scene/Component.cpp

@@ -37,8 +37,7 @@ Component::Component(Context* context) :
     node_(0),
     node_(0),
     id_(0),
     id_(0),
     networkUpdate_(false),
     networkUpdate_(false),
-    enabled_(true),
-    ignoreNetworkUpdate_(false)
+    enabled_(true)
 {
 {
 }
 }
 
 
@@ -144,6 +143,10 @@ void Component::PrepareNetworkUpdate()
     for (unsigned i = 0; i < numAttributes; ++i)
     for (unsigned i = 0; i < numAttributes; ++i)
     {
     {
         const AttributeInfo& attr = attributes->At(i);
         const AttributeInfo& attr = attributes->At(i);
+        
+        if (animationEnabled_ && IsAnimatedNetworkAttribute(attr))
+            continue;
+
         OnGetAttribute(attr, networkState_->currentValues_[i]);
         OnGetAttribute(attr, networkState_->currentValues_[i]);
 
 
         if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
         if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
@@ -185,7 +188,7 @@ void Component::CleanupConnection(Connection* connection)
 
 
 void Component::MarkNetworkUpdate()
 void Component::MarkNetworkUpdate()
 {
 {
-    if (!ignoreNetworkUpdate_ &&  !networkUpdate_ && id_ < FIRST_LOCAL_ID)
+    if (!networkUpdate_ && id_ < FIRST_LOCAL_ID)
     {
     {
         Scene* scene = GetScene();
         Scene* scene = GetScene();
         if (scene)
         if (scene)
@@ -241,8 +244,6 @@ void Component::HandleScenePostUpdate(StringHash eventType, VariantMap& eventDat
 {
 {
     using namespace ScenePostUpdate;
     using namespace ScenePostUpdate;
 
 
-    ignoreNetworkUpdate_ = true;
     UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
     UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
-    ignoreNetworkUpdate_ = false;
 }
 }
 }
 }

+ 0 - 2
Source/Engine/Scene/Component.h

@@ -120,8 +120,6 @@ protected:
     bool networkUpdate_;
     bool networkUpdate_;
     /// Enabled flag.
     /// Enabled flag.
     bool enabled_;
     bool enabled_;
-    /// Ignore network update.
-    bool ignoreNetworkUpdate_;
 };
 };
 
 
 template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }
 template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }

+ 6 - 5
Source/Engine/Scene/Node.cpp

@@ -52,8 +52,7 @@ Node::Node(Context* context) :
     rotation_(Quaternion::IDENTITY),
     rotation_(Quaternion::IDENTITY),
     scale_(Vector3::ONE),
     scale_(Vector3::ONE),
     worldRotation_(Quaternion::IDENTITY),
     worldRotation_(Quaternion::IDENTITY),
-    owner_(0),
-    ignoreNetworkUpdate_(false)
+    owner_(0)
 {
 {
 }
 }
 
 
@@ -1265,6 +1264,10 @@ void Node::PrepareNetworkUpdate()
     for (unsigned i = 0; i < numAttributes; ++i)
     for (unsigned i = 0; i < numAttributes; ++i)
     {
     {
         const AttributeInfo& attr = attributes->At(i);
         const AttributeInfo& attr = attributes->At(i);
+
+        if (animationEnabled_ && IsAnimatedNetworkAttribute(attr))
+            continue;
+
         OnGetAttribute(attr, networkState_->currentValues_[i]);
         OnGetAttribute(attr, networkState_->currentValues_[i]);
 
 
         if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
         if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
@@ -1333,7 +1336,7 @@ void Node::CleanupConnection(Connection* connection)
 
 
 void Node::MarkNetworkUpdate()
 void Node::MarkNetworkUpdate()
 {
 {
-    if (!ignoreNetworkUpdate_ && !networkUpdate_ && scene_ && id_ < FIRST_LOCAL_ID)
+    if (!networkUpdate_ && scene_ && id_ < FIRST_LOCAL_ID)
     {
     {
         scene_->MarkNetworkUpdate(this);
         scene_->MarkNetworkUpdate(this);
         networkUpdate_ = true;
         networkUpdate_ = true;
@@ -1655,9 +1658,7 @@ void Node::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
 {
 {
     using namespace ScenePostUpdate;
     using namespace ScenePostUpdate;
     
     
-    ignoreNetworkUpdate_ = true;
     UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
     UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
-    ignoreNetworkUpdate_ = false;
 }
 }
 
 
 }
 }

+ 0 - 2
Source/Engine/Scene/Node.h

@@ -449,8 +449,6 @@ private:
     StringHash nameHash_;
     StringHash nameHash_;
     /// Attribute buffer for network updates.
     /// Attribute buffer for network updates.
     mutable VectorBuffer attrBuffer_;
     mutable VectorBuffer attrBuffer_;
-    /// Ignore network update.
-    bool ignoreNetworkUpdate_;
 };
 };
 
 
 template <class T> T* Node::CreateComponent(CreateMode mode, unsigned id) { return static_cast<T*>(CreateComponent(T::GetTypeStatic(), mode, id)); }
 template <class T> T* Node::CreateComponent(CreateMode mode, unsigned id) { return static_cast<T*>(CreateComponent(T::GetTypeStatic(), mode, id)); }