2010-08-27 11:53:35 +08:00
|
|
|
#include "TouchesTest.h"
|
|
|
|
#include "Ball.h"
|
|
|
|
#include "Paddle.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
|
|
|
enum tagPlayer
|
|
|
|
{
|
|
|
|
kHighPlayer,
|
|
|
|
kLowPlayer
|
2010-09-13 11:08:14 +08:00
|
|
|
} PlayerTouches;
|
2010-08-27 11:53:35 +08:00
|
|
|
|
|
|
|
#define kStatusBarHeight 20.0f
|
|
|
|
//#define k1UpperLimit (480.0f - kStatusBarHeight)
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kSpriteTag
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PongScene
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
PongScene::PongScene()
|
|
|
|
{
|
|
|
|
PongLayer *pongLayer = new PongLayer();//PongLayer::node();
|
|
|
|
addChild(pongLayer);
|
2010-09-16 16:32:14 +08:00
|
|
|
pongLayer->release();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PongScene::onExit()
|
|
|
|
{
|
2010-09-02 15:15:45 +08:00
|
|
|
TestScene::onExit();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PongLayer
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
PongLayer::PongLayer()
|
|
|
|
{
|
|
|
|
m_ballStartingVelocity = CGPointMake(20.0f, -100.0f);
|
|
|
|
|
|
|
|
m_ball = Ball::ballWithTexture( CCTextureCache::sharedTextureCache()->addImage(s_Ball) );
|
|
|
|
m_ball->setPosition( CGPointMake(160.0f, 240.0f) );
|
|
|
|
m_ball->setVelocity( m_ballStartingVelocity );
|
|
|
|
addChild( m_ball );
|
|
|
|
m_ball->retain();
|
|
|
|
|
|
|
|
CCTexture2D* paddleTexture = CCTextureCache::sharedTextureCache()->addImage(s_Paddle);
|
|
|
|
|
|
|
|
NSMutableArray<NSObject *> *paddlesM = new NSMutableArray<NSObject *>(4);
|
|
|
|
|
|
|
|
Paddle* paddle = Paddle::paddleWithTexture(paddleTexture);
|
|
|
|
paddle->setPosition( CGPointMake(160, 15) );
|
|
|
|
paddlesM->addObject( paddle );
|
|
|
|
|
|
|
|
paddle = Paddle::paddleWithTexture( paddleTexture );
|
|
|
|
paddle->setPosition( CGPointMake(160, 480 - kStatusBarHeight - 15) );
|
|
|
|
paddlesM->addObject( paddle );
|
|
|
|
|
|
|
|
paddle = Paddle::paddleWithTexture( paddleTexture );
|
|
|
|
paddle->setPosition( CGPointMake(160, 100) );
|
|
|
|
paddlesM->addObject( paddle );
|
|
|
|
|
|
|
|
paddle = Paddle::paddleWithTexture( paddleTexture );
|
|
|
|
paddle->setPosition( CGPointMake(160, 480 - kStatusBarHeight - 100) );
|
|
|
|
paddlesM->addObject( paddle );
|
|
|
|
|
|
|
|
m_paddles = paddlesM->copy();
|
|
|
|
|
|
|
|
NSMutableArray<NSObject *>::NSMutableArrayIterator it;
|
|
|
|
for(it = m_paddles->begin(); it != m_paddles->end(); it++)
|
|
|
|
{
|
2010-08-31 17:12:42 +08:00
|
|
|
paddle = (Paddle*)(*it);
|
2010-08-27 11:53:35 +08:00
|
|
|
|
|
|
|
if(!paddle)
|
|
|
|
break;
|
|
|
|
|
|
|
|
addChild(paddle);
|
|
|
|
}
|
|
|
|
|
|
|
|
paddlesM->release();
|
|
|
|
|
|
|
|
schedule( schedule_selector(PongLayer::doStep) );
|
|
|
|
}
|
|
|
|
|
|
|
|
PongLayer::~PongLayer()
|
|
|
|
{
|
|
|
|
m_ball->release();
|
|
|
|
m_paddles->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PongLayer::resetAndScoreBallForPlayer(int player)
|
|
|
|
{
|
|
|
|
m_ballStartingVelocity = ccpMult(m_ballStartingVelocity, -1.1f);
|
|
|
|
m_ball->setVelocity( m_ballStartingVelocity );
|
|
|
|
m_ball->setPosition( CGPointMake(160.0f, 240.0f) );
|
|
|
|
|
|
|
|
// TODO -- scoring
|
|
|
|
}
|
|
|
|
|
|
|
|
void PongLayer::doStep(ccTime delta)
|
|
|
|
{
|
|
|
|
m_ball->move(delta);
|
|
|
|
|
|
|
|
Paddle* paddle;
|
|
|
|
NSMutableArray<NSObject *>::NSMutableArrayIterator it;
|
|
|
|
for(it = m_paddles->begin(); it != m_paddles->end(); it++)
|
|
|
|
{
|
2010-08-31 17:12:42 +08:00
|
|
|
paddle = (Paddle*)(*it);
|
2010-08-27 11:53:35 +08:00
|
|
|
|
|
|
|
if(!paddle)
|
|
|
|
break;
|
|
|
|
|
|
|
|
m_ball->collideWithPaddle( paddle );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ball->getPosition().y > 480 - kStatusBarHeight + m_ball->radius())
|
|
|
|
resetAndScoreBallForPlayer( kLowPlayer );
|
|
|
|
else if (m_ball->getPosition().y < -m_ball->radius())
|
|
|
|
resetAndScoreBallForPlayer( kHighPlayer );
|
|
|
|
m_ball->draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PongScene::runThisTest()
|
|
|
|
{
|
|
|
|
CCDirector::getSharedDirector()->replaceScene(this);
|
|
|
|
}
|