import { _decorator, Component, Node, Vec3, find, instantiate, Prefab, resources } from 'cc'; import { WeaponBullet, BulletInitData } from '../CombatSystem/WeaponBullet'; const { ccclass, property } = _decorator; /** * 测试子弹尾迹效果的脚本 */ @ccclass('TestPelletTrail') export class TestPelletTrail extends Component { @property(Prefab) pelletPrefab: Prefab = null; private testInterval: number = 2; // 每2秒发射一次测试子弹 private timer: number = 0; start() { // 如果没有设置预制体,尝试从resources加载 if (!this.pelletPrefab) { this.loadPelletPrefab(); } } update(deltaTime: number) { this.timer += deltaTime; if (this.timer >= this.testInterval && this.pelletPrefab) { this.timer = 0; this.fireTestBullet(); } } private loadPelletPrefab() { resources.load('prefabs/Pellet', Prefab, (err, prefab) => { if (!err && prefab) { this.pelletPrefab = prefab; console.log('[TestPelletTrail] Pellet预制体加载成功'); } else { console.warn('[TestPelletTrail] 无法加载Pellet预制体:', err); } }); } private fireTestBullet() { if (!this.pelletPrefab) return; // 创建测试子弹初始化数据 const initData: BulletInitData = { weaponId: 'pea_shooter', // 使用默认武器配置 firePosition: new Vec3(0, 0, 0), // 从屏幕中心发射 direction: new Vec3(1, 0.5, 0).normalize(), // 向右上方发射 autoTarget: false }; // 创建子弹实例 const bulletNode = instantiate(this.pelletPrefab); // 添加到场景中 const gameArea = find('Canvas/GameLevelUI/GameArea') || find('Canvas'); if (gameArea) { gameArea.addChild(bulletNode); } else { this.node.addChild(bulletNode); } // 初始化子弹 const weaponBullet = bulletNode.getComponent(WeaponBullet) || bulletNode.addComponent(WeaponBullet); weaponBullet.init(initData); console.log('[TestPelletTrail] 发射测试子弹,带白色尾迹效果'); } /** * 手动触发测试子弹发射(可在编辑器中调用) */ public fireTestBulletManual() { this.fireTestBullet(); } }