car.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. import { _decorator, BoxCollider2D, CircleCollider2D, Collider2D, Component, Contact2DType, Director, director, IPhysics2DContact, Label, math, Node, PolygonCollider2D, Rect, RigidBody, RigidBody2D, Sprite, tween, Tween, UITransform, Vec2, Vec3, View } from 'cc';
  2. import { wall } from './wall';
  3. import { box } from './box';
  4. import { coin } from './coin';
  5. import { tools } from '../tools';
  6. import { SdkUtil } from '../sdkUtil';
  7. import { game } from './game';
  8. import { DirType, model_content_item_data } from '../data';
  9. import { config } from '../config';
  10. import { imageCacheManager } from '../manager/imageCacheManager';
  11. import { userDataManager } from '../manager/userDataManager';
  12. import { audioManager } from '../manager/audioManager';
  13. import { buff_manager } from './buff/buff_manager';
  14. import { hp_bar } from './hp_bar';
  15. const { ccclass, property } = _decorator;
  16. @ccclass('car')
  17. export class car extends Component {
  18. private offsetY:number = 0;
  19. private origin_pos_y:number = -300;
  20. private map:Node = null;
  21. private mDir:Vec3 = Vec3.ZERO;
  22. private mOldDir:Vec3 = Vec3.ZERO;
  23. private mGameOldDir:Vec3 = Vec3.ZERO;
  24. private mDis:number = 0;
  25. private isAction:boolean = false;
  26. private speed:number = 700;
  27. @property(CircleCollider2D) car_collider:CircleCollider2D = null;
  28. @property(Node) lab_hp:Node = null;
  29. private hp:number = 0
  30. private scores:number = 0
  31. private over_cb = null;
  32. private update_scores_cb = null;
  33. private game_manager:game = null;
  34. private buff_xi_status:boolean = false;
  35. private buff_dun_status:boolean = false;
  36. private buff_xing_status:boolean = false;
  37. private speed_bei:number = 1;
  38. @property(Number) move_speed_y:Number = 700;
  39. @property(Number) move_speed_x:Number = 700;
  40. @property(Boolean) is_open_jiaozheng:Boolean = true;
  41. private carDir:DirType = DirType.NONE
  42. private canvas_width:number = 1080;
  43. private isStopY:boolean = false
  44. private isStartGame:Boolean = false
  45. @property(Node) spr:Node = null
  46. @property(Node) hp_bar:Node = null
  47. private mIsReLife:boolean = false
  48. public init(m:Node,cb,cb1,g:game,isReLife:boolean = false){
  49. this.mIsReLife = isReLife
  50. this.spr.getComponent(Sprite).spriteFrame = imageCacheManager.getGameCarImageById(userDataManager.user_car_list.default_car_id)
  51. this.spr.getComponent(buff_manager).init()
  52. this.origin_pos_y = this.node.position.y
  53. this.game_manager = g
  54. this.over_cb = cb
  55. this.update_scores_cb = cb1
  56. this.map = m;
  57. this.isAction = false;
  58. this.mOldDir = this.node.position
  59. this.buff_xi_status = false;
  60. this.buff_dun_status = false;
  61. this.buff_xing_status = false;
  62. this.isStopY = false;
  63. this.canvas_width = View.instance.getVisibleSize().width
  64. if(userDataManager.user_car_list.default_car_id==1){
  65. audioManager.Instance().playSound(config.AUDIO.zi_xing_che_qi_bu)
  66. }else{
  67. audioManager.Instance().playSound(config.AUDIO.car_qi_dong)
  68. }
  69. // let cd = this.node.getComponent(BoxCollider2D)
  70. // cd.off(Contact2DType.BEGIN_CONTACT)
  71. // cd.on(Contact2DType.BEGIN_CONTACT,this.onColliderCoin.bind(this))
  72. // cd.off(Contact2DType.END_CONTACT)
  73. // cd.on(Contact2DType.END_CONTACT,this.onColliderCoin.bind(this))
  74. // cd.off(Contact2DType.PRE_SOLVE)
  75. // cd.on(Contact2DType.PRE_SOLVE,this.onColliderWall.bind(this))
  76. // this.car_collider.off(Contact2DType.BEGIN_CONTACT)
  77. // this.car_collider.on(Contact2DType.BEGIN_CONTACT,this.onColliderCoin.bind(this))
  78. // this.car_collider.off(Contact2DType.END_CONTACT)
  79. // this.car_collider.on(Contact2DType.END_CONTACT,this.onColliderCoin.bind(this))
  80. // this.car_collider.off(Contact2DType.POST_SOLVE)
  81. // this.car_collider.on(Contact2DType.POST_SOLVE,(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null)=>{
  82. // // console.log("this.car_collider",otherCollider.node.name)
  83. // if(otherCollider.node.name.endsWith("fku")){
  84. // if(otherCollider.node.parent.getComponent(box)!=null){
  85. // otherCollider.node.parent.getComponent(box).hurt(this.shake.bind(this),this.buff_dun_status)
  86. // }
  87. // }
  88. // if(otherCollider.node.name.endsWith("wall")){
  89. // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(this.move_speed_x.valueOf(),this.move_speed_y.valueOf())
  90. // }
  91. // },this)
  92. this.move_speed_y = tools.game_config.car_origin_speed
  93. this.hp = tools.game_config.car_init_num
  94. this.hp_bar.getComponent(hp_bar).updateHp(this.hp)
  95. // this.hp = 100000
  96. if(isReLife){
  97. }else{
  98. this.scores = 0
  99. }
  100. this.updateHp()
  101. this.updateScores()
  102. this.node.getComponent(RigidBody2D).gravityScale = -3
  103. this.node.getComponent(RigidBody2D).linearDamping = 1
  104. // this.schedule(this.check.bind(this),0.01)
  105. // circle.radius = tools.game_config.buff_xi_range
  106. // circle.radius = 400
  107. // circle.off(Contact2DType.BEGIN_CONTACT)
  108. // circle.on(Contact2DType.BEGIN_CONTACT,(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null)=>{
  109. // if(otherCollider.node.name.endsWith("coin")){
  110. // if(otherCollider.node.getComponent(coin)!=null){
  111. // otherCollider.node.getComponent(coin).moveToTarget(otherCollider.node.parent.getComponent(UITransform).convertToNodeSpaceAR(this.node.position))
  112. // this.hp+=tools.randomCoin()
  113. // this.updateHp()
  114. // }
  115. // }
  116. // })
  117. this.unschedule(this.addSpeed)
  118. this.schedule(this.addSpeed.bind(this),tools.game_config.car_add_speed_time)
  119. }
  120. public hideEffect(){
  121. this.spr.getComponent(buff_manager).init()
  122. }
  123. public getIsReLife(){
  124. return this.mIsReLife
  125. }
  126. addSpeed(){
  127. let speed = this.move_speed_y.valueOf()+(tools.game_config.car_add_speed_num)
  128. if(speed<tools.game_config.car_add_speed_max){
  129. this.move_speed_y=speed
  130. }
  131. }
  132. addSpeedNum(num:number){
  133. let speed = this.move_speed_y.valueOf()+(num*0.01*this.move_speed_y.valueOf())
  134. if(speed<tools.game_config.car_add_speed_max){
  135. this.move_speed_y=speed
  136. }
  137. }
  138. onColliderCoin(coin_node:Node){
  139. if(coin_node.getComponent(coin)!=null){
  140. audioManager.Instance().playSound(config.AUDIO.qiyou)
  141. coin_node.getComponent(coin).removeSelf()
  142. this.hp+=coin_node.getComponent(coin).getAddHpNum()
  143. this.updateHp()
  144. }
  145. }
  146. onColliderBox(box_node:Node){
  147. if(box_node.getComponent(box)!=null){
  148. audioManager.Instance().playSound(config.AUDIO.peng_zhuang)
  149. box_node.getComponent(box).hurt(this.shake.bind(this),this.buff_dun_status)
  150. }
  151. }
  152. public getIsOpenJZ():boolean{
  153. return this.is_open_jiaozheng.valueOf()
  154. }
  155. public getGame(){
  156. return this.game_manager
  157. }
  158. public getXiStatus(){
  159. return this.buff_xi_status
  160. }
  161. private updateScores(){
  162. if(this.update_scores_cb!=null){
  163. this.update_scores_cb(this.scores)
  164. }
  165. }
  166. private updateHp(){
  167. this.lab_hp.getComponent(Label).string = this.hp+""
  168. this.hp_bar.getComponent(hp_bar).updateHp(this.hp)
  169. }
  170. public getScores(){
  171. return this.scores
  172. }
  173. private shake(data:model_content_item_data,isKill:boolean,scores:number){
  174. // Tween.stopAllByTarget(this.node)
  175. let new_hp = this.hp;
  176. // this.isAction = true;
  177. if(isKill){
  178. this.scores += ( scores*(this.buff_xing_status?2:1) )
  179. }else{
  180. this.scores+=( 1*(this.buff_xing_status?2:1) );
  181. new_hp = this.hp-1;
  182. }
  183. this.hp = new_hp;
  184. this.updateHp()
  185. // this.move_speed_y = 0
  186. this.isStopY = true
  187. this.updateScores()
  188. // this.node.getComponent(RigidBody2D).sleep()
  189. SdkUtil.vibrateShort()
  190. // this.node.position = new Vec3(this.node.position.x, this.node.position.y-8)
  191. // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(0,-2.5)
  192. // director.once(Director.EVENT_AFTER_DRAW,()=>{
  193. // // this.move_speed_y = 0
  194. // })
  195. // tween(this.node).delay(0.1).call(()=>{
  196. // // this.isAction = false;
  197. // // this.node.position = new Vec3(this.node.position.x, this.node.position.y+8)
  198. // // this.node.getComponent(RigidBody2D).wakeUp()
  199. // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(this.move_speed_x.valueOf(),30)
  200. // }).start()
  201. Tween.stopAllByTarget(this.lab_hp)
  202. tween(this.lab_hp).delay(0.05).call(()=>{ this.isStopY = false}).start()
  203. if(this.hp<0){
  204. if(this.isStartGame){
  205. Tween.stopAllByTarget(this.node)
  206. this.stopGame()
  207. // this.node.getComponent(RigidBody2D).gravityScale = 0
  208. // this.node.getComponent(RigidBody2D).linearDamping = 0
  209. this.over_cb()
  210. }
  211. }else{
  212. if(data!=null){
  213. switch (data.buff_type) {
  214. case config.buff_type.dun:
  215. // console.log("获取buff 盾")
  216. this.buff_dun()
  217. break;
  218. case config.buff_type.xing:
  219. // console.log("获取buff 星")
  220. this.buff_xing()
  221. break;
  222. case config.buff_type.xi:
  223. // console.log("获取buff 吸")
  224. this.buff_xi()
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. public buff_xi(){
  231. this.addSpeedNum(tools.game_config.buff_xi_add_speed)
  232. // Tween.stopAllByTarget(this.game_manager.getBuffShowView().getBuffXi())
  233. // this.cancelScheduleBuffXi()
  234. // tween(this.game_manager.getBuffShowView().getBuffXi()).delay(tools.game_config.buff_xi).call(this.cancelScheduleBuffXi.bind(this)).start()
  235. this.game_manager.getBuffShowView().showBuffXi(this.cancelScheduleBuffXi.bind(this),tools.game_config.buff_xi)
  236. audioManager.Instance().playSound(config.AUDIO.buff_xi)
  237. this.spr.getComponent(buff_manager).updateStatus(config.SHOW_BUFF_STATUS.XI)
  238. this.buff_xi_status = true;
  239. }
  240. public cancelScheduleBuffXi(){
  241. if(this.buff_xi_status){
  242. this.spr.getComponent(buff_manager).cancelStatus(config.SHOW_BUFF_STATUS.XI)
  243. this.buff_xi_status = false
  244. this.game_manager.getBuffShowView().hideBuffXi()
  245. }
  246. }
  247. public buff_xing(){
  248. this.addSpeedNum(tools.game_config.buff_xi_add_speed)
  249. // Tween.stopAllByTarget(this.game_manager.getBuffShowView().getBuffXing())
  250. // this.cancelScheduleBuffXing()
  251. // tween(this.game_manager.getBuffShowView().getBuffXing()).delay(tools.game_config.buff_xing).call(this.cancelScheduleBuffXing.bind(this)).start()
  252. this.game_manager.getBuffShowView().showBuffXing(this.cancelScheduleBuffXing.bind(this),tools.game_config.buff_xing)
  253. audioManager.Instance().playSound(config.AUDIO.buff_xing)
  254. this.spr.getComponent(buff_manager).updateStatus(config.SHOW_BUFF_STATUS.XING)
  255. this.buff_xing_status = true;
  256. }
  257. public cancelScheduleBuffXing(){
  258. if(this.buff_xing_status ){
  259. this.spr.getComponent(buff_manager).cancelStatus(config.SHOW_BUFF_STATUS.XING)
  260. this.buff_xing_status = false
  261. this.game_manager.getBuffShowView().hideBuffXing()
  262. }
  263. }
  264. public buff_dun(){
  265. this.addSpeedNum(tools.game_config.buff_dun_add_speed)
  266. // Tween.stopAllByTarget(this.game_manager.getBuffShowView().getBuffDun())
  267. // this.cancelScheduleBuffDun()
  268. // tween(this.game_manager.getBuffShowView().getBuffDun()).delay(tools.game_config.buff_dun).call(this.cancelScheduleBuffDun.bind(this)).start()
  269. // this.scheduleOnce(this.cancelScheduleBuffDun.bind(this),tools.game_config.buff_dun)
  270. this.game_manager.getBuffShowView().showBuffDun(this.cancelScheduleBuffDun.bind(this),tools.game_config.buff_dun)
  271. audioManager.Instance().playSound(config.AUDIO.buff_dun)
  272. this.spr.getComponent(buff_manager).updateStatus(config.SHOW_BUFF_STATUS.DUN)
  273. this.buff_dun_status = true;
  274. }
  275. public cancelScheduleBuffDun(){
  276. if(this.buff_dun_status){
  277. this.buff_dun_status = false
  278. this.game_manager.getBuffShowView().hideBuffDun()
  279. this.spr.getComponent(buff_manager).cancelStatus(config.SHOW_BUFF_STATUS.DUN)
  280. }
  281. }
  282. public setMoveSpeedX(speed_x:number){
  283. // this.move_speed_x = speed_x
  284. }
  285. public startGame(){
  286. this.isStartGame = true
  287. }
  288. public stopGame(){
  289. this.isStartGame = false
  290. }
  291. check(){
  292. }
  293. protected update(dt: number): void {
  294. if(!this.isStartGame){
  295. return
  296. }
  297. if(this.map!=null&&this.hp>=0){
  298. let x = 0
  299. let y = this.isStopY?this.node.position.y:this.node.position.y+this.move_speed_y.valueOf()*dt;
  300. let self_pos = new Vec3(this.node.position.x,this.node.position.y)
  301. let dis = Math.abs(self_pos.x-this.mDir.x)
  302. if(dis>1){
  303. // console.log("dis",this.move_speed_x)
  304. x = self_pos.subtract(this.mDir).normalize().x*this.move_speed_x.valueOf()*dt*this.speed_bei;
  305. }
  306. x = this.node.position.x-x;
  307. let size = this.node.getComponent(UITransform).contentSize;
  308. let rect = new Rect(x-size.width*0.5,y-size.height*0.5,size.width,size.height)
  309. // let rect = this.node.getComponent(UITransform).getBoundingBox()
  310. let list = this.game_manager.carCollider(rect)
  311. if(list.length>0){
  312. for (let index = 0; index < list.length; index++) {
  313. const element = list[index];
  314. if(element.collider_dir==config.collider_dir.top){
  315. y = this.node.position.y
  316. if(element.type==0){
  317. this.onColliderBox(element.node)
  318. }else if(element.type==1){
  319. x-=element.moveOffset
  320. // console.log("carCollider wall",element)
  321. }
  322. }else{
  323. // x = this.node.position.x
  324. if(element.type==2){
  325. this.onColliderCoin(element.node)
  326. }else if(element.type==1||element.type==0){
  327. if(element.moveOffset==0){
  328. x = this.node.position.x
  329. }
  330. }
  331. }
  332. }
  333. }
  334. let left = this.canvas_width*0.5*-1+90*0.5;
  335. let right = this.canvas_width*0.5-90*0.5;
  336. if(this.carDir==DirType.LEFT){
  337. let left_x = this.game_manager.getLeftObstacles(new Rect(this.node.position.x-size.width*0.5,this.node.position.y-size.height*0.5,size.width,size.height))
  338. // console.log("left_x",left_x)
  339. if(left_x!=null&&x<left_x){
  340. x = left_x+5
  341. }
  342. if(x<=left){
  343. x = this.node.position.x
  344. }
  345. }
  346. if(this.carDir==DirType.RIGHT){
  347. let right_x = this.game_manager.getRightObstacles(new Rect(this.node.position.x-size.width*0.5,this.node.position.y-size.height*0.5,size.width,size.height))
  348. // console.log("right_x",right_x)
  349. if(right_x!=null&&x>right_x){
  350. x = right_x-5
  351. }
  352. if(x>=right){
  353. x = this.node.position.x
  354. }
  355. }
  356. // this._curRingPos_1.set(0, 0, 0);
  357. // Vec3.lerp(this._curRingPos_1, this.node.position, new Vec3(x,y), 50 * dt);
  358. // this.node.setPosition(this._curRingPos_1);
  359. this.node.setPosition(x,y)
  360. this.offsetY =Math.abs(this.origin_pos_y-y)
  361. let dir = new Vec3(this.map.position.x,-this.offsetY,this.map.position.z)
  362. this.map.position = dir
  363. }
  364. }
  365. public setDir(dir:DirType){
  366. // if(this.carDir!=dir){
  367. // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(0,this.move_speed_y.valueOf())
  368. // }
  369. this.carDir = dir
  370. }
  371. public setDisDir(dis:number){
  372. this.speed_bei = dis / 10
  373. if(this.speed_bei>5){
  374. if(userDataManager.user_car_list.default_car_id==1){
  375. audioManager.Instance().playSound(config.AUDIO.zi_xing_che_sha_che)
  376. }else{
  377. audioManager.Instance().playSound(config.AUDIO.car_sha_che,0.1)
  378. }
  379. }
  380. if(this.carDir==DirType.LEFT){
  381. this.mDir = new Vec3(this.node.position.x-dis,this.node.position.y)
  382. }
  383. if(this.carDir==DirType.RIGHT){
  384. this.mDir = new Vec3(this.node.position.x+dis,this.node.position.y)
  385. }
  386. }
  387. public setDis(dis:number){
  388. this.setSpeed(new Vec3(dis))
  389. // this.mDis = dis
  390. }
  391. public getDis(){
  392. return this.mDis
  393. }
  394. public getCarDir(){
  395. return this.carDir
  396. }
  397. public setSpeed(dir:Vec3){
  398. // let dis = Math.abs(this.node.position.x - dir.x)
  399. // if(dis>200){
  400. // dis = 500
  401. // }
  402. // this.mDis = dis;
  403. let dis = Math.abs(this.mOldDir.x - dir.x)
  404. // let is_char = false
  405. // if(this.carDir===DirType.LEFT){
  406. // if(dir.x>this.node.position.x){
  407. // is_char = true
  408. // }
  409. // }else if(this.carDir===DirType.RIGHT){
  410. // if(dir.x<this.node.position.x){
  411. // is_char = true
  412. // }
  413. // }
  414. // if(is_char){
  415. // dis = Math.abs(this.mGameOldDir.x - dir.x)
  416. // }else{
  417. // this.mGameOldDir = dir
  418. // }
  419. this.mOldDir = this.node.position
  420. // dis*=2
  421. // console.log("this.mDis",dis)
  422. this.mDis = dis;
  423. // if(this.mDir.x!=0){
  424. // //与目标位置的距离进行速度算法 - 距离越长 速度越快
  425. // if(!this.isAction){
  426. // let dis = Math.abs(this.mOldDir.x - dir.x)
  427. // if(this.carDir===DirType.LEFT){
  428. // if(dir.x>this.node.position.x){
  429. // dis = Math.abs(this.mGameOldDir.x - dir.x)
  430. // }
  431. // }else if(this.carDir===DirType.RIGHT){
  432. // if(dir.x<this.node.position.x){
  433. // dis = Math.abs(this.mGameOldDir.x - dir.x)
  434. // }
  435. // }
  436. // this.mDis = dis;
  437. // // if(dis<=100){
  438. // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(10*x,30)
  439. // // }else if(dis>100&&dis<=200){
  440. // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(20*x,30)
  441. // // }else{
  442. // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(dis/200*35*x,25)
  443. // // }
  444. // // this.node.position = new Vec3(this.node.position.x+x*this.speed*dt,this.node.position.y,this.node.position.z)
  445. // }
  446. // }else{
  447. // if(this.node.getComponent(RigidBody2D).linearVelocity.x!=0){
  448. // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(0,this.move_speed_y.valueOf())
  449. // }
  450. // }
  451. }
  452. // protected update(dt: number): void {
  453. // this.node.position = new Vec3(this.node.position.x,this.node.position.y+dt*500,this.node.position.z)
  454. // this.map.position = new Vec3(this.map.position.x,this.map.position.y-dt*500,this.map.position.z)
  455. // }
  456. }