BaseZhaocha.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { _decorator, color, Color, Component, EventTouch, instantiate, Label, log, Node, Prefab, ScrollView, Sprite, tween, UIOpacity, UITransform, Vec2, Vec3 } from 'cc';
  2. import EventMgr from '../../script/Manager/EventMgr';
  3. import { LayerMgr } from '../../script/Manager/LayerMgr';
  4. import { GameCfg } from '../../script/Config/GameCfg';
  5. import { AudioManager } from '../../script/Manager/AudioMgr';
  6. import { BundleName } from '../../script/Config/EnumCfg';
  7. import { GDM } from './dtta/JsonMgr';
  8. import PlatformService from '../../script/Platform/PlatformService';
  9. import { recordedGameScreenParams } from '../../script/Platform/MiniGameApiBase';
  10. import { page_tishi } from './page/page_tishi';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('BaseZhaocha')
  13. export class BaseZhaocha extends Component {
  14. arr_node_allitem: Array<Node> = []
  15. // @property(Prefab)
  16. // item_cha: Prefab = null
  17. sv_cha: ScrollView = null
  18. num_count: number = 12
  19. arr_areadygetnum: Array<number> = []
  20. answerName: Array<string> = [
  21. // "安全带",
  22. // "可乐",
  23. // "棒球帽",
  24. // "蓝牙耳机",
  25. // "LED",
  26. // "扶手",
  27. // "摄像头",
  28. // "电子屏幕",
  29. // "平板",
  30. // "悬浮座椅",
  31. // "收款机",
  32. // "万斯板鞋",
  33. ]
  34. FindObj() {
  35. this.sv_cha = this.node.getChildByName("ScrollView").getComponent(ScrollView)
  36. for (let index = 0; index < 12; index++) {
  37. this.arr_node_allitem.push(this.node.getChildByName("mask").getChildByName("bg").getChildByName("item_" + index))// this.arr_node_allitem = this.node.getChildByName("mask").getChildByName("bg").children
  38. }
  39. }
  40. bool_cantouch = true
  41. bool_needshowX = true
  42. protected start(): void {
  43. let data: recordedGameScreenParams = {
  44. duration: 300 // 10 否 录屏的时长,单位 s,必须大于 3s,最大值 300s(5 分钟)。
  45. }
  46. PlatformService.getInstance().platformApi.recordedGameScreenStart(data)
  47. EventMgr.ins.addEventListener("dontshow_x", () => {
  48. this.bool_needshowX = false
  49. this.scheduleOnce(() => {
  50. this.bool_needshowX = true
  51. }, 0.1)
  52. }, this)
  53. this.FindObj()
  54. this.node.on(Node.EventType.TOUCH_END, (event: EventTouch) => {
  55. if (this.bool_cantouch == false) return
  56. this.bool_cantouch = false
  57. this.scheduleOnce(() => {
  58. this.bool_cantouch = true
  59. }, 0.1)
  60. if (this.bool_needshowX) {
  61. // AudioManager.instance.playBundleAudio("false")
  62. let worldpos = event.getUILocation()
  63. let localpos = this.arr_node_allitem[0].parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(worldpos.x, worldpos.y))
  64. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/x", (node) => {
  65. node.parent = this.arr_node_allitem[0].parent
  66. node.setPosition(localpos)
  67. this.scheduleOnce(() => {
  68. node?.destroy()
  69. }, 0.3)
  70. })
  71. }
  72. }, this)
  73. EventMgr.ins.addEventListener("game_tishi", this.ShowTishi, this)
  74. for (let i = 0; i < this.arr_node_allitem.length; i++) {
  75. this.arr_node_allitem[i].on(Node.EventType.TOUCH_END, () => {
  76. if (this.arr_areadygetnum.indexOf(i) != -1) {
  77. // AudioManager.instance.playBundleAudio("false")
  78. return
  79. }
  80. AudioManager.instance.playBundleAudio("true")
  81. this.arr_areadygetnum.push(i)
  82. this.num_count--
  83. this.arr_node_allitem[i].getComponent(UIOpacity).opacity = 255
  84. this.arr_node_allitem[i].getComponent(Sprite).color = Color.RED
  85. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/item_cha", (item) => {
  86. item.getChildByName("Label").getComponent(Label).string = this.answerName[i]
  87. let parent = this.getNullItem()
  88. item.parent = this.node
  89. let point = (this.arr_areadygetnum.length - 1) * 1.2 / 12
  90. this.sv_cha.scrollToPercentHorizontal(point, 0.3, true)
  91. // 获取点击项的世界坐标,并转换为当前节点下的局部坐标
  92. let worldpos = this.arr_node_allitem[i].getWorldPosition()
  93. let localpos = this.node.getComponent(UITransform).convertToNodeSpaceAR(worldpos)
  94. item.setPosition(localpos)
  95. // 获取目标位置的世界坐标,并转换为 ScrollView 视口下的局部坐标
  96. let worldpos2 = parent.getWorldPosition()
  97. let scrollViewContainer = this.sv_cha.node.parent as Node // 假设 sv_cha 的父节点是视口容器
  98. let localpos2 = scrollViewContainer.getComponent(UITransform).convertToNodeSpaceAR(worldpos2)
  99. let gamecfg = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo[GameCfg.CurUnit] : GDM.gamecfgMgr.data[GameCfg.CurUnit]
  100. LayerMgr.instance.ShowSprite2("unit" + gamecfg.unit, "image/item" + i, item.getComponent(Sprite), (res) => {
  101. item.getComponent(Sprite).spriteFrame = res
  102. })
  103. // 开始动画,移动到目标位置
  104. tween(item)
  105. .to(0.3, { position: new Vec3(localpos2.x, localpos2.y, 0) })
  106. .call(() => {
  107. item.parent = parent
  108. item.setPosition(Vec3.ZERO)
  109. })
  110. .start()
  111. // 判断是否完成所有操作
  112. if (this.num_count == 0) {
  113. EventMgr.ins.dispatchEvent("game_win")
  114. }
  115. })
  116. }, this)
  117. }
  118. }
  119. getNullItem() {
  120. // for (let index = 0; index < this.sv_cha.content.children.length; index++) {
  121. // if (this.sv_cha.content.children[index].children.length == 0) {
  122. // return this.sv_cha.content.children[index]
  123. // }
  124. // }
  125. return this.sv_cha.content.children[this.arr_areadygetnum.length - 1]
  126. }
  127. ShowTishi() {
  128. for (let index = 0; index < 12; index++) {
  129. if (this.arr_areadygetnum.indexOf(index) == -1 && !this.arr_node_allitem[index].getComponent(Sprite).color.equals(Color.GREEN)) {
  130. if (this.num_showTip == index) {
  131. // if (this.arr_node_allitem[index].active) {
  132. // this.arr_node_allitem[index].getComponent(Sprite).color = Color.YELLOW//new Color(57, 226, 42, 255)
  133. // console.log("active = true")
  134. // } else {
  135. // this.arr_node_allitem[index].active = true
  136. // this.arr_node_allitem[index].getComponent(Sprite).color = Color.YELLOW//new Color(57, 226, 42, 255)
  137. // this.scheduleOnce(() => {
  138. // // this.arr_node_allitem[index].active = false
  139. // }, 0)
  140. // console.log("active = false")
  141. // }
  142. this.arr_node_allitem[index].getComponent(Sprite).color = new Color(57, 226, 42, 255)
  143. this.arr_node_allitem[index].getComponent(UIOpacity).opacity = 255
  144. this.ShowTishiView()
  145. } else {
  146. this.arr_node_allitem[index].getComponent(UIOpacity).opacity = 255
  147. this.arr_node_allitem[index].getComponent(Sprite).color = Color.GREEN
  148. }
  149. return
  150. }
  151. }
  152. }
  153. num_showTip = -1
  154. str_tishi = ""
  155. ShowTishiView() {
  156. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_tishi", (node) => {
  157. node.getComponent(page_tishi).Init(this.str_tishi)
  158. })
  159. }
  160. protected onDestroy(): void {
  161. console.log("退出关卡")
  162. this.unscheduleAllCallbacks()
  163. }
  164. }