game.ts 17 KB

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