game.ts 14 KB

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