BaseZhaocha.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { _decorator, CCInteger, CCString, color, Color, Component, EventTouch, find, 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 = 0
  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 < this.answerName.length; 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. this.num_count = this.answerName.length
  40. }
  41. setActiveItemFrame() {
  42. for (let index = 0; index < this.answerName.length; index++) {
  43. this.sv_cha.node.getChildByName("view").getChildByName("content").getChildByName(index.toString()).active = true
  44. }
  45. }
  46. bool_cantouch = true
  47. bool_needshowX = true
  48. protected start(): void {
  49. let data: recordedGameScreenParams = {
  50. duration: 300 // 10 否 录屏的时长,单位 s,必须大于 3s,最大值 300s(5 分钟)。
  51. }
  52. PlatformService.getInstance().platformApi.recordedGameScreenStart(data)
  53. EventMgr.ins.addEventListener("dontshow_x", () => {
  54. this.bool_needshowX = false
  55. this.scheduleOnce(() => {
  56. this.bool_needshowX = true
  57. }, 0.1)
  58. }, this)
  59. this.FindObj()
  60. this.setActiveItemFrame()
  61. this.node.on(Node.EventType.TOUCH_END, (event: EventTouch) => {
  62. if (this.bool_cantouch == false) return
  63. this.bool_cantouch = false
  64. this.scheduleOnce(() => {
  65. this.bool_cantouch = true
  66. }, 0.1)
  67. if (this.bool_needshowX) {
  68. // AudioManager.instance.playBundleAudio("false")
  69. let worldpos = event.getUILocation()
  70. let localpos = this.arr_node_allitem[0].parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(worldpos.x, worldpos.y))
  71. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/x", (node) => {
  72. node.parent = this.arr_node_allitem[0].parent
  73. node.setPosition(localpos)
  74. this.scheduleOnce(() => {
  75. node?.destroy()
  76. }, 0.3)
  77. })
  78. }
  79. }, this)
  80. EventMgr.ins.addEventListener("game_tishi", this.ShowTishi, this)
  81. for (let i = 0; i < this.arr_node_allitem.length; i++) {
  82. this.arr_node_allitem[i].on(Node.EventType.TOUCH_END, () => {
  83. if (this.arr_areadygetnum.indexOf(i) != -1) {
  84. // AudioManager.instance.playBundleAudio("false")
  85. return
  86. }
  87. AudioManager.instance.playBundleAudio("true")
  88. this.arr_areadygetnum.push(i)
  89. this.num_count--
  90. this.arr_node_allitem[i].getComponent(UIOpacity).opacity = 255
  91. this.arr_node_allitem[i].getComponent(Sprite).color = Color.RED
  92. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/item_cha", (item) => {
  93. item.getChildByName("Label").getComponent(Label).string = this.answerName[i]
  94. let parent = this.getNullItem()
  95. item.parent = this.node
  96. let point = (this.arr_areadygetnum.length - 1) * 1.2 / this.answerName.length
  97. this.sv_cha.scrollToPercentHorizontal(point, 0.3, true)
  98. // 获取点击项的世界坐标,并转换为当前节点下的局部坐标
  99. let worldpos = this.arr_node_allitem[i].getWorldPosition()
  100. let localpos = this.node.getComponent(UITransform).convertToNodeSpaceAR(worldpos)
  101. item.setPosition(localpos)
  102. // 获取目标位置的世界坐标,并转换为 ScrollView 视口下的局部坐标
  103. let worldpos2 = parent.getWorldPosition()
  104. let scrollViewContainer = this.sv_cha.node.parent as Node // 假设 sv_cha 的父节点是视口容器
  105. let localpos2 = scrollViewContainer.getComponent(UITransform).convertToNodeSpaceAR(worldpos2)
  106. let gamecfg = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo[GameCfg.CurUnit] : GDM.gamecfgMgr.data[GameCfg.CurUnit]
  107. LayerMgr.instance.ShowSprite2("unit" + gamecfg.unit, "image/item" + i, item.getComponent(Sprite), (res) => {
  108. item.getComponent(Sprite).spriteFrame = res
  109. })
  110. // 开始动画,移动到目标位置
  111. tween(item)
  112. .to(0.3, { position: new Vec3(localpos2.x, localpos2.y, 0) })
  113. .call(() => {
  114. item.parent = parent
  115. item.setPosition(Vec3.ZERO)
  116. })
  117. .start()
  118. // 判断是否完成所有操作
  119. if (this.num_count == 0) {
  120. EventMgr.ins.dispatchEvent("game_win")
  121. }
  122. })
  123. }, this)
  124. }
  125. }
  126. getNullItem() {
  127. // for (let index = 0; index < this.sv_cha.content.children.length; index++) {
  128. // if (this.sv_cha.content.children[index].children.length == 0) {
  129. // return this.sv_cha.content.children[index]
  130. // }
  131. // }
  132. return this.sv_cha.content.children[this.arr_areadygetnum.length - 1]
  133. }
  134. ShowTishi() {
  135. console.log(this.num_showTip)
  136. for (let index = 0; index < this.answerName.length; index++) {
  137. if (this.arr_areadygetnum.indexOf(index) == -1 && !this.arr_node_allitem[index].getComponent(Sprite).color.equals(Color.GREEN)) {
  138. if (this.tishi_indexs.length != 0) {
  139. let idx = this.tishi_indexs.pop()
  140. this.ShowTishiView2(this.arr_tishi[idx])
  141. }
  142. else if (this.num_showTip == index) {
  143. // if (this.arr_node_allitem[index].active) {
  144. // this.arr_node_allitem[index].getComponent(Sprite).color = Color.YELLOW//new Color(57, 226, 42, 255)
  145. // console.log("active = true")
  146. // } else {
  147. // this.arr_node_allitem[index].active = true
  148. // this.arr_node_allitem[index].getComponent(Sprite).color = Color.YELLOW//new Color(57, 226, 42, 255)
  149. // this.scheduleOnce(() => {
  150. // // this.arr_node_allitem[index].active = false
  151. // }, 0)
  152. // console.log("active = false")
  153. // }
  154. this.arr_node_allitem[index].getComponent(Sprite).color = new Color(57, 226, 42, 255)
  155. this.arr_node_allitem[index].getComponent(UIOpacity).opacity = 255
  156. this.ShowTishiView()
  157. } else {
  158. this.arr_node_allitem[index].getComponent(UIOpacity).opacity = 255
  159. this.arr_node_allitem[index].getComponent(Sprite).color = Color.GREEN
  160. }
  161. return
  162. }
  163. }
  164. }
  165. num_showTip = -1
  166. protected str_tishi = ""
  167. @property([CCString])
  168. public arr_tishi: Array<string> = []
  169. @property([CCInteger])
  170. public tishi_indexs: Array<number> = []
  171. ShowTishiView() {
  172. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_tishi", (node) => {
  173. node.getComponent(page_tishi).Init(this.str_tishi)
  174. })
  175. }
  176. ShowTishiView2(str: string) {
  177. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_tishi", (node) => {
  178. node.getComponent(page_tishi).Init(str)
  179. })
  180. }
  181. protected onDestroy(): void {
  182. console.log("退出关卡")
  183. this.unscheduleAllCallbacks()
  184. }
  185. }