2014-06-04 18:17:09 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-17 15:28:14 +08:00
|
|
|
#include "3d/CCAnimate3D.h"
|
|
|
|
#include "3d/CCSprite3D.h"
|
2014-07-30 18:32:34 +08:00
|
|
|
#include "3d/CCSkeleton3D.h"
|
2014-06-04 18:17:09 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-08-28 18:53:15 +08:00
|
|
|
std::unordered_map<Sprite3D*, Animate3D*> Animate3D::s_fadeInAnimates;
|
|
|
|
std::unordered_map<Sprite3D*, Animate3D*> Animate3D::s_fadeOutAnimates;
|
|
|
|
std::unordered_map<Sprite3D*, Animate3D*> Animate3D::s_runningAnimates;
|
2014-08-29 18:25:42 +08:00
|
|
|
float Animate3D::_transTime = 0.1f;
|
2014-08-28 18:53:15 +08:00
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
//create Animate3D using Animation.
|
|
|
|
Animate3D* Animate3D::create(Animation3D* animation)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto animate = new (std::nothrow) Animate3D();
|
2014-06-04 18:17:09 +08:00
|
|
|
animate->_animation = animation;
|
|
|
|
animation->retain();
|
|
|
|
|
|
|
|
animate->autorelease();
|
2014-06-09 18:37:58 +08:00
|
|
|
animate->setDuration(animation->getDuration());
|
2014-06-04 18:17:09 +08:00
|
|
|
|
|
|
|
return animate;
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:54:09 +08:00
|
|
|
Animate3D* Animate3D::create(Animation3D* animation, float fromTime, float duration)
|
2014-06-20 12:01:54 +08:00
|
|
|
{
|
2014-06-20 18:54:09 +08:00
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
|
|
|
|
float fullDuration = animation->getDuration();
|
2014-06-20 12:01:54 +08:00
|
|
|
if (duration > fullDuration - fromTime)
|
|
|
|
duration = fullDuration - fromTime;
|
|
|
|
|
2014-06-20 18:54:09 +08:00
|
|
|
animate->_start = fromTime / fullDuration;
|
|
|
|
animate->_last = duration / fullDuration;
|
2014-06-26 18:35:12 +08:00
|
|
|
animate->setDuration(duration);
|
2014-06-20 12:01:54 +08:00
|
|
|
|
2014-06-20 18:54:09 +08:00
|
|
|
return animate;
|
2014-06-20 12:01:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
/** returns a clone of action */
|
|
|
|
Animate3D* Animate3D::clone() const
|
|
|
|
{
|
|
|
|
auto animate = const_cast<Animate3D*>(this);
|
2014-06-17 19:18:56 +08:00
|
|
|
auto copy = Animate3D::create(animate->_animation);
|
2014-06-04 18:17:09 +08:00
|
|
|
|
2014-07-09 09:53:15 +08:00
|
|
|
copy->_absSpeed = _absSpeed;
|
2014-06-20 17:55:25 +08:00
|
|
|
copy->_weight = _weight;
|
2014-06-04 18:17:09 +08:00
|
|
|
copy->_elapsed = _elapsed;
|
2014-06-20 12:01:54 +08:00
|
|
|
copy->_start = _start;
|
|
|
|
copy->_last = _last;
|
2014-07-09 09:53:15 +08:00
|
|
|
copy->_playReverse = _playReverse;
|
2014-06-26 18:35:12 +08:00
|
|
|
copy->setDuration(animate->getDuration());
|
2014-06-04 18:17:09 +08:00
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns a new action that performs the exactly the reverse action */
|
|
|
|
Animate3D* Animate3D::reverse() const
|
|
|
|
{
|
|
|
|
auto animate = clone();
|
2014-07-09 09:53:15 +08:00
|
|
|
animate->_playReverse = !animate->_playReverse;
|
2014-06-04 18:17:09 +08:00
|
|
|
return animate;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! called before the action start. It will also set the target.
|
|
|
|
void Animate3D::startWithTarget(Node *target)
|
|
|
|
{
|
|
|
|
Sprite3D* sprite = dynamic_cast<Sprite3D*>(target);
|
2014-08-16 16:56:04 +08:00
|
|
|
CCASSERT(sprite && sprite->getSkeleton() && _animation, "Animate3D apply to Sprite3D only");
|
2014-06-04 18:17:09 +08:00
|
|
|
|
|
|
|
ActionInterval::startWithTarget(target);
|
|
|
|
|
|
|
|
_boneCurves.clear();
|
2014-08-16 16:56:04 +08:00
|
|
|
auto skin = sprite->getSkeleton();
|
2014-08-26 14:19:08 +08:00
|
|
|
bool hasCurve = false;
|
2014-06-26 21:58:26 +08:00
|
|
|
for (unsigned int i = 0; i < skin->getBoneCount(); i++) {
|
2014-06-04 18:17:09 +08:00
|
|
|
auto bone = skin->getBoneByIndex(i);
|
|
|
|
auto curve = _animation->getBoneCurveByName(bone->getName());
|
|
|
|
if (curve)
|
|
|
|
{
|
|
|
|
_boneCurves[bone] = curve;
|
2014-08-26 14:19:08 +08:00
|
|
|
hasCurve = true;
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-08-26 14:19:08 +08:00
|
|
|
}
|
|
|
|
if (!hasCurve)
|
|
|
|
{
|
|
|
|
CCLOG("warning: no animation finde for the skeleton");
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-08-28 18:53:15 +08:00
|
|
|
|
|
|
|
auto runningAction = s_runningAnimates.find(sprite);
|
|
|
|
if (runningAction != s_runningAnimates.end())
|
|
|
|
{
|
|
|
|
//make the running action fade out
|
|
|
|
auto action = (*runningAction).second;
|
2014-08-29 18:25:42 +08:00
|
|
|
if (action != this)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2014-08-28 18:53:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_runningAnimates[sprite] = this;
|
|
|
|
_state = Animate3D::Animate3DState::Running;
|
|
|
|
_weight = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3D::stop()
|
|
|
|
{
|
2014-09-02 10:40:29 +08:00
|
|
|
removeFromMap();
|
2014-08-28 18:53:15 +08:00
|
|
|
|
|
|
|
ActionInterval::stop();
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//! called every frame with it's delta time. DON'T override unless you know what you are doing.
|
|
|
|
void Animate3D::step(float dt)
|
|
|
|
{
|
2014-07-09 09:53:15 +08:00
|
|
|
ActionInterval::step(dt * _absSpeed);
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3D::update(float t)
|
|
|
|
{
|
2014-08-28 18:53:15 +08:00
|
|
|
if (_target)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2014-08-29 18:25:42 +08:00
|
|
|
if (_state == Animate3D::Animate3DState::FadeIn && _lastTime > 0.f)
|
2014-08-28 18:53:15 +08:00
|
|
|
{
|
|
|
|
_accTransTime += (t - _lastTime) * getDuration();
|
|
|
|
|
|
|
|
_weight = _accTransTime / _transTime;
|
|
|
|
if (_weight >= 1.0f)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2014-08-28 18:53:15 +08:00
|
|
|
_accTransTime = _transTime;
|
|
|
|
_weight = 1.0f;
|
2014-08-29 18:25:42 +08:00
|
|
|
_state = Animate3D::Animate3DState::Running;
|
2014-08-28 18:53:15 +08:00
|
|
|
Sprite3D* sprite = static_cast<Sprite3D*>(_target);
|
|
|
|
s_fadeInAnimates.erase(sprite);
|
|
|
|
s_runningAnimates[sprite] = this;
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-08-28 18:53:15 +08:00
|
|
|
}
|
2014-08-29 18:25:42 +08:00
|
|
|
else if (_state == Animate3D::Animate3DState::FadeOut && _lastTime > 0.f)
|
2014-08-28 18:53:15 +08:00
|
|
|
{
|
|
|
|
_accTransTime += (t - _lastTime) * getDuration();
|
|
|
|
|
|
|
|
_weight = 1 - _accTransTime / _transTime;
|
|
|
|
if (_weight <= 0.0f)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2014-08-28 18:53:15 +08:00
|
|
|
_accTransTime = _transTime;
|
|
|
|
_weight = 0.0f;
|
|
|
|
|
|
|
|
Sprite3D* sprite = static_cast<Sprite3D*>(_target);
|
|
|
|
s_fadeOutAnimates.erase(sprite);
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-08-28 18:53:15 +08:00
|
|
|
}
|
|
|
|
_lastTime = t;
|
|
|
|
|
|
|
|
if (_weight > 0.0f)
|
|
|
|
{
|
|
|
|
float transDst[3], rotDst[4], scaleDst[3];
|
|
|
|
float* trans = nullptr, *rot = nullptr, *scale = nullptr;
|
|
|
|
if (_playReverse)
|
|
|
|
t = 1 - t;
|
|
|
|
|
|
|
|
t = _start + t * _last;
|
|
|
|
for (const auto& it : _boneCurves) {
|
|
|
|
auto bone = it.first;
|
|
|
|
auto curve = it.second;
|
|
|
|
if (curve->translateCurve)
|
|
|
|
{
|
|
|
|
curve->translateCurve->evaluate(t, transDst, EvaluateType::INT_LINEAR);
|
|
|
|
trans = &transDst[0];
|
|
|
|
}
|
|
|
|
if (curve->rotCurve)
|
|
|
|
{
|
|
|
|
curve->rotCurve->evaluate(t, rotDst, EvaluateType::INT_QUAT_SLERP);
|
|
|
|
rot = &rotDst[0];
|
|
|
|
}
|
|
|
|
if (curve->scaleCurve)
|
|
|
|
{
|
|
|
|
curve->scaleCurve->evaluate(t, scaleDst, EvaluateType::INT_LINEAR);
|
|
|
|
scale = &scaleDst[0];
|
|
|
|
}
|
|
|
|
bone->setAnimationValue(trans, rot, scale, this, _weight);
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 09:53:15 +08:00
|
|
|
float Animate3D::getSpeed() const
|
|
|
|
{
|
|
|
|
return _playReverse ? -_absSpeed : _absSpeed;
|
|
|
|
}
|
|
|
|
void Animate3D::setSpeed(float speed)
|
|
|
|
{
|
|
|
|
_absSpeed = fabsf(speed);
|
|
|
|
_playReverse = speed < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3D::setWeight(float weight)
|
|
|
|
{
|
|
|
|
CCASSERT(weight >= 0.0f, "invalid weight");
|
|
|
|
_weight = fabsf(weight);
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
Animate3D::Animate3D()
|
2014-07-09 09:59:18 +08:00
|
|
|
: _absSpeed(1.f)
|
2014-06-13 19:20:19 +08:00
|
|
|
, _weight(1.f)
|
2014-06-20 12:01:54 +08:00
|
|
|
, _start(0.f)
|
|
|
|
, _last(1.f)
|
2014-06-04 18:17:09 +08:00
|
|
|
, _animation(nullptr)
|
2014-07-09 09:59:18 +08:00
|
|
|
, _playReverse(false)
|
2014-08-28 18:53:15 +08:00
|
|
|
, _state(Animate3D::Animate3DState::Running)
|
|
|
|
, _accTransTime(0.0f)
|
|
|
|
, _lastTime(0.0f)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
Animate3D::~Animate3D()
|
|
|
|
{
|
2014-09-02 10:40:29 +08:00
|
|
|
removeFromMap();
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
CC_SAFE_RELEASE(_animation);
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:40:29 +08:00
|
|
|
void Animate3D::removeFromMap()
|
|
|
|
{
|
|
|
|
//remove this action from map
|
|
|
|
if (_target)
|
|
|
|
{
|
|
|
|
Sprite3D* sprite = static_cast<Sprite3D*>(_target);
|
|
|
|
if (_state == Animate3D::Animate3DState::FadeIn)
|
|
|
|
s_fadeInAnimates.erase(sprite);
|
|
|
|
else if (_state == Animate3D::Animate3DState::FadeOut)
|
|
|
|
s_fadeOutAnimates.erase(sprite);
|
|
|
|
else
|
|
|
|
s_runningAnimates.erase(sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
NS_CC_END
|