-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoadingScene.cpp
133 lines (112 loc) · 3.44 KB
/
LoadingScene.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
#include "LoadingScene.h"
#include "tools/GameUtils.h"
#include "tools/AnimationUtils.h"
#include "UserData.h"
#include "SceneRule.h"
#include "ResourceManager.h"
#include "FightScene.h"
LoadingScene::LoadingScene()
{
}
LoadingScene::~LoadingScene()
{
}
Scene* LoadingScene::createScene()
{
auto scene = Scene::create();
auto layer = LoadingScene::create();
scene->addChild(layer);
return scene;
}
void LoadingScene::onEnter()
{
Layer::onEnter();
setPosition(INSTANCE(GameUtils)->getWindowsCenterPosition());
startLoad();
}
void LoadingScene::onExit()
{
Layer::onExit();
removeAllChildren();
}
bool LoadingScene::init()
{
if (!Layer::init())
{
return false;
}
initContent();
return true;
}
void LoadingScene::initContent()
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/loadingScene/loadingScene.plist");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/loadingScene/loading_fire.plist");
auto role = Sprite::createWithSpriteFrameName("loadingScene_role.png");
addChild(role);
role->setPosition(Vec2(240,0));
auto logo = Sprite::createWithSpriteFrameName("loadingScene_logo.png");
addChild(logo);
logo->setPosition(Vec2(0,20));
frame = Sprite::createWithSpriteFrameName("loadingScene_frame.png");
addChild(frame);
frame->setPosition(Vec2(0,-150));
loadingBar = ProgressTimer::create(Sprite::createWithSpriteFrameName("loadingScene_bar.png"));
loadingBar->setType(ProgressTimer::Type::BAR);
loadingBar->setBarChangeRate(Vec2::ANCHOR_BOTTOM_RIGHT);
loadingBar->setMidpoint(Vec2::ZERO);
loadingBar->setPercentage(0.0f);
frame->addChild(loadingBar);
loadingBar->setPosition(getCenterPosition(frame));
fire = Sprite::createWithSpriteFrameName("loading_fire_0000.png");
fire->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
frame->addChild(fire);
fire->setPosition(Vec2(50,frame->getContentSize().height/2));
auto animate = Animate::create(
INSTANCE(AnimationUtils)->getCommonAnimation("ui/loadingScene/loading_fire.plist","loading_fire",0.07f));
auto repeat = RepeatForever::create(animate);
fire->runAction(repeat);
}
void LoadingScene::startLoad()
{
int sceneId = INSTANCE(UserData)->getCurrentSceneId();
//npc资源,soSoldierXml有点问题需要修改
vector<int> npcId = INSTANCE(SceneRule)->getAllNpcId(sceneId);
for (auto id : npcId)
{
vector<string> tv = INSTANCE(ResourceManager)->getNpcResourceImg(id);
resourceVector.insert(resourceVector.end(),tv.begin(),tv.end());
}
//主角资源
vector<string> rv = INSTANCE(ResourceManager)->getRoleResourceImg(INSTANCE(UserData)->getCurrentRoleId());
resourceVector.insert(resourceVector.end(),rv.begin(),rv.end());
//地图和UI资源
resourceVector.push_back(string("maps/")+Value(sceneId).asString()+".png");
resourceVector.push_back("ui/fightUI/fightUI.png");
currentLoadNum = 0;
for (auto id : resourceVector)
{
Director::getInstance()->getTextureCache()->addImageAsync(id,CC_CALLBACK_0(LoadingScene::loadHandler,this));
}
//for(auto x : resourceVector)
//{
// log("%s",Value(x).asString().c_str());
//}
}
void LoadingScene::loadHandler()
{
currentLoadNum++;
loadingBar->setPercentage(100.0f * (float)currentLoadNum / resourceVector.size());
int py = 25 + 545 *(loadingBar->getPercentage()/100);
if(py >= 50)
{
fire->setPosition(Vec2(py, fire->getPositionY()));
}
//如果资源加载完成,进入到战斗界面
if (currentLoadNum >= resourceVector.size())
{
resourceVector.clear();
//进入到战斗界面
Director::getInstance()->replaceScene(FightScene::createScene());
}
}