import { _decorator, Animation, AnimationClip, BoxCollider2D, Collider2D, Component, Contact2DType, Node, PhysicsSystem2D, SpriteFrame, UITransform } from 'cc'; import { Tools } from '../../Tools/Tools'; import { Role } from '../Role'; import { LifeBar } from './LifeBar'; const { ccclass, property } = _decorator; @ccclass('Bullet') export class Bullet extends Component { explodeframes: SpriteFrame[] = []; bulletFrames: SpriteFrame[] = []; explodeAni: Animation = null; bulletAni: Animation = null; //是否爆炸 true -> 爆炸 isExplode: boolean = false; //子弹速度 bulletSpeed: number = 100; direction: number = 1; atkLength: number = null; atk: number = null; targetNode: Node = null; otherCollider: BoxCollider2D = null; //role: Role = null; protected onLoad(): void { const collider = this.getComponent(BoxCollider2D); if (collider) { collider.on(Contact2DType.BEGIN_CONTACT, this._onBeginContact, this) } //this.otherCollider = this.targetNode.getComponent(BoxCollider2D); //this.role = this.node.parent.getComponent(Role); } start() { this.explodeAni = Tools.createAnimation(this.explodeframes, 5, this.node, AnimationClip.WrapMode.Normal); this.bulletAni = Tools.createAnimation(this.bulletFrames, 5, this.node, AnimationClip.WrapMode.Loop); this.node.setPosition(0, 0, 0); this.setState(false); } setState(isExplode: boolean) { if (isExplode) { this.explodeAni.play(); // this.explodeAni.once(Animation.EventType.FINISHED, () => { // if(this.node.isValid){ // } // this.node.destroy() // }, // this.node) } else { this.bulletAni.play(); } } _onBeginContact(selfCollider: BoxCollider2D, otherCollider: BoxCollider2D) { //selfCollider = this.node.getComponent(BoxCollider2D); otherCollider = this.otherCollider; const lifeBar = this.targetNode.getComponent(LifeBar); lifeBar.updateProgressBar(lifeBar._curHp - this.atk); this.setState(true); } update(deltaTime: number) { this.move(deltaTime); //this.onBulletCollision(this.targetNode); //this.stop(this.atkLength); } move(dt: number) { let x = this.node.getWorldPosition().x; let y = this.node.getWorldPosition().y; let z = this.node.getWorldPosition().z; if (this.direction) { x = x + this.bulletSpeed * dt; } else { x = x - this.bulletSpeed * dt; } this.node.setWorldPosition(x, y, z) } stop(atkLength: number) { if (this.direction) { if (this.node.position.x >= atkLength) { this.setState(true); this.node.destroy(); } } else { if ((this.node.position.x + atkLength) <= 0) { console.log(this.node.position.x + atkLength) this.setState(true); this.node.destroy(); } } } // 处理子弹碰撞 onBulletCollision(targetNode: Node) { const boxBullet = this.node.getComponent(UITransform).getBoundingBoxToWorld(); const boxTarget = targetNode.getComponent(UITransform).getBoundingBoxToWorld(); if (boxTarget.containsRect(boxBullet)) { const targetLifeBar = targetNode.getComponent(LifeBar); targetLifeBar.updateProgressBar(targetLifeBar._totalHp - this.atk); this.setState(true); this.node.destroy(); } } //targetNode为目标节点,子弹需要攻击的对象 //如果子弹与targetNode碰撞,targetNode的hp(血量)将会减少子弹发射者的atk(攻击力); //同时子弹播放爆炸动画,播放结束后,子弹销毁 }