BaseZhaocha.ts 9.2 KB

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