BaseZhaocha.ts 9.5 KB

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