box.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { _decorator, BoxCollider2D, Collider2D, Component, Director, director, Label, Node, RigidBody2D, Size, Sprite, SpriteFrame, UITransform, Vec3 } from 'cc';
  2. import { model_content_item_data } from '../data';
  3. import { tools } from '../tools';
  4. import { Util } from '../util';
  5. import { base } from './base';
  6. import { car } from './car';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('box')
  9. export class box extends base {
  10. @property(Node) buff:Node = null;
  11. @property(Node) lab_scores:Node = null;
  12. private scores:number = 10;
  13. private CdTime:number =0;
  14. private isOnCollide:boolean = false;
  15. private mCar:car = null;
  16. private active_node:boolean = false
  17. public init(data:model_content_item_data,box_sf:SpriteFrame,buff_sf:SpriteFrame,car:car){
  18. this.node.active = true
  19. this.mData = data;
  20. this.active_node = false
  21. this.isOnCollide = false
  22. this.mCar = car
  23. this.node.position = new Vec3(this.mData.x,this.mData.y)
  24. this.node.getComponent(Sprite).spriteFrame = box_sf
  25. this.buff.getComponent(Sprite).spriteFrame = buff_sf
  26. this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h)
  27. // this.node.getComponent(BoxCollider2D).offset.y = 40
  28. let sd = tools.getBoxRandom(this.mData.item_type)
  29. this.scores = Util.getRandomInt(sd.min,sd.max);
  30. this.updateScores()
  31. // this.node.getComponent(RigidBody2D).wakeUp()
  32. }
  33. protected ReSetNode(){
  34. this.node.position = Vec3.ZERO
  35. this.scores = 0
  36. this.isOnCollide = false
  37. this.node.getComponent(Sprite).spriteFrame = null
  38. this.buff.getComponent(Sprite).spriteFrame = null
  39. }
  40. public hurt(call,isKill=false){
  41. if(!this.isOnCollide){
  42. // this.scheduleOnce(()=>{
  43. // this.isOnCollide = false;
  44. // },this.CdTime)
  45. // director.once(Director.EVENT_AFTER_DRAW,()=>{
  46. // this.isOnCollide = false;
  47. // })
  48. // this.isOnCollide = true;
  49. this.isOnCollide = false;
  50. let isUseBuff:boolean = this.mData.buff_type!=-1
  51. if(this.scores<=1||isKill){
  52. let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  53. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  54. this.mCar.getGame().showBoom(p)
  55. this.removeSelf()
  56. isUseBuff = true
  57. }else{
  58. if(isUseBuff){
  59. isUseBuff = false
  60. }
  61. }
  62. if(call!=null){
  63. if(isUseBuff){
  64. this.mData.pos = this.getWorldPos()
  65. call(this.mData,isKill,this.scores)
  66. }else{
  67. call()
  68. }
  69. }
  70. if(!isUseBuff){
  71. this.scores -= 1;
  72. this.updateScores()
  73. }
  74. }
  75. }
  76. public removeSelf(){
  77. let self = this;
  78. self.node.active = false
  79. self.buff.getComponent(Sprite).spriteFrame = null
  80. // self.node.getComponent(RigidBody2D).sleep()
  81. self.node.position = Vec3.ZERO
  82. self.mCar.getGame().removeBoxForList(self.node)
  83. director.once(Director.EVENT_AFTER_DRAW,()=>{
  84. self.mCar.getGame().getPoolManager().putBox(self.node)
  85. })
  86. }
  87. private updateScores(){
  88. this.lab_scores.getComponent(Label).string = this.scores+""
  89. }
  90. protected lateUpdate(dt: number): void {
  91. if(this.mCar!=null){
  92. let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  93. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  94. if(p.y<(this.mCar.node.position.y+1500)&&p.y>this.mCar.node.position.y&&!this.active_node){
  95. this.active_node = true
  96. }
  97. if(p.y<(this.mCar.node.position.y-1000)&&this.active_node){
  98. this.active_node = false
  99. }
  100. this.updateBoxShowStatus()
  101. }
  102. }
  103. getIsActive(){
  104. return this.active_node
  105. }
  106. getWorldPos(){
  107. return this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  108. }
  109. getIsCanCollider(){
  110. if(!this.node.active){
  111. return false
  112. }
  113. let box_world=this.getWorldPos()
  114. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  115. return Vec3.distance(p,this.mCar.node.position)<500
  116. }
  117. updateBoxShowStatus(){
  118. this.node.getComponent(Sprite).enabled = this.active_node
  119. this.lab_scores.getComponent(Label).enabled = this.active_node
  120. }
  121. }