123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- import { _decorator, Component, Node, tween, Vec3 } from "cc";
- import BaseUI from "../../scripts/base/BaseUI";
- import { Hall } from "../hall/Hall";
- import { RewardLayer } from "../layer/RewardLayer";
- import { GoodsId } from "../../scripts/api/GoodsId";
- const { ccclass, property } = _decorator;
- @ccclass("WheelDialog")
- export class WheelDialog extends BaseUI {
- static async show(taskId: number) {
- let dialog = await Hall.ins.showLayer("prefab/play/WheelDialog");
- dialog.getComponent(WheelDialog).init(taskId);
- }
- private taskId: number;
- init(taskId: number) {
- this.taskId = taskId;
- this.requestAndStartSpin();
- }
- requestAndStartSpin() {
- // random 1-wheelRewardAllCount
- let index = Math.floor(Math.random() * this.wheelRewardAllCount) + 1;
- this.spinToIndex(index);
- }
- private rotateSprite: Node;
- private isSpinning: boolean = false;
- private currentAngle: number = 0;
- @property(Number)
- public wheelRewardAllCount: number = 8;
- @property(Number)
- public spinDuration: number = 3.0; // 轉動持續時間
- @property(Number)
- public spinRounds: number = 5; // 轉動圈數
- protected onLoad(): void {
- super.onLoad();
- this.rotateSprite = this.FindNode("rotateSprite");
- }
- update(deltaTime: number) {}
- protected simpleOnBtnClick(name: string): void {
- if (name.startsWith("btn_start_")) {
- let to = parseInt(name.replace("btn_start_", "")) - 1;
- console.warn("to", to);
- this.spinToIndex(to);
- return;
- }
- switch (name) {
- case "btn_spin":
- if (!this.isSpinning) {
- this.spinToIndex(2);
- }
- break;
- }
- }
- /**
- * 轉動到指定索引的獎品
- * @param index 獎品索引 (0 到 wheelRewardAllCount-1)
- */
- spinToIndex(index: number) {
- if (this.isSpinning) {
- console.warn("轉盤正在轉動中,請稍後再試");
- return;
- }
- this.resetWheel();
- // 確保索引在有效範圍內
- if (index < 0 || index >= this.wheelRewardAllCount) {
- console.error(
- `無效的獎品索引: ${index}, 有效範圍: 0-${this.wheelRewardAllCount - 1}`
- );
- return;
- }
- this.isSpinning = true;
- // 計算每個獎品的角度
- const anglePerReward = 360 / this.wheelRewardAllCount;
- // 計算目標角度
- // 轉盤順時針轉動,索引0在頂部12點鐘方向
- // 每個獎品區域的中心角度
- const targetAngle = anglePerReward * index + anglePerReward / 2;
- // 計算總轉動角度(多轉幾圈然後停在目標位置)
- // 從當前角度開始轉動
- const totalRotation =
- this.currentAngle + this.spinRounds * 360 + targetAngle;
- // 停止之前的動畫
- tween(this.rotateSprite).stop();
- // 開始新的轉動動畫
- tween(this.rotateSprite)
- .to(
- this.spinDuration,
- { angle: totalRotation },
- {
- easing: "expoOut", // 使用指數緩出效果,快速開始然後急劇減速
- }
- )
- .call(() => {
- // 動畫完成後的回調
- this.currentAngle = totalRotation;
- this.onSpinComplete(index);
- })
- .start();
- }
- /**
- * 轉動完成後的回調
- * @param index 最終停止的獎品索引
- */
- private onSpinComplete(index: number) {
- this.isSpinning = false;
- console.log(`轉盤停止在獎品索引: ${index}`);
- // 這裡可以添加獲獎邏輯
- this.onReward(index);
- }
- /**
- * 獲獎處理
- * @param index 獎品索引
- */
- private onReward(index: number) {
- console.log(`恭喜獲得獎品索引: ${index}`);
- RewardLayer.show(
- [
- {
- id: GoodsId.POG,
- count: 100,
- },
- ],
- () => {
- this.resetWheel();
- }
- );
- // 這裡可以觸發獲獎事件或顯示獲獎界面
- // 外部可以監聽這個事件來處理具體的獎品邏輯
- // EM.emit('wheel_reward', index);
- }
- /**
- * 重置轉盤角度
- */
- public resetWheel() {
- tween(this.rotateSprite).stop();
- this.rotateSprite.angle = 0;
- this.currentAngle = 0;
- this.isSpinning = false;
- }
- /**
- * 檢查轉盤是否正在轉動
- */
- public getIsSpinning(): boolean {
- return this.isSpinning;
- }
- /**
- * 設置轉盤配置
- * @param rewardCount 獎品總數
- * @param duration 轉動時間
- * @param rounds 轉動圈數
- */
- public setWheelConfig(
- rewardCount: number,
- duration: number = 3.0,
- rounds: number = 5
- ) {
- this.wheelRewardAllCount = rewardCount;
- this.spinDuration = duration;
- this.spinRounds = rounds;
- }
- /**
- * 開始轉動到指定索引
- * @param index 要轉到的獎品索引
- */
- public startSpin(index: number) {
- if (!this.isSpinning) {
- this.spinToIndex(index);
- }
- }
- }
- function Random() {
- throw new Error("Function not implemented.");
- }
|