PuzzleSDK
PhysicsTest.h
浏览该文件的文档.
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#pragma once
26
27#include <map>
28
29#include "../BaseTest.h"
30
31#if CC_USE_PHYSICS
32
33
34DEFINE_TEST_SUITE(PhysicsTests);
35
36class PhysicsDemo : public TestCase
37{
38public:
39 PhysicsDemo();
40 virtual ~PhysicsDemo();
41
42 virtual bool init() override;
43 virtual void onEnter() override;
44 virtual std::string title() const override;
45
46 void toggleDebugCallback(cocos2d::Ref* sender);
47
48 cocos2d::Sprite* addGrossiniAtPosition(cocos2d::Vec2 p, float scale = 1.0);
49 cocos2d::Sprite* makeBall(cocos2d::Vec2 point, float radius, cocos2d::PhysicsMaterial material = cocos2d::PHYSICSBODY_MATERIAL_DEFAULT);
50 cocos2d::Sprite* makeBox(cocos2d::Vec2 point, cocos2d::Size size, int color = 0, cocos2d::PhysicsMaterial material = cocos2d::PHYSICSBODY_MATERIAL_DEFAULT);
51 cocos2d::Sprite* makeTriangle(cocos2d::Vec2 point, cocos2d::Size size, int color = 0, cocos2d::PhysicsMaterial material = cocos2d::PHYSICSBODY_MATERIAL_DEFAULT);
52
53 bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
54 void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
55 void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
56
57 void toggleDebug();
58
59protected:
60
61 cocos2d::Texture2D* _spriteTexture;
62 cocos2d::SpriteBatchNode* _ball;
63 std::unordered_map<int, cocos2d::Node*> _mouses;
64 bool _debugDraw;
65};
66
67class PhysicsDemoLogoSmash : public PhysicsDemo
68{
69public:
70 CREATE_FUNC(PhysicsDemoLogoSmash);
71
72 void onEnter() override;
73 virtual std::string title() const override;
74};
75
76class PhysicsDemoClickAdd : public PhysicsDemo
77{
78public:
79 CREATE_FUNC(PhysicsDemoClickAdd);
80
81 virtual ~PhysicsDemoClickAdd();
82 void onEnter() override;
83 virtual std::string subtitle() const override;
84
85 void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
86 void onAcceleration(cocos2d::Acceleration* acc, cocos2d::Event* event);
87};
88
89class PhysicsDemoPyramidStack : public PhysicsDemo
90{
91public:
92 CREATE_FUNC(PhysicsDemoPyramidStack);
93
94 void onEnter() override;
95 void updateOnce(float delta);
96 virtual std::string title() const override;
97};
98
99class PhysicsDemoRayCast : public PhysicsDemo
100{
101public:
102 CREATE_FUNC(PhysicsDemoRayCast);
103
104 PhysicsDemoRayCast();
105
106 void onEnter() override;
107 virtual std::string title() const override;
108 void update(float delta) override;
109 void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
110
111 void changeModeCallback(cocos2d::Ref* sender);
112
113 bool anyRay(cocos2d::PhysicsWorld& world, const cocos2d::PhysicsRayCastInfo& info, void* data);
114
115private:
116 float _angle;
117 cocos2d::DrawNode* _node;
118 int _mode;
119};
120
121class PhysicsDemoActions : public PhysicsDemo
122{
123public:
124 CREATE_FUNC(PhysicsDemoActions);
125
126 void onEnter() override;
127 virtual std::string title() const override;
128};
129
130class PhysicsDemoJoints : public PhysicsDemo
131{
132public:
133 CREATE_FUNC(PhysicsDemoJoints);
134
135 void onEnter() override;
136 virtual std::string title() const override;
137};
138
139class PhysicsDemoPump : public PhysicsDemo
140{
141public:
142 CREATE_FUNC(PhysicsDemoPump);
143
144 void onEnter() override;
145 void update(float delta) override;
146 virtual std::string title() const override;
147 virtual std::string subtitle() const override;
148
149 bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
150 void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
151 void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
152
153private:
154 float _distance;
155 float _rotationV;
156};
157
158class PhysicsDemoOneWayPlatform : public PhysicsDemo
159{
160public:
161 CREATE_FUNC(PhysicsDemoOneWayPlatform);
162
163 void onEnter() override;
164 virtual std::string title() const override;
165
166 bool onContactBegin(cocos2d::PhysicsContact& contact);
167};
168
169class PhysicsDemoSlice : public PhysicsDemo
170{
171public:
172 CREATE_FUNC(PhysicsDemoSlice);
173
174 void onEnter() override;
175 virtual std::string title() const override;
176 virtual std::string subtitle() const override;
177
178 bool slice(cocos2d::PhysicsWorld& world, const cocos2d::PhysicsRayCastInfo& info, void* data);
179 void clipPoly(cocos2d::PhysicsShapePolygon* shape, cocos2d::Vec2 normal, float distance);
180
181 void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
182
183private:
184 int _sliceTag;
185};
186
187class PhysicsDemoBug3988 : public PhysicsDemo
188{
189public:
190 CREATE_FUNC(PhysicsDemoBug3988);
191
192 void onEnter() override;
193 virtual std::string title() const override;
194 virtual std::string subtitle() const override;
195};
196
197class PhysicsContactTest : public PhysicsDemo
198{
199public:
200 CREATE_FUNC(PhysicsContactTest);
201
202 void onEnter() override;
203 void resetTest();
204 bool onContactBegin(cocos2d::PhysicsContact& contact);
205 virtual std::string title() const override;
206 virtual std::string subtitle() const override;
207
208 void onDecrease(cocos2d::Ref* sender);
209 void onIncrease(cocos2d::Ref* sender);
210
211private:
212 int _yellowBoxNum;
213 int _blueBoxNum;
214 int _yellowTriangleNum;
215 int _blueTriangleNum;
216};
217
218class PhysicsPositionRotationTest : public PhysicsDemo
219{
220public:
221 CREATE_FUNC(PhysicsPositionRotationTest);
222
223 void onEnter() override;
224 virtual std::string title() const override;
225};
226
227class PhysicsSetGravityEnableTest : public PhysicsDemo
228{
229public:
230 CREATE_FUNC(PhysicsSetGravityEnableTest);
231
232 void onEnter() override;
233 void onScheduleOnce(float delta);
234 virtual std::string title() const override;
235 virtual std::string subtitle() const override;
236};
237
238class PhysicsDemoBug5482 : public PhysicsDemo
239{
240public:
241 CREATE_FUNC(PhysicsDemoBug5482);
242
243 void onEnter() override;
244 void onExit() override;
245 virtual std::string title() const override;
246 virtual std::string subtitle() const override;
247
248 void changeBodyCallback(cocos2d::Ref* sender);
249private:
250 cocos2d::Sprite* _nodeA;
251 cocos2d::Sprite* _nodeB;
252 cocos2d::PhysicsBody* _body;
253 cocos2d::MenuItemFont* _button;
254 bool _bodyInA;
255};
256
257class PhysicsFixedUpdate : public PhysicsDemo
258{
259public:
260 CREATE_FUNC(PhysicsFixedUpdate);
261 void onEnter() override;
262 void updateStart(float delta);
263 void addBall();
264 virtual void update(float delta) override;
265 virtual std::string title() const override;
266 virtual std::string subtitle() const override;
267};
268
269class PhysicsTransformTest : public PhysicsDemo
270{
271public:
272 CREATE_FUNC(PhysicsTransformTest);
273
274 void onEnter() override;
275 virtual std::string title() const override;
276
277 bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
278
279private:
280 cocos2d::Sprite* _parentSprite;
281 cocos2d::Layer* _rootLayer;
282};
283
284class PhysicsIssue9959 : public PhysicsDemo
285{
286public:
287 CREATE_FUNC(PhysicsIssue9959);
288
289 void onEnter() override;
290 virtual std::string title() const override;
291 virtual std::string subtitle() const override;
292};
293
294class PhysicsIssue15932 : public PhysicsDemo
295{
296public:
297 CREATE_FUNC(PhysicsIssue15932);
298
299 void onEnter() override;
300 virtual std::string title() const override;
301 virtual std::string subtitle() const override;
302};
303
304#endif // #if CC_USE_PHYSICS
#define DEFINE_TEST_SUITE(__className__)
Definition: BaseTest.h:215
virtual std::string title() const
Definition: BaseTest.h:59
virtual void onEnter() override
Definition: BaseTest.cpp:430