2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-07-08 18:01:28 +08:00
|
|
|
#include "TouchesTest.h"
|
|
|
|
#include "Ball.h"
|
|
|
|
#include "Paddle.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-04-09 08:37:30 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
enum tagPlayer
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kHighPlayer,
|
|
|
|
kLowPlayer
|
2021-12-28 16:06:23 +08:00
|
|
|
} PlayerTouches;
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
#define kStatusBarHeight 0.0f // 20.0f
|
2010-08-27 11:53:35 +08:00
|
|
|
//#define k1UpperLimit (480.0f - kStatusBarHeight)
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
enum
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
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-01 13:02:53 +08:00
|
|
|
ADD_TEST_CASE(ForceTouchTest);
|
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);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
_ball = Ball::ballWithTexture(Director::getInstance()->getTextureCache()->addImage(s_Ball));
|
|
|
|
_ball->setPosition(VisibleRect::center());
|
|
|
|
_ball->setVelocity(_ballStartingVelocity);
|
|
|
|
addChild(_ball);
|
|
|
|
|
2013-11-07 21:48:39 +08:00
|
|
|
auto paddleTexture = Director::getInstance()->getTextureCache()->addImage(s_Paddle);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-12-10 18:27:13 +08:00
|
|
|
Vector<Paddle*> paddlesM(4);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
float paddleStep =
|
|
|
|
(VisibleRect::getVisibleRect().size.height - kStatusBarHeight - 30 - 3 * paddleTexture->getPixelsHigh()) / 3 +
|
|
|
|
paddleTexture->getPixelsHigh();
|
2019-11-26 13:40:52 +08:00
|
|
|
float nextPaddlePos = VisibleRect::bottom().y + 15;
|
2021-12-28 16:06:23 +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);
|
|
|
|
|
2013-12-10 18:27:13 +08:00
|
|
|
_paddles = paddlesM;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& paddle : _paddles)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
addChild(paddle);
|
|
|
|
}
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
schedule(AX_SCHEDULE_SELECTOR(PongLayer::doStep));
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
PongLayer::~PongLayer() {}
|
2010-08-27 11:53:35 +08:00
|
|
|
|
|
|
|
void PongLayer::resetAndScoreBallForPlayer(int player)
|
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
_ballStartingVelocity = _ballStartingVelocity * -1.1f;
|
2021-12-28 16:06:23 +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
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& paddle : _paddles)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +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())
|
2021-12-28 16:06:23 +08:00
|
|
|
resetAndScoreBallForPlayer(kLowPlayer);
|
|
|
|
else if (_ball->getPosition().y < VisibleRect::bottom().y - _ball->radius())
|
|
|
|
resetAndScoreBallForPlayer(kHighPlayer);
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
2015-12-01 13:02:53 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
const char* _Info_Formatter = "Current force value : %0.02f, maximum possible force : %0.02f";
|
|
|
|
char formatBuffer[256] = {
|
|
|
|
0,
|
|
|
|
};
|
2015-12-01 13:02:53 +08:00
|
|
|
|
|
|
|
ForceTouchTest::ForceTouchTest()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-28 16:06:23 +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);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
listener->onTouchesBegan = AX_CALLBACK_2(ForceTouchTest::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(ForceTouchTest::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(ForceTouchTest::onTouchesEnded, this);
|
2015-12-01 13:02:53 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
ForceTouchTest::~ForceTouchTest() {}
|
2015-12-01 13:02:53 +08:00
|
|
|
|
|
|
|
std::string ForceTouchTest::title() const
|
|
|
|
{
|
|
|
|
return std::string("3D Touch Test");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ForceTouchTest::subtitle() const
|
|
|
|
{
|
2015-12-22 18:41:44 +08:00
|
|
|
return std::string("Touch with force to see info label changes\nOnly work on iPhone6s / iPhone6s Plus");
|
2015-12-01 13:02:53 +08:00
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void ForceTouchTest::onTouchesBegan(const std::vector<axis::Touch*>& touches, axis::Event* event) {}
|
2015-12-01 13:02:53 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void ForceTouchTest::onTouchesMoved(const std::vector<axis::Touch*>& touches, axis::Event* event)
|
2015-12-01 13:02:53 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& t : touches)
|
2015-12-01 13:02:53 +08:00
|
|
|
{
|
|
|
|
float currentForce = t->getCurrentForce();
|
2021-12-28 16:06:23 +08:00
|
|
|
float maxForce = t->getMaxForce();
|
2015-12-01 13:02:53 +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)
|
2015-12-01 13:02:53 +08:00
|
|
|
{
|
|
|
|
sprintf(formatBuffer, _Info_Formatter, 0.0f, 0.0f);
|
|
|
|
_infoLabel->setString(std::string(formatBuffer));
|
|
|
|
}
|