OptionsCtr.ts 6.1 KB

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