BaseZhaocha.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. this.node.on(Node.EventType.MOUSE_UP,(event: EventTouch) => {
  91. if (this.bool_cantouch == false) return
  92. this.bool_cantouch = false
  93. this.scheduleOnce(() => {
  94. this.bool_cantouch = true
  95. }, 0.1)
  96. if (this.bool_needshowX) {
  97. // AudioManager.instance.playBundleAudio("false")
  98. // let worldpos = event.getUILocation()
  99. let scpos = event.getLocation()
  100. let wopos = this.cam.screenToWorld(new Vec3(scpos.x, scpos.y, 0))
  101. // let localpos = this.arr_node_allitem[0].parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(worldpos.x, worldpos.y))
  102. let localPos1 = this.cam.convertToUINode(wopos,this.arr_node_allitem[0].parent)
  103. let localpos = new Vec3(localPos1.x,localPos1.y)
  104. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/x", (node) => {
  105. node.parent = this.arr_node_allitem[0].parent
  106. node.setPosition(localpos)
  107. this.scheduleOnce(() => {
  108. node?.destroy()
  109. }, 0.3)
  110. })
  111. }
  112. }, this)
  113. EventMgr.ins.addEventListener("game_tishi", this.ShowTishi, this)
  114. for (let i = 0; i < this.arr_node_allitem.length; i++) {
  115. this.arr_node_allitem[i].on(Node.EventType.TOUCH_END, () => {
  116. if (this.arr_areadygetnum.indexOf(i) != -1) {
  117. // AudioManager.instance.playBundleAudio("false")
  118. return
  119. }
  120. AudioManager.instance.playBundleAudio("true")
  121. this.arr_areadygetnum.push(i)
  122. this.num_count--
  123. this.arr_node_allitem[i].getComponent(UIOpacity).opacity = 255
  124. this.arr_node_allitem[i].getComponent(Sprite).color = Color.RED
  125. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/item_cha", (item) => {
  126. item.getChildByName("Label").getComponent(Label).string = this.answerName[i]
  127. let parent = this.getNullItem()
  128. item.parent = this.node
  129. let point = (this.arr_areadygetnum.length - 1) * 1.2 / this.answerName.length
  130. this.sv_cha.scrollToPercentHorizontal(point, 0.3, true)
  131. // 获取点击项的世界坐标,并转换为当前节点下的局部坐标
  132. let worldpos = this.arr_node_allitem[i].getWorldPosition()
  133. let localpos = this.node.getComponent(UITransform).convertToNodeSpaceAR(worldpos)
  134. item.setPosition(localpos)
  135. // 获取目标位置的世界坐标,并转换为 ScrollView 视口下的局部坐标
  136. let worldpos2 = parent.getWorldPosition()
  137. let scrollViewContainer = this.sv_cha.node.parent as Node // 假设 sv_cha 的父节点是视口容器
  138. let localpos2 = scrollViewContainer.getComponent(UITransform).convertToNodeSpaceAR(worldpos2)
  139. let gamecfg = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo[GameCfg.CurUnit] : GDM.gamecfgMgr.data[GameCfg.CurUnit]
  140. LayerMgr.instance.ShowSprite2("unit" + gamecfg.unit, "image/item" + i, item.getComponent(Sprite), (res) => {
  141. item.getComponent(Sprite).spriteFrame = res
  142. })
  143. // 开始动画,移动到目标位置
  144. tween(item)
  145. .to(0.3, { position: new Vec3(localpos2.x, localpos2.y, 0) })
  146. .call(() => {
  147. item.parent = parent
  148. item.setPosition(Vec3.ZERO)
  149. })
  150. .start()
  151. // 判断是否完成所有操作
  152. if (this.num_count == 0) {
  153. EventMgr.ins.dispatchEvent("game_win")
  154. }
  155. })
  156. }, this)
  157. }
  158. }
  159. onClickOver()
  160. {
  161. }
  162. getNullItem() {
  163. // for (let index = 0; index < this.sv_cha.content.children.length; index++) {
  164. // if (this.sv_cha.content.children[index].children.length == 0) {
  165. // return this.sv_cha.content.children[index]
  166. // }
  167. // }
  168. return this.sv_cha.content.children[this.arr_areadygetnum.length - 1]
  169. }
  170. ShowTishi() {
  171. console.log(this.num_showTip)
  172. for (let index = 0; index < this.answerName.length; index++) {
  173. if (this.arr_areadygetnum.indexOf(index) == -1 && !this.arr_node_allitem[index].getComponent(Sprite).color.equals(Color.GREEN)) {
  174. if (this.tishi_indexs.length != 0) {
  175. let idx = this.tishi_indexs.pop()
  176. this.ShowTishiView2(this.arr_tishi[idx])
  177. }
  178. else if (this.num_showTip == index) {
  179. // if (this.arr_node_allitem[index].active) {
  180. // this.arr_node_allitem[index].getComponent(Sprite).color = Color.YELLOW//new Color(57, 226, 42, 255)
  181. // console.log("active = true")
  182. // } else {
  183. // this.arr_node_allitem[index].active = true
  184. // this.arr_node_allitem[index].getComponent(Sprite).color = Color.YELLOW//new Color(57, 226, 42, 255)
  185. // this.scheduleOnce(() => {
  186. // // this.arr_node_allitem[index].active = false
  187. // }, 0)
  188. // console.log("active = false")
  189. // }
  190. this.arr_node_allitem[index].getComponent(Sprite).color = new Color(57, 226, 42, 255)
  191. this.arr_node_allitem[index].getComponent(UIOpacity).opacity = 255
  192. this.ShowTishiView()
  193. } else {
  194. this.arr_node_allitem[index].getComponent(UIOpacity).opacity = 255
  195. this.arr_node_allitem[index].getComponent(Sprite).color = Color.GREEN
  196. }
  197. return
  198. }
  199. }
  200. }
  201. num_showTip = -1
  202. protected str_tishi = ""
  203. @property([CCString])
  204. public arr_tishi: Array<string> = []
  205. @property([CCInteger])
  206. public tishi_indexs: Array<number> = []
  207. ShowTishiView() {
  208. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_tishi", (node) => {
  209. node.getComponent(page_tishi).Init(this.str_tishi)
  210. })
  211. }
  212. ShowTishiView2(str: string) {
  213. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_tishi", (node) => {
  214. node.getComponent(page_tishi).Init(str)
  215. })
  216. }
  217. protected onDestroy(): void {
  218. console.log("退出关卡")
  219. this.unscheduleAllCallbacks()
  220. }
  221. }