BulletBase.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { _decorator, Component, js,Node, PhysicsRayResult } from 'cc';
  2. import { GunBase } from './GunBase';
  3. import { PoolManager } from '../../core/manager/PoolManager';
  4. import { Enemy } from '../../game/Enemy';
  5. import { ResUtil } from '../../utils/ResUtil';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('BulletBase')
  8. export class BulletBase extends Component {
  9. public isDead: boolean = false;
  10. //枪的数据 在gunBase中
  11. public gunBase: GunBase = null!;
  12. //子弹自动回收的时间3s
  13. public recycleTime: number = 3;
  14. constructor(){
  15. super();
  16. this.isDead = false;
  17. }
  18. start() {
  19. this.scheduleOnce(this.autoRecycle.bind(this),this.recycleTime);
  20. }
  21. /**
  22. * 加入射击中的效果
  23. * @param e 碰撞到的结果
  24. */
  25. public addHitImpact(e:PhysicsRayResult) {
  26. ResUtil.loadRes("enemy/sundries/hit_impact", this.node)
  27. .then((impact: Node | null) => {
  28. if(impact){
  29. impact.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01));
  30. impact.forward = e.hitNormal.multiplyScalar(-1);
  31. impact.setParent(e.collider.node,true);
  32. }
  33. this.scheduleOnce(()=>{
  34. if(impact.parent){
  35. PoolManager.putNode(impact);
  36. }
  37. },3);
  38. })
  39. .catch((error: Error) => {
  40. console.error("加载sundries/hit_impact异常", error);
  41. });
  42. }
  43. /**
  44. * 自动回收子弹
  45. */
  46. public autoRecycle(){
  47. if(!this.isDead){
  48. this.isDead = true;
  49. PoolManager.putNode(this.node);
  50. }
  51. }
  52. }