car.ts 17 KB

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