LeveRankItem.ts
4.83 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
// 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 { SaveDataManager } from "../component/SaveDataManager";
import { GlobalEvent, tMgr, WindowName } from "../Global";
import { getGlobalNode } from "../kernel/battle/BattleConst";
import { GamePlayTable, TableName } from "../kernel/table/TableDefine";
import { UIManager } from "../ui/UIManager";
import { UITitleData } from "../ui/uiView/UITitleData";
const { ccclass, property } = cc._decorator;
@ccclass
export default class LevelRankItem extends cc.Component {
private _levelId: number = 0;
private _lineLayer: cc.Node;
private _line: cc.Node;
protected override onLoad(): void {
this._line = this.node.getChildByName('line');
}
protected override start(): void {
getGlobalNode().on(GlobalEvent.LEVEL_STAR_CHANGE, this.onStarChange, this);
}
protected onDestroy(): void {
getGlobalNode().targetOff(this);
}
setLevelId(id: number) {
this._levelId = id;
this.refresh();
}
setLineLayer(node: cc.Node) {
this._lineLayer = node;
// this._line.removeFromParent();
// this._lineLayer.addChild(this._line);
// this._line.active = true;
}
refresh() {
let isUnlocked = (this._levelId <= SaveDataManager.ins.runtimeData.curLevel);
this.node.getChildByName('btn').getComponentInChildren(cc.Label).string = this._levelId.toString();
let curNext = (this._levelId == SaveDataManager.ins.runtimeData.curLevel + 1);
if (!isUnlocked && curNext) {
this.node.getChildByName('back').active = false;
this.node.getChildByName('btn').active = true;
let backSp = this.node.getChildByName('btn').getChildByName('Background').getComponent(cc.Sprite);
backSp.spriteFrame = this.node.getChildByName('linkRes').getChildByName('UI_gqlb_2').getComponent(cc.Sprite).spriteFrame;
this.node.getChildByName('imgCur').active = true;
}
else {
this.node.getChildByName('btn').active = isUnlocked;
if (isUnlocked) {
let backSp = this.node.getChildByName('btn').getChildByName('Background').getComponent(cc.Sprite);
backSp.spriteFrame = this.node.getChildByName('linkRes').getChildByName('UI_gqlb_1').getComponent(cc.Sprite).spriteFrame;
}
this.node.getChildByName('back').active = !isUnlocked;
this.node.getChildByName('imgCur').active = false;
}
let star = 0;
if (SaveDataManager.ins.runtimeData.levelScoreMap[this._levelId]) {
star = SaveDataManager.ins.runtimeData.levelScoreMap[this._levelId];
}
let starContainer = cc.find('btn/Background/starContainer', this.node);
for (let i = 0; i < 3; ++i) {
let starNode = starContainer.children[i];
starNode.getComponent(cc.Sprite).spriteFrame = this.node.getChildByName('linkRes').getChildByName('star' + (i + 1 <= star ? '1' : '2')).getComponent(cc.Sprite).spriteFrame;
}
this.scheduleOnce(this.updateLine.bind(this), 0);
}
private updateLine() {
if (this._line.parent != this._lineLayer) {
this._line.removeFromParent();
this._lineLayer.addChild(this._line);
this._line.active = true;
}
let nextLv = this._levelId + 1;
let maxLv = (tMgr.getConfig(TableName.GAME_PLAY, 22) as GamePlayTable).Value;
if (nextLv > maxLv) {
this._line.active = false;
return;
}
let nextIsLihgt = (nextLv <= SaveDataManager.ins.runtimeData.curLevel + 1);
this._line.getComponent(cc.Sprite).spriteFrame = (this.node.getChildByName('linkRes').getChildByName('line' + (nextIsLihgt ? '1' : '2'))).getComponent(cc.Sprite).spriteFrame;
let isEnd = (this._levelId % 5 == 0);
let isReverse = (Math.ceil(this._levelId / 5) % 2 == 0); //是否反向
if (isEnd) {
this._line.angle = 90;
this._line.setPosition(this.node.position.x, this.node.position.y - this.node.height / 2);
}
else {
this._line.angle = 0;
this._line.setPosition(this.node.position.x + this.node.width / 2 * (isReverse ? -1 : 1), this.node.position.y);
}
}
onStarChange(level: number) {
if (level == this._levelId || level == this._levelId - 1) {
this.refresh();
}
}
onBtnClick() {
UITitleData.id = this._levelId;
// UIManager.ins.closeWindow(WindowName.LEVEL_RANK);
UIManager.ins.openWindow(WindowName.PREVIEW, UITitleData.id)
}
}