gameRank.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import EventMgr from "../game/EventMgr";
  2. import { GameEvent } from "../game/GameEvent";
  3. import { SDKIpt } from "../game/SDKIpt";
  4. import { SDK as sdk, CMD} from '../game/sdk';
  5. import Main from "../game/Main";
  6. import { GameMode } from "../game/GameMode";
  7. const {ccclass, property} = cc._decorator;
  8. @ccclass
  9. export default class gameRank extends cc.Component {
  10. @property(cc.Node)
  11. private mainView:cc.Node = null
  12. @property(cc.Button)
  13. goBackNode:cc.Button = null
  14. @property(cc.Button)
  15. rankFriends:cc.Button = null
  16. @property(cc.Button)
  17. rankWorlds:cc.Button = null
  18. @property(cc.Node)
  19. content:cc.Node = null
  20. @property(cc.Node)
  21. sdkActTipView:cc.Node = null
  22. @property(cc.Sprite)
  23. empty:cc.Sprite = null
  24. @property(cc.Sprite)
  25. myBlock:cc.Sprite = null
  26. @property(cc.Prefab)
  27. itemPrefab:cc.Prefab = null
  28. private data:Array<any> = []
  29. private user:any = {}
  30. private get isActive(): boolean { return this.node.active; }
  31. start () {
  32. this.goBackNode.node.on(cc.Node.EventType.TOUCH_START,this.goBack,this)
  33. this.rankFriends.node.on(cc.Node.EventType.TOUCH_START,this.rankFriendHandler,this)
  34. this.rankWorlds.node.on(cc.Node.EventType.TOUCH_START,this.rankWorldHandler,this)
  35. EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  36. }
  37. protected onDestroy(): void {
  38. EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  39. }
  40. ON_SDK_UI_IPT(SDK_UI_IPT: GameEvent, ipt: SDKIpt, ON_SDK_UI_IPT: any) {
  41. console.error("mainview 收到 sdk 动作:"+ipt );
  42. if (!this.isActive) { return; }
  43. switch (ipt) {
  44. case SDKIpt.left:
  45. this.rankFriendHandler()
  46. break;
  47. case SDKIpt.right:
  48. this.rankWorldHandler()
  49. break;
  50. case SDKIpt.rightFoot://主界面确认进入游戏//y
  51. this.goBack()
  52. break;
  53. case SDKIpt.leftFoot:
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. //隐藏排行榜
  60. goBack(){
  61. this.node.active = false
  62. this.sdkActTipView.active = true
  63. this.mainView.active = true
  64. }
  65. //点击好友榜事件
  66. rankFriendHandler(){
  67. this.rankTypeSelected(this.rankFriends)
  68. this.rankTypeUnselected(this.rankWorlds)
  69. this.init(1);
  70. }
  71. //点击世界榜事件
  72. rankWorldHandler(){
  73. this.rankTypeSelected(this.rankWorlds)
  74. this.rankTypeUnselected(this.rankFriends)
  75. this.init(0);
  76. }
  77. //tab未选中样式
  78. rankTypeUnselected(v:cc.Button){
  79. let node = v.node.getChildByName('Background')
  80. let arrow = v.node.getChildByName('arrow')
  81. arrow.active = false
  82. node.color = new cc.Color(63,64,64,1)
  83. }
  84. //tab选中样式
  85. rankTypeSelected(v:cc.Button){
  86. let node = v.node.getChildByName('Background')
  87. let arrow = v.node.getChildByName('arrow')
  88. arrow.active = true
  89. node.color = new cc.Color(113,170,0,1)
  90. }
  91. //打开排行榜
  92. openRankDialog(){
  93. this.sdkActTipView.active = false
  94. this.node.active = true;
  95. this.init(1);
  96. }
  97. // //滚动回调
  98. // scrollHandler(event){
  99. // console.log(this.content.y);
  100. // }
  101. //创建内容项
  102. async createItem(){
  103. this.empty.node.active = this.data.length<=0
  104. this.content.removeAllChildren()
  105. this.content.height = this.data.length *100 +30*(this.data.length-1)
  106. this.content.getComponent(cc.Widget).top = 0
  107. for(let index=0; index<this.data.length;index++){
  108. let itemPrefab = cc.instantiate(this.itemPrefab)
  109. if(this.data[index].rank==1) itemPrefab.getChildByName('ranking').getChildByName('one').active = true;
  110. if(this.data[index].rank==2) itemPrefab.getChildByName('ranking').getChildByName('two').active = true;
  111. if(this.data[index].rank==3) itemPrefab.getChildByName('ranking').getChildByName('three').active = true;
  112. if(this.data[index].rank>3) {
  113. itemPrefab.getChildByName('ranking').getChildByName('other').active = true;
  114. itemPrefab.getChildByName('ranking').getChildByName('other').getComponent(cc.Label).string = this.data[index].rank +''
  115. }
  116. itemPrefab.getChildByName('name').getComponent(cc.Label).string = this.data[index].user.nickname
  117. itemPrefab.getChildByName('score').getChildByName('value').getComponent(cc.Label).string = this.data[index].score
  118. try{
  119. var spriteFrame = await this.resourcesLoad(this.data[index].user.avatar)
  120. }catch(err){
  121. var spriteFrame:unknown = new cc.SpriteFrame()
  122. }
  123. itemPrefab.getChildByName('avatar').getChildByName('sprite').getComponent(cc.Sprite).spriteFrame = <cc.SpriteFrame>spriteFrame
  124. itemPrefab.getComponent(cc.Widget).top = 130 * index
  125. this.content.addChild(itemPrefab)
  126. }
  127. }
  128. //更新个人信息
  129. async updateUserInfo(){
  130. let BestScoreNormal = Main.Ins.getBestScore(GameMode.normal) || 0
  131. let BestScoreEndless = Main.Ins.getBestScore(GameMode.endless) || 0
  132. this.myBlock.node.getChildByName('name').getComponent(cc.Label).string = this.user.nickname
  133. try{
  134. var spriteFrame = await this.resourcesLoad(this.user.avatar)
  135. }catch(err){
  136. var spriteFrame:unknown = new cc.SpriteFrame()
  137. }
  138. this.myBlock.node.getChildByName('avatar').getChildByName('sprite').getComponent(cc.Sprite).spriteFrame = <cc.SpriteFrame>spriteFrame
  139. const currentInfo = this.data.find(item=>item.user.id === this.user.id)
  140. if(currentInfo){
  141. this.myBlock.node.getChildByName('score').getChildByName('value').getComponent(cc.Label).string = currentInfo.score+''
  142. this.myBlock.node.getChildByName('ranking').getChildByName('val').getComponent(cc.Label).string = currentInfo.rank
  143. this.myBlock.node.getChildByName('ranking').active = true
  144. this.myBlock.node.getChildByName('notRanking').active = false
  145. }else{
  146. this.myBlock.node.getChildByName('score').getChildByName('value').getComponent(cc.Label).string = (Math.max(BestScoreNormal,BestScoreEndless))+''
  147. this.myBlock.node.getChildByName('notRanking').active = true
  148. this.myBlock.node.getChildByName('ranking').active = false
  149. }
  150. this.myBlock.node.active = true
  151. }
  152. resourcesLoad(url:string){
  153. return new Promise((resolve,reject)=>{
  154. cc.assetManager.loadRemote<cc.Texture2D>(url, function (err, texture) {
  155. if(err) reject('未找到资源')
  156. resolve(new cc.SpriteFrame(texture));
  157. });
  158. })
  159. return new Promise((resolve,reject)=>{
  160. cc.loader.loadRes(url, cc.SpriteFrame, (err, asset:cc.SpriteFrame) => {
  161. if(err) reject('未找到资源')
  162. resolve(asset);
  163. });
  164. })
  165. }
  166. getUserInfo(){
  167. return new Promise((resolve)=>{
  168. // resolve({id:1,nickname:'张三',avatar:'12784.png'})
  169. sdk.getUserInfo((data) => {
  170. resolve(data)
  171. });
  172. })
  173. }
  174. //获取事件
  175. getRankData(type:number){
  176. return new Promise((resolve)=>{
  177. // resolve([
  178. // {rank:1,score:10000,user:{id:1,nickname:'张三',avatar:'12784.png'}},
  179. // {rank:2,score:11000,user:{id:2,nickname:'李四',avatar:'12784.png'}},
  180. // {rank:3,score:12000,user:{id:3,nickname:'王五',avatar:'12784.png'}},
  181. // ])
  182. sdk.getRank(type, function(data){
  183. resolve(Array.isArray(data)&&data.length>1&&data[1].list&&data[1].list.length>0?data[1].list:[])
  184. }.bind(this)) //0:世界榜 1:好友榜
  185. })
  186. }
  187. async init(type:number){
  188. let user = await this.getUserInfo()
  189. this.user = <any>user
  190. let data = await this.getRankData(type)
  191. this.data = <any>data
  192. this.updateUserInfo()
  193. this.createItem()
  194. }
  195. // update (dt) {}
  196. }