2010-08-26 10:44:00 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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 "CCParallaxNode.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCPointExtension.h"
|
2010-09-24 18:10:32 +08:00
|
|
|
#include "support/data_support/ccCArray.h"
|
2010-08-26 10:44:00 +08:00
|
|
|
|
|
|
|
namespace cocos2d {
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
class CCPointObject : CCObject
|
2010-08-26 11:30:52 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SYNTHESIZE(CCPoint, m_tRatio, Ratio)
|
|
|
|
CC_SYNTHESIZE(CCPoint, m_tOffset, Offset)
|
|
|
|
CC_SYNTHESIZE(CCNode *,m_pChild, Child) // weak ref
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
static CCPointObject * pointWithCCPoint(CCPoint ratio, CCPoint offset)
|
2010-08-26 11:30:52 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPointObject *pRet = new CCPointObject();
|
|
|
|
pRet->initWithCCPoint(ratio, offset);
|
2010-08-26 11:30:52 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
bool initWithCCPoint(CCPoint ratio, CCPoint offset)
|
2010-08-26 11:30:52 +08:00
|
|
|
{
|
|
|
|
m_tRatio = ratio;
|
|
|
|
m_tOffset = offset;
|
|
|
|
m_pChild = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CCParallaxNode::CCParallaxNode()
|
|
|
|
{
|
|
|
|
m_pParallaxArray = ccArrayNew(5);
|
2011-03-07 17:11:57 +08:00
|
|
|
m_tLastPosition = CCPointMake(-100,-100);
|
2010-08-26 11:30:52 +08:00
|
|
|
}
|
|
|
|
CCParallaxNode::~CCParallaxNode()
|
|
|
|
{
|
|
|
|
if( m_pParallaxArray )
|
|
|
|
{
|
|
|
|
ccArrayFree(m_pParallaxArray);
|
|
|
|
m_pParallaxArray = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-08-26 11:34:22 +08:00
|
|
|
CCParallaxNode * CCParallaxNode::node()
|
|
|
|
{
|
|
|
|
CCParallaxNode *pRet = new CCParallaxNode();
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2010-12-28 15:17:59 +08:00
|
|
|
void CCParallaxNode::addChild(CCNode * child, int zOrder, int tag)
|
2010-08-26 11:30:52 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
|
2010-08-26 11:30:52 +08:00
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCParallaxNode::addChild(CCNode *child, int z, CCPoint ratio, CCPoint offset)
|
2010-08-26 11:30:52 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( child != NULL, "Argument must be non-nil");
|
|
|
|
CCPointObject *obj = CCPointObject::pointWithCCPoint(ratio, offset);
|
2010-08-26 11:30:52 +08:00
|
|
|
obj->setChild(child);
|
2011-03-07 17:11:57 +08:00
|
|
|
ccArrayAppendObjectWithResize(m_pParallaxArray, (CCObject*)obj);
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint pos = m_tPosition;
|
2010-12-28 15:17:59 +08:00
|
|
|
pos.x = pos.x * ratio.x + offset.x;
|
|
|
|
pos.y = pos.y * ratio.y + offset.y;
|
|
|
|
child->setPosition(pos);
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2010-12-28 15:17:59 +08:00
|
|
|
CCNode::addChild(child, z, child->getTag());
|
2010-08-26 11:30:52 +08:00
|
|
|
}
|
|
|
|
void CCParallaxNode::removeChild(CCNode* child, bool cleanup)
|
|
|
|
{
|
|
|
|
for( unsigned int i=0;i < m_pParallaxArray->num;i++)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPointObject *point = (CCPointObject*)m_pParallaxArray->arr[i];
|
2010-08-26 11:30:52 +08:00
|
|
|
if( point->getChild()->isEqual(child))
|
|
|
|
{
|
|
|
|
ccArrayRemoveObjectAtIndex(m_pParallaxArray, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-09-02 14:54:42 +08:00
|
|
|
CCNode::removeChild(child, cleanup);
|
2010-08-26 11:30:52 +08:00
|
|
|
}
|
|
|
|
void CCParallaxNode::removeAllChildrenWithCleanup(bool cleanup)
|
|
|
|
{
|
|
|
|
ccArrayRemoveAllObjects(m_pParallaxArray);
|
2010-09-02 14:54:42 +08:00
|
|
|
CCNode::removeAllChildrenWithCleanup(cleanup);
|
2010-08-26 11:30:52 +08:00
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint CCParallaxNode::absolutePosition()
|
2010-08-26 11:30:52 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint ret = m_tPosition;
|
2010-08-26 11:30:52 +08:00
|
|
|
CCNode *cn = this;
|
|
|
|
while (cn->getParent() != NULL)
|
|
|
|
{
|
|
|
|
cn = cn->getParent();
|
|
|
|
ret = ccpAdd( ret, cn->getPosition());
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
The positions are updated at visit because:
|
|
|
|
- using a timer is not guaranteed that it will called after all the positions were updated
|
|
|
|
- overriding "draw" will only precise if the children have a z > 0
|
|
|
|
*/
|
|
|
|
void CCParallaxNode::visit()
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
// CCPoint pos = position_;
|
|
|
|
// CCPoint pos = [self convertToWorldSpace:CCPointZero];
|
|
|
|
CCPoint pos = this->absolutePosition();
|
|
|
|
if( ! CCPoint::CCPointEqualToPoint(pos, m_tLastPosition) )
|
2010-08-26 11:30:52 +08:00
|
|
|
{
|
|
|
|
for(unsigned int i=0; i < m_pParallaxArray->num; i++ )
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPointObject *point = (CCPointObject*)m_pParallaxArray->arr[i];
|
2010-08-26 11:30:52 +08:00
|
|
|
float x = -pos.x + pos.x * point->getRatio().x + point->getOffset().x;
|
|
|
|
float y = -pos.y + pos.y * point->getRatio().y + point->getOffset().y;
|
|
|
|
point->getChild()->setPosition(ccp(x,y));
|
|
|
|
}
|
|
|
|
m_tLastPosition = pos;
|
|
|
|
}
|
2010-09-02 14:54:42 +08:00
|
|
|
CCNode::visit();
|
2010-08-26 11:30:52 +08:00
|
|
|
}
|
2010-08-26 10:44:00 +08:00
|
|
|
|
|
|
|
}// namespace cocos2d
|