-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestscene1.cpp
346 lines (285 loc) · 7.69 KB
/
testscene1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "stdafx.h"
#include "testscene1.h"
#include "globalvar.h"
#include "director.h"
#include "button.h"
#include <cmath>
#include "printText.h"
testscene1::testscene1(string name) :Scene(name), followSprite(0)
{
}
testscene1::~testscene1()
{
delete world;
}
void testscene1::init()
{
initPhysics();
srand(time(NULL)); //初始化随机数发生器
Sprite* S1 = new Sprite("HelloWorld.png", 0); //创建背景精灵
Sprite* S2 = new Sprite("CloseNormal.png", 0); //创建发射者
Button* btn = new Button("HelloWorld.png", 0); //创建按钮
Sprite* coinSprite = new Sprite("core2.png", 0);//金币精灵
coinSprite->setsize(50, 50);
coinSprite->setpos(200, 200);
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(200 / PTM_RATIO, 200 / PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
body->SetUserData(coinSprite);
bodyDef.bullet = false;
b2CircleShape circle;
circle.m_p.Set(0.0f, 0.0f);
circle.m_radius = 22.0f / PTM_RATIO;
// 夹具定义
b2FixtureDef fixtureDef;
fixtureDef.restitution = 0.8f;
//设置夹具的形状
fixtureDef.shape = &circle;
//设置密度
fixtureDef.density = 1.0f;
//设置摩擦系数
fixtureDef.friction = 0.0f;
fixtureDef.isSensor=true;
//使用夹具固定形状到物体上
body->CreateFixture(&fixtureDef);
world->SetContactListener(this);
Size btnSize; //设置按钮尺寸
btnSize.height = 100;
btnSize.width = 100;
btn->setsize(btnSize);
btn->setRectSize(btnSize);
btn->setType(RECTANGLE); //设置按钮类型(矩形)
btn->setpos(400, 400); //设置按钮位置
btn->setrotation(-0.8*M_PI); //设置旋转(逆时针为正)
S1->setflipY(true); //设置左右翻转
S1->setsize(WINDOW_HEIGHT, WINDOW_WIDTH);
S1->setpos(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
S2->setsize(100, 100);
S2->setpos(WINDOW_WIDTH / 2, WINDOW_HEIGHT/2);
S2->setrotation(0);
S2->setflipX(true);
//addchild(S1); //将背景加入到场景中使其显示出来
followSprite=addchild(S2); //保留ID
addchild(btn);
coin=addchild(coinSprite);
registerMouseEvent(S2);
registerMouseEvent(btn); //注册鼠标事件
Director::getTheInstance()->registerKey(VK_A); //注册键盘事件
Director::getTheInstance()->registerKey(VK_S);
Director::getTheInstance()->registerKey(VK_D);
Director::getTheInstance()->registerKey(VK_W);
Director::getTheInstance()->registerKey(VK_R);
//Director::getTheInstance()->registerMouseEvent((this), &testscene1::testCallback);
Director::getTheInstance()->registerEvent((this), &testscene1::addNewSpriteCallback);
btn->registerEvent(this, &testscene1::removeAllSprites, "remove");
}
void testscene1::clean()
{
//第一步:删除精灵
if (_sprites.size())
{
vector<Sprite*>::iterator it = _sprites.begin();
while (it != _sprites.end())
{
delete (*it);
it = _sprites.erase(it);
}
}
if (_keyBoardEventList.size())
{
vector<Sprite*>::iterator it = _keyBoardEventList.begin();
while (it != _keyBoardEventList.end())
{
it = _keyBoardEventList.erase(it);
}
}
if (_mouseEventList.size())
{
vector<Sprite*>::iterator it = _mouseEventList.begin();
while (it != _mouseEventList.end())
{
it = _mouseEventList.erase(it);
}
}
//第二步,删除世界
delete world;
}
void testscene1::update(float dt)
{
if (followSprite)
{
Sprite* c=getChildByID(followSprite);
///////////////////
//跟随鼠标
/*
POINT MOUSE = Director::getTheInstance()->getMousePos();
Size s = c->getsize();
c->setpos(MOUSE.x-s.width/2, MOUSE.y-s.height/2);
*/
Point pos=c->getpos();
if (_keys[VK_W])
{
pos.y += 5;
}
if (_keys[VK_S])
{
pos.y -= 5;
}
if (_keys[VK_D])
{
pos.x += 5;
}
if (_keys[VK_A])
{
pos.x -= 5;
}
c->setpos(pos);
if (_keys[VK_R])
{
c->setpos(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
}
Point mouse = Director::getTheInstance()->getMousePos();
c->setrotation(atan2(mouse.y-pos.y,mouse.x-pos.x)+90.0/180*M_PI);
}
float timeStep = dt;
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(timeStep, velocityIterations, positionIterations);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != nullptr) {
Sprite* sprite = (Sprite*)b->GetUserData();
Point pt;
pt.x = b->GetPosition().x * PTM_RATIO;
pt.y = b->GetPosition().y * PTM_RATIO;
sprite->setpos(pt);
float rot =b->GetAngle();
sprite->setrotation(rot);
}
else
{
break;
}
}
}
void testscene1::BeginContact(b2Contact* contact)
{
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();
Sprite* spriteA = (Sprite*)bodyA->GetUserData();
Sprite* spriteB = (Sprite*)bodyB->GetUserData();
if (spriteA&&spriteB)
{
if (spriteA->getid() == coin || spriteB->getid() == coin)
{
glRasterPos2f(0, 0);
glPrint("+1");
}
}
}
void testscene1::testCallback(EventMsg msg)
{
Sprite* c;
Point pt;
Size sz;
if (msg.name == "test")
{
switch (msg._event.type)
{
case DOWN:
case DRAG:
if (followSprite&&_sprites.size())
{
c = getChildByID(followSprite);
pt = msg._event.point;
sz = c->getsize();
c->setpos(pt);
}
break;
default:
break;
}
}
}
void testscene1::initPhysics()
{
b2Vec2 gravity;
gravity.Set(0.0f, 0.0f);
world = new b2World(gravity);
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);
b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2EdgeShape groundBox;
groundBox.Set(b2Vec2(0, 0), b2Vec2(WINDOW_WIDTH / PTM_RATIO, 0));
groundBody->CreateFixture(&groundBox, 0);
groundBox.Set(b2Vec2(0, WINDOW_HEIGHT / PTM_RATIO), b2Vec2(WINDOW_WIDTH / PTM_RATIO, WINDOW_HEIGHT / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
groundBox.Set(b2Vec2(0, WINDOW_HEIGHT / PTM_RATIO), b2Vec2(0, 0));
groundBody->CreateFixture(&groundBox, 0);
groundBox.Set(b2Vec2(WINDOW_WIDTH / PTM_RATIO, WINDOW_HEIGHT / PTM_RATIO), b2Vec2(WINDOW_WIDTH / PTM_RATIO, 0));
groundBody->CreateFixture(&groundBox, 0);
}
void testscene1::addNewSpriteCallback(EventMsg msg)
{
if (msg._event.type == DOWN&&followSprite)
{
Sprite* sprite = new Sprite("CloseNormal.png", 0);
addchild(sprite);
Sprite* c=getChildByID(followSprite);
Point pt = c->getpos();
Point p = msg._event.point;
sprite->setsize(32, 32);
sprite->setpos(pt);
float dir = atan2(p.y - pt.y, p.x - pt.x);
//物体定义
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(pt.x / PTM_RATIO, pt.y / PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
body->SetUserData(sprite);
bodyDef.bullet = true;
b2CircleShape circle;
circle.m_p.Set(0.0f, 0.0f);
circle.m_radius = 0.5f;
// 夹具定义
b2FixtureDef fixtureDef;
fixtureDef.restitution = 1.0f;
//设置夹具的形状
fixtureDef.shape = &circle;
//设置密度
fixtureDef.density = 1.0f;
//设置摩擦系数
fixtureDef.friction = 0.0f;
//使用夹具固定形状到物体上
body->CreateFixture(&fixtureDef);
//body->ApplyLinearImpulse(b2Vec2(rand() / (float)RAND_MAX * 10, rand() / (float)RAND_MAX * 10), b2Vec2(1.0f, 1.0f), true);
b2MassData massdata;
body->GetMassData(&massdata);
float Velocity = 20;
body->SetLinearVelocity(b2Vec2(Velocity*cos(dir),Velocity*sin(dir)));
body->SetAngularVelocity((0.5 - rand() / (float)RAND_MAX));
}
}
void testscene1::removeAllSprites(EventMsg msg)
{
if (msg.name == "remove")
{
for (b2Body* b = world->GetBodyList(); b; )
{
if (b->GetUserData() != nullptr) {
Sprite* sprite = (Sprite*)b->GetUserData();
b2Body* p = b;
b = b->GetNext();
world->DestroyBody(p);
removeChildByID(sprite->getid());
}
else
{
break;
}
}
}
}