import { _decorator, BoxCollider2D, Collider2D, Component, Director, director, Label, Node, RigidBody2D, Size, Sprite, SpriteFrame, UITransform, Vec3 } from 'cc'; import { model_content_item_data } from '../data'; import { tools } from '../tools'; import { Util } from '../util'; import { base } from './base'; import { car } from './car'; const { ccclass, property } = _decorator; @ccclass('box') export class box extends base { @property(Node) buff:Node = null; @property(BoxCollider2D) collider:BoxCollider2D = null; @property(Node) lab_scores:Node = null; private scores:number = 10; private CdTime:number =0; private isOnCollide:boolean = false; private mCar:car = null; public init(data:model_content_item_data,box_sf:SpriteFrame,buff_sf:SpriteFrame,car:car){ this.node.active = true this.mData = data; this.mCar = car this.node.position = new Vec3(this.mData.x,this.mData.y) this.node.getComponent(Sprite).spriteFrame = box_sf this.buff.getComponent(Sprite).spriteFrame = buff_sf this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h) this.node.getComponent(BoxCollider2D).size = new Size(this.mData.w,this.mData.h) // this.node.getComponent(BoxCollider2D).offset.y = 40 this.collider.node.getComponent(UITransform).contentSize = new Size(this.mData.w,15) this.collider.node.getComponent(BoxCollider2D).size = new Size(this.mData.w,15) let sd = tools.getBoxRandom(this.mData.item_type) this.scores = Util.getRandomInt(sd.min,sd.max); this.updateScores() // this.node.getComponent(RigidBody2D).wakeUp() } protected ReSetNode(){ this.node.position = Vec3.ZERO this.scores = 0 this.isOnCollide = false this.node.getComponent(Sprite).spriteFrame = null this.buff.getComponent(Sprite).spriteFrame = null } public hurt(call,isKill=false){ if(!this.isOnCollide){ this.scheduleOnce(()=>{ this.isOnCollide = false; },this.CdTime) // director.once(Director.EVENT_AFTER_DRAW,()=>{ // this.isOnCollide = false; // }) this.isOnCollide = true; let isUseBuff:boolean = this.mData.buff_type!=-1 if(this.scores<=1||isKill){ this.removeSelf() isUseBuff = true }else{ if(isUseBuff){ isUseBuff = false } } if(call!=null){ if(isUseBuff){ call(this.mData,isKill,this.scores) }else{ call() } } if(!isUseBuff){ this.scores -= 1; this.updateScores() } } } public removeSelf(){ let self = this; self.node.active = false self.buff.getComponent(Sprite).spriteFrame = null // self.node.getComponent(RigidBody2D).sleep() self.node.position = Vec3.ZERO self.mCar.getGame().removeBoxForList(self.node) director.once(Director.EVENT_AFTER_DRAW,()=>{ self.mCar.getGame().getPoolManager().putBox(self.node) }) } private updateScores(){ this.lab_scores.getComponent(Label).string = this.scores+"" } }