123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- import EventMgr from "../game/EventMgr";
- import { GameEvent } from "../game/GameEvent";
- import { GameState } from "../game/GameState";
- import Main from "../game/Main";
- import { SDKIpt } from "../game/SDKIpt";
- import { SDK as sdk, CMD} from '../game/sdk';
- import { GameMode } from "../game/GameMode";
- import Button from "../game/Button";
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class resultView extends cc.Component {
- @property(cc.Button)
- rePlay: cc.Button = null;
- @property(cc.Button)
- returnMain: cc.Button = null;
- @property(cc.Node)
- bestStar: cc.Node = null;
- @property(cc.Node)
- myScore: cc.Node = null;
- @property(cc.Node)
- list: cc.Node = null;
- @property(cc.Sprite)
- resultListEmpty: cc.Sprite = null;
- @property(cc.Prefab)
- finishRankBlock:cc.Prefab = null
-
- @property(cc.Label)
- private lbKill: cc.Label = null;
- @property(cc.Label)
- private lbRound: cc.Label = null;
- @property([cc.Node])
- private failNodes: cc.Node[] = [];
- @property([cc.Node])
- private winNodes: cc.Node[] = [];
- private get isActive(): boolean { return this.node.active; }
- private index:number = 0
- private prevRanking:number = 0
- private data:Array<any> = []
- private user:any = {}
- // LIFE-CYCLE CALLBACKS:
- // onLoad () {}
- onLoad () {
- // this.node.active = false;
- this.rePlay.node.on("click",this.onRePlay,this);
- this.returnMain.node.on("click",this.onReturnMain,this);
-
- EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
- }
- ON_SDK_UI_IPT(SDK_UI_IPT: GameEvent, ipt: SDKIpt, ON_SDK_UI_IPT: any) {
- if (!this.isActive) { return; }
- switch (ipt) {
- case SDKIpt.left:
- this.index = this.index===0?1:0
- this._ButtonResultActive()
- break;
- case SDKIpt.right:
- this.index = this.index===1?0:1
- this._ButtonResultActive()
- break;
- case SDKIpt.rightFoot:
- if(this.index === 1)
- this.onRePlay();
- else
- this.onReturnMain();
- break;
- case SDKIpt.leftFoot:
- break;
- default:
- break;
- }
- }
- onRePlay() {
- this.index = 1
- this._ButtonResultActive()
- Main.Ins.PlayGame();
- this.Hide();
- }
- onReturnMain() {
- this.index = 0
- this._ButtonResultActive()
- Main.Ins.ShowMainView();
- this.Hide();
- }
- public async Show() {
-
- this.index = 0
- this._ButtonResultActive()
- // EventMgr.Instance.dispatch_event(GameEvent.sdkActionTip,[new ActionTipData(true,"返回大厅"),new ActionTipData(false,"再来一局")])
- EventMgr.Instance.dispatch_event(GameEvent.sdkActionTip,[new ActionTipData(true,"上一项"),new ActionTipData(false,"下一项")])
- //杀死数目
- this.lbKill.string = Main.Ins.Mode == GameMode.normal? Main.Ins.Score.toString():Main.Ins.EndlessRecordScore.toString();
- //最高分展示星星
- this.bestStar.active = Main.Ins.IsNewBestKill;
- //无尽模式
- this.lbRound.node.active = Main.Ins.Mode == GameMode.endless;
- if (Main.Ins.Mode == GameMode.endless) {
- this.lbRound.string = "第" + Main.Ins.Round + "波";
- }
- for (let i = 0; i < this.failNodes.length; i++) {
- const element = this.failNodes[i];
- element.active = Main.Ins.State == GameState.fail;
- }
- for (let i = 0; i < this.winNodes.length; i++) {
- const element = this.winNodes[i];
- element.active = Main.Ins.State == GameState.win;
- }
- let user = await this.getUserInfo()
- let data = await this.getRankData(GameMode.normal?1:0)
- //上一次的排名
- let prevRanking = (<any>data).find(item=>item.user.id===(<any>user).id)
- this.prevRanking = prevRanking?prevRanking.rank:0
-
- let currentScore = Main.Ins.Mode == GameMode.normal? Main.Ins.Score:Main.Ins.EndlessRecordScore
-
- sdk.onGameEnd(0,currentScore,0,Main.Ins.GetSDKGameMode(Main.Ins.Mode),0,(data)=>{
- this.updateInfo(data)
- this.init(0)
- this.node.active = true;
- });
- }
-
- public Hide(){
- this.node.active = false;
- }
- //创建内容项
- async createItem(){
- this.list.removeAllChildren()
- let currentRanking = 0
- let idx = this.data.findIndex(item=>item.user.id===this.user.id)
- if(this.prevRanking===0){//上次没排名
- if(idx>=0){//这次有排名
- currentRanking = this.data[idx].rank
- this.data = this.data.slice(idx===0?0:idx-1,idx===0?3:idx+2)
- }else{//这次没排名
- currentRanking = 0
- let arr = [{user:{id:this.user.id,nickname:this.user.nickname,gender:this.user.gender,avatar:this.user.avatar},score:Main.Ins.Mode == GameMode.normal? Main.Ins.Score:Main.Ins.EndlessRecordScore,rank:'未上榜'}]
- this.data = this.data.slice(-2).concat(arr)
- }
- }else{//上次有排名
- currentRanking = idx>=0?this.data[idx].rank:0
- this.data = this.data.slice(idx===0?0:idx-1,idx===0?3:idx+2)
- }
-
- for(let index=0; index<this.data.length;index++){
- let finishRankBlock = cc.instantiate(this.finishRankBlock)
-
- //上次有排名且比这次排名低 显示提升排名数
- if(this.prevRanking>0&&this.prevRanking>currentRanking&&this.user.id===this.data[index].user.id){
- finishRankBlock.getChildByName('item').getChildByName('goUpNum').getChildByName('val').getComponent(cc.Label).string = '+'+(this.prevRanking-currentRanking)
- finishRankBlock.getChildByName('item').getChildByName('goUpNum').active = true
- finishRankBlock.getChildByName('item').getChildByName('goUp').active = false
- }
-
- //上次没排名 这次有排名 显示提升icon
- if(this.prevRanking===0&&idx>=0&&this.user.id===this.data[index].user.id){
- finishRankBlock.getChildByName('item').getChildByName('goUpNum').active = false
- finishRankBlock.getChildByName('item').getChildByName('goUp').active = true
- }
- finishRankBlock.getChildByName('item').getChildByName('num').getComponent(cc.Label).string = this.data[index].rank +''
- finishRankBlock.getChildByName('item').getChildByName('name').getComponent(cc.Label).string = this.data[index].user.nickname
- finishRankBlock.getChildByName('item').getChildByName('score').getChildByName('value').getComponent(cc.Label).string = this.data[index].score
- try{
- var spriteFrame = await this.resourcesLoad(this.data[index].user.avatar)
- }catch(err){
- var spriteFrame:unknown = new cc.SpriteFrame()
- }
- finishRankBlock.getChildByName('item').getChildByName('avatar').getChildByName('sprite').getComponent(cc.Sprite).spriteFrame = <cc.SpriteFrame>spriteFrame
- finishRankBlock.getComponent(cc.Widget).top = 84 * index
- this.list.addChild(finishRankBlock)
- }
- }
- updateInfo(data:any){
- let currentScore = Main.Ins.Mode == GameMode.normal? Main.Ins.Score:Main.Ins.EndlessRecordScore
- let calorie = this.myScore.getChildByName('calorie')
- let step = this.myScore.getChildByName('step')
- let score = this.myScore.getChildByName('score')
- // let mark = this.myScore.getChildByName('mark')
- // mark.getComponent(cc.Label).string = GameMode.normal?'击杀一只狼 +1分':'每一分值 +1分'
- data.consume||(+data.consume)===0?calorie.getComponent(cc.Label).string = `本局消耗${data.consume}大卡,用时 ${this.timeDifference(Main.Ins.gameStartTime,Date.now())}`:calorie.active = false
- data.equivalent||(+data.equivalent)===0?step.getChildByName('num').getComponent(cc.Label).string = data.equivalent:step.active = false
- data.unit?step.getChildByName('unit').getComponent(cc.Label).string = data.unit:''
- score.getChildByName('value').getComponent(cc.Label).string =`${currentScore} 分`
- }
- resourcesLoad(url:string){
- return new Promise((resolve,reject)=>{
- cc.loader.loadRes(url, cc.SpriteFrame, (err, asset:cc.SpriteFrame) => {
- if(err) reject('未找到资源')
- resolve(asset);
- });
- })
- }
- _ButtonResultActive(){
- if(this.index == 0){
- this.returnMain.node.getComponent(Button).selected();
- this.rePlay.node.getComponent(Button).unselected()
- }else{
- this.returnMain.node.getComponent(Button).unselected();
- this.rePlay.node.getComponent(Button).selected()
- }
- }
- getUserInfo(){
- return new Promise((resolve)=>{
- // resolve({id:1,nickname:'张三',avatar:'12784.png'})
- sdk.getUserInfo((data) => {
- resolve(data)
- });
- })
-
- }
- //获取事件
- getRankData(type:number){
- return new Promise((resolve)=>{
- // resolve([
- // {rank:1,score:10000,user:{id:1,nickname:'张三',avatar:'12784.png'}},
- // {rank:2,score:11000,user:{id:2,nickname:'李四',avatar:'12784.png'}},
- // {rank:3,score:12000,user:{id:3,nickname:'王五',avatar:'12784.png'}},
- // ])
- sdk.getRank(type, function(data){
- resolve(Array.isArray(data)&&data.length>1&&data[1].list&&data[1].list.length>0?data[1].list:[])
- }.bind(this)) //0:世界榜 1:好友榜
- })
- }
- async init(type:number){
- let user = await this.getUserInfo()
- this.user = <any>user
- let data = await this.getRankData(type)
- this.data = <any>data
- // this.resultListEmpty.node.active = (<any>data).length===0
- this.createItem()
- }
- timeDifference(startTime:any, endTime:any) { //可以传日期时间或时间戳
- let start = typeof(startTime) == "number" ? startTime : new Date(startTime).getTime(),
- end = typeof(endTime) == "number" ? endTime : new Date(endTime).getTime(),
- difference = end - start, //时间差的毫秒数
- leave1 = difference % (24 * 3600 * 1000), //计算天数后剩余的毫秒数
- hours = ('0' + Math.floor(leave1 / (3600 * 1000))).slice(-2), //计算相差分钟数
- leave2 = leave1 % (3600 * 1000), //计算小时数后剩余的毫秒数
- minutes = ('0' + Math.floor(leave2 / (60 * 1000))).slice(-2), //计算相差秒数
- leave3 = leave2 % (60 * 1000), //计算分钟数后剩余的毫秒数
- seconds = ('0' + Math.round(leave3 / 1000)).slice(-2);
- return `${hours}:${minutes}:${seconds}`
- }
-
- protected onDestroy(): void {
- EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
- }
- }
|