-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGateScene.cpp
More file actions
328 lines (296 loc) · 10.8 KB
/
Copy pathGateScene.cpp
File metadata and controls
328 lines (296 loc) · 10.8 KB
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
#include "GateScene.h"
#include <string>
#include "SimpleAudioEngine.h"
#include "GameOverScene.h"
using namespace std;
using namespace CocosDenshion;
int Gate::Score = 0;
void Gate::setPhysicsWorld(PhysicsWorld* world) { m_world = world; }
//物理世界
Scene* Gate::createScene()
{
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setGravity(Point(0, 0));
auto layer = Gate::create(scene->getPhysicsWorld());
//创建物理世界场景
scene->addChild(layer);
return scene;
}
bool Gate::init(PhysicsWorld* world)
{
if (!Layer::init())
{
return false;
}
Score = 0;
this->setPhysicsWorld(world);
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
//调用加载函数
preloadMusic();
playBgm();
addBackground();
addball();
addContactListener();
addKeyboardListener();
this->schedule(schedule_selector(Gate::update), 0.1f, kRepeatForever, 0);
//每0.1秒更新一次
return true;
}
//预加载以及播放音乐
void Gate::preloadMusic()
{
SimpleAudioEngine::getInstance()->preloadBackgroundMusic("music/backgroundmusic3.mp3");
}
void Gate::playBgm()
{
SimpleAudioEngine::getInstance()->playBackgroundMusic("music/backgroundmusic3.mp3", true);
}
void Gate::addBackground()
{
//通过TMXTiledMap加载自己创作的砖块地图
TMXTiledMap* tmx = TMXTiledMap::create("map4.tmx");
tmx->setPosition(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);
tmx->setAnchorPoint(Vec2(0.5, 0.5));
this->addChild(tmx, 0);
TMXObjectGroup *objGroup = tmx->objectGroupNamed("objects");
ValueVector tmxBlocks = objGroup->getObjects();
blocknum = tmxBlocks.size();
//分为两种砖块,有特殊颜色的砖块额外加分(Tag不同)
for (int i = 0; i < blocknum; i++)
{
ValueMap objInfo = tmxBlocks.at(i).asValueMap();
float posx = objInfo.at("x").asFloat();
float posy = objInfo.at("y").asFloat();
if (i == 60 || i == 14 || i == 8) {
Sprite* block = Sprite::create("block1.png");
block->setAnchorPoint(Vec2(0.5, 0.5));
block->setPhysicsBody(PhysicsBody::createBox(block->getContentSize(), PhysicsMaterial(0.0f, 1.0f, 0.0f)));
block->setPosition(origin + Vec2(posx + 150, posy + 30) + tmx->getContentSize() / 7);
block->getPhysicsBody()->setTag(9);
block->setColor(ccc3(27, 193, 13));
block->getPhysicsBody()->setDynamic(false);
block->getPhysicsBody()->setContactTestBitmask(0x01);
addChild(block);
}
else {
Sprite* block = Sprite::create("block1.png");
block->setAnchorPoint(Vec2(0.5, 0.5));
block->setPhysicsBody(PhysicsBody::createBox(block->getContentSize(), PhysicsMaterial(0.0f, 1.0f, 0.0f)));
block->setPosition(origin + Vec2(posx + 150, posy + 30) + tmx->getContentSize() / 7);
block->getPhysicsBody()->setTag(4);
block->getPhysicsBody()->setDynamic(false);
block->getPhysicsBody()->setContactTestBitmask(0x01);
addChild(block);
}
}
//记分
TTFConfig ttfConfig;
ttfConfig.fontFilePath = "fonts/arial.ttf";
ttfConfig.fontSize = 24;
CCString *str = CCString::createWithFormat("Score : %d", Score);
std::string ScoreStr = str->getCString();
ScoreLabel = Label::createWithTTF(ttfConfig, ScoreStr);
ScoreLabel->setPosition(Vec2(origin.x + visibleSize.width - ScoreLabel->getContentSize().width * 2,
origin.y + visibleSize.height - ScoreLabel->getContentSize().height));
addChild(ScoreLabel);
auto edgeSp = Sprite::create();
auto boundBody = PhysicsBody::createEdgeBox(visibleSize, PhysicsMaterial(0.0f, 1.0f, 0.0f));
boundBody->setDynamic(false);
boundBody->setTag(0);
edgeSp->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
edgeSp->setPhysicsBody(boundBody);
edgeSp->getPhysicsBody()->setTag(3);
edgeSp->getPhysicsBody()->setContactTestBitmask(0x10);
this->addChild(edgeSp);
//背景图片
auto bgmap = Sprite::create("background3.jpg");
bgmap->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
Size mywinsize = Director::getInstance()->getWinSize();
float winw = mywinsize.width; //获取屏幕宽度
float winh = mywinsize.height;//获取屏幕高度
float spx = bgmap->getTextureRect().getMaxX();
float spy = bgmap->getTextureRect().getMaxY();
bgmap->setScaleX(winw / spx); //设置精灵宽度缩放比例
bgmap->setScaleY(winh / spy);
this->addChild(bgmap, -1);
//底下的边界
auto deadSp = Sprite::create("deadbar.png");
deadSp->setAnchorPoint(Vec2(0.5, 0.5));
deadSp->setScaleX(visibleSize.width / deadSp->getContentSize().width);
deadSp->setPhysicsBody(PhysicsBody::createBox(deadSp->getContentSize()));
deadSp->setPosition(origin.x + visibleSize.width / 2, origin.y + deadSp->getContentSize().height);
deadSp->getPhysicsBody()->setDynamic(false);
deadSp->getPhysicsBody()->setTag(5);
deadSp->getPhysicsBody()->setContactTestBitmask(0x01);
addChild(deadSp);
//板
player = Sprite::create("player.jpg");
player->setScaleX(1.5f);
player->setAnchorPoint(Vec2(0.5, 0.5));
player->setPhysicsBody(PhysicsBody::createBox(player->getContentSize(), PhysicsMaterial(0.0f, 1.0f, 0.0f)));
player->setPosition(origin.x + visibleSize.width / 2, origin.y + player->getContentSize().height / 2);
player->getPhysicsBody()->setTag(1);
player->getPhysicsBody()->setAngularVelocityLimit(0);
player->getPhysicsBody()->setRotationEnable(false);
player->getPhysicsBody()->setMass(10000);
player->getPhysicsBody()->setContactTestBitmask(0x10);
addChild(player);
}
//球
void Gate::addball()
{
ball = Sprite::create("irolball.png");
ball->setPosition(origin.x + visibleSize.width / 2, origin.y + ball->getContentSize().height);
ball->setPhysicsBody(PhysicsBody::createCircle(ball->getContentSize().height / 2, PhysicsMaterial(0.0f, 1.0f, 0.0f)));
ball->getPhysicsBody()->setTag(2);
ball->getPhysicsBody()->setVelocity(Vec2(0, 300));
ball->getPhysicsBody()->applyImpulse(Vec2(50, 10));
ball->getPhysicsBody()->setMass(10);
ball->getPhysicsBody()->setContactTestBitmask(0x01);
addChild(ball);
}
void Gate::fireBoom() {
/* 创建背景精灵 */
Sprite* bkSnow = Sprite::create("bkSky.jpg");
bkSnow->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(bkSnow);
//烟花效果
CCParticleSystem* particleSystem1 = CCParticleFireworks::create();
particleSystem1->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
particleSystem1->setPosition(Point(80, 0));
addChild(particleSystem1);
CCParticleSystem* particleSystem2 = CCParticleFireworks::create();
particleSystem2->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
particleSystem2->setPosition(Point(240, 0));
addChild(particleSystem2);
CCParticleSystem* particleSystem3 = CCParticleFireworks::create();
particleSystem3->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
particleSystem3->setPosition(Point(400, 0));
addChild(particleSystem3);
return;
}
void Gate::addContactListener()
{
auto touchListener = EventListenerPhysicsContact::create();
touchListener->onContactBegin = CC_CALLBACK_1(Gate::onConcactBegan, this);
_eventDispatcher->addEventListenerWithFixedPriority(touchListener, 1);
}
void Gate::addKeyboardListener()
{
auto keboardListener = EventListenerKeyboard::create();
keboardListener->onKeyPressed = CC_CALLBACK_2(Gate::onKeyPressed, this);
keboardListener->onKeyReleased = CC_CALLBACK_2(Gate::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keboardListener, this);
}
//通过Tag碰撞检测
bool Gate::onConcactBegan(PhysicsContact& contact) {
auto node1 = contact.getShapeA()->getBody();
auto node2 = contact.getShapeB()->getBody();
if (node1 && node2)
{
if ((node1->getTag() == 2 && node2->getTag() == 4) || (node1->getTag() == 4 && node2->getTag() == 2))
{
if (node1->getTag() == 2 && node2->getTag() == 4)
{
node2->getNode()->removeFromParentAndCleanup(true);
}
else if (node1->getTag() == 4 && node2->getTag() == 2)
{
node1->getNode()->removeFromParentAndCleanup(true);
}
blocknum = blocknum - 1;
Score = Score + 12;
CCString *strss = CCString::createWithFormat("Score : %d", Score);
std::string ScoreStrrr = strss->getCString();
ScoreLabel->setString(ScoreStrrr);
if (blocknum == 0)
{
CCDirector::sharedDirector()->replaceScene(GameOverScene::createScene());
}
}
if ((node1->getTag() == 2 && node2->getTag() == 9) || (node1->getTag() == 9 && node2->getTag() == 2)) {
if (node1->getTag() == 2 && node2->getTag() == 9) {
node2->getNode()->removeFromParentAndCleanup(true);
}
else if (node1->getTag() == 9 && node2->getTag() == 2)
{
node1->getNode()->removeFromParentAndCleanup(true);
}
blocknum -= 1;
Score = Score + 50;
CCString *strsqw = CCString::createWithFormat("Score : %d", Score);
std::string ScoreStrqw = strsqw->getCString();
ScoreLabel->setString(ScoreStrqw);
if (blocknum == 0)
{
CCDirector::sharedDirector()->replaceScene(GameOverScene::createScene());
}
}
if ((node1->getTag() == 1 && node2->getTag() == 3) || (node1->getTag() == 3 && node2->getTag() == 1)) {
if (node1->getTag() == 1 && node2->getTag() == 3)
node1->setVelocity(Vec2(0, 0));
else if (node1->getTag() == 3 && node2->getTag() == 1)
node2->setVelocity(Vec2(0, 0));
}
if ((node1->getTag() == 2 && node2->getTag() == 5) || (node1->getTag() == 5 && node2->getTag() == 2)) {
CCDirector::sharedDirector()->replaceScene(GameOverScene::createScene());
}
}
return true;
}
//更新函数
void Gate::update(float f)
{
float velx = ball->getPhysicsBody()->getVelocity().x;
float vely = ball->getPhysicsBody()->getVelocity().y;
if (velx > 300 || vely > 300)
{
if (velx > 300)
ball->getPhysicsBody()->setVelocity(Vec2(300, vely));
else if (vely > 300)
ball->getPhysicsBody()->setVelocity(Vec2(velx, 300));
}
else if (velx < -300 || vely < -300)
{
if (velx < -300)
ball->getPhysicsBody()->setVelocity(Vec2(-300, vely));
else if (vely < -300)
ball->getPhysicsBody()->setVelocity(Vec2(velx, -300));
}
else if (velx > -50 && velx < 50)
{
ball->getPhysicsBody()->setVelocity(Vec2(3 * velx, vely));
}
else if (vely > -50 && vely < 50)
{
ball->getPhysicsBody()->setVelocity(Vec2(velx, 3 * vely));
}
}
//键盘按下
void Gate::onKeyPressed(EventKeyboard::KeyCode code, Event* event) {
switch (code) {
case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW:
player->getPhysicsBody()->setVelocity(Point(-300, player->getPhysicsBody()->getVelocity().y));
break;
case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
player->getPhysicsBody()->setVelocity(Point(300, player->getPhysicsBody()->getVelocity().y));
break;
default:
break;
}
}
//键盘放开
void Gate::onKeyReleased(EventKeyboard::KeyCode code, Event* event) {
player->getPhysicsBody()->setVelocity(Vec2(0, 0));
}
Gate * Gate::create(PhysicsWorld * world) {
Gate* pRet = new(std::nothrow) Gate();
if (pRet && pRet->init(world)) {
pRet->autorelease();
return pRet;
}
delete pRet;
pRet = NULL;
return NULL;
}