import { _decorator, Component, js,Node, PhysicsRayResult, Vec3 } from 'cc'; import { GunBase } from './GunBase'; import { PoolManager } from '../../core/manager/PoolManager'; import { Enemy } from '../../game/Enemy'; import { ResUtil } from '../../utils/ResUtil'; import { Game } from '../../game/Game'; const { ccclass, property } = _decorator; @ccclass('BulletBase') export class BulletBase extends Component { public isDead: boolean = false; //枪的数据 在gunBase中 public gunBase: GunBase = null!; //子弹自动回收的时间3s public recycleTime: number = 3; constructor(){ super(); this.isDead = false; } start() { this.scheduleOnce(this.autoRecycle.bind(this),this.recycleTime); } /** * 敌人流血特效 */ public enemyBlood(e:PhysicsRayResult){ ResUtil.playParticle( `effects/Prefabs/blood`, 1, new Vec3(0.02,0.02,0.02), (blood) => { blood.active = true; blood.setParent(e.collider.node.parent); blood.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01)); blood.forward = e.hitNormal.multiplyScalar(-1); } ); } /** * 加入射击中的效果 * @param e 碰撞到的结果 */ public addHitImpact(e:PhysicsRayResult) { ResUtil.loadRes("enemy/sundries/hit_impact", this.node) .then((impact: Node | null) => { if(impact){ impact.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01)); impact.forward = e.hitNormal.multiplyScalar(-1); impact.setParent(e.collider.node,true); } this.scheduleOnce(()=>{ if(impact.parent){ PoolManager.putNode(impact); } },3); }) .catch((error: Error) => { console.error("加载sundries/hit_impact异常", error); }); } /** * 计算子弹用时 */ public getBulletTime(): number{ let endWorldPos: Vec3 = Game.I.player.shootUI.getCrossHairPos(); const bulletPos: Vec3 = this.node.position.clone(); //计算3D空间距离 忽略Y轴 const horizontalDistance = Vec3.distance( new Vec3(bulletPos.x, 0, bulletPos.z), new Vec3(endWorldPos.x, 0, endWorldPos.z) ); //计算时间 const minTime: number = 0.25; const factor: number = 5; return Math.max(horizontalDistance / (this.gunBase.data.bulletSpeed * factor), minTime); } /** * 自动回收子弹 */ public autoRecycle(){ if(!this.isDead){ this.isDead = true; PoolManager.putNode(this.node); } } }