2013-09-20 22:23:13 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-26 16:29:59 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "cocostudio/CCActionObject.h"
|
|
|
|
#include "cocostudio/DictionaryHelper.h"
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-11-08 14:34:24 +08:00
|
|
|
using namespace cocos2d;
|
2013-10-15 18:00:03 +08:00
|
|
|
|
|
|
|
namespace cocostudio {
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
ActionObject::ActionObject()
|
2013-12-26 23:40:50 +08:00
|
|
|
: _name("")
|
2013-09-20 22:23:13 +08:00
|
|
|
, _loop(false)
|
|
|
|
, _bPause(false)
|
|
|
|
, _bPlaying(false)
|
|
|
|
, _fUnitTime(0.1f)
|
|
|
|
, _currentTime(0.0f)
|
2013-12-26 23:40:50 +08:00
|
|
|
, _pScheduler(nullptr)
|
|
|
|
, _CallBack(nullptr)
|
2013-12-24 20:33:55 +08:00
|
|
|
, _fTotalTime(0.0f)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-11-19 00:57:22 +08:00
|
|
|
_pScheduler = Director::getInstance()->getScheduler();
|
|
|
|
CC_SAFE_RETAIN(_pScheduler);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ActionObject::~ActionObject()
|
|
|
|
{
|
2013-12-25 19:46:09 +08:00
|
|
|
_actionNodeList.clear();
|
2013-11-19 00:57:22 +08:00
|
|
|
CC_SAFE_RELEASE(_pScheduler);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::setName(const char* name)
|
|
|
|
{
|
|
|
|
_name.assign(name);
|
|
|
|
}
|
|
|
|
const char* ActionObject::getName()
|
|
|
|
{
|
|
|
|
return _name.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::setLoop(bool bLoop)
|
|
|
|
{
|
|
|
|
_loop = bLoop;
|
|
|
|
}
|
|
|
|
bool ActionObject::getLoop()
|
|
|
|
{
|
|
|
|
return _loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::setUnitTime(float fTime)
|
|
|
|
{
|
|
|
|
_fUnitTime = fTime;
|
2013-12-27 17:21:13 +08:00
|
|
|
for(const auto &e : _actionNodeList)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-12-27 10:43:49 +08:00
|
|
|
e->setUnitTime(_fUnitTime);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
float ActionObject::getUnitTime()
|
|
|
|
{
|
|
|
|
return _fUnitTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ActionObject::getCurrentTime()
|
|
|
|
{
|
|
|
|
return _currentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::setCurrentTime(float fTime)
|
|
|
|
{
|
|
|
|
_currentTime = fTime;
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
float ActionObject::getTotalTime()
|
|
|
|
{
|
|
|
|
return _fTotalTime;
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
bool ActionObject::isPlaying()
|
|
|
|
{
|
|
|
|
return _bPlaying;
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
void ActionObject::initWithDictionary(const rapidjson::Value& dic, Object* root)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-12-26 16:29:59 +08:00
|
|
|
setName(DICTOOL->getStringValue_json(dic, "name"));
|
|
|
|
setLoop(DICTOOL->getBooleanValue_json(dic, "loop"));
|
2013-09-20 22:23:13 +08:00
|
|
|
setUnitTime(DICTOOL->getFloatValue_json(dic, "unittime"));
|
2013-12-26 16:29:59 +08:00
|
|
|
int actionNodeCount = DICTOOL->getArrayCount_json(dic, "actionnodelist");
|
2013-12-24 20:33:55 +08:00
|
|
|
int maxLength = 0;
|
2013-12-26 16:29:59 +08:00
|
|
|
for (int i=0; i<actionNodeCount; i++) {
|
|
|
|
ActionNode* actionNode = new ActionNode();
|
2013-09-20 22:23:13 +08:00
|
|
|
actionNode->autorelease();
|
2013-12-24 20:33:55 +08:00
|
|
|
const rapidjson::Value& actionNodeDic = DICTOOL->getDictionaryFromArray_json(dic, "actionnodelist", i);
|
2013-12-26 16:29:59 +08:00
|
|
|
actionNode->initWithDictionary(actionNodeDic,root);
|
2013-09-20 22:23:13 +08:00
|
|
|
actionNode->setUnitTime(getUnitTime());
|
2013-12-26 16:29:59 +08:00
|
|
|
_actionNodeList.pushBack(actionNode);
|
2013-12-24 20:33:55 +08:00
|
|
|
|
|
|
|
int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
|
|
|
|
if(length > maxLength)
|
|
|
|
maxLength = length;
|
2013-12-26 16:29:59 +08:00
|
|
|
}
|
2013-12-24 20:33:55 +08:00
|
|
|
_fTotalTime = maxLength*_fTotalTime;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::addActionNode(ActionNode* node)
|
|
|
|
{
|
2013-12-26 23:40:50 +08:00
|
|
|
if (node == nullptr)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-25 19:46:09 +08:00
|
|
|
_actionNodeList.pushBack(node);
|
2013-09-20 22:23:13 +08:00
|
|
|
node->setUnitTime(_fUnitTime);
|
|
|
|
}
|
|
|
|
void ActionObject::removeActionNode(ActionNode* node)
|
|
|
|
{
|
2013-12-26 23:40:50 +08:00
|
|
|
if (node == nullptr)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-25 19:46:09 +08:00
|
|
|
_actionNodeList.eraseObject(node);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::play()
|
|
|
|
{
|
2013-12-26 16:29:59 +08:00
|
|
|
stop();
|
2013-11-08 14:34:24 +08:00
|
|
|
this->updateToFrameByTime(0.0f);
|
2013-12-27 17:21:13 +08:00
|
|
|
for(const auto &e : _actionNodeList)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
e->playAction();
|
2013-11-08 14:34:24 +08:00
|
|
|
}
|
|
|
|
if (_loop)
|
|
|
|
{
|
|
|
|
_pScheduler->scheduleSelector(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f , kRepeatForever, 0.0f, false);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-12-24 20:33:55 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_pScheduler->scheduleSelector(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f, false);
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
void ActionObject::play(CallFunc* func)
|
|
|
|
{
|
|
|
|
this->play();
|
|
|
|
this->_CallBack = func;
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
void ActionObject::pause()
|
|
|
|
{
|
|
|
|
_bPause = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::stop()
|
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
for(const auto &e : _actionNodeList)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
e->stopAction();
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-11-08 14:34:24 +08:00
|
|
|
_pScheduler->unscheduleSelector(schedule_selector(ActionObject::simulationActionUpdate), this);
|
2013-09-20 22:23:13 +08:00
|
|
|
_bPause = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::updateToFrameByTime(float fTime)
|
|
|
|
{
|
|
|
|
_currentTime = fTime;
|
2013-12-27 17:21:13 +08:00
|
|
|
for(const auto &e : _actionNodeList)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
e->updateActionToTimeLine(fTime);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 14:34:24 +08:00
|
|
|
void ActionObject::simulationActionUpdate(float dt)
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
bool isEnd = true;
|
2013-12-27 17:21:13 +08:00
|
|
|
|
|
|
|
for(const auto &e : _actionNodeList)
|
2013-11-08 14:34:24 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
if (!e->isActionDoneOnce())
|
2013-11-08 14:34:24 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
isEnd = false;
|
|
|
|
break;
|
2013-11-08 14:34:24 +08:00
|
|
|
}
|
2013-12-24 20:33:55 +08:00
|
|
|
}
|
2013-12-27 17:21:13 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
if (isEnd)
|
|
|
|
{
|
2013-12-26 23:40:50 +08:00
|
|
|
if (_CallBack != nullptr)
|
2013-12-24 20:33:55 +08:00
|
|
|
{
|
|
|
|
_CallBack->execute();
|
|
|
|
}
|
|
|
|
if (_loop)
|
2013-11-08 14:34:24 +08:00
|
|
|
{
|
|
|
|
this->play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|