From 8ef13c8089c29c93a263a993a12137ff9447b289 Mon Sep 17 00:00:00 2001 From: TankorSmash Date: Sat, 4 Jul 2015 22:04:40 -0400 Subject: [PATCH 1/2] fix deprecation, change class name --- .../code/shatter/ensShatterNode.h | 133 +++++++++--------- 1 file changed, 64 insertions(+), 69 deletions(-) diff --git a/effectNodes-for-cocos2dx-3x/code/shatter/ensShatterNode.h b/effectNodes-for-cocos2dx-3x/code/shatter/ensShatterNode.h index 8b32113..d21b090 100644 --- a/effectNodes-for-cocos2dx-3x/code/shatter/ensShatterNode.h +++ b/effectNodes-for-cocos2dx-3x/code/shatter/ensShatterNode.h @@ -1,90 +1,85 @@ - - #ifndef HelloWorld_ensShatterNode_h #define HelloWorld_ensShatterNode_h #include -#include using namespace std; #include "cocos2d.h" using namespace cocos2d; -#include "ensCommon.h" -namespace_ens_begin -namespace shatter{ -class Cfrag:public CCSprite +namespace shatter { -public: - int m_randomNumber; - Cfrag(){ - m_randomNumber=-1; - } - bool initWithTexture(Texture2D* texture){ - this->CCSprite::initWithTexture(texture); - return true; - } -}; + class ShatterFrag : public Sprite + { + public: + int m_randomNumber; + ShatterFrag() { m_randomNumber = -1; } + bool initWithTexture(Texture2D* texture) + { + this->CCSprite::initWithTexture(texture); + return true; + } + }; } namespace shatter{ - class CshatterAction; + class ShatterAction; } -class CshatterSprite:public CCSprite + +class ShatterSprite:public Sprite { public: - friend class shatter::CshatterAction; -public: - CshatterSprite(){ - m_gridSideLen=2.7; - m_initalFrageScale=4.5; - m_fragBatchNode=NULL; - } - virtual~CshatterSprite(){} - bool init(const string&texFileName); -protected: - void createShatter(); - void resetShatter(); - void updateShatterAction(float time,float dt,float growSpeedOfTargetR); + friend class shatter::ShatterAction; + + ShatterSprite(){ + m_gridSideLen = 2.7f; + m_initalFragScale = 4.5f; + m_fragBatchNode = NULL; + } + virtual ~ShatterSprite(){} + bool init(const string&texFileName); protected: - float m_gridSideLen;//grid side length, also frag size - float m_initalFrageScale;//we want to make frags a litte bigger at start time. - vector > m_grid;//hold all the frags - CCSpriteBatchNode* m_fragBatchNode;//all frags add to this batchNode + void createShatter(); + void resetShatter(); + void updateShatterAction(float time, float dt, float growSpeedOfTargetR); + + float m_gridSideLen;//grid side length, also frag size + float m_initalFragScale;//we want to make frags a litte bigger at start time. + vector > m_grid;//hold all the frags + SpriteBatchNode* m_fragBatchNode;//all frags add to this batchNode }; -namespace shatter{ -class CshatterAction : public CCActionInterval -//ref to CCRotateBy -//why use action: http://user.qzone.qq.com/350479720/blog/1403318648 +namespace shatter { -public: - //creates the action - static CshatterAction* create(float fDuration); - //initializes the action - bool initWithDuration(float fDuration); - virtual void stop(); - virtual void startWithTarget(CCNode *pTarget); - virtual void update(float time); - virtual bool isDone(); - CshatterAction(){initMembers();} - virtual~CshatterAction(){ - } -protected: - void initMembers(){ - m_timeFoe=0; - m_timeCur=0; - - m_growSpeedOfTargetR=50; - } -protected: - float m_timeFoe; - float m_timeCur; -protected: - - float m_growSpeedOfTargetR; - -}; - } -namespace_ens_end + class ShatterAction : public ActionInterval + { + public: + //creates the action + static ShatterAction* create(float duration); + //initializes the action + bool initWithDuration(float duration); + + virtual void startWithTarget(Node *pTarget); + virtual void update(float time); + virtual void stop(); + virtual bool isDone(); + + ShatterAction(){ initMembers(); } + virtual~ShatterAction(){ } + + protected: + void initMembers(){ + m_timeFoe = 0; + m_timeCur = 0; + + m_growSpeedOfTargetR = 50; + } + + float m_timeFoe; + float m_timeCur; + + float m_growSpeedOfTargetR; + + }; +} #endif From f4346579cc3c0239071fbb860fc495c8fa4876c7 Mon Sep 17 00:00:00 2001 From: TankorSmash Date: Sat, 4 Jul 2015 22:05:20 -0400 Subject: [PATCH 2/2] Update ensShatterNode.cpp --- .../code/shatter/ensShatterNode.cpp | 295 ++++++++++-------- 1 file changed, 158 insertions(+), 137 deletions(-) diff --git a/effectNodes-for-cocos2dx-3x/code/shatter/ensShatterNode.cpp b/effectNodes-for-cocos2dx-3x/code/shatter/ensShatterNode.cpp index c95c568..2fe4ca9 100644 --- a/effectNodes-for-cocos2dx-3x/code/shatter/ensShatterNode.cpp +++ b/effectNodes-for-cocos2dx-3x/code/shatter/ensShatterNode.cpp @@ -1,165 +1,186 @@ +#include "ensShatterNode.h" +using namespace shatter; +bool ShatterSprite::init(const string&texFileName){ + this->Sprite::initWithFile(texFileName.c_str()); + createShatter(); + return true; +} -#include "ensShatterNode.h" -namespace_ens_begin - using namespace shatter; - -bool CshatterSprite::init(const string&texFileName){ - this->CCSprite::initWithFile(texFileName.c_str()); - createShatter(); - return true; - } - void CshatterSprite::createShatter(){ - //----calculate grid size and fragCount - CCSize contentSize=this->getContentSize(); - const int nRow=(int)floorf(contentSize.height/m_gridSideLen);//grid row count - const int nCol=(int)floorf(contentSize.width/m_gridSideLen);//grid col count - const int fragCount=nRow*nCol; - //----create fragBatchNode - m_fragBatchNode=CCSpriteBatchNode::createWithTexture(this->getTexture(),fragCount); - this->addChild(m_fragBatchNode); - m_fragBatchNode->setVisible(false); - //----create frags and add them to fragBatchNode and grid - //make m_grid - m_grid.resize(nRow); - for(int i=0;iautorelease(); - frag->initWithTexture(this->getTexture()); - //set to grid - m_grid[i][j]=frag; - //add to batchNode - m_fragBatchNode->addChild(frag); - //random - frag->m_randomNumber=rand(); - } - } - } - void CshatterSprite::resetShatter(){ - CCSize contentSize=this->getContentSize(); - int nRow=(int)m_grid.size(); - int nCol=(nRow==0?0:(int)m_grid[0].size()); - const float halfGridSideLen=0.5*m_gridSideLen; - for(int i=0;isetTextureRect(CCRect(x-halfGridSideLen,(contentSize.height-y)-halfGridSideLen,m_gridSideLen,m_gridSideLen)); - //set position - frag->setPosition(ccp(x,y)); - //scale - frag->setScale(m_initalFrageScale); - //opacity - frag->setOpacity(255); - //visible - frag->setVisible(true); - } - } - - } - void CshatterSprite::updateShatterAction(float time,float dt,float growSpeedOfTargetR){ - //update frags - CCSize contentSize=this->getContentSize(); - CCPoint center=CCPoint(contentSize.width/2,contentSize.height/2); - float initalTargetR=ccpLength(ccp(contentSize.width,contentSize.height))/2;//radius of srounding circle - int nRow=(int)m_grid.size(); - int nCol=nRow?(int)m_grid[0].size():0; - for(int i=0;igetOpacity()==0||frag->getScale()==0){ - frag->setVisible(false); - continue; - } - //current target R - float targetR=initalTargetR+time*growSpeedOfTargetR; - //update postion - CCPoint fragPos=frag->getPosition(); - float disToCenter=ccpLength(fragPos-center); - CCPoint dir; - if(disToCenter==0){ - dir=CCPoint(0,0); - }else{ - dir=fragPos-center; - dir.x/=disToCenter; - dir.y/=disToCenter; - } - float disToEdge=targetR-disToCenter; - float disToEdgeWithRandom=disToEdge+frag->m_randomNumber%(int)initalTargetR-initalTargetR/2;//add random to disToEdge - float movLen=disToEdgeWithRandom*0.0333;//we only move some percent of disToEdgeWithRandom - CCPoint movVec=ccpMult(dir,movLen); - frag->setPosition(fragPos+movVec); - //update opacity - float opacity=MAX(0,255-255*disToCenter/initalTargetR); - frag->setOpacity(opacity); - //update scale - frag->setScale(MAX(0,m_initalFrageScale-m_initalFrageScale*disToCenter/initalTargetR)); - } - } - } +void ShatterSprite::createShatter(){ + //----calculate grid size and fragCount + Size contentSize = this->getContentSize(); + const int nRow = (int)floorf(contentSize.height/m_gridSideLen);//grid row count + const int nCol = (int)floorf(contentSize.width/m_gridSideLen);//grid col count + const int fragCount = nRow*nCol; + //----create fragBatchNode + m_fragBatchNode = SpriteBatchNode::createWithTexture(this->getTexture(),fragCount); + this->addChild(m_fragBatchNode); + m_fragBatchNode->setVisible(false); + //----create frags and add them to fragBatchNode and grid + //make m_grid + m_grid.resize(nRow); + for(int i = 0; i < nRow; i++){ + m_grid[i].resize(nCol); + } + + const float halfGridSideLen = 0.5 * m_gridSideLen; + + for (int i = 0; i < nRow; i++){ + for (int j = 0; j < nCol; j++){ + ShatterFrag* frag = new ShatterFrag(); + frag->autorelease(); + frag->initWithTexture(this->getTexture()); + //set to grid + m_grid[i][j] = frag; + //add to batchNode + m_fragBatchNode->addChild(frag); + //random + frag->m_randomNumber = rand(); + } + } +} + +void ShatterSprite::resetShatter(){ + Size contentSize = this->getContentSize(); + int nRow = (int)m_grid.size(); + int nCol = (nRow == 0?0:(int)m_grid[0].size()); + const float halfGridSideLen = 0.5*m_gridSideLen; + for (int i = 0; i < nRow; i++){ + for(int j = 0; j < nCol; j++){ + ShatterFrag* frag = m_grid[i][j]; + //position + float x = j * m_gridSideLen+halfGridSideLen; + float y = contentSize.height - (i * m_gridSideLen+halfGridSideLen); + //texture and textureRect + frag->setTextureRect(Rect( + x-halfGridSideLen, + (contentSize.height-y) - halfGridSideLen, + m_gridSideLen, + m_gridSideLen + )); + + //set position + frag->setPosition(Vec2(x,y)); + //scale + frag->setScale(m_initalFragScale); + //opacity + frag->setOpacity(255); + //visible + frag->setVisible(true); + } + } + +} +void ShatterSprite::updateShatterAction(float time,float dt,float growSpeedOfTargetR){ + //update frags + Size contentSize = this->getContentSize(); + Vec2 center = Vec2(contentSize.width/2, contentSize.height/2); + + //radius of surrounding circle + float initalTargetR = Vec2(contentSize.width, contentSize.height).getLength()/2; + + int nRow = (int)m_grid.size(); + int nCol = nRow?(int)m_grid[0].size():0; + + for (int i = 0; i < nRow; i++){ + for (int j = 0; j < nCol;j ++){ + ShatterFrag*frag = m_grid[i][j]; + + if (frag->getOpacity() == 0 || frag->getScale() == 0){ + frag->setVisible(false); + continue; + } + + //current target R + float targetR = initalTargetR + time*growSpeedOfTargetR; + //update postion + + Vec2 fragPos = frag->getPosition(); + float disToCenter = Vec2(fragPos-center).getLength(); + Vec2 dir; + if (disToCenter == 0) + { + dir = Vec2(0,0); + } + else + { + dir = fragPos-center; + dir.x /= disToCenter; + dir.y /= disToCenter; + } + + float disToEdge = targetR-disToCenter; + float disToEdgeWithRandom = disToEdge + frag->m_randomNumber % (int)initalTargetR - initalTargetR/2;//add random to disToEdge + float movLen = disToEdgeWithRandom*0.0333;//we only move some percent of disToEdgeWithRandom + + Vec2 movVec = dir * movLen; + frag->setPosition(fragPos + movVec); + //update opacity + float opacity = MAX(0, 255 - 255 * disToCenter / initalTargetR); + frag->setOpacity(opacity); + //update scale + float scale = MAX( + 0, + m_initalFragScale - m_initalFragScale * disToCenter / initalTargetR + ); + frag->setScale(scale); + } + } +} //----------------------------------------- //creates the action -CshatterAction* CshatterAction::create(float fDuration){ - - CshatterAction *p = new CshatterAction(); - p->initWithDuration(fDuration); +ShatterAction* ShatterAction::create(float duration){ + + ShatterAction *p = new ShatterAction(); + p->initWithDuration(duration); p->autorelease(); - + return p; } + //initializes the action -bool CshatterAction::initWithDuration(float fDuration){ - if (CCActionInterval::initWithDuration(fDuration)) +bool ShatterAction::initWithDuration(float duration){ + if (ActionInterval::initWithDuration(duration)) { //my init code initMembers(); - return true; } - + return false; } -bool CshatterAction::isDone(){ - return CCActionInterval::isDone(); - -} +bool ShatterAction::isDone(){ + return ActionInterval::isDone(); +} -void CshatterAction::stop(){ +void ShatterAction::stop(){ //clean up and reset - ((CshatterSprite*)_target)->m_fragBatchNode->setVisible(true); + ((ShatterSprite*)_target)->m_fragBatchNode->setVisible(true); //call father stop - CCActionInterval::stop(); + ActionInterval::stop(); } -void CshatterAction::startWithTarget(CCNode *pTarget){ +void ShatterAction::startWithTarget(Node *pTarget){ //reset - ((CshatterSprite*)pTarget)->resetShatter(); - ((CshatterSprite*)pTarget)->m_fragBatchNode->setVisible(true); + ((ShatterSprite*)pTarget)->resetShatter(); + ((ShatterSprite*)pTarget)->m_fragBatchNode->setVisible(true); //call father startWithTarget - CCActionInterval::startWithTarget(pTarget); - + ActionInterval::startWithTarget(pTarget); + } -void CshatterAction::update(float time){//the time actually is percentage, namely eclipsedTime/totalDuration + +void ShatterAction::update(float time){ + //the time actually is percentage, namely eclipsedTime/totalDuration //the t in action's update(t) and sprite's update(t) is different meaning - //t in action's update(t) means the progress percentage, see CCActionInterval::step() - //t in sprite's update(t) means the deltaTime between current frame and previous frame, see CCDirector::drawScene() + //t in action's update(t) means the progress percentage, see ActionInterval::step() + //t in sprite's update(t) means the deltaTime between current frame and previous frame, see Director::drawScene() //cout<<"time:"<updateShatterAction(m_timeCur,m_timeCur-m_timeFoe,m_growSpeedOfTargetR); - -} + m_timeFoe = m_timeCur; + float progressPercentage = time; + m_timeCur = progressPercentage*getDuration(); + ((ShatterSprite*)_target)->updateShatterAction(m_timeCur,m_timeCur-m_timeFoe,m_growSpeedOfTargetR); - - - -namespace_ens_end \ No newline at end of file +}