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()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ballStartingVelocity = ccp(20.0f, -100.0f);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
_ball = Ball::ballWithTexture( TextureCache::sharedTextureCache()->addImage(s_Ball) );
|
2013-06-15 14:03:30 +08:00
|
|
|
_ball->setPosition( VisibleRect::center() );
|
|
|
|
_ball->setVelocity( _ballStartingVelocity );
|
|
|
|
addChild( _ball );
|
|
|
|
_ball->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Texture2D* paddleTexture = TextureCache::sharedTextureCache()->addImage(s_Paddle);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Array *paddlesM = Array::createWithCapacity(4);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
Paddle* paddle = Paddle::createWithTexture(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 );
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
paddle = Paddle::createWithTexture( 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 );
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
paddle = Paddle::createWithTexture( 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 );
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
paddle = Paddle::createWithTexture( 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 );
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
_paddles = paddlesM->clone();
|
|
|
|
_paddles->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_paddles, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
paddle = static_cast<Paddle*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if(!paddle)
|
|
|
|
break;
|
|
|
|
|
|
|
|
addChild(paddle);
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule( schedule_selector(PongLayer::doStep) );
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PongLayer::~PongLayer()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ball->release();
|
|
|
|
_paddles->release();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PongLayer::resetAndScoreBallForPlayer(int player)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ballStartingVelocity = ccpMult(_ballStartingVelocity, -1.1f);
|
|
|
|
_ball->setVelocity( _ballStartingVelocity );
|
|
|
|
_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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ball->move(delta);
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
Paddle* paddle = NULL;
|
2013-06-20 14:17:10 +08:00
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_paddles, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
paddle = static_cast<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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_ball->collideWithPaddle( paddle );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_ball->getPosition().y > VisibleRect::top().y - kStatusBarHeight + _ball->radius())
|
2012-04-19 14:35:52 +08:00
|
|
|
resetAndScoreBallForPlayer( kLowPlayer );
|
2013-06-15 14:03:30 +08:00
|
|
|
else if (_ball->getPosition().y < VisibleRect::bottom().y-_ball->radius())
|
2012-04-19 14:35:52 +08:00
|
|
|
resetAndScoreBallForPlayer( kHighPlayer );
|
2013-06-15 14:03:30 +08:00
|
|
|
_ball->draw();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PongScene::runThisTest()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(this);
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|