PuzzleSDK
TouchesTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "TouchesTest.h"
26#include "Ball.h"
27#include "Paddle.h"
28#include "../testResource.h"
29
31
33{
37
38#define kStatusBarHeight 0.0f //20.0f
39//#define k1UpperLimit (480.0f - kStatusBarHeight)
40
41enum
42{
44};
45
46TouchesTests::TouchesTests()
47{
50}
51//------------------------------------------------------------------
52//
53// PongScene
54//
55//------------------------------------------------------------------
57{
58 if (TestCase::init())
59 {
60 auto pongLayer = PongLayer::create();
61 addChild(pongLayer);
62
63 return true;
64 }
65 return false;
66}
67
68//------------------------------------------------------------------
69//
70// PongLayer
71//
72//------------------------------------------------------------------
74{
75 _ballStartingVelocity = Vec2(20.0f, -100.0f);
76
77 _ball = Ball::ballWithTexture( Director::getInstance()->getTextureCache()->addImage(s_Ball) );
78 _ball->setPosition( VisibleRect::center() );
80 addChild( _ball );
81
82 auto paddleTexture = Director::getInstance()->getTextureCache()->addImage(s_Paddle);
83
84 Vector<Paddle*> paddlesM(4);
85
86 float paddleStep =(VisibleRect::getVisibleRect().size.height - kStatusBarHeight - 30 - 3*paddleTexture->getPixelsHigh())/3 + paddleTexture->getPixelsHigh();
87 float nextPaddlePos = VisibleRect::bottom().y + 15;
88 Paddle* paddle = Paddle::createWithTexture(paddleTexture);
89 paddle->setPosition( Vec2(VisibleRect::center().x, nextPaddlePos));
90 paddlesM.pushBack( paddle );
91
92 paddle = Paddle::createWithTexture( paddleTexture );
93 paddle->setPosition( Vec2(VisibleRect::center().x, nextPaddlePos+=paddleStep) );
94 paddlesM.pushBack( paddle );
95
96 paddle = Paddle::createWithTexture( paddleTexture );
97 paddle->setPosition( Vec2(VisibleRect::center().x, nextPaddlePos+=paddleStep) );
98 paddlesM.pushBack( paddle );
99
100 paddle = Paddle::createWithTexture( paddleTexture );
101 paddle->setPosition( Vec2(VisibleRect::center().x, nextPaddlePos+=paddleStep) );
102 paddlesM.pushBack( paddle );
103
104 _paddles = paddlesM;
105
106 for (auto& paddle : _paddles)
107 {
108 addChild(paddle);
109 }
110
111 schedule( CC_SCHEDULE_SELECTOR(PongLayer::doStep) );
112}
113
115{
116}
117
119{
122 _ball->setPosition( VisibleRect::center() );
123
124 // TODO -- scoring
125}
126
127void PongLayer::doStep(float delta)
128{
129 _ball->move(delta);
130
131 for (auto& paddle : _paddles)
132 {
133 _ball->collideWithPaddle( paddle );
134 }
135
136 if (_ball->getPosition().y > VisibleRect::top().y - kStatusBarHeight + _ball->radius())
138 else if (_ball->getPosition().y < VisibleRect::bottom().y-_ball->radius())
140}
141
142const char * _Info_Formatter = "Current force value : %0.02f, maximum possible force : %0.02f";
143char formatBuffer[256] = {0, };
144
146{
147 auto s = Director::getInstance()->getWinSize();
148
149 _infoLabel = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "Current force value : 0.00, maximum possible force : 0.00");
150 _infoLabel->setPosition(s.width / 2, s.height / 2);
151 addChild(_infoLabel);
152
153 auto listener = EventListenerTouchAllAtOnce::create();
154 listener->onTouchesBegan = CC_CALLBACK_2(ForceTouchTest::onTouchesBegan, this);
155 listener->onTouchesMoved = CC_CALLBACK_2(ForceTouchTest::onTouchesMoved, this);
156 listener->onTouchesEnded = CC_CALLBACK_2(ForceTouchTest::onTouchesEnded, this);
157 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
158}
159
161{
162}
163
164std::string ForceTouchTest::title() const
165{
166 return std::string("3D Touch Test");
167}
168
169std::string ForceTouchTest::subtitle() const
170{
171 return std::string("Touch with force to see info label changes\nOnly work on iPhone6s / iPhone6s Plus");
172}
173
174void ForceTouchTest::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
175{
176}
177
178void ForceTouchTest::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
179{
180 for(auto& t : touches)
181 {
182 float currentForce = t->getCurrentForce();
183 float maxForce = t->getMaxForce();
184 sprintf(formatBuffer, _Info_Formatter, currentForce, maxForce);
185 _infoLabel->setString(std::string(formatBuffer));
186 }
187}
188
189void ForceTouchTest::onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
190{
191 sprintf(formatBuffer, _Info_Formatter, 0.0f, 0.0f);
192 _infoLabel->setString(std::string(formatBuffer));
193}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
const char * _Info_Formatter
@ kSpriteTag
Definition: TouchesTest.cpp:43
enum tagPlayer PlayerTouches
char formatBuffer[256]
#define kStatusBarHeight
Definition: TouchesTest.cpp:38
tagPlayer
Definition: TouchesTest.cpp:33
@ kHighPlayer
Definition: TouchesTest.cpp:34
@ kLowPlayer
Definition: TouchesTest.cpp:35
USING_NS_CC
Definition: TouchesTest.cpp:30
void move(float delta)
Definition: Ball.cpp:53
void collideWithPaddle(Paddle *paddle)
Definition: Ball.cpp:69
void setVelocity(cocos2d::Vec2 velocity)
Definition: Ball.h:47
float radius()
Definition: Ball.cpp:39
static Ball * ballWithTexture(cocos2d::Texture2D *aTexture)
Definition: Ball.cpp:44
virtual ~ForceTouchTest()
virtual std::string title() const override
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
cocos2d::Label * _infoLabel
Definition: TouchesTest.h:74
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string subtitle() const override
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
Definition: Paddle.h:38
static Paddle * createWithTexture(cocos2d::Texture2D *aTexture)
Definition: Paddle.cpp:43
cocos2d::Vec2 _ballStartingVelocity
Definition: TouchesTest.h:48
void resetAndScoreBallForPlayer(int player)
void doStep(float delta)
Ball * _ball
Definition: TouchesTest.h:46
cocos2d::Vector< Paddle * > _paddles
Definition: TouchesTest.h:47
virtual bool init() override
Definition: TouchesTest.cpp:56
static cocos2d::Vec2 top()
Definition: VisibleRect.cpp:57
static cocos2d::Rect getVisibleRect()
Definition: VisibleRect.cpp:39
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69
static cocos2d::Vec2 bottom()
Definition: VisibleRect.cpp:63
static const char s_Paddle[]
Definition: testResource.h:54
static const char s_Ball[]
Definition: testResource.h:53