car.ts 18 KB

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