/
StartGame.cpp
339 lines (303 loc) · 11.2 KB
/
StartGame.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
//
// StartGame.cpp
// goldMiner
//
// Created by student on 15/8/17.
//
//
#include "StartGame.h"
#include "Data.h"
#include "Tools.h"
#include "MIner.h"
#include "Hook.h"
Scene * StartGame:: secne(){
auto scene=Scene::create();
auto layer=StartGame::create();
scene->addChild(layer);
return scene;
}
StartGame::~ StartGame(){
hook->release();
miner->release();
itemArray->release();
}
bool StartGame:: init(){
if (!Layer::init()) {
return false;
}
itemArray=__Array::create();
itemArray->retain();
int k=arc4random()%6+1;
std::string str= StringUtils::format("mineBG%d.png",k);
auto visibleSize=Director::getInstance()->getVisibleSize();
auto sprite=Sprite::create(str);
sprite->setPosition(visibleSize.width/2,visibleSize.height/2);
this->addChild(sprite);
//离子
ParticleSystemQuad* quad = ParticleSystemQuad::create("fengye.plist");
quad->setBlendAdditive(true);
quad->setAutoRemoveOnFinish(true);
quad->setPosition(Vec2(400, 300));
this->addChild(quad);
//黄金
char a[50];
sprintf(a, "%d",score[level-1]);
auto sprite1=Sprite::create("Gold.png");
sprite1->setPosition(visibleSize.width/2-200,visibleSize.height/2+130);
auto lable1=Label::createWithSystemFont(a, "fonts/Marker Felt.ttf ", 28);
lable1->setTextColor(Color4B::GREEN);
lable1->setPosition(visibleSize.width/2-150,visibleSize.height/2+130);
lable1->setTag(10);
this->addChild(lable1);
this->addChild(sprite1);
//钱
auto sprite2=Sprite::create("rmbImage.png");
sprite2->setPosition(visibleSize.width/2-200,visibleSize.height/2+90);
auto lable2=Label::createWithSystemFont("0", "fonts/Marker Felt.ttf ", 28);
lable2->setPosition(visibleSize.width/2-150,visibleSize.height/2+90);
lable2->setTag(20);
lable2->setTextColor(Color4B::RED);
this->addChild(lable2);
this->addChild(sprite2);
//矿工
auto sprite3=Sprite::create("miner_0707.png");
sprite3->setPosition(visibleSize.width/2+180,visibleSize.height/2+130);
char c[50];
sprintf(c, "%d",level);
auto lable3=Label::createWithSystemFont(c, "fonts/Marker Felt.ttf ", 28);
lable3->setPosition(visibleSize.width/2+220,visibleSize.height/2+130);
lable3->setTag(30);
lable3->setTextColor(Color4B::GREEN);
this->addChild(lable3);
this->addChild(sprite3);
char d[50];
sprintf(d, "%d",totalTime[level-1]);
auto sprite4=Sprite::create("clockPlate.png");
sprite4->setTag(4);
sprite4->setPosition(visibleSize.width/2+180,visibleSize.height/2+90);
auto lable4=Label::createWithSystemFont(d, "fonts/Marker Felt.ttf ", 28);
lable4->setPosition(visibleSize.width/2+220,visibleSize.height/2+90);
lable4->setTag(40);
lable4->setTextColor(Color4B::GREEN);
this->addChild(lable4);
this->addChild(sprite4);
auto item=MenuItemImage::create("qianglishui.png", "qianglishui.png");
auto item1=MenuItemImage::create("qianglishui.png", "qianglishui.png");
toggle=MenuItemToggle::create();
toggle->addSubItem(item);
toggle->addSubItem(item1);
toggle->setScale(0.5);
toggle->setSelectedIndex(0);
toggle->setCallback([this](Ref*){
//减少PowerWater数量
auto lable=(Label*)this->getChildByTag(50);
int num= atoi(lable->getString().c_str());
if (num!=0){
Water--;
miner->setScale(1.5);
//this->schedule(schedule_selector(StartGame::changeSelect),8);
this->schedule(CC_CALLBACK_1(StartGame::changeSelect,this), 8, "changeSelect");
lable->setString(StringUtils::format("%d",Water));
this->testCollin(1.0);
}else if(num==0){
toggle->setSelectedIndex(0);
log("%d",toggle->getSelectedIndex());
}
});
menu=Menu::create(toggle, NULL);
menu->setPosition(Vec2(visibleSize.width/2+180, visibleSize.height/2+50));
std::string str1= StringUtils::format("%d",Water);
lable5=Label::createWithSystemFont(str1,"fonts/Marker Felt.ttf", 28);
lable5->setPosition(Vec2(visibleSize.width/2+220, visibleSize.height/2+50));
lable5->setTag(50);
lable5->setTextColor(Color4B::GREEN);
this->addChild(lable5);
this->addChild(menu);
if (Boom) {
auto item2=MenuItemImage::create("article_2001.png", "article_2001.png");
item2->setScale(1.3);
item2->setCallback(CC_CALLBACK_0(StartGame::removeBoom, this));
auto menu=Menu::create(item2,NULL);
menu->setPosition(Vec2(visibleSize.width/2+220, visibleSize.height/2+10));
menu->setTag(60);
this->addChild(menu);
}
miner=new Miner();
miner->setPosition(visibleSize.width/2,visibleSize.height/2);
this->addChild(miner);
hook=Hook::createHook();
hook->setAnchorPoint(Point(0.5, 1));
hook->setPosition(visibleSize.width/2-5,visibleSize.height/2-20);
hook->rotate();
this->addChild(hook,1);
//添加石头 黄金
this->item();
auto dispather=Director::getInstance()->getEventDispatcher();
auto listener=EventListenerTouchOneByOne::create();
listener->onTouchBegan=CC_CALLBACK_2(StartGame::onTouchBegan, this);
//点击事件
listener->onTouchEnded=[this](Touch *touch, Event *unused_event){
if(hook->getPosition().x==235 && hook->getPosition().y == 140){
hook->stopAllActions();
hook->runTarget();
}
};
dispather->addEventListenerWithSceneGraphPriority(listener ,this);
this->schedule(CC_CALLBACK_1(StartGame::consumeTime,this), 1, "consumTime");
this->schedule(schedule_selector(StartGame::testCollin), 0.01);
return true;
}
void StartGame::removeBoom(){
auto menu=this->getChildByTag(60);
menu->removeFromParent();
Boom=1;
log("Boom=%d",Boom);
}
void StartGame:: changeSelect(float dt){
log("changeSelect");
if (toggle->getSelectedIndex()==1) {
toggle->setSelectedIndex(0);
miner->setScale(1);
unschedule("changeSelect");
}else{
return;
}
}
//碰撞检测
void StartGame::testCollin(float dt){
auto visibleSize=Director::getInstance()->getWinSize();
__Array *tempItemArray=__Array::create();
tempItemArray->retain();
Item *item;
for (int i=0; i<itemArray->count(); i++) {
item=(Item*)itemArray->getObjectAtIndex(i);
if (Tools::isCollision(hook, item)&&!hook->hookAction&&!item->itemAction) {
hook->stopAllActions();
miner->MinerAction();
tempItemArray->addObject(item);
hook->hookAction = true;
item->itemAction=true;
if (toggle->getSelectedIndex()==1) {
dt=0.5;
}else{
dt=2.0;
}
auto move=MoveTo::create(dt, Vec2(visibleSize.width/2-5,visibleSize.height/2-20));
auto call=CallFunc::create(CC_CALLBACK_0(StartGame::move, this));
auto sequence=Sequence::create(move,call, NULL);
hook->runAction(sequence);
if (item->itemAction) {
item->setRotation(hook->getRotation());
item->setAnchorPoint(Point(0.5, 1));
hook->setAnchorPoint(Point(0.5, 0.7));
item->setPosition(hook->getPosition());
hook->setAnchorPoint(Point(0.5, 1));
}
if (item->_type==diamond||item->_type==secret) {
ParticleSystemQuad* quad = ParticleSystemQuad::create("Boom.plist");
quad->setBlendAdditive(true);
quad->setAutoRemoveOnFinish(true);
quad->setPosition(item->getPosition());
quad->setDuration(0.2);
this->addChild(quad);
}
auto move1=MoveTo::create(dt, Vec2(visibleSize.width/2-5,visibleSize.height/2-20));
auto call1=CallFuncN::create(CC_CALLBACK_1(StartGame::removeItem, this));
auto sequence1=Sequence::create(move1,call1, NULL);
item->runAction(sequence1);
}
}
itemArray->removeObjectsInArray(tempItemArray);
tempItemArray->release();
}
void StartGame:: move(){
hook->rotate();
}
void StartGame:: removeItem(Node *render){
auto item=static_cast<Item*>(render);
hook->hookAction=false;
item->itemAction=false;
item->removeFromParent();
miner->stopAllActions();
auto label=(Label*)this->getChildByTag(20);
int score= atoi(label->getString().c_str())+item->score;
label->setString(StringUtils::format("%d",score));
}
//画线
void StartGame:: draw(Renderer *renderer, const Mat4& transform,uint32_t flags){
_customCommand.init(11);
_customCommand.func =CC_CALLBACK_0(StartGame::onDraw, this,transform,flags);
renderer->addCommand(&_customCommand);
}
void StartGame:: onDraw( const cocos2d::Mat4 &transform, uint32_t flags){
Size s=Director::getInstance()->getVisibleSize();
Director*director=Director::getInstance();
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
CHECK_GL_ERROR_DEBUG();
glLineWidth(6.0f);
DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
DrawPrimitives::drawLine(Vec2(s.width/2-5,s.height/2-20),hook->getPosition());
}
//闹钟
void StartGame::consumeTime(float dt){
auto lable1=(Label*)this->getChildByTag(40);
int time= atoi(lable1->getString().c_str())-1;
std::string str= StringUtils::format("%d",time);
lable1->setString(str);
if (time<=10) {
auto sprite1=(Sprite*)this->getChildByTag(4);
auto rotateto=RotateTo::create(0.1,10);
auto rotateto1=RotateTo::create(0.1,-10);
auto sequence=Sequence::create(rotateto,rotateto1, NULL);
sprite1->runAction(RepeatForever::create(sequence));
}
if (time==0) {
unschedule("consumTime");
auto lable2=(Label*)this->getChildByTag(20);
int lable2Num=atoi(lable2->getString().c_str());
if (lable2Num<score[level-1]) {
level=1;
}else{
level=level+1;
}
Director::getInstance()->pushScene(Shop::scene());
}
}
void StartGame:: item(){
auto num=itemNum[level-1];
this->chooseItem(gold,num);
this->chooseItem(stone,num);
auto num1=diamondNum[level-1];
this->chooseItem(diamond, num1);
auto num2=secretNum[level-1];
this->chooseItem(secret, num2);
}
void StartGame:: chooseItem(ItemType type,int count){
for (int i=0; i<count; i++) {
int x=arc4random()%400;
int y=arc4random()%100;
int k=arc4random()%3;
Item *item=new Item();
switch (k) {
case 0:
item->ItemLoaction(type, item_large);
break;
case 1:
item->ItemLoaction(type, item_middle);
break;
case 2:
item->ItemLoaction(type, item_small);
break;
default:
break;
}
item->setPosition(Vec2(x, y));
item->setAnchorPoint(Point(0.5, 1));
itemArray->addObject(item);
this->addChild(item,100);
}
}
bool StartGame:: onTouchBegan(Touch *touch, Event *unused_event){
return true;
}