/**
 * 单机游戏场景
 */ class StandaloneModel extends egret . DisplayObjectContainer { /**
     * 容器
     */ private layout ; private textfield : egret . TextField ; private puker : PukerContainer ; private pukerPlay : PukerPlayContainer ; private pukerRight : PukerOtherContainer ; private pukerLeft : PukerOtherContainer ; private pukerVerticalRight : PukerVerticalContainer ; private pukerVerticalLeft : PukerVerticalContainer ; private portrait : PortraitContainer ; private portraitLeft : PortraitOtherContainer ; private portraitRight : PortraitOtherContainer ; private leftCount : OtherPukerCountContains ; private rightCount : OtherPukerCountContains ; private pukerBottom : PukerBottomContainer ; private textRight : TextOtherContainer ; private textLeft : TextOtherContainer ; private buttons : PlayerButtonContainer ; private bottom : BottomContainer ; /**
     * 是否是开发模式，开发模式下将显示明牌
     */ private isDevelop : boolean = false ; private poc1Index : number = Math . floor ( Math . random ( ) * 10 ) + 1 ; private poc2Index : number = Math . floor ( Math . random ( ) * 10 ) + 1 ; private poc3Index : number = Math . floor ( Math . random ( ) * 10 ) + 1 ; /**
     * 变量
     */ private index : number = 0 ; private mySeat : number = 1 ; private landlordSeat : number = 1 ; private playingSeat : number = 1 ; /**
     * 当前一手牌
     */ private playedPuker : Array < Poker > = [ ] ; private playedSeat : number = 1 ; private player2 : User = PukerUtils . getRandomUser ( ) ; private player3 : User = PukerUtils . getRandomUser ( ) ; private playerPukers1 : Array < Poker > = new Array < Poker > ( ) ; private playerPukers2 : Array < Poker > = new Array < Poker > ( ) ; private playerPukers3 : Array < Poker > = new Array < Poker > ( ) ; private pukerSelectArray : Array < Poker > = new Array < Poker > ( ) ; /**
     * 发牌
     */ private pokers : Array < Poker > = PukerUtils . getRandomPokers ( ) ; private pukers1 : Array < Poker > = PukerUtils . sortDescPokers ( this . pokers . slice ( 0 , 17 ) ) ; private pukers2 : Array < Poker > = PukerUtils . sortDescPokers ( this . pokers . slice ( 17 , 34 ) ) ; private pukers3 : Array < Poker > = PukerUtils . sortDescPokers ( this . pokers . slice ( 34 , 51 ) ) ; private pukers4 : Array < Poker > = this . pokers . slice ( 51 ) ; private user : User ; /**
     * 单机游戏场景
     */ public constructor ( user ) { super ( ) ; this . user = user ; this . layout = RES . getRes ( "layout_json" ) . layout ; let sky = new Layout ( this . layout . stageWidth , this . layout . stageHeight ) ; this . addChild ( sky ) ; let tip = new TextTip ( "返回大厅" , 10 , 10 , 64 ) ; tip . touchEnabled = true ; tip . addEventListener ( egret . TouchEvent . TOUCH_TAP , this . button_gameOver , this ) ; this . addChild ( tip ) ; this . show ( ) ; } /**
     * 发牌，显示一张一张发牌的效果
     * 1.缓慢显示自己的牌
     * 2.缓慢刷新其他玩家的牌的计数
     */ private initPukers ( ) { let index = 17 ; let pukers = new Array < Poker > ( ) ; let i = 0 ; this . createTimer ( 300 , index , ( ) => { console . log ( "发牌：" , i ) ; pukers . push ( this . pukers1 [ i ] ) ; this . clearPukers ( ) ; this . puker = new PukerContainer ( this , pukers ) ; this . puker . name = "puker" ; this . addChild ( this . puker ) ; this . showCount ( 2 , i + 1 ) ; this . showCount ( 3 , i + 1 ) ; i ++ ; } , ( ) => { console . log ( "发牌结束" ) ; /** 显示叫地主按钮 */ this . showButtons ( RoomManager . ButtonsCallTheLandlord ) ; } ) ; } private show ( ) { /** 显示玩家的牌 */ this . initPukers ( ) ; this . showCount ( 2 , this . pukers2 . length ) ; this . showCount ( 3 , this . pukers3 . length ) ; if ( this . isDevelop ) { /** 显示下家的牌 */ this . pukerVerticalRight = new PukerVerticalContainer ( this . pukers2 , false ) ; this . pukerVerticalRight . name = "pukerVerticalRight" ; this . addChild ( this . pukerVerticalRight ) ; /** 显示上家的牌 */ this . pukerVerticalLeft = new PukerVerticalContainer ( this . pukers3 , true ) ; this . pukerVerticalLeft . name = "pukerVerticalLeft" ; this . addChild ( this . pukerVerticalLeft ) ; } /** 显示底牌 */ this . pukerBottom = new PukerBottomContainer ( this . pukers4 ) ; this . pukerBottom . name = "pukerBottom" ; this . addChild ( this . pukerBottom ) ; /** 显示玩家头像 */ this . showPortrait ( false ) ; /** 显示上家头像 */ this . showOtherPortrait ( 2 , false ) ; /** 显示上家头像 */ this . showOtherPortrait ( 3 , false ) ; this . bottom = new BottomContainer ( 1 ) ; this . bottom . name = "bottom" ; this . addChild ( this . bottom ) ; } /**
     * 监听点击“出牌”按钮的动作
     * 点击出牌后：
     *  1.从自己牌的数组中移除已选择的牌
     *  2.刷新自己的牌
     *  3.刷新出牌区
     *  4.清空已选择牌的数组
     */ public buttonChuPai ( evt ) { if ( this . playedSeat != 1 ) { if ( ! PukerCompareUtils . comparePukers ( this . pukerSelectArray , this . playedPuker ) ) { console . log ( "没有上家的牌大,上家座位号：" , this . playedSeat ) ; return ; } } if ( PukerTypeUtils . getType ( this . pukerSelectArray ) == null ) { console . log ( "你这选的是啥呀" ) ; return ; } this . pukers1 = PukerUtils . removePokers ( this . pukers1 , this . pukerSelectArray ) ; console . log ( "已出牌，剩余牌数" , this . pukers1 ) ; if ( this . getChildByName ( "puker" ) != null ) { this . removeChild ( this . getChildByName ( "puker" ) ) ; } this . puker = new PukerContainer ( this , this . pukers1 ) ; this . puker . name = "puker" ; this . addChild ( this . puker ) ; this . showPukerPlay ( ) ; this . playedPuker = PukerUtils . sortDescPokers ( this . pukerSelectArray ) ; this . playedSeat = 1 ; this . playingSeat = 2 ; this . autoPlay ( ) ; this . pukerSelectArray = new Array < Poker > ( ) ; this . clearButtons ( ) ; } /**
     * 不叫地主，
     * 下家或上家成为地主，得到底牌，
     * 刷新地主头像，刷新地主牌，
     * 叫地主按钮消失，等待地主出牌
     */ public buttonBuJiao ( evt ) { console . log ( "不叫" ) ; this . clearButtons ( ) ; let randNumber = Math . floor ( Math . random ( ) * 2 ) ; if ( randNumber === 0 ) { this . pukers2 = PukerUtils . sortDescPokers ( this . pukers2 . concat ( this . pukers4 ) ) ; if ( this . isDevelop ) this . showPukerVertival ( 2 ) ; this . showCount ( 2 , this . pukers2 . length ) ; this . playingSeat = 2 ; this . landlordSeat = 2 ; this . autoPlay ( ) ; this . showOtherPortrait ( 2 , true ) ; } else { this . pukers3 = PukerUtils . sortDescPokers ( this . pukers3 . concat ( this . pukers4 ) ) ; if ( this . isDevelop ) this . showPukerVertival ( 3 ) ; this . showCount ( 3 , this . pukers3 . length ) ; this . playingSeat = 3 ; this . landlordSeat = 3 ; this . autoPlay ( ) ; this . showOtherPortrait ( 3 , true ) ; } } /**
     * 叫地主，玩家成为地主，玩家得到底牌，刷新玩家的牌
     */ public buttonJiaoDiZhu ( evt ) { console . log ( "叫地主" ) ; this . pukers1 = PukerUtils . sortDescPokers ( this . pukers1 . concat ( this . pukers4 ) ) ; this . pukerSelectArray = new Array < Poker > ( ) ; this . showPukers ( ) ; this . showButtons ( RoomManager . ButtonsDiscard ) ; this . showPortrait ( true ) ; } public buttonBuQiang ( evt ) { console . log ( "不抢" ) ; } public buttonQiangDiZhu ( evt ) { console . log ( "抢地主" ) ; } public buttonBuYao ( evt ) { if ( this . landlordSeat == 1 && this . pukers1 . length == 20 ) { console . log ( "地主首张牌" , this . pukers1 . length ) ; } else { this . clearButtons ( ) ; this . playingSeat = 2 ; this . autoPlay ( ) ; } } public buttonTiShi ( evt ) { console . log ( PukerTypeUtils . getType ( this . pukerSelectArray ) ) ; } /**
     * 游戏结束，返回大厅
     */ public button_gameOver ( ) { console . log ( "返回大厅" ) ; let gameHall = new GameHall ( this . user ) ; gameHall . name = "gameHall" ; this . parent . addChild ( gameHall ) ; this . parent . removeChild ( this ) ; } /**
     * 再来一局
     */ public button_restart ( ) { let playerP2c = new StandaloneModel ( this . user ) ; this . parent . addChild ( playerP2c ) ; this . parent . removeChild ( this ) ; } private clearPukerPlay ( ) { if ( this . getChildByName ( "pukerPlay" ) != null ) { this . removeChild ( this . getChildByName ( "pukerPlay" ) ) ; } } private showPukerPlay ( ) { this . clearPukerPlay ( ) ; this . pukerPlay = new PukerPlayContainer ( PukerUtils . sortDescPokers ( this . pukerSelectArray ) ) ; this . pukerPlay . name = "pukerPlay" ; this . addChild ( this . pukerPlay ) ; } /**
     * 显示游戏结果
     */ private showResult ( isVictory ) { if ( this . getChildByName ( "result" ) != null ) { this . removeChild ( this . getChildByName ( "result" ) ) ; } let result = new GameResultContainer ( isVictory ) ; result . name = "result" ; this . addChild ( result ) ; console . log ( "showResult 游戏结束" ) ; this . showButtons ( RoomManager . ButtonsGameOver ) ; } /**
     * 判断游戏是否结束
     */ private gameOver ( ) { if ( this . pukers1 == null || this . pukers1 . length == 0 ) { this . showResult ( true ) ; return true ; } if ( this . pukers2 == null || this . pukers2 . length == 0 ) { if ( this . landlordSeat !== 3 ) { this . showResult ( false ) ; } else { this . showResult ( true ) ; } return true ; } if ( this . pukers3 == null || this . pukers3 . length == 0 ) { if ( this . landlordSeat !== 2 ) { this . showResult ( false ) ; } else { this . showResult ( true ) ; } return true ; } return false ; } /**
     * 自动出牌的控制
     * 当轮到玩家出牌时，退出自动出牌，
     * 当轮到系统出牌时，随机延迟一段时间，同时倒计时15秒（相当与人的思考时间）
     */ private autoPlay ( ) { if ( this . gameOver ( ) ) return ; if ( this . playingSeat === 1 ) { return ; } this . clearTextTip ( this . playingSeat ) ; let index = Math . floor ( Math . random ( ) * 5 ) + 1 ; let time = 15 ; this . createTimer ( 1000 , index , ( ) => { this . showOPTimer ( time -- ) ; } , ( ) => { this . clearOPTimer ( ) ; this . doAutoPlay ( ) ; } ) ; } /**
     * 自动出牌执行逻辑
     */ private doAutoPlay ( ) { if ( this . playingSeat === 2 ) { this . playerPukers2 = PukerSeekUtils . autoPlay ( this . pukers2 , 2 , this . landlordSeat , this . playedPuker , this . playedSeat ) ; if ( this . playerPukers2 == null || this . playerPukers2 . length == 0 ) { this . showTextTip ( this . playingSeat ) ; } else { this . showOtherPuker ( 2 ) ; this . pukers2 = PukerUtils . sortDescPokers ( PukerUtils . removePokers ( this . pukers2 , this . playerPukers2 ) ) ; if ( this . isDevelop ) this . showPukerVertival ( 2 ) ; this . playedPuker = this . playerPukers2 ; this . playedSeat = 2 ; this . showCount ( 2 , this . pukers2 . length ) ; if ( this . gameOver ( ) ) return ; } this . playingSeat = 3 ; this . autoPlay ( ) ; } else if ( this . playingSeat === 3 ) { this . playerPukers3 = PukerSeekUtils . autoPlay ( this . pukers3 , 3 , this . landlordSeat , this . playedPuker , this . playedSeat ) ; if ( this . playerPukers3 == null || this . playerPukers3 . length == 0 ) { this . showTextTip ( this . playingSeat ) ; } else { this . showOtherPuker ( 3 ) ; this . pukers3 = PukerUtils . sortDescPokers ( PukerUtils . removePokers ( this . pukers3 , this . playerPukers3 ) ) ; if ( this . isDevelop ) this . showPukerVertival ( 3 ) ; this . playedPuker = this . playerPukers3 ; this . playedSeat = 3 ; this . showCount ( 3 , this . pukers3 . length ) ; if ( this . gameOver ( ) ) return ; } this . playingSeat = 1 ; this . clearPukerPlay ( ) ; this . showButtons ( RoomManager . ButtonsDiscard ) ; } } /**
     * 监听点击扑克的动作
     */ public pukerClick ( evt ) { let p = RES . getRes ( "layout_json" ) . puker ; let y = p . pukerUpMove ; let draggedObject = evt . currentTarget ; let id = parseInt ( draggedObject . name . split ( "," ) [ 0 ] ) ; let orderValue = parseInt ( draggedObject . name . split ( "," ) [ 1 ] ) ; if ( draggedObject . y == y ) { draggedObject . y = 0 ; this . pukerSelectArray . push ( new Poker ( id , orderValue ) ) ; } else { draggedObject . y = y ; let poker = new Poker ( id , orderValue ) ; console . log ( "poker" , poker . toString ( ) , "array" , this . pukerSelectArray ) ; this . pukerSelectArray = PukerUtils . removePokers ( this . pukerSelectArray , [ poker ] ) ; console . log ( "removedArray" , this . pukerSelectArray ) ; } } private createTimer ( delay , times , timerRun = ( times ) => { } , timerEnd = ( ) => { } ) { var timer = new egret . Timer ( delay , times ) ; timer . addEventListener ( egret . TimerEvent . TIMER , timerRun , this ) ; timer . addEventListener ( egret . TimerEvent . TIMER_COMPLETE , timerEnd , this ) ; timer . start ( ) ; } /**
     * 清理玩家出牌展示区和不出牌提示区
     * seat 玩家座位号
     */ private clearTextTip ( seat ) { if ( seat === 3 ) { if ( this . getChildByName ( "pukerLeft" ) != null ) { this . removeChild ( this . getChildByName ( "pukerLeft" ) ) ; } if ( this . getChildByName ( "textLeft" ) != null ) { this . removeChild ( this . getChildByName ( "textLeft" ) ) ; } } else { if ( this . getChildByName ( "pukerRight" ) != null ) { this . removeChild ( this . getChildByName ( "pukerRight" ) ) ; } if ( this . getChildByName ( "textRight" ) != null ) { this . removeChild ( this . getChildByName ( "textRight" ) ) ; } } } /**
     *  展示其他玩家不出牌时的文字提示区
     * seat  玩家座位号
     */ private showTextTip ( seat ) { this . clearTextTip ( seat ) ; if ( seat === 3 ) { this . textLeft = new TextOtherContainer ( PukerUtils . getRandomTextTip ( ) , true ) ; this . textLeft . name = "textLeft" ; this . addChild ( this . textLeft ) ; } else { this . textRight = new TextOtherContainer ( PukerUtils . getRandomTextTip ( ) , false ) ; this . textRight . name = "textRight" ; this . addChild ( this . textRight ) ; } } /**
     * 清理玩家的明牌
     * seat  玩家座位号
     */ private clearPukerVertical ( seat ) { if ( seat === 2 ) { if ( this . getChildByName ( "pukerVerticalRight" ) != null ) { this . removeChild ( this . getChildByName ( "pukerVerticalRight" ) ) ; } } else { if ( this . getChildByName ( "pukerVerticalLeft" ) != null ) { this . removeChild ( this . getChildByName ( "pukerVerticalLeft" ) ) ; } } } /**
     * 显示其他玩家的明牌
     * seat 玩家座位号
     */ private showPukerVertival ( seat ) { this . clearPukerVertical ( seat ) ; if ( seat === 2 ) { this . pukerVerticalRight = new PukerVerticalContainer ( this . pukers2 , false ) ; this . pukerVerticalRight . name = "pukerVerticalRight" ; this . addChild ( this . pukerVerticalRight ) ; } else { this . pukerVerticalLeft = new PukerVerticalContainer ( this . pukers3 , true ) ; this . pukerVerticalLeft . name = "pukerVerticalLeft" ; this . addChild ( this . pukerVerticalLeft ) ; } } /**
     * 显示其他玩家头像
     * seat 玩家座位号
     * isLandlord 是不是地主
     */ private showOtherPortrait ( seat , isLandlord ) { this . clearOtherPortrait ( seat ) ; if ( seat === 3 ) { this . portraitLeft = new PortraitOtherContainer ( this . player3 , this . poc3Index , true , isLandlord ) ; this . portraitLeft . name = "portraitLeft" ; this . addChild ( this . portraitLeft ) ; } else { this . portraitRight = new PortraitOtherContainer ( this . player2 , this . poc2Index , false , isLandlord ) ; this . portraitRight . name = "portraitRight" ; this . addChild ( this . portraitRight ) ; } } /**
     * 清理其他玩家头像
     * seat 玩家座位号
     */ private clearOtherPortrait ( seat ) { if ( seat === 3 ) { if ( this . getChildByName ( "portraitLeft" ) != null ) { this . removeChild ( this . getChildByName ( "portraitLeft" ) ) ; } } else { if ( this . getChildByName ( "portraitRight" ) != null ) { this . removeChild ( this . getChildByName ( "portraitRight" ) ) ; } } } private clearPortrait ( ) { if ( this . getChildByName ( "portrait" ) != null ) { this . removeChild ( this . getChildByName ( "portrait" ) ) ; } } private showPortrait ( isLandlord ) { this . clearPortrait ( ) ; this . portrait = new PortraitContainer ( this . user , this . poc1Index , isLandlord ) ; this . portrait . name = "portrait" ; this . addChild ( this . portrait ) ; } /**
     * 清理其他玩家剩余的牌的数量
     * seat 玩家座位号
     */ private clearCount ( seat ) { if ( seat === 3 ) { if ( this . getChildByName ( "leftCount" ) != null ) { this . removeChild ( this . getChildByName ( "leftCount" ) ) ; } } else { if ( this . getChildByName ( "rightCount" ) != null ) { this . removeChild ( this . getChildByName ( "rightCount" ) ) ; } } } /**
     * 显示其他玩家剩余的牌的数量
     * seat 玩家座位号
     * count 剩余牌数量
     */ private showCount ( seat , count ) { this . clearCount ( seat ) ; if ( seat === 3 ) { this . leftCount = new OtherPukerCountContains ( count , true ) ; this . leftCount . name = "leftCount" ; this . addChild ( this . leftCount ) ; } else { this . rightCount = new OtherPukerCountContains ( count , false ) ; this . rightCount . name = "rightCount" ; this . addChild ( this . rightCount ) ; } } /**
    * 显示其他玩家出的牌
    * seat 座位号
    */ private showOtherPuker ( seat ) { this . clearTextTip ( seat ) ; if ( seat === 3 ) { this . pukerLeft = new PukerOtherContainer ( this . playerPukers3 , true ) ; this . pukerLeft . name = "pukerLeft" ; this . addChild ( this . pukerLeft ) ; } else { this . pukerRight = new PukerOtherContainer ( this . playerPukers2 , false ) ; this . pukerRight . name = "pukerRight" ; this . addChild ( this . pukerRight ) ; } } /**
     * 清理出牌按钮组
     */ private clearButtons ( ) { if ( this . getChildByName ( "buttons" ) != null ) { this . removeChild ( this . getChildByName ( "buttons" ) ) ; } } /**
     * 显示出牌按钮组
     */ private showButtons ( type ) { this . clearButtons ( ) ; this . buttons = new PlayerButtonContainer ( this , type ) ; this . buttons . name = "buttons" ; this . addChild ( this . buttons ) ; } /**
     * 清理自己的牌
     */ private clearPukers ( ) { if ( this . getChildByName ( "puker" ) != null ) { this . removeChild ( this . getChildByName ( "puker" ) ) ; } } /**
     * 显示自己的牌
     */ private showPukers ( ) { this . clearPukers ( ) ; this . puker = new PukerContainer ( this , this . pukers1 ) ; this . puker . name = "puker" ; this . addChild ( this . puker ) ; } private clearOPTimer ( ) { if ( this . getChildByName ( "otherPlayingTimer" ) != null ) { this . removeChild ( this . getChildByName ( "otherPlayingTimer" ) ) ; } } private showOPTimer ( index ) { this . clearOPTimer ( ) ; let timer = new OtherPlayingTimer ( index + "" , this . playingSeat === 3 ) ; timer . name = "otherPlayingTimer" ; this . addChild ( timer ) ; } }