game.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 { GameManager } from '../GameManager';
  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(Node) collider:Node = 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<=6){
  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. GameManager.gameStart()
  98. }
  99. public isOut(pos:Vec3){
  100. if(pos.y<this.collider.position.y){
  101. return true
  102. }
  103. return false
  104. }
  105. public showBoom(box_p:Vec3){
  106. this.mBoomManager.showBoom(box_p)
  107. }
  108. public showQiYouEffect(qiyou_p:Vec3){
  109. this.mQiYouManager.showQiYou(qiyou_p)
  110. }
  111. public getPoolManager(){
  112. return this.game_pool_manager
  113. }
  114. public getBuffShowView(){
  115. return this.bf_show
  116. }
  117. getOldDir(){
  118. return this.move_pos
  119. }
  120. updateScores(scores:number){
  121. this.lab_scores.getComponent(Label).string = scores.toString()
  122. }
  123. onOver(){
  124. this.unscheduleAllCallbacks()
  125. this.count_time.active = false
  126. this.car.getComponent(car).hideEffect()
  127. director.once(Director.EVENT_AFTER_DRAW,()=>{
  128. this.removeAll()
  129. this.results_view.getComponent(results).show(this.car.getComponent(car).getScores(),(()=>{
  130. }), (isReLife:boolean=false)=>{
  131. this.show(this.mCallBackHome,isReLife)
  132. },()=>{
  133. this.removeSelf()
  134. this.mCallBackHome()
  135. },!this.car.getComponent(car).getIsReLife())
  136. })
  137. GameManager.gameOver()
  138. }
  139. removeSelf(){
  140. this.node.removeFromParent()
  141. }
  142. removeAll(){
  143. let temp_list = []
  144. for (let index = 0; index < this.content.children.length; index++) {
  145. const element = this.content.children[index];
  146. if(element.name!="car"&&element.name!="weiqi"){
  147. temp_list.push(element)
  148. }
  149. }
  150. for (let index = 0; index < temp_list.length; index++) {
  151. const element = temp_list[index];
  152. element.removeFromParent()
  153. }
  154. }
  155. deleteItem(node:Node){
  156. director.once(Director.EVENT_AFTER_DRAW,()=>{
  157. let index = this.content.children.indexOf(node)
  158. if(index!=-1){
  159. this.content.children[index].getComponent(run_level_item).removeAll()
  160. }
  161. })
  162. }
  163. addItem( index:number){
  164. // yield Coroutine.createWaitForSecond(waitSecond);
  165. let l:string[] = tools.levels[index].split(",")
  166. let randomID = this.getRandomInt(0,l.length-1)
  167. let item = instantiate(this.prefab_item)
  168. item.parent = this.content
  169. let id = parseInt(l[randomID])
  170. // console.log("randomID",id,tools.levels[index],index)
  171. let m_d:model_item_data = this.getModelDataById(id)
  172. if(m_d==null){
  173. console.error("m_d==null",id)
  174. return
  175. }
  176. item.position = new Vec3(0,this.mHeight)
  177. this.mHeight+=m_d.height
  178. this.car.setSiblingIndex(this.content.children.length)
  179. item.getComponent(run_level_item).initView(m_d,this.sfList,this.sfBuffList,this.car.getComponent(car))
  180. }
  181. getRandomInt (min: number, max: number) {
  182. let r: number= Math.random();
  183. let rr: number= r * (max - min + 1) + min;
  184. return Math.floor(rr);
  185. }
  186. getModelDataById(id:number){
  187. for (let index = 0; index < tools.tpl_list.length; index++) {
  188. const element = tools.tpl_list[index];
  189. if(element.id===id){
  190. return element
  191. }
  192. }
  193. return null
  194. }
  195. addCoinForList(node:Node){
  196. this.gameCoinList.push(node)
  197. }
  198. removeCoinForList(node:Node){
  199. let index = this.gameCoinList.indexOf(node)
  200. if(index!=-1){
  201. this.gameCoinList.splice(index,1)
  202. }
  203. }
  204. addBoxForList(node:Node){
  205. this.gameBoxList.push(node)
  206. }
  207. removeBoxForList(node:Node){
  208. let index = this.gameBoxList.indexOf(node)
  209. if(index!=-1){
  210. this.gameBoxList.splice(index,1)
  211. }
  212. }
  213. addWallForList(node:Node){
  214. this.gameWallList.push(node)
  215. }
  216. removeWallForList(node:Node){
  217. let index = this.gameWallList.indexOf(node)
  218. if(index!=-1){
  219. this.gameWallList.splice(index,1)
  220. }
  221. }
  222. move(){
  223. // let info = this.joystick.getJoyInfo()
  224. // if(info!=null){
  225. // this.car.getComponent(car).setDir(info.dir)
  226. // this.car.getComponent(car).setDis(info.dis)
  227. // }
  228. }
  229. carCollider(car_rect:Rect){
  230. let list = []
  231. let origin_car_pos = this.car.position
  232. let origin_car_pos_y = origin_car_pos.y+car_rect.height*0.5
  233. let isColliderBox = false
  234. for (let index = 0; index < this.gameBoxList.length; index++) {
  235. const box_node = this.gameBoxList[index];
  236. let isCanCollider = box_node.getComponent(box).getIsCanCollider()
  237. if(box_node.parent&&isCanCollider){
  238. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  239. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  240. let width = box_node.getComponent(box).getData().w
  241. let height = box_node.getComponent(box).getData().h
  242. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  243. let dir = -1
  244. if(car_rect.intersects(rect)){
  245. isColliderBox = true
  246. // let car_top_rect = new Rect(car_rect.xMin,car_rect.yMax-10,car_rect.width,20)
  247. let box_bottom_rect = new Rect(rect.xMin,rect.yMin,rect.width,20)
  248. if(origin_car_pos_y<=box_bottom_rect.yMin){
  249. // if((car_top_rect.yMin-origin_car_pos_y)>20){
  250. // }
  251. dir = config.collider_dir.top
  252. }
  253. // if(car_top_rect.intersects(box_bottom_rect)){
  254. // dir = config.collider_dir.top
  255. // }
  256. list.push({"node":box_node,"rect":rect,"type":0,"collider_dir":dir,"moveOffset":0})
  257. }
  258. }
  259. }
  260. let car_top_rect = new Rect(car_rect.xMin,car_rect.yMax-5,car_rect.width,10)
  261. for (let index = 0; index < this.gameWallList.length; index++) {
  262. const box_node = this.gameWallList[index];
  263. let isCanCollider = box_node.getComponent(wall).getIsCanCollider()
  264. if(box_node.parent&&isCanCollider){
  265. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  266. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  267. let width = box_node.getComponent(wall).getData().w
  268. let height = box_node.getComponent(wall).getData().h
  269. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  270. let dir = -1
  271. let offset = 0
  272. if(car_rect.intersects(rect)){
  273. let box_bottom_rect = new Rect(rect.xMin,rect.yMin,rect.width,10)
  274. // if(car_top_rect.intersects(box_bottom_rect)){
  275. // dir = config.collider_dir.top
  276. // if(box_bottom_rect.xMax>(car_top_rect.xMin+car_top_rect.width*0.5)){ //右边
  277. // offset = (car_top_rect.width*0.5)
  278. // }else{
  279. // offset = (car_top_rect.width*-0.5)
  280. // }
  281. // }
  282. if(origin_car_pos_y<=box_bottom_rect.yMin){
  283. dir = config.collider_dir.top
  284. if(box_bottom_rect.xMax>(car_top_rect.xMin+car_top_rect.width*0.5)){ //右边
  285. offset = (car_top_rect.width*0.5)
  286. }else{
  287. offset = (car_top_rect.width*-0.5)
  288. }
  289. }
  290. list.push({"node":box_node,"rect":rect,"type":1,"collider_dir":dir,"moveOffset":offset})
  291. }
  292. }
  293. }
  294. for (let index = 0; index < this.gameCoinList.length; index++) {
  295. const box_node = this.gameCoinList[index];
  296. let isCanCollider = box_node.getComponent(coin).getIsCanCollider()
  297. if(box_node.parent&&isCanCollider){
  298. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  299. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  300. let width = box_node.getComponent(coin).getData().w
  301. let height = box_node.getComponent(coin).getData().h
  302. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  303. let dir = -1
  304. let offset = 0
  305. if(car_rect.intersects(rect)){
  306. list.push({"node":box_node,"rect":rect,"type":2,"collider_dir":dir,"moveOffset":offset})
  307. }
  308. }
  309. }
  310. return list
  311. }
  312. getLeftObstacles(car_rect:Rect){
  313. let list:number[] = []
  314. for (let index = 0; index < this.gameBoxList.length; index++) {
  315. const box_node = this.gameBoxList[index];
  316. let isCanCollider = box_node.getComponent(box).getIsCanCollider()
  317. if(box_node.parent&&isCanCollider){
  318. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  319. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  320. let width = box_node.getComponent(box).getData().w
  321. let height = box_node.getComponent(box).getData().h
  322. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  323. if(car_rect.xMin>=rect.xMax){
  324. if(new Rect(rect.x,car_rect.y,car_rect.width,car_rect.height).intersects(rect)){
  325. list.push(rect.xMax+car_rect.width*0.5)
  326. // box_node.active = false
  327. }
  328. }
  329. }
  330. }
  331. for (let index = 0; index < this.gameWallList.length; index++) {
  332. const box_node = this.gameWallList[index];
  333. let isCanCollider = box_node.getComponent(wall).getIsCanCollider()
  334. if(box_node.parent&&isCanCollider){
  335. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  336. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  337. let width = box_node.getComponent(wall).getData().w
  338. let height = box_node.getComponent(wall).getData().h
  339. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  340. if(car_rect.xMin>=rect.xMax){
  341. if(new Rect(rect.x,car_rect.y,car_rect.width,car_rect.height).intersects(rect)){
  342. list.push(rect.xMax+car_rect.width*0.5)
  343. // box_node.active = false
  344. }
  345. }
  346. }
  347. }
  348. if(list.length<=0){
  349. return null
  350. }
  351. list.sort((a,b)=>{
  352. return b-a
  353. })
  354. // console.log("list",list)
  355. return list[0]
  356. }
  357. getRightObstacles(car_rect:Rect){
  358. let list:number[] = []
  359. for (let index = 0; index < this.gameBoxList.length; index++) {
  360. const box_node = this.gameBoxList[index];
  361. if(box_node.parent){
  362. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  363. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  364. let width = box_node.getComponent(box).getData().w
  365. let height = box_node.getComponent(box).getData().h
  366. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  367. if(car_rect.xMax<=rect.xMin){
  368. if(new Rect(rect.x,car_rect.y,car_rect.width,car_rect.height).intersects(rect)){
  369. list.push(rect.xMin-car_rect.width*0.5)
  370. }
  371. }
  372. }
  373. }
  374. for (let index = 0; index < this.gameWallList.length; index++) {
  375. const box_node = this.gameWallList[index];
  376. if(box_node.parent){
  377. let box_world = box_node.parent.getComponent(UITransform).convertToWorldSpaceAR(box_node.position)
  378. let p = this.content.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  379. let width = box_node.getComponent(wall).getData().w
  380. let height = box_node.getComponent(wall).getData().h
  381. let rect = new Rect(p.x-width*0.5,p.y-height*0.5,width,height)
  382. if(car_rect.xMax<=rect.xMin){
  383. if(new Rect(rect.x,car_rect.y,car_rect.width,car_rect.height).intersects(rect)){
  384. list.push(rect.xMin-car_rect.width*0.5)
  385. // box_node.active = false
  386. }
  387. }
  388. }
  389. }
  390. if(list.length<=0){
  391. return null
  392. }
  393. list.sort((a,b)=>{
  394. return a-b
  395. })
  396. return list[0]
  397. }
  398. changeMovePos(){
  399. }
  400. }