1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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);
- }
- /**
- * 加入射击中的效果
- * @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);
- }
- }
- }
|