game.ts 17 KB

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