2011-07-08 18:01:28 +08:00
|
|
|
#include "TouchesTest.h"
|
|
|
|
#include "Ball.h"
|
|
|
|
#include "Paddle.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
TouchesTests::TouchesTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(PongScene);
|
2015-12-02 11:02:27 +08:00
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0 // Use 9.0 or higher SDK to compile
|
2015-12-01 13:02:53 +08:00
|
|
|
ADD_TEST_CASE(ForceTouchTest);
|
|
|
|
#endif
|
2015-04-03 14:31:03 +08:00
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PongScene
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2015-04-03 14:31:03 +08:00
|
|
|
bool PongScene::init()
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
if (TestCase::init())
|
|
|
|
{
|
|
|
|
auto pongLayer = PongLayer::create();
|
|
|
|
addChild(pongLayer);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PongLayer
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
PongLayer::PongLayer()
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
_ballStartingVelocity = Vec2(20.0f, -100.0f);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-07 21:48:39 +08:00
|
|
|
_ball = Ball::ballWithTexture( Director::getInstance()->getTextureCache()->addImage(s_Ball) );
|
2013-06-15 14:03:30 +08:00
|
|
|
_ball->setPosition( VisibleRect::center() );
|
|
|
|
_ball->setVelocity( _ballStartingVelocity );
|
|
|
|
addChild( _ball );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-07 21:48:39 +08:00
|
|
|
auto paddleTexture = Director::getInstance()->getTextureCache()->addImage(s_Paddle);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-10 18:27:13 +08:00
|
|
|
Vector<Paddle*> paddlesM(4);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
Paddle* paddle = Paddle::createWithTexture(paddleTexture);
|
2014-05-15 01:07:09 +08:00
|
|
|
paddle->setPosition( Vec2(VisibleRect::center().x, VisibleRect::bottom().y + 15) );
|
2013-12-10 18:27:13 +08:00
|
|
|
paddlesM.pushBack( paddle );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
paddle = Paddle::createWithTexture( paddleTexture );
|
2014-05-15 01:07:09 +08:00
|
|
|
paddle->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 15) );
|
2013-12-10 18:27:13 +08:00
|
|
|
paddlesM.pushBack( paddle );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
paddle = Paddle::createWithTexture( paddleTexture );
|
2014-05-15 01:07:09 +08:00
|
|
|
paddle->setPosition( Vec2(VisibleRect::center().x, VisibleRect::bottom().y + 100) );
|
2013-12-10 18:27:13 +08:00
|
|
|
paddlesM.pushBack( paddle );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
paddle = Paddle::createWithTexture( paddleTexture );
|
2014-05-15 01:07:09 +08:00
|
|
|
paddle->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 100) );
|
2013-12-10 18:27:13 +08:00
|
|
|
paddlesM.pushBack( paddle );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-10 18:27:13 +08:00
|
|
|
_paddles = paddlesM;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-10 18:27:13 +08:00
|
|
|
for (auto& paddle : _paddles)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
addChild(paddle);
|
|
|
|
}
|
|
|
|
|
2014-10-04 00:38:36 +08:00
|
|
|
schedule( CC_SCHEDULE_SELECTOR(PongLayer::doStep) );
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PongLayer::~PongLayer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PongLayer::resetAndScoreBallForPlayer(int player)
|
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
_ballStartingVelocity = _ballStartingVelocity * -1.1f;
|
2013-06-15 14:03:30 +08:00
|
|
|
_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
|
|
|
|
2013-12-10 18:27:13 +08:00
|
|
|
for (auto& paddle : _paddles)
|
2012-04-19 14:35:52 +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 );
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
2015-12-01 13:02:53 +08:00
|
|
|
|
2015-12-02 11:02:27 +08:00
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0 // Use 9.0 or higher SDK to compile
|
2015-12-01 13:02:53 +08:00
|
|
|
|
2015-12-02 11:02:27 +08:00
|
|
|
const char * _Info_Formatter = "Current force value : %0.02f, maximum possible force : %0.02f";
|
2015-12-01 13:02:53 +08:00
|
|
|
char formatBuffer[256] = {0, };
|
|
|
|
|
|
|
|
ForceTouchTest::ForceTouchTest()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2015-12-02 11:02:27 +08:00
|
|
|
_infoLabel = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "Current force value : 0.00, maximum possible force : 0.00");
|
2015-12-01 13:02:53 +08:00
|
|
|
_infoLabel->setPosition(s.width / 2, s.height / 2);
|
|
|
|
addChild(_infoLabel);
|
|
|
|
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(ForceTouchTest::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(ForceTouchTest::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(ForceTouchTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ForceTouchTest::~ForceTouchTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ForceTouchTest::title() const
|
|
|
|
{
|
|
|
|
return std::string("3D Touch Test");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ForceTouchTest::subtitle() const
|
|
|
|
{
|
|
|
|
return std::string("Touch with force to see info label changes");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForceTouchTest::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForceTouchTest::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
|
|
|
{
|
|
|
|
for(auto& t : touches)
|
|
|
|
{
|
|
|
|
float currentForce = t->getCurrentForce();
|
|
|
|
float maxForce = t->getMaxForce();
|
|
|
|
sprintf(formatBuffer, _Info_Formatter, currentForce, maxForce);
|
|
|
|
_infoLabel->setString(std::string(formatBuffer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForceTouchTest::onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
|
|
|
{
|
|
|
|
sprintf(formatBuffer, _Info_Formatter, 0.0f, 0.0f);
|
|
|
|
_infoLabel->setString(std::string(formatBuffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|