forked from XingMo/ARPG2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapSwitcher.cpp
178 lines (142 loc) · 4.07 KB
/
MapSwitcher.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
#include "MapSwitcher.h"
#include "Hero.h"
#include "JoyStick.h"
#include "Map1Page1Scene.h"
#include "Map1Page2Scene.h"
#include "Map1Page3Scene.h"
#include "Map1Page4Scene.h"
#include "Map1Page5Scene.h"
#include "Map1Page6Scene.h"
#include "Map1Page7Scene.h"
USING_NS_CC;
MapSwitcher * MapSwitcher::instance = nullptr;
MapSwitcher * MapSwitcher::getInstance() {
if (!MapSwitcher::instance) MapSwitcher::instance = new MapSwitcher();
return MapSwitcher::instance;
}
MapSwitcher::~MapSwitcher() {
this->hero->release();
this->rocker->release();
for (auto & m : *(this->maps))
m->release();
this->maps->clear();
}
void MapSwitcher::init() {
srand(time(0));
this->initMaps();
this->isSwitching = true;
this->hero = Hero::create("Hero");
this->hero->initSprite("hero1.png");
this->hero->initPhysicsBody();
this->hero->setScale(0.25f);
this->hero->retain();
// KeyboardListener
typedef EventKeyboard::KeyCode KeyCode;
KBListener = EventListenerKeyboard::create();
KBListener->onKeyPressed = [&](KeyCode code, Event* event){
switch (code) {
case KeyCode::KEY_LEFT_ARROW:
this->hero->setWalking(true, Hero::BTN_LEFT);
break;
case KeyCode::KEY_RIGHT_ARROW:
this->hero->setWalking(true, Hero::BTN_RIGHT);
break;
case KeyCode::KEY_UP_ARROW:
this->hero->actJump();
break;
case KeyCode::KEY_DOWN_ARROW:
this->hero->resetAction();
this->hero->setWalking(false, Hero::BTN_LEFT);
this->hero->setWalking(false, Hero::BTN_RIGHT);
break;
default:
break;
}
};
KBListener->onKeyReleased = [&](KeyCode code, Event* event){
switch (code) {
case KeyCode::KEY_LEFT_ARROW:
this->hero->setWalking(false, Hero::BTN_LEFT);
break;
case KeyCode::KEY_RIGHT_ARROW:
this->hero->setWalking(false, Hero::BTN_RIGHT);
break;
default:
break;
}
};
tDirector->getEventDispatcher()->addEventListenerWithFixedPriority(KBListener, -1000000);
//
this->rocker = JoyStick::create();
this->rocker->setPosition(Point::ZERO);
this->rocker->setHero(this->hero);
this->rocker->retain();
Director::getInstance()->getScheduler()->scheduleUpdate(this, 0, false);
}
void MapSwitcher::setHero(Hero * hero) {
this->hero = hero;
}
Hero * MapSwitcher::getHero() {
return this->hero;
}
void MapSwitcher::setSwitching(bool isSwitching) {
this->isSwitching = isSwitching;
}
Scene * MapSwitcher::getCurMap() {
return this->curMap;
}
void MapSwitcher::setCurMap(int mapType) {
this->curMapNum = curMapNum;
this->curMap = maps->at(mapType-1);
}
void MapSwitcher::update(float delta) {
if (isSwitching) return;
auto preMap = dynamic_cast<MapScene *>(curMap->getChildByTag(13));
bool isOut = preMap->isGoOtherMap();
// CCLOG("%d", isOut);
if (isOut) {
this->isSwitching = true;
this->setCurMap(preMap->nextMap());
auto newPos = preMap->newPostion();
preMap->clearNodes();
this->hero->resetAction();
this->hero->setWalking(false, Hero::BTN_LEFT);
this->hero->setWalking(false, Hero::BTN_RIGHT);
// CCLOG("test");
this->hero->setPosition(newPos);
dynamic_cast<MapScene *>(curMap->getChildByTag(13))->addChildAndRecord(hero, 1, 16);
dynamic_cast<MapScene *>(curMap->getChildByTag(13))->addChildAndRecord(rocker, 2, 999);
tDirector->setSendCleanupToScene(false);
tDirector->replaceScene(curMap);
this->isSwitching = false;
}
}
MapSwitcher::MapSwitcher() {
this->init();
}
void MapSwitcher::initMaps() {
MapScene::loadResources();
this->maps = new std::vector<Scene *>();
auto map11 = Map1Page1Scene::createScene();
map11->retain();
this->maps->push_back(map11);
auto map12 = Map1Page2Scene::createScene();
map12->retain();
this->maps->push_back(map12);
auto map13 = Map1Page3Scene::createScene();
map13->retain();
this->maps->push_back(map13);
auto map14 = Map1Page4Scene::createScene();
map14->retain();
this->maps->push_back(map14);
auto map15 = Map1Page5Scene::createScene();
map15->retain();
this->maps->push_back(map15);
auto map16 = Map1Page6Scene::createScene();
map16->retain();
this->maps->push_back(map16);
auto map17 = Map1Page7Scene::createScene();
map17->retain();
this->maps->push_back(map17);
CCLOG("Map loaded");
}