223 lines
7.5 KiB
TypeScript
223 lines
7.5 KiB
TypeScript
import { _decorator, Node, tween, Vec3, Sprite, Asset, ImageAsset, SpriteFrame, Texture2D, Label } from 'cc';
|
||
import BaseView from '../../../../../../extensions/app/assets/base/BaseView';
|
||
import { PageTips } from '../../tips/native/PageTips';
|
||
import { app } from 'db://assets/app/app';
|
||
import { Tools } from 'db://assets/res-native/tools/Tools';
|
||
import { PageRewardhistory } from '../../rewardhistory/native/PageRewardhistory';
|
||
import { USERDATA } from 'db://assets/res-native/data/UserData';
|
||
const { ccclass, property } = _decorator;
|
||
@ccclass('PageMain')
|
||
export class PageMain extends BaseView {
|
||
/** 标题 */
|
||
@property(Node)
|
||
title: Node = null!;
|
||
/** 奖励 */
|
||
@property(Node)
|
||
reward_bg: Node = null!;
|
||
/** 奖励 */
|
||
@property(Node)
|
||
noreward_bg: Node = null!;
|
||
/** 音乐按钮 */
|
||
@property(Node)
|
||
btn_music: Node = null!;
|
||
/** 灯光 */
|
||
@property(Node)
|
||
turn_light: Node = null!;
|
||
/** 转盘 */
|
||
@property(Node)
|
||
turn: Node = null!;
|
||
/** 奖励文字 */
|
||
@property(Label)
|
||
lab_rw: Label = null!;
|
||
/** 奖励icon */
|
||
@property(Sprite)
|
||
icon_rw: Sprite = null!;
|
||
/** 剩余次数 */
|
||
@property(Label)
|
||
lab_remain: Label = null!;
|
||
/** 奖励展示列表 */
|
||
@property(Node)
|
||
rw_list: Node[] = [];
|
||
/** 奖励icon */
|
||
@property(Sprite)
|
||
game_logo: Sprite = null!;
|
||
/** 奖励icon */
|
||
@property(Sprite)
|
||
game_logo_bg: Sprite = null!;
|
||
|
||
/** 转盘是否正在转 */
|
||
private _is_turing = false;
|
||
|
||
// 初始化的相关逻辑写在这
|
||
onLoad() {}
|
||
|
||
// 界面打开时的相关逻辑写在这(onShow可被多次调用-它与onHide不成对)
|
||
onShow(params: any) {
|
||
this.titleAnimation();
|
||
this.lab_remain.string = "REMAINING CHANCE : " + USERDATA.point.toString();
|
||
|
||
this.setRw();
|
||
|
||
// 加载配置的logo
|
||
if (USERDATA.logo && this.game_logo) {
|
||
Tools.remoteLoadSprite(USERDATA.logo, this.game_logo)
|
||
}
|
||
|
||
if (USERDATA.logo_back && this.game_logo_bg) {
|
||
Tools.remoteLoadSprite(USERDATA.logo_back, this.game_logo_bg)
|
||
}
|
||
}
|
||
|
||
setRw(){
|
||
for (let i = 0; i < this.rw_list.length; i++) {
|
||
const element = this.rw_list[i];
|
||
if (USERDATA.prize_list[i]){
|
||
Tools.SetText(element, USERDATA.prize_list[i].name)
|
||
Tools.remoteLoadSprite(USERDATA.prize_list[i].pic, Tools.GetChildComp(element, "icon", Sprite))
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 开始灯光切换 */
|
||
startLightToggle() {
|
||
this.schedule(this.switchLight, 0.3);
|
||
}
|
||
|
||
/** 切换灯光 */
|
||
switchLight(){
|
||
this.turn_light.active = !this.turn_light.active;
|
||
}
|
||
|
||
/** 标题动画 */
|
||
titleAnimation() {
|
||
const scaleDown = tween().to(0.6, { scale: new Vec3(0.96, 0.96, 1) });
|
||
const scaleUp = tween().to(0.6, { scale: new Vec3(1, 1, 1) });
|
||
|
||
tween(this.title)
|
||
.sequence(scaleDown, scaleUp) // 使用sequence串联动画
|
||
.repeatForever()
|
||
.start();
|
||
}
|
||
|
||
|
||
// 界面关闭时的相关逻辑写在这(已经关闭的界面不会触发onHide)
|
||
onHide(result: undefined) {
|
||
// app.manager.ui.show<PageMain>({name: 'PageMain', onHide:(result) => { 接收到return的数据,并且有类型提示 }})
|
||
return result;
|
||
}
|
||
|
||
|
||
/**
|
||
* 旋转转盘
|
||
* @param segmentIndex 目标段落索引 (0-9)
|
||
* @param callback 旋转完成后的回调函数
|
||
*/
|
||
rotateTurntable(segmentIndex: number, callback?: () => void) {
|
||
app.manager.sound.playEffect({name: "effect/turn"})
|
||
this._is_turing = true;
|
||
// 每个扇形的角度
|
||
const segmentAngle = 36; // 360度 / 10份 = 36度每份
|
||
// 随机左右12度
|
||
let random_rat = 12 * (1 - Math.random() * 2)
|
||
|
||
// 计算目标角度(从15度开始,顺时针)
|
||
// segmentIndex: 0-9 对应第1-10个扇形
|
||
const targetAngle = (18 + segmentIndex * segmentAngle) + random_rat;
|
||
|
||
// 为了让转盘看起来更真实,我们先旋转几圈再停到目标位置
|
||
const rotations = Math.floor(Math.random() * 3) + 6; // 旋转圈数
|
||
const totalRotation = rotations * 360 + targetAngle;
|
||
|
||
// 获取当前旋转角度
|
||
const currentRotation = this.turn.eulerAngles;
|
||
|
||
// 计算需要旋转的总角度
|
||
const rotationAngle = totalRotation - currentRotation.z;
|
||
|
||
// 使用tween动画旋转转盘
|
||
tween(this.turn)
|
||
.by(2.2, { eulerAngles: new Vec3(0, 0, rotationAngle) }, {
|
||
easing: 'cubicOut' // 使用缓动函数使旋转更自然
|
||
})
|
||
.call(() => {
|
||
// 确保最终角度正确
|
||
this.turn.eulerAngles = new Vec3(0, 0, targetAngle);
|
||
if (callback) callback();
|
||
})
|
||
.start();
|
||
}
|
||
|
||
/** 点击奖励确定 */
|
||
onClickRewardSure(){
|
||
this.reward_bg.active = false
|
||
this.noreward_bg.active = false
|
||
}
|
||
|
||
/** 点击提示 */
|
||
onClickTips(){
|
||
app.manager.ui.show<PageTips>({name: 'PageTips'})
|
||
}
|
||
|
||
/** 点击音乐 */
|
||
oncClickMusic(){
|
||
let music = app.manager.sound.isMusicPlaying
|
||
music ? app.manager.sound.stopMusic() : app.manager.sound.playDefaultMusic()
|
||
this.loadRes(music ? "main_btn_sounds_off" : "main_btn_sounds_on", Asset, (res: ImageAsset)=>{
|
||
let sp = new SpriteFrame()
|
||
let tex = new Texture2D();
|
||
tex.image = res;
|
||
sp.texture = tex
|
||
this.btn_music.getComponent(Sprite).spriteFrame = sp
|
||
});
|
||
}
|
||
|
||
/** 点击历史 */
|
||
onClickRwHis(){
|
||
app.manager.ui.show<PageRewardhistory>({name: 'PageRewardhistory'})
|
||
}
|
||
|
||
/** 点击转盘 */
|
||
btnClickTurn(){
|
||
if (this._is_turing) return;
|
||
if (USERDATA.point <= 0) {
|
||
app.manager.ui.showToast(Tools.GetLocalized("no chance"));
|
||
return;
|
||
}
|
||
|
||
this.icon_rw.spriteFrame = null;
|
||
|
||
Tools.httpReq("lottery", {}, (res:any)=>{
|
||
this.startLightToggle(); // 启动灯光切换
|
||
this.turn.setPosition(new Vec3(0, 0, 18))
|
||
|
||
let n = null
|
||
for (let i = 0; i < USERDATA.prize_list.length; i++) {
|
||
const element = USERDATA.prize_list[i];
|
||
if (res.prize_id == element.id){
|
||
n = i
|
||
break
|
||
}
|
||
}
|
||
|
||
if (n == null){
|
||
app.manager.ui.showToast(Tools.GetLocalized("fail"))
|
||
return
|
||
}
|
||
|
||
USERDATA.point--
|
||
this.lab_remain.string = "REMAINING CHANCE : " + USERDATA.point.toString()
|
||
|
||
this.rotateTurntable(n, ()=>{
|
||
this._is_turing = false
|
||
this.lab_rw.string = Tools.StringLFormat(res.message)
|
||
Tools.remoteLoadSprite(USERDATA.prize_list[n].pic, this.icon_rw)
|
||
this.unschedule(this.switchLight)
|
||
this.scheduleOnce(()=>{
|
||
this.reward_bg.active = res.prize_type != 3
|
||
this.noreward_bg.active = res.prize_type == 3
|
||
app.manager.sound.playEffect({name: res.prize_type == 3 ? "effect/lose" : "effect/win"})
|
||
}, 0.3)
|
||
})
|
||
})
|
||
}
|
||
} |