game.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import { _decorator, BoxCollider2D, Collider2D, Color, Component, Contact2DType, director, Director, EventTouch, instantiate, IPhysics2DContact, Label, log, Node, Prefab, Rect, Sprite, SpriteFrame, Tween, UITransform, Vec2, Vec3 } from 'cc';
  2. import { tools } from '../tools';
  3. import { DirType, model_item_data } from '../data';
  4. import { run_level_item } from './run_level_item';
  5. import { car } from './car';
  6. import Coroutine from '../lib/Coroutine';
  7. import { results } from './results';
  8. import { buff_show } from './buff_show';
  9. import { pool } from './pool';
  10. import { box } from './box';
  11. import { Util } from '../util';
  12. import { wall } from './wall';
  13. import { Joystick } from './joystick';
  14. import { touch } from './touch';
  15. import { config } from '../config';
  16. import { coin } from './coin';
  17. import { audioManager } from '../manager/audioManager';
  18. import { count_time_start } from './count_time_start';
  19. import { weiqi_manager } from './effect/weiqi_manager';
  20. import { cheyin_manager } from './cheyin/cheyin_manager';
  21. import { boom_manager } from './boom/boom_manager';
  22. import { qiyou_manager } from './qiyou_manager';
  23. import { SdkUtil } from '../sdkUtil';
  24. const { ccclass, property } = _decorator;
  25. @ccclass('game')
  26. export class game extends Component {
  27. @property(SpriteFrame) sfList:SpriteFrame[] = [];
  28. @property(SpriteFrame) sfBuffList:SpriteFrame[] = [];
  29. @property(BoxCollider2D) collider:BoxCollider2D = null;
  30. @property(Node) car:Node = null;
  31. @property(Node) content:Node = null;
  32. @property(Prefab) prefab_item:Prefab = null;
  33. @property(Node) results_view:Node = null;
  34. @property(Node) lab_scores:Node = null;
  35. @property(buff_show) bf_show:buff_show = null
  36. @property(pool) game_pool_manager:pool = null
  37. @property(Node) touch_node:Node = null
  38. @property(Node) count_time:Node = null
  39. @property(weiqi_manager) mWeiqiManager:weiqi_manager = null
  40. @property(cheyin_manager) mCheyinManager:cheyin_manager = null
  41. @property(boom_manager) mBoomManager:boom_manager = null
  42. @property(qiyou_manager) mQiYouManager:qiyou_manager = null
  43. private mCallBackHome = null
  44. private mIndex:number = 0;
  45. private mHeight:number = 0;
  46. private move_pos:Vec3 = Vec3.ZERO;
  47. public offsetX:number = 0;
  48. private gameBoxList:Node[] = []
  49. private gameWallList:Node[] = []
  50. private gameCoinList:Node[] = []
  51. private isFristOverGame:boolean = true
  52. public show(cb,isReLife:boolean = false){
  53. this.mCallBackHome = cb
  54. this.removeAll()
  55. audioManager.Instance().playMusic(config.AUDIO.game_bgm)
  56. this.count_time.active = true
  57. this.count_time.getComponent(count_time_start).startGame(3,()=>{
  58. this.car.getComponent(car).startGame()
  59. })
  60. this.gameBoxList = []
  61. this.gameWallList = []
  62. this.gameCoinList = []
  63. this.mHeight = 0
  64. this.game_pool_manager.init()
  65. this.bf_show.initView()
  66. this.content.position = Vec3.ZERO
  67. this.car.position = new Vec3(0,-300,this.car.position.z)
  68. this.car.getComponent(car).init(this.content,this.onOver.bind(this),this.updateScores.bind(this),this,isReLife)
  69. for (let index = 0; index < 3; index++) {
  70. this.addItem(index)
  71. }
  72. this.mBoomManager.init(this.car.getComponent(car))
  73. this.mWeiqiManager.init(this.car.getComponent(car))
  74. this.mQiYouManager.init(this.car.getComponent(car))
  75. this.mWeiqiManager.startEffect()
  76. this.mCheyinManager.init(this.car.getComponent(car))
  77. this.touch_node.getComponent(touch).init(this.car.getComponent(car))
  78. this.mIndex = 2;
  79. this.schedule(()=>{
  80. if(this.node.active){
  81. if(this.content.children.length<=5){
  82. this.mIndex++;
  83. if(this.mIndex>=tools.levels.length){
  84. this.mIndex = 1;
  85. }
  86. // Coroutine.instance.start(this, this.addItem, 0, 60, this.mIndex);
  87. this.addItem(this.mIndex)
  88. }
  89. }
  90. },0.25)
  91. this.collider.off(Contact2DType.END_CONTACT)
  92. this.collider.on(Contact2DType.END_CONTACT,(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null)=>{
  93. if(otherCollider.node.name.endsWith("run_level_item")){
  94. this.deleteItem(otherCollider.node)
  95. }
  96. },this)
  97. this.onTTStartRecordVideo()
  98. }
  99. public showBoom(box_p:Vec3){
  100. this.mBoomManager.showBoom(box_p)
  101. }
  102. public showQiYouEffect(qiyou_p:Vec3){
  103. this.mQiYouManager.showQiYou(qiyou_p)
  104. }
  105. public getPoolManager(){
  106. return this.game_pool_manager
  107. }
  108. public getBuffShowView(){
  109. return this.bf_show
  110. }
  111. getOldDir(){
  112. return this.move_pos
  113. }
  114. updateScores(scores:number){
  115. this.lab_scores.getComponent(Label).string = scores.toString()
  116. }
  117. onOver(){
  118. this.unscheduleAllCallbacks()
  119. this.count_time.active = false
  120. this.car.getComponent(car).hideEffect()
  121. director.once(Director.EVENT_AFTER_DRAW,()=>{
  122. this.removeAll()
  123. this.results_view.getComponent(results).show(this.car.getComponent(car).getScores(),(()=>{
  124. this.onTTStopRecordVideo()
  125. }), (isReLife:boolean=false)=>{
  126. this.show(this.mCallBackHome,isReLife)
  127. },()=>{
  128. this.removeSelf()
  129. this.mCallBackHome()
  130. },!this.car.getComponent(car).getIsReLife())
  131. })
  132. }
  133. private onTTStartRecordVideo() {
  134. SdkUtil.ttStartScreenRecording()
  135. }
  136. private onTTStopRecordVideo() {
  137. setTimeout(()=>{
  138. SdkUtil.ttStopScreenRecording()
  139. },1500)
  140. }
  141. removeSelf(){
  142. this.node.removeFromParent()
  143. }
  144. removeAll(){
  145. let temp_list = []
  146. for (let index = 0; index < this.content.children.length; index++) {
  147. const element = this.content.children[index];
  148. if(element.name!="car"&&element.name!="weiqi"){
  149. temp_list.push(element)
  150. }
  151. }
  152. for (let index = 0; index < temp_list.length; index++) {
  153. const element = temp_list[index];
  154. element.removeFromParent()
  155. }
  156. }
  157. deleteItem(node:Node){
  158. director.once(Director.EVENT_AFTER_DRAW,()=>{
  159. let index = this.content.children.indexOf(node)
  160. if(index!=-1){
  161. this.content.children[index].getComponent(run_level_item).removeAll()
  162. }
  163. })
  164. }
  165. addItem( index:number){
  166. // yield Coroutine.createWaitForSecond(waitSecond);
  167. let l:string[] = tools.levels[index].split(",")
  168. let randomID = this.getRandomInt(0,l.length-1)
  169. let item = instantiate(this.prefab_item)
  170. item.parent = this.content
  171. let id = parseInt(l[randomID])
  172. // console.log("randomID",id,tools.levels[index],index)
  173. let m_d:model_item_data = this.getModelDataById(id)
  174. if(m_d==null){
  175. console.error("m_d==null",id)
  176. return
  177. }
  178. item.position = new Vec3(0,this.mHeight)
  179. this.mHeight+=m_d.height
  180. this.car.setSiblingIndex(this.content.children.length)
  181. item.getComponent(run_level_item).initView(m_d,this.sfList,this.sfBuffList,this.car.getComponent(car))
  182. }
  183. getRandomInt (min: number, max: number) {
  184. let r: number= Math.random();
  185. let rr: number= r * (max - min + 1) + min;
  186. return Math.floor(rr);
  187. }
  188. getModelDataById(id:number){
  189. for (let index = 0; index < tools.tpl_list.length; index++) {
  190. const element = tools.tpl_list[index];
  191. if(element.id===id){
  192. return element
  193. }
  194. }
  195. return null
  196. }
  197. addCoinForList(node:Node){
  198. this.gameCoinList.push(node)
  199. }
  200. removeCoinForList(node:Node){
  201. let index = this.gameCoinList.indexOf(node)
  202. if(index!=-1){
  203. this.gameCoinList.splice(index,1)
  204. }
  205. }
  206. addBoxForList(node:Node){
  207. this.gameBoxList.push(node)
  208. }
  209. removeBoxForList(node:Node){
  210. let index = this.gameBoxList.indexOf(node)
  211. if(index!=-1){
  212. this.gameBoxList.splice(index,1)
  213. }
  214. }
  215. addWallForList(node:Node){
  216. this.gameWallList.push(node)
  217. }
  218. removeWallForList(node:Node){
  219. let index = this.gameWallList.indexOf(node)
  220. if(index!=-1){
  221. this.gameWallList.splice(index,1)
  222. }
  223. }
  224. move(){
  225. // let info = this.joystick.getJoyInfo()
  226. // if(info!=null){
  227. // this.car.getComponent(car).setDir(info.dir)
  228. // this.car.getComponent(car).setDis(info.dis)
  229. // }
  230. }
  231. carCollider(car_rect:Rect){
  232. let list = []
  233. let origin_car_pos = this.car.position
  234. let origin_car_pos_y = origin_car_pos.y+car_rect.height*0.5
  235. let isColliderBox = false
  236. for (let index = 0; index < this.gameBoxList.length; index++) {
  237. const box_node = this.gameBoxList[index];
  238. let isCanCollider = box_node.getComponent(box).getIsCanCollider()
  239. if(box_node.parent&&isCanCollider){
  240. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  241. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  242. let width = box_node.getComponent(box).getData().w
  243. let height = box_node.getComponent(box).getData().h
  244. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  245. let dir = -1
  246. if(car_rect.intersects(rect)){
  247. isColliderBox = true
  248. // let car_top_rect = new Rect(car_rect.xMin,car_rect.yMax-10,car_rect.width,20)
  249. let box_bottom_rect = new Rect(rect.xMin,rect.yMin,rect.width,20)
  250. if(origin_car_pos_y<=box_bottom_rect.yMin){
  251. // if((car_top_rect.yMin-origin_car_pos_y)>20){
  252. // }
  253. dir = config.collider_dir.top
  254. }
  255. // if(car_top_rect.intersects(box_bottom_rect)){
  256. // dir = config.collider_dir.top
  257. // }
  258. list.push({"node":box_node,"rect":rect,"type":0,"collider_dir":dir,"moveOffset":0})
  259. }
  260. }
  261. }
  262. let car_top_rect = new Rect(car_rect.xMin,car_rect.yMax-5,car_rect.width,10)
  263. for (let index = 0; index < this.gameWallList.length; index++) {
  264. const box_node = this.gameWallList[index];
  265. let isCanCollider = box_node.getComponent(wall).getIsCanCollider()
  266. if(box_node.parent&&isCanCollider){
  267. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  268. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  269. let width = box_node.getComponent(wall).getData().w
  270. let height = box_node.getComponent(wall).getData().h
  271. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  272. let dir = -1
  273. let offset = 0
  274. if(car_rect.intersects(rect)){
  275. let box_bottom_rect = new Rect(rect.xMin,rect.yMin,rect.width,10)
  276. // if(car_top_rect.intersects(box_bottom_rect)){
  277. // dir = config.collider_dir.top
  278. // if(box_bottom_rect.xMax>(car_top_rect.xMin+car_top_rect.width*0.5)){ //右边
  279. // offset = (car_top_rect.width*0.5)
  280. // }else{
  281. // offset = (car_top_rect.width*-0.5)
  282. // }
  283. // }
  284. if(origin_car_pos_y<=box_bottom_rect.yMin){
  285. dir = config.collider_dir.top
  286. if(box_bottom_rect.xMax>(car_top_rect.xMin+car_top_rect.width*0.5)){ //右边
  287. offset = (car_top_rect.width*0.5)
  288. }else{
  289. offset = (car_top_rect.width*-0.5)
  290. }
  291. }
  292. list.push({"node":box_node,"rect":rect,"type":1,"collider_dir":dir,"moveOffset":offset})
  293. }
  294. }
  295. }
  296. for (let index = 0; index < this.gameCoinList.length; index++) {
  297. const box_node = this.gameCoinList[index];
  298. let isCanCollider = box_node.getComponent(coin).getIsCanCollider()
  299. if(box_node.parent&&isCanCollider){
  300. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  301. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  302. let width = box_node.getComponent(coin).getData().w
  303. let height = box_node.getComponent(coin).getData().h
  304. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  305. let dir = -1
  306. let offset = 0
  307. if(car_rect.intersects(rect)){
  308. list.push({"node":box_node,"rect":rect,"type":2,"collider_dir":dir,"moveOffset":offset})
  309. }
  310. }
  311. }
  312. return list
  313. }
  314. getLeftObstacles(car_rect:Rect){
  315. let list:number[] = []
  316. for (let index = 0; index < this.gameBoxList.length; index++) {
  317. const box_node = this.gameBoxList[index];
  318. let isCanCollider = box_node.getComponent(box).getIsCanCollider()
  319. if(box_node.parent&&isCanCollider){
  320. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  321. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  322. let width = box_node.getComponent(box).getData().w
  323. let height = box_node.getComponent(box).getData().h
  324. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  325. if(car_rect.xMin>=rect.xMax){
  326. if(new Rect(rect.x,car_rect.y,car_rect.width,car_rect.height).intersects(rect)){
  327. list.push(rect.xMax+car_rect.width*0.5)
  328. // box_node.active = false
  329. }
  330. }
  331. }
  332. }
  333. for (let index = 0; index < this.gameWallList.length; index++) {
  334. const box_node = this.gameWallList[index];
  335. let isCanCollider = box_node.getComponent(wall).getIsCanCollider()
  336. if(box_node.parent&&isCanCollider){
  337. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  338. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  339. let width = box_node.getComponent(wall).getData().w
  340. let height = box_node.getComponent(wall).getData().h
  341. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  342. if(car_rect.xMin>=rect.xMax){
  343. if(new Rect(rect.x,car_rect.y,car_rect.width,car_rect.height).intersects(rect)){
  344. list.push(rect.xMax+car_rect.width*0.5)
  345. // box_node.active = false
  346. }
  347. }
  348. }
  349. }
  350. if(list.length<=0){
  351. return null
  352. }
  353. list.sort((a,b)=>{
  354. return b-a
  355. })
  356. // console.log("list",list)
  357. return list[0]
  358. }
  359. getRightObstacles(car_rect:Rect){
  360. let list:number[] = []
  361. for (let index = 0; index < this.gameBoxList.length; index++) {
  362. const box_node = this.gameBoxList[index];
  363. if(box_node.parent){
  364. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  365. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  366. let width = box_node.getComponent(box).getData().w
  367. let height = box_node.getComponent(box).getData().h
  368. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  369. if(car_rect.xMax<=rect.xMin){
  370. if(new Rect(rect.x,car_rect.y,car_rect.width,car_rect.height).intersects(rect)){
  371. list.push(rect.xMin-car_rect.width*0.5)
  372. }
  373. }
  374. }
  375. }
  376. for (let index = 0; index < this.gameWallList.length; index++) {
  377. const box_node = this.gameWallList[index];
  378. if(box_node.parent){
  379. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  380. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  381. let width = box_node.getComponent(wall).getData().w
  382. let height = box_node.getComponent(wall).getData().h
  383. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  384. if(car_rect.xMax<=rect.xMin){
  385. if(new Rect(rect.x,car_rect.y,car_rect.width,car_rect.height).intersects(rect)){
  386. list.push(rect.xMin-car_rect.width*0.5)
  387. // box_node.active = false
  388. }
  389. }
  390. }
  391. }
  392. if(list.length<=0){
  393. return null
  394. }
  395. list.sort((a,b)=>{
  396. return a-b
  397. })
  398. return list[0]
  399. }
  400. changeMovePos(){
  401. }
  402. }