-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayLayer.cpp
328 lines (288 loc) · 10.7 KB
/
PlayLayer.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
#include "PlayLayer.h"
#include "EndLayer.h"
#include "Popup.h"
//电脑上运行的时候设为True,点一下小人就可以跳跃,发布到手机端的时候设成False
#define onComputer false
PlayLayer::PlayLayer()
:score(0),
level(1),
target(3000),
animation(NULL),
animate(NULL),
hero(NULL),
bgSprite1(NULL),
bgSprite2(NULL),
groundSprite1(NULL),
groundSprite2(NULL),
man(NULL)
{
}
Scene *PlayLayer::createScene()
{
auto scene = Scene::createWithPhysics();
//最后的参数DEBUGDRAW_ALL影响的是显示的红框
//scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
scene->getPhysicsWorld()->setGravity(Vect(0,-600));
/*
//创建一个边界
Size visibleSize = Director::getInstance()->getVisibleSize();
//使用PhysicalsBody的create方法创建自己想要的物体。
//PHYSICSBODY_MATERIAL_DEFAULT宏表示的是创建的Body的默认材质,3是边线宽度。编译运行我们会看到场景边上有红色的边界。
auto body = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
auto edgeNode = Node::create();
edgeNode->setPosition(Point(visibleSize.width / 2, visibleSize.height /2));
edgeNode->setPhysicsBody(body);
scene->addChild(edgeNode);
*/
auto layer = PlayLayer::create();
layer->setPhyWorld(scene->getPhysicsWorld());
layer->setTag(13);
scene->addChild(layer);
return scene;
}
bool PlayLayer::init()
{
if (!Layer::init()) {
return false;
}
//start from here
man = Manager::create();
this->addChild(man, 3);
initBG();
//开启update
this->scheduleUpdate();
hero = Runner::create();
this->addChild(hero,11);
hero->setPosition(300, 350);
hero->Run();
//设置为单点响应
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PlayLayer::onTouchBegan, this);
if (!onComputer)
touchListener->onTouchMoved = CC_CALLBACK_2(PlayLayer::onTouchMoved, this);// 触摸移动时触发
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(PlayLayer::onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
//响应键盘消息
auto keyListener = EventListenerKeyboard::create();
keyListener->onKeyReleased = CC_CALLBACK_2(PlayLayer::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this);
return true;
}
void PlayLayer::initBG(){
auto visibleSize = Director::getInstance()->getVisibleSize();
//解决手机端的背景不显示的问题
Point origin = Director::getInstance()->getVisibleOrigin();
//初始化雾霾
this->haze = Sprite::create("OverBG.png");
//设置透明度
haze->setOpacity(210);
haze->setPosition(visibleSize.width / 2, visibleSize.height / 2);
this->addChild(haze, 10);
//根据当前关卡选择不同的背景
int bgid = level % 4 - 1;
//背景1
bgSprite1 = Sprite::create(CCString::createWithFormat("bg/bg%d.png", bgid)->getCString());
bgSprite1->setPosition(origin.x + visibleSize.width / 2, origin.y+visibleSize.height / 2);
this->addChild(bgSprite1,1);
//地面1
groundSprite1 = Sprite::create("ground.png");
groundSprite1->setTag(11);
groundSprite1->setPosition(origin.x + visibleSize.width / 2, origin.y + groundSprite1->getContentSize().height / 2);
this->man->height = groundSprite1->getPositionY()+groundSprite1->getContentSize().height;
auto body1 = PhysicsBody::createBox(groundSprite1->getContentSize());
//弹性设为0,奔跑的东西不会颠倒
//body1->getShape(0)->setRestitution(0);
body1->setDynamic(false);
groundSprite1->setPhysicsBody(body1);
this->addChild(groundSprite1,2);
//背景2
bgSprite2 = Sprite::create(CCString::createWithFormat("bg/bg%d.png", bgid+1)->getCString());
bgSprite2->setPosition(bgSprite1->getContentSize().width + visibleSize.width / 2, visibleSize.height / 2);
this->addChild(bgSprite2,1);
//地面2
groundSprite2 = Sprite::create("ground.png");
groundSprite2->setTag(12);
groundSprite2->setPosition(bgSprite1->getContentSize().width + visibleSize.width / 2, groundSprite2->getContentSize().height / 2);
auto body2 = PhysicsBody::createBox(groundSprite2->getContentSize());
body2->setDynamic(false);
//弹性设为0,奔跑的东西不会颠倒
//body2->getShape(0)->setRestitution(0);
groundSprite2->setPhysicsBody(body2);
this->addChild(groundSprite2,2);
//关卡
m_level = LabelTTF::create("Level 1","fonts/jokerman.ttf", 48);
m_level->setPosition(visibleSize.width/2,visibleSize.height-30);
m_level->setColor(Color3B::YELLOW);
this->addChild(m_level,12);
//分数背景
auto board_score = Sprite::create("board_score.png");
auto board_target = Sprite::create("board_target.png");
board_score->setPosition(board_score->getContentSize().width/2,
visibleSize.height-board_score->getContentSize().height*2/3);
board_target->setPosition(visibleSize.width-board_target->getContentSize().width/2,
visibleSize.height-board_target->getContentSize().height*2/3);
this->addChild(board_target,13);
this->addChild(board_score,13);
//分数和目标
m_score = LabelTTF::create("distance", "fonts/jokerman.ttf", 48);
m_score->setPosition(board_score->getContentSize().width/2,
visibleSize.height-board_score->getContentSize().height*2/3-10);
m_score->setColor(Color3B(139, 248, 178));
this->addChild(m_score,14);
m_target = LabelTTF::create(CCString::createWithFormat("%d",target)->getCString(), "fonts/jokerman.ttf", 48);
m_target->setPosition(visibleSize.width-board_target->getContentSize().width/2,
visibleSize.height-board_target->getContentSize().height*2/3-10);
m_target->setColor(Color3B(255,209,27));
this->addChild(m_target,14);
//能量背景
auto board_energy = Sprite::create("board_energy.png");
board_energy->setPosition(visibleSize.width/2,visibleSize.height-board_energy->getContentSize().height*3/2+10);
this->addChild(board_energy,12);
//能量
proBar = ProgressTimer::create(Sprite::create("energy.png"));
proBar->setPosition(visibleSize.width/2+18,visibleSize.height-board_energy->getContentSize().height*3/2+10);
proBar->setMidpoint(Point(0, 0));
//从左向右递减
proBar->setBarChangeRate(Point(1, 0));
proBar->setType(ProgressTimer::Type::BAR);
proBar->setPercentage(60);
this->addChild(proBar,15);
}
//背景向后移动
void PlayLayer::update(float dt){
int posX1 = bgSprite1->getPositionX();
int posX2 = bgSprite2->getPositionX();
float timer = proBar->getPercentage();
//背景向后移动
posX1 -= BACKGROUND_SPEED;
posX2 -= BACKGROUND_SPEED;
//距离增加
this->score += 1;
this->m_score->setString(CCString::createWithFormat("%d",score)->getCString());
//能量(时间)条减少
timer -= 0.05;
proBar->setPercentage(timer);
auto mapSize = bgSprite1->getContentSize();
if (posX1 < -mapSize.width / 2){
posX1 = mapSize.width + mapSize.width / 2;
posX2 = mapSize.width / 2;
this->man->height = groundSprite2->getPositionY()+groundSprite2->getContentSize().height;
}
if (posX2 < -mapSize.width / 2){
posX2 = mapSize.width + mapSize.width / 2;
posX1 = mapSize.width / 2;
this->man->height = groundSprite1->getPositionY()+groundSprite1->getContentSize().height;
}
bgSprite1->setPositionX(posX1);
bgSprite2->setPositionX(posX2);
groundSprite1->setPositionX(posX1);
groundSprite2->setPositionX(posX2);
//检查是否游戏结束 移除屏幕外或者时间条到了
if (hero->getPositionX()< 0 || timer <= 0){
//Director::getInstance()->end();
//跳转到失败界面
Scene* newScene = EndLayer::createScene();
EndLayer* layer = (EndLayer*)(newScene->getChildren().at(0));
layer->setNext(false,0,0);
layer->end_score->setString(CCString::createWithFormat("%d",score)->getCString());
layer->target->setString(CCString::createWithFormat("%d",target)->getCString());
layer->level->setString(CCString::createWithFormat("%d",level)->getCString());
Director::sharedDirector()->replaceScene(newScene);
}
//围住小雨滴
if (this->score >= this->target){
Scene* newScene = GameLayer::createScene();
GameLayer* layer = (GameLayer*)(newScene->getChildren().at(0));
//设定下一关的初始值
layer->setLevel(this->level);
layer->setTarget(this->target);
layer->setScore(this->score);
Director::sharedDirector()->replaceScene(newScene);
}
}
//触摸回调
bool PlayLayer::onTouchBegan(Touch *touch, Event *unused){
if (onComputer)
this->hero->Jump();
else
startPoint = touch->getLocation().y;
return true;
}
void PlayLayer::onTouchMoved(Touch *touch, Event *unused){
auto location = touch->getLocation();
if (location.y > startPoint)
this->hero->Jump();
}
//碰撞回调
bool PlayLayer::onContactBegin(PhysicsContact& contact)
{
auto body_1 = (Sprite*)contact.getShapeA()->getBody()->getNode();
auto body_2 = (Sprite*)contact.getShapeB()->getBody()->getNode();
//碰到金币
if (body_1->getTag() == 2 && body_2->getTag() == 1){
if (body_1->isVisible()){
runEffect(body_2);
this->proBar->setPercentage(proBar->getPercentage() + 10);
}
body_1->setVisible(false);
}
//碰到虫子
else if (body_1->getTag() == 4 && body_2->getTag() == 1){
if (body_1->isVisible()){
body_2->runAction(Blink::create(0.5,3));
this->proBar->setPercentage(proBar->getPercentage() - 10);
}
body_1->setVisible(false);
}
else if (body_1->getTag() == 1 && body_2->getTag() == 4){
if (body_2->isVisible()){
body_1->runAction(Blink::create(0.5, 3));
this->proBar->setPercentage(proBar->getPercentage() - 10);
}
body_2->setVisible(false);
}
return true;
}
void PlayLayer::runEffect(Sprite* coin){
double time = 0.2;
//1.action for circle
auto circleSprite = Sprite::create("circle.png");
this->addChild(circleSprite, 10);
circleSprite->setPosition(coin->getPosition());
circleSprite->setScale(0);// start size
circleSprite->runAction(Sequence::create(ScaleTo::create(time, 2.0),
CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, circleSprite)),
NULL));
//2.particle effect
auto particleStars = ParticleSystemQuad::create("stars.plist");
particleStars->setAutoRemoveOnFinish(true);
particleStars->setBlendAdditive(false);
particleStars->setPosition(coin->getPosition());
particleStars->setScale(0.3);
this->addChild(particleStars, 20);
}
void PlayLayer::onKeyReleased(EventKeyboard::KeyCode keycode,Event* event){
if(keycode == EventKeyboard::KeyCode::KEY_BACKSPACE){
Director::sharedDirector()->pushScene(Popup::createScene());
}
}
void PlayLayer::setLevel(unsigned lev){
this->level = lev;
int bgid = (lev-1) % 4;
this->m_level->setString(CCString::createWithFormat("Level %d",this->level)->getCString());
this->bgSprite1->setTexture(CCString::createWithFormat("bg/bg%d.png", bgid)->getCString());
this->bgSprite2->setTexture(CCString::createWithFormat("bg/bg%d.png", bgid+1)->getCString());
}
void PlayLayer::setTarget(unsigned tar){
this->target = tar;
this->m_target->setString(CCString::createWithFormat("%d",target)->getCString());
}
Sprite* PlayLayer::getHaze(){
return this->haze;
}
ProgressTimer* PlayLayer::getProbar(){
return this->proBar;
}