UIBattleUserSkillBtn.ts
4.16 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
// 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 { BattleManager } from "../../kernel/battle/BattleManager";
import { TableName, UserSkillTable } from "../../kernel/table/TableDefine";
import { UIManager } from "../UIManager";
const { ccclass, property } = cc._decorator;
@ccclass
export default class UIBattleUserSkillBtn extends cc.Component {
private _icon: cc.Sprite;
private _lockNode: cc.Node;
private _numNode: cc.Node;
private _txtNum: cc.Label;
private _locked: boolean = true;
private _skillConfig: UserSkillTable = null;
//#region LIFE-CYCLE CALLBACKS:
onLoad() {
this._icon = this.node.getChildByName('icon').getComponent(cc.Sprite);
this._lockNode = this.node.getChildByName('imgLock');
this._numNode = this.node.getChildByName('nodeNum');
this._txtNum = this._numNode.getComponentInChildren(cc.Label);
}
protected onEnable(): void {
getGlobalNode().on(GlobalEvent.SKILL_NUM_CHANGE, this.onSkillNumChange, this);
}
protected onDisable(): void {
getGlobalNode().off(GlobalEvent.SKILL_NUM_CHANGE, this.onSkillNumChange, this);
}
// update (dt) {}
//#endregion
//#region public method
setId(skillId: number) {
this._skillConfig = tMgr.getConfig(TableName.USER_SKILL, skillId) as UserSkillTable;
if (this._skillConfig) {
// this.setlo
cc.resources.load('ui/battle/' + this._skillConfig.Icon, cc.SpriteFrame, (error, asset) => {
if (!error) {
this._icon.spriteFrame = asset as cc.SpriteFrame;
}
});
this._locked = BattleManager.ins.curBattle.levelId < this._skillConfig.UnlockLevel;
if (BattleManager.ins.curBattle.levelId == 0) {
this._locked = false;
}
this._icon.node.active = !this._locked;
this._numNode.active = !this._locked;
this._lockNode.active = this._locked;
let free = (BattleManager.ins.curBattle.levelId == this._skillConfig.UnlockLevel);
this.node.getChildByName('hand').active = free;
if (!this._locked || free) {
this.updateNum(free);
}
}
}
// setLock(value: boolean) {
// this._locked = value;
// this._icon.node.active = !value;
// this._addNode.active = !value;
// this._numNode.active = !value;
// this._lockNode.active = value;
// }
//#endregion
//#region event
onClick() {
if (!this._locked) {
let free = (BattleManager.ins.curBattle.levelId == this._skillConfig.UnlockLevel);
if (this.node.getChildByName('hand').active) {
this.node.getChildByName('hand').active = false;
}
let num = SaveDataManager.ins.getUserSkillNum(this._skillConfig.ID);
if (num > 0 || free) {
BattleManager.ins.curBattle.useSkill(this._skillConfig.ID, free);
}
else {
UIManager.ins.openWindow(WindowName.USER_SKILL_INFO, this._skillConfig.ID);
}
}
}
private onSkillNumChange(skillId: number) {
if (skillId == this._skillConfig.ID) {
this.updateNum(false);
}
}
//#endregion
//#region private method
private updateNum(free: boolean) {
//读取本地存档
let num = SaveDataManager.ins.getUserSkillNum(this._skillConfig.ID);
this._numNode.active = (num > 0 || free);
this._txtNum.string = num.toString();
this._numNode.getChildByName('txtNum').active = !free;
this._numNode.getChildByName('imgFree').active = free;
}
//#endregion
}