MainScene.ts
8.81 KB
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
// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import { GuideManager } from "../component/GuideManager";
import { SaveDataManager } from "../component/SaveDataManager";
import { SoundManager } from "../component/SoundManager";
import { GlobalEvent, ResType, tMgr } from "../Global";
import { getGlobalNode } from "../kernel/battle/BattleConst";
import { GameSceneObjTableMgr } from "../kernel/table/GameSceneObjTableMgr";
import { GamePlayTable, GameSceneObjTable, TableName } from "../kernel/table/TableDefine";
import GameSceneControl from "../ui/gameScene/GameSceneControl";
import { UIManager } from "../ui/UIManager";
import ResWidget from "../ui/uiView/widget/ResWidget";
const { ccclass, property } = cc._decorator;
@ccclass
export default class MainScene extends cc.Component {
private _pageView: cc.PageView;
private _buildScenePrefab: cc.Node;
private _charNode: cc.Node;
private _tipNode: cc.Node;
private _parGreenNode: cc.Node;
private _bgLightNode: cc.Node;
private _unlockSpineNode: cc.Node;
//#region LIFE-CYCLE CALLBACKS:
onLoad() {
getGlobalNode().on(GlobalEvent.OBJ_UNLOCK_ANI_FINISH, this.onObjUnlock, this);
getGlobalNode().on(GlobalEvent.SOUND_MUSIC_CONFIG_CHANGE, this.onSoundConfigChange, this);
let guideNode = this.node.getChildByName('guideNode');
if (guideNode.childrenCount > 0) {
GuideManager.ins.init(guideNode);
}
}
start() {
this.node.getChildByName('widgetStar').getComponent(ResWidget).setType(ResType.STAR);
this.node.getChildByName('widgetDiamond').getComponent(ResWidget).setType(ResType.DIAMOND);
this._pageView = cc.find('gameSceneRoot/pageView', this.node).getComponent(cc.PageView);
this._buildScenePrefab = cc.find('gameSceneRoot/container', this.node);
this._charNode = cc.find('gameSceneRoot/' + (SaveDataManager.ins.runtimeData.gender == 0 ? 'girl' : 'boy'), this.node);
this._tipNode = cc.find('gameSceneRoot/unlockTip', this.node);
this._parGreenNode = cc.find('gameSceneRoot/parGreen', this.node);
this._bgLightNode = cc.find('gameSceneRoot/bg', this.node);
this._unlockSpineNode = cc.find('gameSceneRoot/unlockSpine', this.node);
let curScene = SaveDataManager.ins.runtimeData.curGameScene;
for (let i = 1; i < 4; ++i) {
if (i <= curScene) {
this.addScene(i);
}
}
if (this._pageView.getPages().length == 1) {
//只有一页,手动刷新
this._pageView.getPages()[0].getComponent(GameSceneControl).onSlideShow();
}
else {
this._pageView.setCurrentPageIndex(this._pageView.getPages().length - 1);
}
this.scheduleOnce(this.checkGuide.bind(this), 0); //等待自适应布局完毕后再检查
}
protected onEnable(): void {
UIManager.ins.widgetLayer.active = true;
if (SaveDataManager.ins.mainSceneAddStar > 0) {
SaveDataManager.ins.adjustStar(SaveDataManager.ins.mainSceneAddStar, true);
SaveDataManager.ins.mainSceneAddStar = 0;
}
//预加载地图
let maxLevel = (tMgr.getConfig(TableName.GAME_PLAY, 22) as GamePlayTable).Value;
let nextLevelId = SaveDataManager.ins.runtimeData.curLevel + 1;
if (nextLevelId > maxLevel) {
nextLevelId = maxLevel;
}
cc.resources.preload('map/map-' + nextLevelId.toString(), cc.TiledMapAsset);
this.onSoundConfigChange(SaveDataManager.ins.runtimeData.musicMute);
}
protected onDisable(): void {
this._sceneEffectAudioId > -1 && cc.audioEngine.stopEffect(this._sceneEffectAudioId);
}
protected onDestroy(): void {
// getGlobalNode().off(GlobalEvent.OBJ_UNLOCK_ANI_FINISH, this.onObjUnlock, this);
getGlobalNode().targetOff(this);
}
update(dt) {
let backNode = cc.find('gameSceneRoot/pageView/backImgContainer', this.node);
let contentNode = cc.find('gameSceneRoot/pageView/content', this.node);
backNode.setPosition(contentNode.position);
let lightNode = cc.find('gameSceneRoot/pageView/effectLayer', this.node);
lightNode.setPosition(contentNode.position);
}
//#endregion
//#region event
onPageSlide(view: cc.PageView) {
let page = view.getCurrentPageIndex();
let pages = view.getPages();
for (let i = 0; i < pages.length; ++i) {
pages[i].getComponent(GameSceneControl).onSlideLeave();
}
pages[page].getComponent(GameSceneControl).onSlideShow();
}
onToolClick(e: cc.Event.EventTouch) {
let curPage = this._pageView.getCurrentPageIndex();
this._pageView.getPages()[curPage].getComponent(GameSceneControl).onToolClick(e);
}
onObjUnlock(objId: number) {
let obj = tMgr.getConfig(TableName.GAME_SCENE_OBJ, objId) as GameSceneObjTable;
let sceneList = GameSceneObjTableMgr.ins.getByScene(obj.SceneId);
let allUnlock: boolean = true;
for (let i = 0; i < sceneList.length; ++i) {
//判断此场景内的每个物件是否解锁
if (!SaveDataManager.ins.runtimeData.unlockObj.includes(sceneList[i].ID)) {
allUnlock = false;
break;
}
}
if (allUnlock && obj.SceneId < 3) {
//添加下一场景
SaveDataManager.ins.runtimeData.curGameScene++;
SaveDataManager.ins.saveData();
this.addScene(obj.SceneId + 1);
this._pageView.setCurrentPageIndex(obj.SceneId);
}
}
private _sceneEffectAudioId: number = -1;
private onSoundConfigChange(mute: boolean) {
let sceneEffect = cc.resources.get('soundEffect/Scene', cc.AudioClip) as cc.AudioClip;
if (!mute) {
SoundManager.ins.playMusic();
this._sceneEffectAudioId = cc.audioEngine.playEffect(sceneEffect, true);
}
else {
SoundManager.ins.stopMusic();
this._sceneEffectAudioId > -1 && cc.audioEngine.stopEffect(this._sceneEffectAudioId);
}
}
//#endregion
//#region public
onGuideClick() {
this.node.getChildByName('mask').active = false;
GuideManager.ins.hideTalk();
GuideManager.ins.hideHand();
// SaveDataManager.ins.runtimeData.guideStep++;
// SaveDataManager.ins.saveData();
}
//#endregion
//#region private
private addScene(sceneId: number) {
let buidScene = cc.instantiate(this._buildScenePrefab);
buidScene.active = true;
buidScene.getComponent(GameSceneControl).init(sceneId - 1, this._charNode, this._tipNode, this._parGreenNode, this._bgLightNode, this._unlockSpineNode);
buidScene.getComponent(GameSceneControl).loadScene(sceneId);
this._pageView.addPage(buidScene);
}
private checkGuide() {
//引导
if (SaveDataManager.ins.runtimeData.guideStep == 0) {
let wpos = this._charNode.convertToWorldSpaceAR(cc.v2(0, 380));
let txt = (tMgr.getConfig(TableName.GAME_PLAY, 25) as GamePlayTable).Comment;
GuideManager.ins.showTalk(wpos, txt);
this.scheduleOnce(() => {
GuideManager.ins.hideTalk();
SaveDataManager.ins.runtimeData.guideStep++;
SaveDataManager.ins.saveData();
this.checkGuide();
}, 3);
}
else if (SaveDataManager.ins.runtimeData.guideStep == 1) {
let wpos = this.node.getChildByName('UITitle').getChildByName('bottom').convertToWorldSpaceAR(cc.v2(0, 120));
GuideManager.ins.showHand(wpos, true);
wpos = this.node.getChildByName('UITitle').getChildByName('bottom').convertToWorldSpaceAR(cc.v2(0, 100));
wpos.y += 200;
let txt = (tMgr.getConfig(TableName.GAME_PLAY, 26) as GamePlayTable).Comment;
txt = txt.replace('{0}', (tMgr.getConfig(TableName.GAME_PLAY, SaveDataManager.ins.runtimeData.gender == 0 ? 24 : 23) as GamePlayTable).Comment);
GuideManager.ins.showTalk(wpos, txt);
let btn = cc.find('UITitle/bottom/naviBar/btnLayer/btn3', this.node);
wpos = btn.convertToWorldSpaceAR(cc.Vec2.ZERO);
UIManager.ins.widgetLayer.convertToNodeSpaceAR(wpos, wpos);
let newBtn = cc.instantiate(btn);
newBtn.setPosition(wpos);
newBtn.getComponent(cc.Button).clickEvents[0].customEventData = '99';
UIManager.ins.widgetLayer.addChild(newBtn);
this.node.getChildByName('mask').active = true;
}
}
//#endregion
}