TestPelletTrail.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { _decorator, Component, Node, Vec3, find, instantiate, Prefab, resources } from 'cc';
  2. import { WeaponBullet, BulletInitData } from '../CombatSystem/WeaponBullet';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 测试子弹尾迹效果的脚本
  6. */
  7. @ccclass('TestPelletTrail')
  8. export class TestPelletTrail extends Component {
  9. @property(Prefab)
  10. pelletPrefab: Prefab = null;
  11. private testInterval: number = 2; // 每2秒发射一次测试子弹
  12. private timer: number = 0;
  13. start() {
  14. // 如果没有设置预制体,尝试从resources加载
  15. if (!this.pelletPrefab) {
  16. this.loadPelletPrefab();
  17. }
  18. }
  19. update(deltaTime: number) {
  20. this.timer += deltaTime;
  21. if (this.timer >= this.testInterval && this.pelletPrefab) {
  22. this.timer = 0;
  23. this.fireTestBullet();
  24. }
  25. }
  26. private loadPelletPrefab() {
  27. resources.load('prefabs/Pellet', Prefab, (err, prefab) => {
  28. if (!err && prefab) {
  29. this.pelletPrefab = prefab;
  30. console.log('[TestPelletTrail] Pellet预制体加载成功');
  31. } else {
  32. console.warn('[TestPelletTrail] 无法加载Pellet预制体:', err);
  33. }
  34. });
  35. }
  36. private fireTestBullet() {
  37. if (!this.pelletPrefab) return;
  38. // 创建测试子弹初始化数据
  39. const initData: BulletInitData = {
  40. weaponId: 'pea_shooter', // 使用默认武器配置
  41. firePosition: new Vec3(0, 0, 0), // 从屏幕中心发射
  42. direction: new Vec3(1, 0.5, 0).normalize(), // 向右上方发射
  43. autoTarget: false
  44. };
  45. // 创建子弹实例
  46. const bulletNode = instantiate(this.pelletPrefab);
  47. // 添加到场景中
  48. const gameArea = find('Canvas/GameLevelUI/GameArea') || find('Canvas');
  49. if (gameArea) {
  50. gameArea.addChild(bulletNode);
  51. } else {
  52. this.node.addChild(bulletNode);
  53. }
  54. // 初始化子弹
  55. const weaponBullet = bulletNode.getComponent(WeaponBullet) || bulletNode.addComponent(WeaponBullet);
  56. weaponBullet.init(initData);
  57. console.log('[TestPelletTrail] 发射测试子弹,带白色尾迹效果');
  58. }
  59. /**
  60. * 手动触发测试子弹发射(可在编辑器中调用)
  61. */
  62. public fireTestBulletManual() {
  63. this.fireTestBullet();
  64. }
  65. }