2011-07-08 18:01:28 +08:00
|
|
|
#include "TouchesTest.h"
|
|
|
|
#include "Ball.h"
|
|
|
|
#include "Paddle.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
2010-08-27 11:53:35 +08:00
|
|
|
enum tagPlayer
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kHighPlayer,
|
|
|
|
kLowPlayer
|
|
|
|
} PlayerTouches;
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2012-05-30 16:55:46 +08:00
|
|
|
#define kStatusBarHeight 0.0f //20.0f
|
2010-08-27 11:53:35 +08:00
|
|
|
//#define k1UpperLimit (480.0f - kStatusBarHeight)
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kSpriteTag
|
2010-08-27 11:53:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PongScene
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
PongScene::PongScene()
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
PongLayer *pongLayer = new PongLayer();//PongLayer::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(pongLayer);
|
2010-09-16 16:32:14 +08:00
|
|
|
pongLayer->release();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PongLayer
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
PongLayer::PongLayer()
|
|
|
|
{
|
2012-10-23 17:48:50 +08:00
|
|
|
m_ballStartingVelocity = ccp(20.0f, -100.0f);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2010-08-27 11:53:35 +08:00
|
|
|
m_ball = Ball::ballWithTexture( CCTextureCache::sharedTextureCache()->addImage(s_Ball) );
|
2012-10-23 17:48:50 +08:00
|
|
|
m_ball->setPosition( VisibleRect::center() );
|
2012-04-19 14:35:52 +08:00
|
|
|
m_ball->setVelocity( m_ballStartingVelocity );
|
|
|
|
addChild( m_ball );
|
|
|
|
m_ball->retain();
|
|
|
|
|
|
|
|
CCTexture2D* paddleTexture = CCTextureCache::sharedTextureCache()->addImage(s_Paddle);
|
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
CCArray *paddlesM = CCArray::createWithCapacity(4);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
Paddle* paddle = Paddle::paddleWithTexture(paddleTexture);
|
2012-10-23 17:48:50 +08:00
|
|
|
paddle->setPosition( ccp(VisibleRect::center().x, VisibleRect::bottom().y + 15) );
|
2012-04-19 14:35:52 +08:00
|
|
|
paddlesM->addObject( paddle );
|
|
|
|
|
|
|
|
paddle = Paddle::paddleWithTexture( paddleTexture );
|
2012-10-23 17:48:50 +08:00
|
|
|
paddle->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 15) );
|
2012-04-19 14:35:52 +08:00
|
|
|
paddlesM->addObject( paddle );
|
|
|
|
|
|
|
|
paddle = Paddle::paddleWithTexture( paddleTexture );
|
2012-10-23 17:48:50 +08:00
|
|
|
paddle->setPosition( ccp(VisibleRect::center().x, VisibleRect::bottom().y + 100) );
|
2012-04-19 14:35:52 +08:00
|
|
|
paddlesM->addObject( paddle );
|
|
|
|
|
|
|
|
paddle = Paddle::paddleWithTexture( paddleTexture );
|
2012-10-23 17:48:50 +08:00
|
|
|
paddle->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 100) );
|
2012-04-19 14:35:52 +08:00
|
|
|
paddlesM->addObject( paddle );
|
|
|
|
|
|
|
|
m_paddles = (CCArray*)paddlesM->copy();
|
|
|
|
|
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_paddles, pObj)
|
|
|
|
{
|
|
|
|
paddle = (Paddle*)(pObj);
|
|
|
|
|
|
|
|
if(!paddle)
|
|
|
|
break;
|
|
|
|
|
|
|
|
addChild(paddle);
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule( schedule_selector(PongLayer::doStep) );
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PongLayer::~PongLayer()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_ball->release();
|
|
|
|
m_paddles->release();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PongLayer::resetAndScoreBallForPlayer(int player)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_ballStartingVelocity = ccpMult(m_ballStartingVelocity, -1.1f);
|
|
|
|
m_ball->setVelocity( m_ballStartingVelocity );
|
2012-10-23 17:48:50 +08:00
|
|
|
m_ball->setPosition( VisibleRect::center() );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// TODO -- scoring
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void PongLayer::doStep(float delta)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_ball->move(delta);
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
Paddle* paddle = NULL;
|
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_paddles, pObj)
|
|
|
|
{
|
|
|
|
paddle = (Paddle*)(pObj);
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if(!paddle)
|
|
|
|
break;
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_ball->collideWithPaddle( paddle );
|
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2012-10-23 17:48:50 +08:00
|
|
|
if (m_ball->getPosition().y > VisibleRect::top().y - kStatusBarHeight + m_ball->radius())
|
2012-04-19 14:35:52 +08:00
|
|
|
resetAndScoreBallForPlayer( kLowPlayer );
|
2012-10-23 17:48:50 +08:00
|
|
|
else if (m_ball->getPosition().y < VisibleRect::bottom().y-m_ball->radius())
|
2012-04-19 14:35:52 +08:00
|
|
|
resetAndScoreBallForPlayer( kHighPlayer );
|
2010-08-27 11:53:35 +08:00
|
|
|
m_ball->draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PongScene::runThisTest()
|
|
|
|
{
|
2010-11-11 11:18:58 +08:00
|
|
|
CCDirector::sharedDirector()->replaceScene(this);
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|