import Hero from "./Hero"; import MonsterHit from "./MonsterHit"; import Weapon from "./Weapon"; const {ccclass, property} = cc._decorator; @ccclass export default class HeroHit extends cc.Component { private hero: Hero = null; public get Hero(): Hero { return this.hero; } private _material; public get WPos(){ return this.hero.WPos; } onLoad () { this.hero = this.node.parent.getComponent(Hero); // 获取材质 if (this.node.getComponent(cc.Sprite)) { this._material = this.node.getComponent(cc.Sprite).getMaterial(0); } // 设置材质对应的属性 this._material.setProperty("uSize", this.realVal); let cols = this.getComponents(cc.BoxCollider); for (let i = 0; i < cols.length; i++) { const element = cols[i]; if (element.tag == 2) { this.hurtCollider = element; } } Ticker.register(this.tickFun,this); } // 碰撞回调 onCollisionEnter(other:cc.BoxCollider, self:cc.BoxCollider){ if(self.tag == 1 && other.node.group == 'enemy' && other.tag == 2) {//击中怪物 if (self.size.width ==0 || self.size.height ==0 ) { return;} // if (this.hero.bigWeaponAtk()) { // } } else if(self.tag == 2 && other.node.group == 'enemy' && other.tag == 1) {//受击 if (other.size.width ==0 || other.size.height ==0 ) { return;} let enemyHit = other.node.getComponent(MonsterHit); let eWPosX = enemyHit.WPos.x; let hWposX = this.WPos.x; this.onHurt(eWPosX < hWposX); } } public onHurt(isEnemyInleft: boolean,weapon:Weapon = null) { if (this.hero.OnHurt(isEnemyInleft,weapon)) { this.realVal = 0; this.blinkCount = 2; this.isHurt = true; } } private isHurt:boolean = false; private realVal:number = 0; private maxVal:number = 5; private dir:number = 10; private blinkCount:number; tickFun(dt) { if (!this.isHurt) {return;} this.realVal+=dt * this.dir; if (this.realVal >= this.maxVal || this.realVal <= 0) { this.dir = -this.dir; if (this.blinkCount-- <= 0) { this.realVal = 0; this.isHurt = false; } } this._material.setProperty("uSize", this.realVal); } private hurtCollider:cc.BoxCollider; public dunCollider(){ if (this.hurtCollider) { this.hurtCollider.size.height = 20; this.hurtCollider.offset.y = this.hurtCollider.size.height*0.5; } } public standCollider(){ if (this.hurtCollider) { this.hurtCollider.size.height = 140; this.hurtCollider.offset.y = this.hurtCollider.size.height*0.5; } } public jumpCollider(){ if (this.hurtCollider) { this.hurtCollider.size.height = 60; this.hurtCollider.offset.y =180;// this.hurtCollider.size.height; } } public superSkillCollider(){ if (this.hurtCollider) { this.hurtCollider.size.height = 0; this.hurtCollider.offset.y = 0; } } protected onDestroy(): void { Ticker.unregister(this.tickFun,this); } }