OptionsCtr.ts 6.4 KB

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