File size: 6,138 Bytes
e1191aa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | /****************************************************************************
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.
****************************************************************************/
#include "CCSequence3D.h"
NS_CC_BEGIN
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
Sequence3D* Sequence3D::variadicCreate(Animate3D *action1, ...)
{
va_list params;
va_start(params, action1);
Sequence3D *ret = Sequence3D::createWithVariableList(action1, params);
va_end(params);
return ret;
}
#else
Sequence3D* Sequence3D::create(Animate3D *action1, ...)
{
va_list params;
va_start(params, action1);
Sequence3D *ret = Sequence3D::createWithVariableList(action1, params);
va_end(params);
return ret;
}
#endif
Sequence3D* Sequence3D::createWithVariableList(Animate3D *action1, va_list args)
{
Animate3D *now;
Vector<Animate3D*> actions;
if (action1)
actions.pushBack(action1);
while (action1)
{
now = va_arg(args, Animate3D*);
if (now)
{
actions.pushBack(now);
}
else
break;
}
return create(actions);
}
Sequence3D* Sequence3D::create(const Vector<Animate3D*>& arrayOfActions)
{
Sequence3D* ret = nullptr;
ret = new (std::nothrow) Sequence3D();
if (ret->initWithActions(arrayOfActions))
{
ret->autorelease();
return ret;
}
CC_SAFE_RELEASE(ret);
return ret;
}
bool Sequence3D::initWithActions(const Vector<Animate3D*>& arrayOfActions)
{
if (arrayOfActions.size() == 0)
return false;
_actions.clear();
_startTime.clear();
_endTime.clear();
Animate3D* preAction = nullptr;
float duration = 0.f, dur = 0.f;
_startTime.push_back(duration);
for (auto& it : arrayOfActions) {
float d1 = 0.f;
if (preAction)
d1 = preAction->getDuration();
float d2 = it->getDuration();
if (d1 < Animate3D::getTransitionTime() || d2 < Animate3D::getTransitionTime())
dur = d1 > d2 ? d1 : d2;
else
dur = d1 + d2 - Animate3D::getTransitionTime();
preAction = it;
_actions.pushBack(it);
_startTime.push_back(dur);
_endTime.push_back(dur);
duration += dur;
}
_startTime.erase(_startTime.end() - 1);
for (ssize_t i = 0; i < _startTime.size(); i++) {
_startTime[i] /= duration;
_endTime[i] /= duration;
}
setDuration(duration);
_curActionIdx = -1;
return true;
}
Sequence3D* Sequence3D::clone() const
{
// no copy constructor
auto seq3d = new (std::nothrow) Sequence3D();
seq3d->initWithActions(_actions);
seq3d->autorelease();
return seq3d;
}
Sequence3D::Sequence3D()
: _curActionIdx(-1)
{
}
Sequence3D::~Sequence3D(void)
{
_actions.clear();
}
void Sequence3D::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);
_curActionIdx = -1;
}
void Sequence3D::stop(void)
{
for (auto& action : _actions) {
action->stop();
}
ActionInterval::stop();
}
Sequence3D* Sequence3D::reverse() const
{
Vector<Animate3D*> actions;
for (auto it = _actions.rbegin(); it != _actions.rend(); ++it) {
actions.pushBack(*it);
}
return create(actions);
}
void Sequence3D::update(float t)
{
if (_target)
{
int nextIdx = _curActionIdx + 1;
if (nextIdx < _actions.size())
{
if (t >= _startTime[nextIdx])
{
_actions.at(nextIdx)->startWithTarget(_target);
}
}
//update action
int preActionIdx = _curActionIdx - 1;
if (preActionIdx >= 0 && _endTime[preActionIdx] > t)
{
float newt = (t - _startTime[preActionIdx]) / (_endTime[preActionIdx] - _startTime[preActionIdx]);
_actions.at(preActionIdx)->update(newt);
}
if (_curActionIdx >= 0 && _curActionIdx < _actions.size())
{
float newt = (t - _startTime[_curActionIdx]) / (_endTime[_curActionIdx] - _startTime[_curActionIdx]);
_actions.at(_curActionIdx)->update(newt);
}
}
}
void Sequence3D::step(float t)
{
ActionInterval::step(t) ;
if (_target)
{
float newt=MAX(0, MIN(1, _elapsed /MAX(_duration, FLT_EPSILON)));
int nextIdx = _curActionIdx + 1;
if (nextIdx < _actions.size())
{
if (newt >= _startTime[nextIdx])
{
_curActionIdx = nextIdx;
}
}
//update action
int preActionIdx = _curActionIdx - 1;
if (preActionIdx >= 0 && _endTime[preActionIdx] >newt)
{
_actions.at(preActionIdx)->step(t);
}
if (_curActionIdx >= 0 && _curActionIdx < _actions.size())
{
_actions.at(_curActionIdx)->step(t);
}
}
}
NS_CC_END
|