box.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if(isUseBuff){
  56. this.mData.pos = this.getWorldPos()
  57. }
  58. this.removeSelf()
  59. isUseBuff = true
  60. }else{
  61. if(isUseBuff){
  62. isUseBuff = false
  63. }
  64. }
  65. if(call!=null){
  66. if(isUseBuff){
  67. call(this.mData,isKill,this.scores)
  68. }else{
  69. call()
  70. }
  71. }
  72. if(!isUseBuff){
  73. this.scores -= 1;
  74. this.updateScores()
  75. }
  76. }
  77. }
  78. public removeSelf(){
  79. let self = this;
  80. self.node.active = false
  81. self.buff.getComponent(Sprite).spriteFrame = null
  82. // self.node.getComponent(RigidBody2D).sleep()
  83. self.node.position = Vec3.ZERO
  84. self.mCar.getGame().removeBoxForList(self.node)
  85. director.once(Director.EVENT_AFTER_DRAW,()=>{
  86. self.mCar.getGame().getPoolManager().putBox(self.node)
  87. })
  88. }
  89. private updateScores(){
  90. this.lab_scores.getComponent(Label).string = this.scores+""
  91. }
  92. protected lateUpdate(dt: number): void {
  93. if(this.mCar!=null){
  94. let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  95. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  96. if(p.y<(this.mCar.node.position.y+1500)&&p.y>this.mCar.node.position.y&&!this.active_node){
  97. this.active_node = true
  98. }
  99. if(p.y<(this.mCar.node.position.y-1000)&&this.active_node){
  100. this.active_node = false
  101. }
  102. this.updateBoxShowStatus()
  103. }
  104. }
  105. getIsActive(){
  106. return this.active_node
  107. }
  108. getWorldPos(){
  109. return this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  110. }
  111. getIsCanCollider(){
  112. if(!this.node.active){
  113. return false
  114. }
  115. let box_world=this.getWorldPos()
  116. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  117. return Vec3.distance(p,this.mCar.node.position)<500
  118. }
  119. updateBoxShowStatus(){
  120. this.node.getComponent(Sprite).enabled = this.active_node
  121. this.lab_scores.getComponent(Label).enabled = this.active_node
  122. }
  123. }