2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-07-10 09:47:41 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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 "TouchesTest.h"
|
|
|
|
#include "Ball.h"
|
|
|
|
#include "Paddle.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
enum tagPlayer
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
kHighPlayer,
|
|
|
|
kLowPlayer
|
2021-12-31 12:12:40 +08:00
|
|
|
} PlayerTouches;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
#define kStatusBarHeight 0.0f // 20.0f
|
2019-11-23 20:27:39 +08:00
|
|
|
//#define k1UpperLimit (480.0f - kStatusBarHeight)
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
enum
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
kSpriteTag
|
|
|
|
};
|
|
|
|
|
|
|
|
TouchesTests::TouchesTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(PongScene);
|
|
|
|
ADD_TEST_CASE(ForceTouchTest);
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PongScene
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
bool PongScene::init()
|
|
|
|
{
|
|
|
|
if (TestCase::init())
|
|
|
|
{
|
|
|
|
auto pongLayer = PongLayer::create();
|
|
|
|
addChild(pongLayer);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PongLayer
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
PongLayer::PongLayer()
|
|
|
|
{
|
|
|
|
_ballStartingVelocity = Vec2(20.0f, -100.0f);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
_ball = Ball::ballWithTexture(Director::getInstance()->getTextureCache()->addImage(s_Ball));
|
|
|
|
_ball->setPosition(VisibleRect::center());
|
|
|
|
_ball->setVelocity(_ballStartingVelocity);
|
|
|
|
addChild(_ball);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto paddleTexture = Director::getInstance()->getTextureCache()->addImage(s_Paddle);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Vector<Paddle*> paddlesM(4);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
float paddleStep =
|
|
|
|
(VisibleRect::getVisibleRect().size.height - kStatusBarHeight - 30 - 3 * paddleTexture->getPixelsHigh()) / 3 +
|
|
|
|
paddleTexture->getPixelsHigh();
|
2019-11-27 00:49:56 +08:00
|
|
|
float nextPaddlePos = VisibleRect::bottom().y + 15;
|
2021-12-31 12:12:40 +08:00
|
|
|
Paddle* paddle = Paddle::createWithTexture(paddleTexture);
|
|
|
|
paddle->setPosition(Vec2(VisibleRect::center().x, nextPaddlePos));
|
|
|
|
paddlesM.pushBack(paddle);
|
|
|
|
|
|
|
|
paddle = Paddle::createWithTexture(paddleTexture);
|
|
|
|
paddle->setPosition(Vec2(VisibleRect::center().x, nextPaddlePos += paddleStep));
|
|
|
|
paddlesM.pushBack(paddle);
|
|
|
|
|
|
|
|
paddle = Paddle::createWithTexture(paddleTexture);
|
|
|
|
paddle->setPosition(Vec2(VisibleRect::center().x, nextPaddlePos += paddleStep));
|
|
|
|
paddlesM.pushBack(paddle);
|
|
|
|
|
|
|
|
paddle = Paddle::createWithTexture(paddleTexture);
|
|
|
|
paddle->setPosition(Vec2(VisibleRect::center().x, nextPaddlePos += paddleStep));
|
|
|
|
paddlesM.pushBack(paddle);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_paddles = paddlesM;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto& paddle : _paddles)
|
|
|
|
{
|
|
|
|
addChild(paddle);
|
|
|
|
}
|
|
|
|
|
2022-07-15 19:44:31 +08:00
|
|
|
schedule(CC_SCHEDULE_SELECTOR(PongLayer::doStep));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
PongLayer::~PongLayer() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void PongLayer::resetAndScoreBallForPlayer(int player)
|
|
|
|
{
|
|
|
|
_ballStartingVelocity = _ballStartingVelocity * -1.1f;
|
2021-12-31 12:12:40 +08:00
|
|
|
_ball->setVelocity(_ballStartingVelocity);
|
|
|
|
_ball->setPosition(VisibleRect::center());
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// TODO -- scoring
|
|
|
|
}
|
|
|
|
|
|
|
|
void PongLayer::doStep(float delta)
|
|
|
|
{
|
|
|
|
_ball->move(delta);
|
|
|
|
|
|
|
|
for (auto& paddle : _paddles)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
_ball->collideWithPaddle(paddle);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_ball->getPosition().y > VisibleRect::top().y - kStatusBarHeight + _ball->radius())
|
2021-12-31 12:12:40 +08:00
|
|
|
resetAndScoreBallForPlayer(kLowPlayer);
|
|
|
|
else if (_ball->getPosition().y < VisibleRect::bottom().y - _ball->radius())
|
|
|
|
resetAndScoreBallForPlayer(kHighPlayer);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
const char* _Info_Formatter = "Current force value : %0.02f, maximum possible force : %0.02f";
|
|
|
|
char formatBuffer[256] = {
|
|
|
|
0,
|
|
|
|
};
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ForceTouchTest::ForceTouchTest()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
_infoLabel =
|
|
|
|
Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "Current force value : 0.00, maximum possible force : 0.00");
|
2019-11-23 20:27:39 +08:00
|
|
|
_infoLabel->setPosition(s.width / 2, s.height / 2);
|
|
|
|
addChild(_infoLabel);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-15 19:44:31 +08:00
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(ForceTouchTest::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(ForceTouchTest::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(ForceTouchTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ForceTouchTest::~ForceTouchTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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\nOnly work on iPhone6s / iPhone6s Plus");
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void ForceTouchTest::onTouchesBegan(const std::vector<axis::Touch*>& touches, axis::Event* event) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void ForceTouchTest::onTouchesMoved(const std::vector<axis::Touch*>& touches, axis::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (auto& t : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
float currentForce = t->getCurrentForce();
|
2021-12-31 12:12:40 +08:00
|
|
|
float maxForce = t->getMaxForce();
|
2019-11-23 20:27:39 +08:00
|
|
|
sprintf(formatBuffer, _Info_Formatter, currentForce, maxForce);
|
|
|
|
_infoLabel->setString(std::string(formatBuffer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void ForceTouchTest::onTouchesEnded(const std::vector<axis::Touch*>& touches, axis::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
sprintf(formatBuffer, _Info_Formatter, 0.0f, 0.0f);
|
|
|
|
_infoLabel->setString(std::string(formatBuffer));
|
|
|
|
}
|