123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- import EventMgr from "../game/EventMgr";
- import { GameEvent } from "../game/GameEvent";
- import { SDKIpt } from "../game/SDKIpt";
- import { SDK as sdk, CMD} from '../game/sdk';
- import Main from "../game/Main";
- import { GameMode } from "../game/GameMode";
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class gameRank extends cc.Component {
- @property(cc.Node)
- private mainView:cc.Node = null
- @property(cc.Button)
- goBackNode:cc.Button = null
- @property(cc.Button)
- rankFriends:cc.Button = null
- @property(cc.Button)
- rankWorlds:cc.Button = null
- @property(cc.Node)
- content:cc.Node = null
- @property(cc.Node)
- sdkActTipView:cc.Node = null
- @property(cc.Sprite)
- empty:cc.Sprite = null
- @property(cc.Sprite)
- myBlock:cc.Sprite = null
- @property(cc.Prefab)
- itemPrefab:cc.Prefab = null
- private data:Array<any> = []
- private user:any = {}
- private get isActive(): boolean { return this.node.active; }
- start () {
-
- this.goBackNode.node.on(cc.Node.EventType.TOUCH_START,this.goBack,this)
- this.rankFriends.node.on(cc.Node.EventType.TOUCH_START,this.rankFriendHandler,this)
- this.rankWorlds.node.on(cc.Node.EventType.TOUCH_START,this.rankWorldHandler,this)
- EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
- }
- protected onDestroy(): void {
- EventMgr.Instance.remove_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) {
- console.error("mainview 收到 sdk 动作:"+ipt );
- if (!this.isActive) { return; }
- switch (ipt) {
- case SDKIpt.left:
- this.rankFriendHandler()
- break;
- case SDKIpt.right:
- this.rankWorldHandler()
- break;
- case SDKIpt.rightFoot://主界面确认进入游戏//y
- this.goBack()
- break;
- case SDKIpt.leftFoot:
-
- break;
- default:
- break;
- }
-
- }
- //隐藏排行榜
- goBack(){
- this.node.active = false
- this.sdkActTipView.active = true
- this.mainView.active = true
- }
- //点击好友榜事件
- rankFriendHandler(){
- this.rankTypeSelected(this.rankFriends)
- this.rankTypeUnselected(this.rankWorlds)
- this.init(1);
- }
- //点击世界榜事件
- rankWorldHandler(){
- this.rankTypeSelected(this.rankWorlds)
- this.rankTypeUnselected(this.rankFriends)
- this.init(0);
- }
- //tab未选中样式
- rankTypeUnselected(v:cc.Button){
- let node = v.node.getChildByName('Background')
- let arrow = v.node.getChildByName('arrow')
- arrow.active = false
- node.color = new cc.Color(63,64,64,1)
- }
- //tab选中样式
- rankTypeSelected(v:cc.Button){
- let node = v.node.getChildByName('Background')
- let arrow = v.node.getChildByName('arrow')
- arrow.active = true
- node.color = new cc.Color(113,170,0,1)
- }
- //打开排行榜
- openRankDialog(){
- this.sdkActTipView.active = false
- this.node.active = true;
- this.init(1);
- }
- // //滚动回调
- // scrollHandler(event){
- // console.log(this.content.y);
- // }
- //创建内容项
- async createItem(){
- this.empty.node.active = this.data.length<=0
- this.content.removeAllChildren()
- this.content.height = this.data.length *100 +30*(this.data.length-1)
- this.content.getComponent(cc.Widget).top = 0
- for(let index=0; index<this.data.length;index++){
- let itemPrefab = cc.instantiate(this.itemPrefab)
- if(this.data[index].rank==1) itemPrefab.getChildByName('ranking').getChildByName('one').active = true;
- if(this.data[index].rank==2) itemPrefab.getChildByName('ranking').getChildByName('two').active = true;
- if(this.data[index].rank==3) itemPrefab.getChildByName('ranking').getChildByName('three').active = true;
- if(this.data[index].rank>3) {
- itemPrefab.getChildByName('ranking').getChildByName('other').active = true;
- itemPrefab.getChildByName('ranking').getChildByName('other').getComponent(cc.Label).string = this.data[index].rank +''
- }
- itemPrefab.getChildByName('name').getComponent(cc.Label).string = this.data[index].user.nickname
- itemPrefab.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()
- }
- itemPrefab.getChildByName('avatar').getChildByName('sprite').getComponent(cc.Sprite).spriteFrame = <cc.SpriteFrame>spriteFrame
- itemPrefab.getComponent(cc.Widget).top = 130 * index
- this.content.addChild(itemPrefab)
- }
- }
- //更新个人信息
- async updateUserInfo(){
- let BestScoreNormal = Main.Ins.getBestScore(GameMode.normal) || 0
- let BestScoreEndless = Main.Ins.getBestScore(GameMode.endless) || 0
- this.myBlock.node.getChildByName('name').getComponent(cc.Label).string = this.user.nickname
- try{
- var spriteFrame = await this.resourcesLoad(this.user.avatar)
- }catch(err){
- var spriteFrame:unknown = new cc.SpriteFrame()
- }
- this.myBlock.node.getChildByName('avatar').getChildByName('sprite').getComponent(cc.Sprite).spriteFrame = <cc.SpriteFrame>spriteFrame
- const currentInfo = this.data.find(item=>item.user.id === this.user.id)
- if(currentInfo){
- this.myBlock.node.getChildByName('score').getChildByName('value').getComponent(cc.Label).string = currentInfo.score+''
- this.myBlock.node.getChildByName('ranking').getChildByName('val').getComponent(cc.Label).string = currentInfo.rank
- this.myBlock.node.getChildByName('ranking').active = true
- this.myBlock.node.getChildByName('notRanking').active = false
- }else{
- this.myBlock.node.getChildByName('score').getChildByName('value').getComponent(cc.Label).string = (Math.max(BestScoreNormal,BestScoreEndless))+''
- this.myBlock.node.getChildByName('notRanking').active = true
- this.myBlock.node.getChildByName('ranking').active = false
- }
- this.myBlock.node.active = true
- }
- resourcesLoad(url:string){
- return new Promise((resolve,reject)=>{
- cc.assetManager.loadRemote<cc.Texture2D>(url, function (err, texture) {
- if(err) reject('未找到资源')
- resolve(new cc.SpriteFrame(texture));
- });
- })
- return new Promise((resolve,reject)=>{
- cc.loader.loadRes(url, cc.SpriteFrame, (err, asset:cc.SpriteFrame) => {
- if(err) reject('未找到资源')
- resolve(asset);
- });
- })
- }
- 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.updateUserInfo()
- this.createItem()
- }
-
- // update (dt) {}
- }
|