box.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(BoxCollider2D) collider:BoxCollider2D = null;
  12. @property(Node) lab_scores:Node = null;
  13. private scores:number = 10;
  14. private CdTime:number =0;
  15. private isOnCollide:boolean = false;
  16. private mCar:car = null;
  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.mCar = car
  21. this.node.position = new Vec3(this.mData.x,this.mData.y)
  22. this.node.getComponent(Sprite).spriteFrame = box_sf
  23. this.buff.getComponent(Sprite).spriteFrame = buff_sf
  24. this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h)
  25. this.node.getComponent(BoxCollider2D).size = new Size(this.mData.w,this.mData.h)
  26. // this.node.getComponent(BoxCollider2D).offset.y = 40
  27. this.collider.node.getComponent(UITransform).contentSize = new Size(this.mData.w,15)
  28. this.collider.node.getComponent(BoxCollider2D).size = new Size(this.mData.w,15)
  29. let sd = tools.getBoxRandom(this.mData.item_type)
  30. this.scores = Util.getRandomInt(sd.min,sd.max);
  31. this.updateScores()
  32. // this.node.getComponent(RigidBody2D).wakeUp()
  33. }
  34. protected ReSetNode(){
  35. this.node.position = Vec3.ZERO
  36. this.scores = 0
  37. this.isOnCollide = false
  38. this.node.getComponent(Sprite).spriteFrame = null
  39. this.buff.getComponent(Sprite).spriteFrame = null
  40. }
  41. public hurt(call,isKill=false){
  42. if(!this.isOnCollide){
  43. this.scheduleOnce(()=>{
  44. this.isOnCollide = false;
  45. },this.CdTime)
  46. // director.once(Director.EVENT_AFTER_DRAW,()=>{
  47. // this.isOnCollide = false;
  48. // })
  49. this.isOnCollide = true;
  50. let isUseBuff:boolean = this.mData.buff_type!=-1
  51. if(this.scores<=1||isKill){
  52. this.removeSelf()
  53. isUseBuff = true
  54. }else{
  55. if(isUseBuff){
  56. isUseBuff = false
  57. }
  58. }
  59. if(call!=null){
  60. if(isUseBuff){
  61. call(this.mData,isKill,this.scores)
  62. }else{
  63. call()
  64. }
  65. }
  66. if(!isUseBuff){
  67. this.scores -= 1;
  68. this.updateScores()
  69. }
  70. }
  71. }
  72. public removeSelf(){
  73. let self = this;
  74. self.node.active = false
  75. self.buff.getComponent(Sprite).spriteFrame = null
  76. // self.node.getComponent(RigidBody2D).sleep()
  77. self.node.position = Vec3.ZERO
  78. self.mCar.getGame().removeBoxForList(self.node)
  79. director.once(Director.EVENT_AFTER_DRAW,()=>{
  80. self.mCar.getGame().getPoolManager().putBox(self.node)
  81. })
  82. }
  83. private updateScores(){
  84. this.lab_scores.getComponent(Label).string = this.scores+""
  85. }
  86. }