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/CCAnimation3D.h"
|
|
|
|
#include "3d/CCSprite3D.h"
|
|
|
|
#include "3d/CCMeshSkin.h"
|
2014-06-04 18:17:09 +08:00
|
|
|
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
//create Animate3D using Animation.
|
|
|
|
Animate3D* Animate3D::create(Animation3D* animation)
|
|
|
|
{
|
|
|
|
auto animate = new Animate3D();
|
|
|
|
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);
|
|
|
|
CCASSERT(sprite && sprite->getSkin() && _animation, "Animate3D apply to Sprite3D only");
|
|
|
|
|
|
|
|
ActionInterval::startWithTarget(target);
|
|
|
|
|
|
|
|
_boneCurves.clear();
|
|
|
|
auto skin = sprite->getSkin();
|
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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("warning: bone %s not find in animation", bone->getName().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! 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-07-09 09:53:15 +08:00
|
|
|
if (_target && _weight > 0.f)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2014-06-13 19:20:19 +08:00
|
|
|
float transDst[3], rotDst[4], scaleDst[3];
|
|
|
|
float* trans = nullptr, *rot = nullptr, *scale = nullptr;
|
2014-07-09 09:53:15 +08:00
|
|
|
if (_playReverse)
|
2014-06-04 18:17:09 +08:00
|
|
|
t = 1 - t;
|
|
|
|
|
2014-06-20 12:01:54 +08:00
|
|
|
t = _start + t * _last;
|
2014-06-04 18:17:09 +08:00
|
|
|
for (const auto& it : _boneCurves) {
|
|
|
|
auto bone = it.first;
|
|
|
|
auto curve = it.second;
|
|
|
|
if (curve->translateCurve)
|
|
|
|
{
|
2014-06-18 14:01:57 +08:00
|
|
|
curve->translateCurve->evaluate(t, transDst, EvaluateType::INT_LINEAR);
|
2014-06-13 19:20:19 +08:00
|
|
|
trans = &transDst[0];
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
if (curve->rotCurve)
|
|
|
|
{
|
2014-06-18 14:01:57 +08:00
|
|
|
curve->rotCurve->evaluate(t, rotDst, EvaluateType::INT_QUAT_SLERP);
|
2014-06-13 19:20:19 +08:00
|
|
|
rot = &rotDst[0];
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
if (curve->scaleCurve)
|
|
|
|
{
|
2014-06-18 14:01:57 +08:00
|
|
|
curve->scaleCurve->evaluate(t, scaleDst, EvaluateType::INT_LINEAR);
|
2014-06-13 19:20:19 +08:00
|
|
|
scale = &scaleDst[0];
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-06-20 17:55:25 +08:00
|
|
|
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:53:15 +08:00
|
|
|
: _absSpeed(1)
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
Animate3D::~Animate3D()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_animation);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|