game.ts 17 KB

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