coin.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { _decorator, CircleCollider2D, Component, Director, director, Label, Node, RigidBody2D, Size, Sprite, tween, UITransform, Vec2, Vec3 } from 'cc';
  2. import { model_content_item_data } from '../data';
  3. import { car } from './car';
  4. import { tools } from '../tools';
  5. import { base } from './base';
  6. import { audioManager } from '../manager/audioManager';
  7. import { config } from '../config';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('coin')
  10. export class coin extends base {
  11. @property(Node) lab_num:Node = null
  12. private mCar:car = null;
  13. private isMove:boolean = false;
  14. private addHpNum:number = 0
  15. private active_node:boolean = false
  16. public init(data:model_content_item_data,car:car){
  17. this.mCar = car
  18. this.mData = data
  19. this.active_node = false
  20. this.node.position = new Vec3(this.mData.x,this.mData.y,this.node.position.z)
  21. this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h)
  22. this.node.getComponent(CircleCollider2D).radius = this.mData.w*0.5
  23. this.unscheduleAllCallbacks()
  24. this.schedule(this.on_update,0.2)
  25. this.addHpNum = tools.randomCoin()
  26. this.lab_num.getComponent(Label).string = this.addHpNum +""
  27. this.getComponent(RigidBody2D).wakeUp()
  28. }
  29. protected ReSetNode(){
  30. this.node.position = Vec3.ZERO
  31. this.isMove = false
  32. }
  33. public getAddHpNum(){
  34. return this.addHpNum
  35. }
  36. public removeSelf(){
  37. let self = this;
  38. director.once(Director.EVENT_AFTER_DRAW,()=>{
  39. self.mCar.getGame().removeCoinForList(this.node)
  40. self.mCar.getGame().getPoolManager().putCoin(this.node)
  41. })
  42. }
  43. public moveToTarget(target_pos:Vec3){
  44. // this.node.getComponent(CircleCollider2D).enabled = false
  45. this.node.getComponent(RigidBody2D).sleep()
  46. tween(this.node).to(0.2,{position:target_pos}).call(()=>{
  47. audioManager.Instance().playSound(config.AUDIO.qiyou)
  48. this.removeSelf()
  49. }).start()
  50. }
  51. protected on_update(dt: number): void {
  52. if(this.mCar.getXiStatus()&&!this.isMove){
  53. let selfPos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  54. let dis = Vec3.distance(this.mCar.node.position, this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(selfPos))
  55. let carPos = this.mCar.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.mCar.node.position)
  56. if(dis<=tools.game_config.buff_xi_range){
  57. this.isMove = true
  58. this.unschedule(this.on_update)
  59. this.moveToTarget(this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(carPos))
  60. }
  61. }
  62. if(this.mCar!=null){
  63. let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  64. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  65. if(p.y<(this.mCar.node.position.y+1800)&&p.y>this.mCar.node.position.y&&!this.active_node){
  66. this.active_node = true
  67. }
  68. if(p.y<(this.mCar.node.position.y-1000)&&this.active_node){
  69. this.active_node = false
  70. }
  71. this.updateBoxShowStatus()
  72. }
  73. }
  74. getIsActive(){
  75. return this.active_node
  76. }
  77. getIsCanCollider(){
  78. let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  79. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  80. return Vec3.distance(p,this.mCar.node.position)<500
  81. }
  82. updateBoxShowStatus(){
  83. this.node.getComponent(Sprite).enabled = this.active_node
  84. this.lab_num.getComponent(Label).enabled = this.active_node
  85. }
  86. }