-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloWorldScene.cpp
244 lines (197 loc) · 6.3 KB
/
HelloWorldScene.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
#include "HelloWorldScene.h"
#include"UserData.h"
#include <algorithm>
USING_NS_CC;
HelloWorld::~HelloWorld()
{
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
}
void HelloWorld::initLevelDataList()
{
auto str = FileUtils::getInstance()->getStringFromFile("leveldata.csv");
std::vector<std::string> rowList;
char *row = strtok((char*)str.c_str(), "\n");
while (row)
{
rowList.push_back(row);
row = strtok(nullptr, "\n");
}
for (auto row = rowList.begin(); row != rowList.end(); ++row) {
char* rows = strtok((char*)row->c_str(), ",");
char* columns = strtok(nullptr, ",");
char* loss = strtok(nullptr, ",");
if (rows == nullptr || columns == nullptr || loss == nullptr) {
continue;
}
LevelData level;
level.rows = atoi(rows);
level.columns = atoi(columns);
level.loss = atoi(loss);
if (level.rows*level.columns <= 0 || (level.rows*level.columns) % 2 != 0 || level.loss<0) {
continue;
}
_levelDataList.push_back(level);
}
_allLevel = static_cast<int>(_levelDataList.size());
}
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
initLevelDataList();
SimpleAudioEngine::getInstance()->stopBackgroundMusic();
// 预加载背景音乐和音效
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("music/va1.mp3");
isPause = false;
// 先默认播放背景音乐
SimpleAudioEngine::sharedEngine()->playBackgroundMusic("music/va1.mp3", true);
// SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(1.0);
_nowLevel = -1;
initUI();
nextLevel();
timer = ScoreTimer::create();
this->addChild(timer);
return true;
}
void HelloWorld::initUI()
{
Size viewSize = Director::getInstance()->getVisibleSize();
Layer* background = LayerColor::create(Color4B(255,255,255,128));
addChild(background);
// 添加 声音 的开关按钮
auto _turnOn = MenuItemImage::create(
"button_voi_on.png",
"button_voi_on.png");
auto _turnOff = MenuItemImage::create(
"button_voi_off.png",
"button_voi_off.png");
MenuItemToggle *toggleItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(HelloWorld::menuMusicCallback, this), _turnOn, _turnOff, NULL);
toggleItem->setScale(0.3f);
toggleItem->setPosition(Point(viewSize.width - 50, 50));
auto menu = Menu::create(toggleItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu);
_pauseButton = ui::Button::create("pause.png");
_pauseButton->setAnchorPoint(Vec2(0, 1));
_pauseButton->setPosition(Vec2(0, viewSize.height));
this->addChild(_pauseButton);
_pauseButton->addClickEventListener([this](Ref* ref){
timer->stopTimer();
auto pauseBox = Pause::create();
pauseBox->registerCallback([this, pauseBox](){
timer->startTimer();
pauseBox->removeFromParent();
//this->scheduleUpdate();
//update用来根据时间计分,这里先不用
}, [this](){
gameOver();
});
this->addChild(pauseBox);
});
}
void HelloWorld::nextLevel()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
_nowLevel++;
if (_nowLevel >= _allLevel) {
gameOver();
}
_nowLevelData = _levelDataList[_nowLevel];
photo = CuttingPhotos::create("psu.jpg", _nowLevelData.rows, _nowLevelData.columns);
photo->setAnchorPoint(Vec2(0.5, 0.5));
auto size = photo->getContentSize();
float scale = std::min((visibleSize.width - 100) / size.width, (visibleSize.height - 100) / size.height);
photo->setScale(scale);
//缩放以后真实锚点位置仍是相对于原图的位置
photo->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
addChild(photo);
CallbackTimeCounter* counter = CallbackTimeCounter::create();
this->addChild(counter);
photo->registerCallFunc([this,counter](){
Size visibleSize = Director::getInstance()->getVisibleSize();
auto particle = ParticleSystemQuad::create("ExplodingRing.plist");
particle->setPosition(visibleSize.width / 2, visibleSize.height / 2);
particle->setAutoRemoveOnFinish(true);
this->addChild(particle);
auto ye=SimpleAudioEngine::getInstance()->playEffect("music/victory_screen_start.mp3");
//等待2秒再切下一关
counter->start(2.0f,[this](){
this->removeChild(photo);
this->nextLevel();
});
});
}
void HelloWorld::gameOver()
{
timer->stopTimer();//这里暂时没有处理time值是负数的情况
UserDefault::getInstance()->setIntegerForKey(NEW_SCORE, _nowLevel);
UserDefault::getInstance()->setIntegerForKey(NEW_TIME, timer->getTime());
std::vector<int> scoreList;
std::vector<int> timeList;
for (int j = 0; j<5; j++) {
int score = UserDefault::getInstance()->getIntegerForKey(StringUtils::format("%s%d", RANK_SCORE, j).c_str(), 0);
int time = UserDefault::getInstance()->getIntegerForKey(StringUtils::format("%s%d", CONSUME_TIME, j).c_str(), 10000);
scoreList.push_back(score);
timeList.push_back(time);
}
scoreList.push_back(_nowLevel);
timeList.push_back(timer->getTime());
/*
std::sort(scoreList.begin(), scoreList.end(), [](int &a, int &b){
return a>b;
});
*/
int i;
for (i = scoreList.size() - 2; i >= 0; i--) {
if (_nowLevel < scoreList[i]){
break;
}
}
for (i++; i < scoreList.size() - 1; i++)
{
if (timer->getTime() < timeList[i])break;
if (_nowLevel>scoreList[i])break;
}
for (int j = scoreList.size() - 2; j >i; j--)
{
scoreList[j + 1] = scoreList[j];
timeList[j + 1] = timeList[j];
}
scoreList[i] = _nowLevel;
timeList[i] = timer->getTime();
for (i = 0; i < 5; i++){
UserDefault::getInstance()->setIntegerForKey(StringUtils::format("%s%d", RANK_SCORE, i).c_str(), scoreList[i]);
UserDefault::getInstance()->setIntegerForKey(StringUtils::format("%s%d", CONSUME_TIME, i).c_str(),timeList[i] );
}
SceneMediator::getInstance()->gotoScoreScene();
}
void HelloWorld::menuMusicCallback(cocos2d::Ref* pSender)
{
if (isPause == false)
{
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
isPause = true;
}
else
{
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
isPause = false;
}
}