UIWheel.ts
7.13 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
// 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 { TableName, WheelTable } from "../../kernel/table/TableDefine";
import WheelItem from "../../prefabs/WheelItem";
import { UIBase } from "../UIBase";
import { UIManager } from "../UIManager";
const { ccclass, property } = cc._decorator;
@ccclass
export default class UIWheel extends UIBase {
//#region LIFE-CYCLE CALLBACKS:
@property(cc.Integer)
public radius: number = 1
@property(cc.Prefab)
itemPrefab: cc.Prefab = null;
@property(cc.Node)
adNode: cc.Node = null;
@property(cc.Node)
btn_ad: cc.Node = null;
private _wheelNode: cc.Node;
private _tweenObject = { progress: 0 };
/**权重列表 */
private _weightList: number[] = [];
private _reward: WheelTable;
start() {
// [3]
this._wheelNode = cc.find('main/wheel', this.node);
this.btn_ad.active = true;
let num = tMgr.getTableCount(TableName.WHEEL);
let rad = -2 * Math.PI / num;
// let deg = slib.MathUtil.getDegreeByRadian(rad);
let vec = new cc.Vec2(0, this.radius);
for (let i = 0; i < num; ++i) {
if (i > 0) {
vec.rotateSelf(rad)
}
let config = tMgr.getConfig(TableName.WHEEL, i + 1) as WheelTable;
this._weightList.push(config.Rate);
let foo = cc.instantiate(this.itemPrefab);
foo.getComponent(WheelItem).setId(i + 1);
foo.setPosition(vec.x, vec.y);
// foo.angle = i * deg;
foo.scale = 0.7;
cc.find('main/UI_wheel_zp', this.node).addChild(foo);
}
this.refresh();
}
hide_btn_ad() {
this.btn_ad.active = false;
}
protected onEnable(): void {
this.refresh();
this.btn_ad.active = true;
// this.adNode.getComponent(CustomNativeAdView).showNativeAd((result) => {
// if (result == true) {
// this.btn_ad.active = true;
// } else {
// this.btn_ad.active = false;
// }
// });
this.btn_ad.getChildByName('Background').getChildByName('UI_wheel_btn_ckgg').active = true;
this.btn_ad.getChildByName('Background').getChildByName('UI_wheel_btn_kxsx').active = false;
}
protected onDisable(): void {
}
protected onLoad(): void {
getGlobalNode().on(GlobalEvent.WHEEL_REWARD_GET, this.refresh, this);
}
protected onDestroy(): void {
getGlobalNode().targetOff(this);
}
private refresh() {
cc.find('main/UI_wheel_zp/UI_wheel_cjcs', this.node).active = (SaveDataManager.ins.runtimeData.freeWheelNum > 0);
cc.find('main/btnGo', this.node).active = (SaveDataManager.ins.runtimeData.freeWheelNum > 0);
cc.find('main/btnAdGo', this.node).active = (SaveDataManager.ins.runtimeData.freeWheelNum <= 0);
// cc.find('main/btn_kxsx', this.node).active = (SaveDataManager.ins.runtimeData.freeWheelNum <= 0);
let container = cc.find('main/UI_wheel_zp', this.node);
for (let i = 0; i < container.childrenCount; ++i) {
let cmpt = container.children[i].getComponent(WheelItem);
cmpt && cmpt.refresh();
}
}
private go() {
this._wheelNode.angle = 0;
let configId = 0;
// if (SaveDataManager.ins.runtimeData.freeWheelNum) {
// //免费的,固定奖励
// configId = 1;
// }
// else {
configId = slib.MathUtil.randomTestByWeightList(this._weightList) + 1;
// }
this._reward = tMgr.getConfig(TableName.WHEEL, configId) as WheelTable;
// log('奖品', configId);
let num = tMgr.getTableCount(TableName.WHEEL);
let rad = 2 * Math.PI / num;
let deg = slib.MathUtil.getDegreeByRadian(rad);
let total = -6 * 360 + (-deg) * (configId - 1);
this._tweenObject.progress = 0;
cc.tween(this._tweenObject).to(5, { progress: 1 }, {
easing: 'sineOut',
onUpdate: (target, ratio) => {
// this._wheelNode.setRotationFromEuler(0, 0, total * ratio);
this._wheelNode.angle = total * (target['progress']);
}
}).call(this.onTweenComplete.bind(this)).start();
// SoundManager.ins.playEffect(SOUND_EFFECT.WHEEL);
}
private onTweenComplete() {
// log('complete');
if (SaveDataManager.ins.runtimeData.freeWheelNum > 0) {
SaveDataManager.ins.runtimeData.freeWheelNum--;
}
// if (this._reward.Type == 1) {
// //钻石
// // uiMgr().open('UI/UIGet', { type: 3, num: this._reward.Num });
// }
// else if (this._reward.Type == 2) {
// //炸弹
// // battleMgr().battle.startSkill(2);
// // uiMgr().open('UI/UIGet', { type: 2, num: 1 });
// }
// else if (this._reward.Type == 3) {
// //分裂炮
// // uiMgr().open('UI/UIGet', { type: 1, num: 1 });
// // battleMgr().battle.startSkill(1);
// }
// else if (this._reward.Type == 4) {
// //飞轮
// }
// else if (this._reward.Type == 5) {
// //西瓜球
// }
UIManager.ins.openWindow(WindowName.GET, this._reward);
SaveDataManager.ins.saveData();
// console.log('reward', this._reward.ID);
cc.find('main/btnClose', this.node).getComponent(cc.Button).interactable = true;
cc.find('main/btnAd', this.node).getComponent(cc.Button).interactable = true;
cc.find('main/btnAdGo', this.node).getComponent(cc.Button).interactable = true;
cc.find('main/btnGo', this.node).getComponent(cc.Button).interactable = true;
this.refresh();
}
onCloseClick() {
UIManager.ins.closeWindow(WindowName.WHEEL);
}
onBtnAdClick() {
//AD 纯放广告
}
onBtnGoClick() {
if (SaveDataManager.ins.runtimeData.freeWheelNum > 0) {
//免费次数
this.go();
}
else {
//AD 放广告,播完后调用 go
// HeyGamePlatform.instance.showVideoAd(ADID.ADID_ADDCOIN, (_result) => {
if (true) {
this.go();
} else {
console.log('视频播放失败');
}
// });
}
cc.find('main/btnClose', this.node).getComponent(cc.Button).interactable = false;
cc.find('main/btnAd', this.node).getComponent(cc.Button).interactable = false;
cc.find('main/btnGo', this.node).getComponent(cc.Button).interactable = false;
cc.find('main/btnAdGo', this.node).getComponent(cc.Button).interactable = false;
}
//#endregion
}