OptionsCtr.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. processCtr.doneProcessing()
  48. }
  49. index++
  50. })
  51. .start()
  52. }
  53. }
  54. setOptions(options: string[]) {
  55. if (options.length !== 4) {
  56. console.warn("Expected 4 options!")
  57. return
  58. }
  59. //显示前先制空lebel
  60. for (let i = 0; i < 4; i++) {
  61. const nodeLabel = this.optionNodes[i].getChildByName("text_option").getComponent(Label)
  62. nodeLabel.string = ""
  63. this.optionNodes[i].getComponent(UIOpacity).opacity = 255
  64. }
  65. tween(this.node.getComponent(UIOpacity))
  66. .to(1, { opacity: 255 })
  67. .call(() => {
  68. for (let i = 0; i < 4; i++) {
  69. this.setOptionText(this.optionNodes[i], options[i])
  70. }
  71. })
  72. .start()
  73. }
  74. setOptionText(node: Node, text: string) {
  75. const labelComponent = node.getChildByName("text_option").getComponent(Label)
  76. const uiOpacity = labelComponent.node.getComponent(UIOpacity) || labelComponent.node.addComponent(UIOpacity)
  77. uiOpacity.opacity = 0 // 初始设置为透明
  78. labelComponent.string = text
  79. // 使用tween实现淡出效果
  80. tween(uiOpacity)
  81. .to(1, { opacity: 255 }) // 1秒内淡入到完全不透明
  82. .start()
  83. }
  84. selectOption(index: number) {
  85. if (index < 1 || index > 4) {
  86. console.warn("Invalid option index!")
  87. return
  88. }
  89. // 重置所有选项的背景和颜色
  90. for (let i = 0; i < 4; i++) {
  91. const optionNode = this.optionNodes[i]
  92. const labelComponent = optionNode.getChildByName("text_option").getComponent(Label)
  93. if (i === index - 1) {
  94. // 这是被选中的选项
  95. labelComponent.color = this.selectedColor
  96. // 设置选中背景图
  97. const spriteComponent = optionNode.getComponent(Sprite)
  98. if (spriteComponent) {
  99. spriteComponent.spriteFrame = this.selectedSprite.spriteFrame
  100. }
  101. this.selectedAction(optionNode)
  102. } else {
  103. // 这是未被选中的选项
  104. labelComponent.color = Color.WHITE // 假设默认颜色为白色,你可以根据需要更改
  105. // 移除选中背景图
  106. const spriteComponent = optionNode.getComponent(Sprite)
  107. if (spriteComponent) {
  108. spriteComponent.spriteFrame = this.notSelectedSprite.spriteFrame
  109. }
  110. }
  111. }
  112. }
  113. selectedAction(optionNode: Node) {
  114. tween(optionNode)
  115. .to(0.1, { scale: new Vec3(1.1, 1.1, 1.1) }) // 放大到原始大小的110%
  116. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位
  117. .by(0.1, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  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(5, 0, 0) }) // 向右移动5个单位回到原位置
  121. .to(0.1, { scale: new Vec3(1, 1, 1) }) // 缩小回原始大小
  122. .start()
  123. }
  124. private scaleUp(node: Node) {
  125. return tween(node).to(0.1, { scale: new Vec3(1.1, 1.1, 1.1) }) // 放大到原始大小的110%
  126. }
  127. private scaleDown(node: Node) {
  128. return tween(node).to(0.1, { scale: new Vec3(1, 1, 1) }) // 缩小回原始大小
  129. }
  130. private shake(node: Node) {
  131. return tween(node)
  132. .by(0.05, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位
  133. .by(0.05, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  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(5, 0, 0) }) // 向右移动5个单位回到原位置
  137. }
  138. update(deltaTime: number) {}
  139. }