car.ts 20 KB

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