美文网首页
Creator 转盘组件实现

Creator 转盘组件实现

作者: Jaymz_2b11 | 来源:发表于2021-07-09 10:08 被阅读0次
    export default class UILotteryPanel extends UI 
    {
        
        @property(cc.Node) btn_start : cc.Node = null;
    
        @property(cc.Node) btn_close : cc.Node = null;
    
        @property(cc.Node) lotteryNode : cc.Node = null;
        
        
         private isRun = false; //是否运行
    
        private isStop = false; //准备停止
    
        private isSub = false; //是否开始减速
    
        private startAngle = 0; //初始转盘差值
    
        private curSpeed = 0;
    
        private addSpeed = 800; //second/px
        
        private addSpeedTime = 1; //加速时间
    
        private subSpeedTime = 1; //减速时间
    
        private runMinTime = 2; //转盘最小运行时间
    
        
        
        //开启转盘
        public StartLottery()
        {
            this.isRun = true;
        }
        
        //停止转盘
        public StopLottery(message)
        {
            let list = GameConfig.Instance.GetLotteryList(); //配置表
            if(message.rewardId < 1 || message.rewardId > list.length)
            {
                console.error("抽奖下标越界");
                return;
            }
            let info = list[message.rewardId-1]; //取配置
            if(info == null)
            {
                console.error("抽奖配表生效");
            }
            this.reawrdInfo = info;
            this.endIdx = message.rewardId;
            console.log( "结束 :"+ this.endIdx);
            this.isStop = true;
        }
        
        //重置转盘状态
        public ResetLottery()
        {
            this.curSpeed = 0;
            this.isRun = false;
            this.isStop = false;
            this.isSub = false;
            this.timer = 0;
            this.endOffset = 0;
            //动画状态重置
            this.tempRotation = 0;
        }
        
        
        private timer = 0;  //计时器
        private endIdx = 1; //结束点
        private endOffset = 0; //结束点偏移
        private middleOffset = 0; //与中心点偏移值 补间角度 (在规定时间内,使指针指向中心点)
        update(dt)
        {
            if(this.isRun)
            {
                if(this.isStop && this.timer >= this.runMinTime)  //收到抽奖通知 且 时间达到最小转盘时间 开始找合适位置停下
                {
                    if(this.endOffset == 0)
                    {
                        this.GetStopOffset();
                    }
                    let endAngle = ((this.endIdx * -45) + this.startAngle)%360;//找到目标位置停下
                    if(endAngle == 0)
                    {
                        endAngle = -360;
                    }
                    //使用匀减速运动算出位移 ,然后到达减速点开始减速 S = vt - (a * math.pow(t,2))/2
                    let endPoint = (this.lotteryNode.angle - this.endOffset) % 360;
                    //因为是角度是递增的,所以判断界限这边给刚进入的点设置为30 而不是45,设置为45,则可能出现指针刚好停在界限不远处
                    //若区间设置的太小,当速度值过快时,则可能出现无法找到区间,多跑很多圈的情况
                    //console.log(endPoint);
                    // -100                             -55
                    if(endPoint > endAngle && endPoint < endAngle + 45 && !this.isSub)
                    {
                        this.isSub = true;
                        console.error("当前速度:" + this.curSpeed);
                        //正数表示
                        this.middleOffset = endPoint - (endAngle + 19); //-22.5 -0 -45  最终位置减去中点位置 结果若为正数那么未旋转到位, 负数说明旋转过头
                        console.error("middleOffset : " + this.middleOffset);
                        this.curSpeed += this.middleOffset; //速度加上偏移值
                        //console.log("开始减速 : " + this.lotteryNode.rotation);
                    }
                    if(this.isSub) //开始减速
                    {
                        this.curSpeed -= (this.addSpeed) * dt;
                        if(this.curSpeed<=0)
                        {
                            //转盘完全停止 弹窗
                            //this.lotteryNode.angle -= this.middleOffset;
                            UIEventListener.Get(this.btn_start).SetAvailable(true);
                            //弹窗
                            this.ResetLottery();
                            let pop = LayoutMrg.Instance.GetLogicModule<LotteryPopModule>(LayoutName.S_LottertPopPanel);
                            if(pop)
                            {   
                                pop.LoadUI(UIPath.S_LottertPopPanel,AssetType.eUIModule,()=>
                                {
                                    pop.UpdatePanel(this.reawrdInfo);
    
                                    if(this.reawrdInfo.type < 4)  //金币 钻石 分红刷新
                                    {
                                        console.error("刷新大厅界面展示");
                                        //Player.Instance.playerInfo.diamond += this.reawrdInfo.rewardNum;
                                        let main = LayoutMrg.Instance.GetLogicModule<MainModule>(LayoutName.S_MainPanel);
                                        if(main && main.IsShow())
                                        {
                                            main.UpdateTopBar();
                                        }
                                    }
                                    this.UpdateDouble();
                                })
                            }
                            return;
                        }
                    }
                }
                else
                {
                    if(this.timer <= this.addSpeedTime) //加速阶段
                    {
                        this.curSpeed += this.addSpeed * dt;
                    }
                }
    
                this.lotteryNode.angle -= this.curSpeed * dt;
                this.lotteryNode.angle %= 360;
                if(this.lotteryNode.angle == 0)
                {
                    this.lotteryNode.angle = -360;
                }
    
                this.timer += dt;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Creator 转盘组件实现

          本文链接:https://www.haomeiwen.com/subject/vcxfpltx.html