rankCtr.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { _decorator, Component, instantiate, Label, Node, Prefab, ScrollView, tween, Vec2, Vec3 } from "cc"
  2. const { ccclass, property } = _decorator
  3. @ccclass("rankCtr")
  4. export class rankCtr extends Component {
  5. @property({ type: Node })
  6. public textLiveRank: Node | null = null
  7. @property({ type: Node })
  8. public rankNods: Node[] = []
  9. @property({ type: Node })
  10. public textPointsRank: Node | null = null
  11. @property({ type: Prefab })
  12. gameItem = null
  13. @property({ type: Node })
  14. content = null
  15. @property({ type: ScrollView })
  16. rankScroll = null
  17. private isLiveRank: boolean = true
  18. private moveDistance: number = -150
  19. private lng: string = "cn"
  20. public rankList = []
  21. private nowRankNO = 0
  22. private rankNodes = []
  23. private startAnimate = false
  24. start() {}
  25. // 触发方法
  26. updateRankTop() {
  27. this.isLiveRank = !this.isLiveRank
  28. this.updateRankDisplay()
  29. }
  30. private updateRankDisplay() {
  31. if (!this.textLiveRank || !this.textPointsRank) return
  32. let moveOffset = this.isLiveRank ? -this.moveDistance : this.moveDistance
  33. let moveAction = new Vec3(moveOffset, 0, 0)
  34. tween(this.textLiveRank).by(0.5, { position: moveAction }).start()
  35. tween(this.textPointsRank).by(0.5, { position: moveAction }).start()
  36. }
  37. updateRank(data: any[]) {
  38. if (this.rankNodes.length === 0) {
  39. this.rankList = data
  40. if(data.length>0){
  41. data.forEach((item, index) => {
  42. let node = instantiate(this.gameItem)
  43. let number = node.getChildByName("text_rank_number").getComponent(Label)
  44. number.string = index + 1
  45. let nickName = node.getChildByName("text_player_name").getComponent(Label)
  46. nickName.string = item.userInfo.name
  47. let days = node.getChildByName("text_value").getComponent(Label)
  48. if (this.lng === "cn") {
  49. days.string = item.survival + "天"
  50. } else {
  51. days.string = item.survival + (item.survival == "1" ? "day" : "days")
  52. }
  53. this.content.addChild(node)
  54. this.rankNodes.push(node)
  55. })
  56. }
  57. // 数据变化开启动画
  58. // if (this.startAnimate) {
  59. // setTimeout(() => {
  60. // let info = data[this.nowRankNO]
  61. // data.splice(this.nowRankNO, 1)
  62. // data.splice(3, 0, info)
  63. // console.log(data)
  64. // this.updateRank(data)
  65. // }, 1000)
  66. // }
  67. } else {
  68. this.changeRank(3, data)
  69. }
  70. }
  71. changeRank(newRankNO, data) {
  72. let nowY = this.rankNodes[this.nowRankNO].position.y
  73. let goY = this.rankNodes[newRankNO].position.y
  74. this.rankScroll.scrollToOffset(new Vec2(0, -300 - nowY), 0.1)
  75. // this.rankScroll.scrollToOffset(new Vec2(0, goY), 1)
  76. setTimeout(() => {
  77. this.rankMove(this.rankNodes[this.nowRankNO], goY, data)
  78. }, 1000)
  79. }
  80. rankMove(_node, goY, data) {
  81. let node = instantiate(_node)
  82. tween(_node)
  83. .to(0.3, {
  84. innerHeight: 0
  85. })
  86. .start()
  87. this.rankScroll.scrollToOffset(new Vec2(0, -300 - goY), 1)
  88. this.content.removeChild(_node)
  89. this.content.parent.addChild(node)
  90. node.position.y = node.position.y + this.rankScroll.getScrollOffset().y + 200
  91. // // this.label_length = node.getComponent(UITransform).contentSize.x;
  92. tween(node)
  93. .to(0.3, { scale: new Vec3(1.2, 1.2, 1) })
  94. .to(1, { position: new Vec3(node.position.x, goY + 200) })
  95. .to(0.3, { scale: new Vec3(1, 1, 1) })
  96. .to(0, { scale: new Vec3(0, 0, 1) })
  97. .call(() => {
  98. this.rankNodes = []
  99. this.startAnimate = false
  100. this.content.removeAllChildren()
  101. this.updateRank(data)
  102. })
  103. .start()
  104. }
  105. update(deltaTime: number) {}
  106. }