game.ts 18 KB

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