//! called before the action start. It will also set the target. void Animate3D::startWithTarget(Node *target) { Sprite3D* sprite = dynamic_cast<Sprite3D*>(target); CCASSERT(sprite && sprite->getSkeleton() && _animation, "Animate3D apply to Sprite3D only"); ActionInterval::startWithTarget(target); _boneCurves.clear(); auto skin = sprite->getSkeleton(); bool hasCurve = false; for (int i = 0; i < skin->getBoneCount(); i++) { auto bone = skin->getBoneByIndex(static_cast<unsigned int>(i)); auto curve = _animation->getBoneCurveByName(bone->getName()); if (curve) { _boneCurves[bone] = curve; hasCurve = true; } } if (!hasCurve) { CCLOG("warning: no animation finde for the skeleton"); } auto runningAction = s_runningAnimates.find(sprite); if (runningAction != s_runningAnimates.end()) { //make the running action fade out auto action = (*runningAction).second; if (action != this) { if (_transTime < 0.001f) { s_runningAnimates[sprite] = this; _state = Animate3D::Animate3DState::Running; _weight = 1.0f; } else { s_fadeOutAnimates[sprite] = action; action->_state = Animate3D::Animate3DState::FadeOut; action->_accTransTime = 0.0f; action->_weight = 1.0f; action->_lastTime = 0.f; s_fadeInAnimates[sprite] = this; _accTransTime = 0.0f; _state = Animate3D::Animate3DState::FadeIn; _weight = 0.f; _lastTime = 0.f; } } } else { s_runningAnimates[sprite] = this; _state = Animate3D::Animate3DState::Running; _weight = 1.0f; } }
//! called before the action start. It will also set the target. void Animate3D::startWithTarget(Node *target) { bool needReMap = (_target != target); ActionInterval::startWithTarget(target); if (needReMap) { _boneCurves.clear(); _nodeCurves.clear(); bool hasCurve = false; Sprite3D* sprite = dynamic_cast<Sprite3D*>(target); if(sprite) { if (_animation) { const std::unordered_map<std::string, Animation3D::Curve*>& boneCurves = _animation->getBoneCurves(); for (const auto& iter: boneCurves) { const std::string& boneName = iter.first; auto skin = sprite->getSkeleton(); if(skin) { auto bone = skin->getBoneByName(boneName); if (bone) { auto curve = _animation->getBoneCurveByName(boneName); _boneCurves[bone] = curve; hasCurve = true; } else { Node* node = nullptr; if (target->getName() == boneName) node = target; else node = findChildByNameRecursively(target, boneName); if (node) { auto curve = _animation->getBoneCurveByName(boneName); if (curve) { _nodeCurves[node] = curve; hasCurve = true; } } } } } } } else { const std::unordered_map<std::string, Animation3D::Curve*>& boneCurves = _animation->getBoneCurves(); for (const auto& iter: boneCurves) { const std::string& boneName = iter.first; Node* node = nullptr; if (target->getName() == boneName) node = target; else node = findChildByNameRecursively(target, boneName); if (node) { auto curve = _animation->getBoneCurveByName(boneName); if (curve) { _nodeCurves[node] = curve; hasCurve = true; } } } } if (!hasCurve) { CCLOG("warning: no animation found for the skeleton"); } } auto runningAction = s_runningAnimates.find(target); if (runningAction != s_runningAnimates.end()) { //make the running action fade out auto action = (*runningAction).second; if (action != this) { if (_transTime < 0.001f) { s_runningAnimates[target] = this; _state = Animate3D::Animate3DState::Running; _weight = 1.0f; } else { s_fadeOutAnimates[target] = action; action->_state = Animate3D::Animate3DState::FadeOut; action->_accTransTime = 0.0f; action->_weight = 1.0f; action->_lastTime = 0.f; s_runningAnimates.erase(target); s_fadeInAnimates[target] = this; _accTransTime = 0.0f; _state = Animate3D::Animate3DState::FadeIn; _weight = 0.f; _lastTime = 0.f; } } } else { auto it = s_fadeInAnimates.find(target); if (it != s_fadeInAnimates.end()) { s_fadeInAnimates.erase(it); } s_runningAnimates[target] = this; _state = Animate3D::Animate3DState::Running; _weight = 1.0f; } }