OptionsCtr.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { _decorator, color, Color, Component, Label, Node, Sprite, tween, UIOpacity, Vec3 } from "cc"
  2. import { processCtr } from "./socket/processCtr"
  3. const { ccclass, property } = _decorator
  4. @ccclass("OptionsCtr")
  5. export class OptionsCtr extends Component {
  6. // 直接引用四个子节点
  7. @property({ type: Node })
  8. public optionNodes: Node[] = []
  9. selectedColor: Color = new Color(180, 173, 121) // B4AD79
  10. @property({ type: Sprite })
  11. selectedSprite: Sprite = null // 设置你想要的选中背景图
  12. @property({ type: Sprite })
  13. notSelectedSprite: Sprite = null // 设置你想要的选中背景图
  14. private votesOrgPos_x:number = -894
  15. private votesWidth = 890
  16. start() {
  17. }
  18. resetVotes()
  19. {
  20. for(let i=0;i<4;i++)
  21. {
  22. const vote: Node = this.optionNodes[i].getChildByName("votes").getChildByName("toupiaojindu")
  23. vote.active = false
  24. vote.getChildByName('votePannel_particle').active = false
  25. vote.setPosition(this.votesOrgPos_x,0,0)
  26. }
  27. }
  28. showVotes(num: number[] , processCtr: processCtr) {
  29. let index = 0
  30. //获取投票范围,修改合适步长
  31. let scope:number = 10
  32. const maxValue = Math.max(...num)
  33. if(maxValue > 10 && maxValue < 100)
  34. {
  35. scope = 100
  36. }
  37. else if(maxValue > 99)
  38. {
  39. scope = 1000
  40. }
  41. for(let i=0;i<4;i++)
  42. {
  43. const vote: Node = this.optionNodes[i].getChildByName("votes").getChildByName("toupiaojindu")
  44. if(!vote.active || !vote.getChildByName('votePannel_particle').active )
  45. {
  46. vote.active = true
  47. vote.getChildByName('votePannel_particle').active = true
  48. }
  49. tween(vote).to(1, {
  50. position: new Vec3(this.votesOrgPos_x + (this.votesWidth / scope) * num[i], 0, 0)
  51. })
  52. .call(()=>{
  53. if(index ===3)
  54. {
  55. processCtr.doneProcessing()
  56. }
  57. index++
  58. })
  59. .start()
  60. }
  61. }
  62. setOptions(options: string[]) {
  63. if (options.length !== 4) {
  64. console.warn("Expected 4 options!")
  65. return
  66. }
  67. //显示前先制空lebel
  68. for (let i = 0; i < 4; i++) {
  69. const nodeLabel = this.optionNodes[i].getChildByName("text_option").getComponent(Label)
  70. nodeLabel.string = ""
  71. }
  72. tween(this.node.getComponent(UIOpacity))
  73. .to(1, { opacity: 255 })
  74. .call(() => {
  75. for (let i = 0; i < 4; i++) {
  76. this.setOptionText(this.optionNodes[i], options[i])
  77. }
  78. })
  79. .start()
  80. }
  81. setOptionText(node: Node, text: string) {
  82. const labelComponent = node.getChildByName("text_option").getComponent(Label)
  83. const uiOpacity = labelComponent.node.getComponent(UIOpacity) || labelComponent.node.addComponent(UIOpacity)
  84. uiOpacity.opacity = 0 // 初始设置为透明
  85. labelComponent.string = text
  86. // 使用tween实现淡出效果
  87. tween(uiOpacity)
  88. .to(1, { opacity: 255 }) // 1秒内淡入到完全不透明
  89. .start()
  90. }
  91. selectOption(index: number) {
  92. if (index < 1 || index > 4) {
  93. console.warn("Invalid option index!")
  94. return
  95. }
  96. // 重置所有选项的背景和颜色
  97. for (let i = 0; i < 4; i++) {
  98. const optionNode = this.optionNodes[i]
  99. const labelComponent = optionNode.getChildByName("text_option").getComponent(Label)
  100. if (i === index - 1) {
  101. // 这是被选中的选项
  102. labelComponent.color = this.selectedColor
  103. // 设置选中背景图
  104. const spriteComponent = optionNode.getComponent(Sprite)
  105. if (spriteComponent) {
  106. spriteComponent.spriteFrame = this.selectedSprite.spriteFrame
  107. }
  108. this.selectedAction(optionNode)
  109. } else {
  110. // 这是未被选中的选项
  111. labelComponent.color = Color.WHITE // 假设默认颜色为白色,你可以根据需要更改
  112. // 移除选中背景图
  113. const spriteComponent = optionNode.getComponent(Sprite)
  114. if (spriteComponent) {
  115. spriteComponent.spriteFrame = this.notSelectedSprite.spriteFrame
  116. }
  117. }
  118. }
  119. }
  120. selectedAction(optionNode: Node) {
  121. tween(optionNode)
  122. .to(0.1, { scale: new Vec3(1.1, 1.1, 1.1) }) // 放大到原始大小的110%
  123. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位
  124. .by(0.1, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  125. .by(0.1, { position: new Vec3(10, 0, 0) }) // 向右移动10个单位
  126. .by(0.1, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  127. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位回到原位置
  128. .to(0.1, { scale: new Vec3(1, 1, 1) }) // 缩小回原始大小
  129. .start()
  130. }
  131. private scaleUp(node: Node) {
  132. return tween(node).to(0.1, { scale: new Vec3(1.1, 1.1, 1.1) }) // 放大到原始大小的110%
  133. }
  134. private scaleDown(node: Node) {
  135. return tween(node).to(0.1, { scale: new Vec3(1, 1, 1) }) // 缩小回原始大小
  136. }
  137. private shake(node: Node) {
  138. return tween(node)
  139. .by(0.05, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位
  140. .by(0.05, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  141. .by(0.05, { position: new Vec3(10, 0, 0) }) // 向右移动10个单位
  142. .by(0.05, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  143. .by(0.05, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位回到原位置
  144. }
  145. update(deltaTime: number) {}
  146. }