UIBallSkinPreview.ts
6.34 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
// 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 { ADID, GlobalEvent, tMgr, WindowName } from "../../Global";
import { getGlobalNode } from "../../kernel/battle/BattleConst";
import { PhysicsControl } from "../../kernel/physics/PhysicsControl";
import { BallSkinTable, TableName } from "../../kernel/table/TableDefine";
import SkinPreviewBall from "../../prefabs/SkinPreviewBall";
import { UIBase } from "../UIBase";
import { UIManager } from "../UIManager";
const { ccclass, property } = cc._decorator;
@ccclass
export default class UIBallSkinPreview extends UIBase {
@property(cc.Node)
imgTitle: cc.Node = null;
@property(cc.Node)
prg: cc.Node = null;
private _curId: number = 0;
//#region lifecycle
@property(cc.Node)
btn_ad: cc.Node = null;
@property(cc.Node)
btn_start: cc.Node = null;
@property(cc.Node)
btn_bonus: cc.Node = null;
@property(cc.Node)
btn_sign: cc.Node = null;
@property(cc.Node)
btn_wheel: cc.Node = null;
@property(cc.Node)
btn_use: cc.Node = null;
@property(cc.Node)
btn_useing: cc.Node = null;
openCount: number = 0;
constructor() {
super();
// this._useCloseEffect = false;
// this._usePopUpEffect = false;
}
protected onEnable(): void {
getGlobalNode().on(GlobalEvent.BALL_SKIN_UNLOCK, this.refreshState, this);
getGlobalNode().on(GlobalEvent.BALL_SKIN_USE, this.refreshState, this);
this.refresh(this.enableParam);
this.btn_ad.getChildByName('Background').getChildByName('UI_General_Btn_yellow_ckgg').active = true;
this.btn_ad.getChildByName('Background').getChildByName('UI_Finish_kxsx').active = false;
this.openCount++;
if (this.openCount > 1) {
this.BtnPos_Change();
}
}
//按钮位置轮换
BtnPos_Change() {
let tempPos = this.btn_start.position;
this.btn_start.position = this.btn_bonus.position;
this.btn_sign.position = this.btn_bonus.position;
this.btn_wheel.position = this.btn_bonus.position;
this.btn_use.position = this.btn_bonus.position;
this.btn_useing.position = this.btn_bonus.position;
this.btn_bonus.position = this.btn_ad.position;
this.btn_ad.position = tempPos;
}
protected onPopUpEffectFinish(): void {
this.node.getChildByName('physics').active = true;
PhysicsControl.ins.setPhysicsEnable(true);
}
onBeforeClose(): void {
super.onBeforeClose();
this.node.getChildByName('physics').active = false;
PhysicsControl.ins.setPhysicsEnable(false);
}
protected update(dt: number): void {
PhysicsControl.ins.update(dt);
}
protected onDisable(): void {
getGlobalNode().targetOff(this);
}
//#endregion
//#region public method
//#endregion
//#region event
onBtnCloseClick() {
UIManager.ins.closeWindow(WindowName.BALL_SKIN_PREVIEW);
}
onPreClick() {
if (this._curId > 1) {
this.refresh(this._curId - 1);
}
}
onNextClick() {
let maxId = tMgr.getTableMaxID(TableName.BALL_SKIN);
if (this._curId < maxId) {
this.refresh(this._curId + 1);
}
}
onBuyClick() {
let wpos = this.node.getChildByName('btnDiamond').convertToWorldSpaceAR(cc.v3(0, 0, 0));
let config = tMgr.getConfig(TableName.BALL_SKIN, this._curId) as BallSkinTable;
if (SaveDataManager.ins.adjustDiamond(-config.Price, true, wpos)) {
SaveDataManager.ins.unlockBallSkin(config.ID);
}
}
onSignClick() {
UIManager.ins.openWindow(WindowName.SIGN);
}
onWheelClick() {
UIManager.ins.openWindow(WindowName.WHEEL);
}
onAdTryClick() {
//AD 放广告,放完后调setTrySkin
// HeyGamePlatform.instance.showVideoAd(ADID.ADID_ADDCOIN, (_result) => {
if (true) {
this.setTrySkin();
} else {
console.log('视频播放失败');
}
// });
}
onUseClick() {
SaveDataManager.ins.useBallSkin(this._curId);
}
//#endregion
//#region private method
private refresh(skinId: number) {
if (skinId != this._curId) {
this._curId = skinId;
let config = tMgr.getConfig(TableName.BALL_SKIN, skinId) as BallSkinTable;
this.prg.getComponent(cc.ProgressBar).progress = 0.33 * config.Size;
this.refreshState();
cc.resources.load('ui/ballSkinName/name' + skinId.toString(), cc.SpriteFrame, (error, asset: cc.SpriteFrame) => {
!error && (this.imgTitle.getComponent(cc.Sprite).spriteFrame = asset);
})
let ballNode = cc.find('physics/ball', this.node);
ballNode.getComponent(SkinPreviewBall).setSkin(skinId);
}
}
private refreshState() {
let config = tMgr.getConfig(TableName.BALL_SKIN, this._curId) as BallSkinTable;
let isUnlocked = SaveDataManager.ins.runtimeData.ballSkinUnlocked.includes(config.ID);
let isUsing = (SaveDataManager.ins.runtimeData.ballSkinCurrent == config.ID);
this.node.getChildByName('btnAd').getComponent(cc.Button).interactable = !isUnlocked;
this.node.getChildByName('btnDiamond').active = !isUnlocked && config.Price > 0;
if (this.node.getChildByName('btnDiamond').active) {
this.node.getChildByName('btnDiamond').getComponentInChildren(cc.Label).string = config.Price.toString();
}
this.node.getChildByName('btnSign').active = !isUnlocked && config.Price == -1;
this.node.getChildByName('btnWheel').active = !isUnlocked && config.Price == -2;
this.node.getChildByName('btnUse').active = isUnlocked && !isUsing;
this.node.getChildByName('btnUsing').active = isUnlocked && isUsing;
}
private setTrySkin() {
SaveDataManager.ins.runtimeData.ballSkinTry = this._curId;
SaveDataManager.ins.saveData();
UIManager.ins.closeWindow(WindowName.BALL_SKIN_PREVIEW);
}
//#endregion
}