2013-09-20 22:23:13 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 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
|
|
|
|
2020-08-03 17:51:40 +08:00
|
|
|
#include "cocostudio/CCActionObject.h"
|
|
|
|
#include "cocostudio/CocoLoader.h"
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCActionInstant.h"
|
2014-07-14 20:45:24 +08:00
|
|
|
#include "base/ccUtils.h"
|
2014-01-17 13:35:58 +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
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
_pScheduler = Director::getInstance()->getScheduler();
|
|
|
|
CC_SAFE_RETAIN(_pScheduler);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ActionObject::~ActionObject()
|
|
|
|
{
|
2015-10-26 10:38:17 +08:00
|
|
|
_loop = false;
|
|
|
|
_pScheduler->unscheduleAllForTarget(this);
|
2014-06-18 15:55:01 +08:00
|
|
|
_actionNodeList.clear();
|
|
|
|
CC_SAFE_RELEASE(_pScheduler);
|
|
|
|
CC_SAFE_RELEASE(_CallBack);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::setName(const char* name)
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
_name.assign(name);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
const char* ActionObject::getName()
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
return _name.c_str();
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::setLoop(bool bLoop)
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
_loop = bLoop;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
bool ActionObject::getLoop()
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
return _loop;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::setUnitTime(float fTime)
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
_fUnitTime = fTime;
|
2013-12-27 17:21:13 +08:00
|
|
|
for(const auto &e : _actionNodeList)
|
2014-06-18 15:55:01 +08:00
|
|
|
{
|
2013-12-27 10:43:49 +08:00
|
|
|
e->setUnitTime(_fUnitTime);
|
2014-06-18 15:55:01 +08:00
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
float ActionObject::getUnitTime()
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
return _fUnitTime;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float ActionObject::getCurrentTime()
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
return _currentTime;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::setCurrentTime(float fTime)
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
_currentTime = fTime;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
float ActionObject::getTotalTime()
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
return _fTotalTime;
|
2013-12-24 20:33:55 +08:00
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
bool ActionObject::isPlaying()
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
return _bPlaying;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ActionObject::initWithDictionary(const rapidjson::Value& dic, Ref* root)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
setName(DICTOOL->getStringValue_json(dic, "name"));
|
|
|
|
setLoop(DICTOOL->getBooleanValue_json(dic, "loop"));
|
|
|
|
setUnitTime(DICTOOL->getFloatValue_json(dic, "unittime"));
|
|
|
|
int actionNodeCount = DICTOOL->getArrayCount_json(dic, "actionnodelist");
|
|
|
|
int maxLength = 0;
|
|
|
|
for (int i=0; i<actionNodeCount; i++) {
|
2014-08-28 07:31:57 +08:00
|
|
|
ActionNode* actionNode = new (std::nothrow) ActionNode();
|
2014-06-18 15:55:01 +08:00
|
|
|
actionNode->autorelease();
|
|
|
|
const rapidjson::Value& actionNodeDic = DICTOOL->getDictionaryFromArray_json(dic, "actionnodelist", i);
|
|
|
|
actionNode->initWithDictionary(actionNodeDic,root);
|
|
|
|
actionNode->setUnitTime(getUnitTime());
|
|
|
|
_actionNodeList.pushBack(actionNode);
|
|
|
|
|
|
|
|
int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
|
|
|
|
if(length > maxLength)
|
|
|
|
maxLength = length;
|
|
|
|
}
|
|
|
|
_fTotalTime = maxLength*_fUnitTime;
|
|
|
|
}
|
|
|
|
|
2014-06-23 10:02:09 +08:00
|
|
|
void ActionObject::initWithBinary(CocoLoader *cocoLoader,
|
|
|
|
stExpCocoNode *cocoNode,
|
2014-06-18 15:55:01 +08:00
|
|
|
cocos2d::Ref *root)
|
|
|
|
{
|
2014-07-01 16:31:17 +08:00
|
|
|
stExpCocoNode *stChildNode = cocoNode->GetChildArray(cocoLoader);
|
2014-06-23 10:12:54 +08:00
|
|
|
stExpCocoNode *actionNodeList = nullptr;
|
2014-06-23 10:02:09 +08:00
|
|
|
int count = cocoNode->GetChildNum();
|
2014-06-18 15:55:01 +08:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2014-06-23 10:02:09 +08:00
|
|
|
std::string key = stChildNode[i].GetName(cocoLoader);
|
2014-07-01 16:31:17 +08:00
|
|
|
std::string value = stChildNode[i].GetValue(cocoLoader);
|
2014-06-18 15:55:01 +08:00
|
|
|
if (key == "name") {
|
|
|
|
setName(value.c_str());
|
|
|
|
}else if (key == "loop"){
|
|
|
|
setLoop(valueToBool(value));
|
|
|
|
}else if(key == "unittime"){
|
|
|
|
setUnitTime(valueToFloat(value));
|
|
|
|
}else if (key == "actionnodelist"){
|
|
|
|
actionNodeList = &stChildNode[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 10:12:54 +08:00
|
|
|
if(nullptr != actionNodeList)
|
2014-06-18 15:55:01 +08:00
|
|
|
{
|
|
|
|
int actionNodeCount = actionNodeList->GetChildNum();
|
2014-07-01 16:31:17 +08:00
|
|
|
stExpCocoNode *actionNodeArray = actionNodeList->GetChildArray(cocoLoader);
|
2014-06-18 15:55:01 +08:00
|
|
|
int maxLength = 0;
|
|
|
|
for (int i=0; i<actionNodeCount; i++) {
|
2014-08-28 07:31:57 +08:00
|
|
|
ActionNode* actionNode = new (std::nothrow) ActionNode();
|
2014-06-18 15:55:01 +08:00
|
|
|
actionNode->autorelease();
|
|
|
|
|
2014-06-23 10:02:09 +08:00
|
|
|
actionNode->initWithBinary(cocoLoader, &actionNodeArray[i] , root);
|
2014-06-18 15:55:01 +08:00
|
|
|
|
|
|
|
actionNode->setUnitTime(getUnitTime());
|
|
|
|
|
|
|
|
_actionNodeList.pushBack(actionNode);
|
|
|
|
|
|
|
|
int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
|
|
|
|
if(length > maxLength)
|
|
|
|
maxLength = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_fTotalTime = maxLength* _fUnitTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 10:12:54 +08:00
|
|
|
int ActionObject::valueToInt(const std::string& value)
|
2014-06-18 15:55:01 +08:00
|
|
|
{
|
|
|
|
return atoi(value.c_str());
|
|
|
|
}
|
2014-06-23 10:12:54 +08:00
|
|
|
bool ActionObject::valueToBool(const std::string& value)
|
2014-06-18 15:55:01 +08:00
|
|
|
{
|
|
|
|
int intValue = valueToInt(value);
|
|
|
|
if (1 == intValue) {
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-06-23 10:12:54 +08:00
|
|
|
float ActionObject::valueToFloat(const std::string& value)
|
2014-06-18 15:55:01 +08:00
|
|
|
{
|
2014-07-14 20:45:24 +08:00
|
|
|
return utils::atof(value.c_str());
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::addActionNode(ActionNode* node)
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
if (node == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_actionNodeList.pushBack(node);
|
|
|
|
node->setUnitTime(_fUnitTime);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
void ActionObject::removeActionNode(ActionNode* node)
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
if (node == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_actionNodeList.eraseObject(node);
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionObject::play()
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
stop();
|
|
|
|
this->updateToFrameByTime(0.0f);
|
2013-12-27 17:21:13 +08:00
|
|
|
for(const auto &e : _actionNodeList)
|
2014-06-18 15:55:01 +08:00
|
|
|
{
|
|
|
|
e->playAction();
|
|
|
|
}
|
|
|
|
if (_loop)
|
|
|
|
{
|
2014-10-04 00:38:36 +08:00
|
|
|
_pScheduler->schedule(CC_SCHEDULE_SELECTOR(ActionObject::simulationActionUpdate), this, 0.0f , CC_REPEAT_FOREVER, 0.0f, false);
|
2014-06-18 15:55:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-04 00:38:36 +08:00
|
|
|
_pScheduler->schedule(CC_SCHEDULE_SELECTOR(ActionObject::simulationActionUpdate), this, 0.0f, false);
|
2014-06-18 15:55:01 +08:00
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
void ActionObject::play(CallFunc* func)
|
|
|
|
{
|
2014-06-18 15:55:01 +08:00
|
|
|
this->play();
|
2013-12-24 20:33:55 +08:00
|
|
|
this->_CallBack = func;
|
2014-05-08 10:52:31 +08:00
|
|
|
CC_SAFE_RETAIN(_CallBack);
|
2013-12-24 20:33:55 +08:00
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
void ActionObject::pause()
|
|
|
|
{
|
|
|
|
_bPause = true;
|
2014-06-14 08:26:30 +08:00
|
|
|
_bPlaying = false;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2014-06-14 08:26:30 +08:00
|
|
|
_bPlaying = false;
|
2014-10-04 00:38:36 +08:00
|
|
|
_pScheduler->unschedule(CC_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void ActionObject::simulationActionUpdate(float /*dt*/)
|
2013-11-08 14:34:24 +08:00
|
|
|
{
|
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();
|
|
|
|
}
|
2014-05-08 10:52:31 +08:00
|
|
|
else
|
|
|
|
{
|
2014-06-14 08:26:30 +08:00
|
|
|
_bPlaying = false;
|
2014-10-04 00:38:36 +08:00
|
|
|
_pScheduler->unschedule(CC_SCHEDULE_SELECTOR(ActionObject::simulationActionUpdate), this);
|
2014-05-08 10:52:31 +08:00
|
|
|
}
|
2013-11-08 14:34:24 +08:00
|
|
|
}
|
|
|
|
}
|
2014-06-14 08:26:30 +08:00
|
|
|
}
|