| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import { _decorator, Component, Node, MotionStreak, Color, Texture2D } from 'cc';
- const { ccclass, property } = _decorator;
- /**
- * 子弹拖尾控制器
- * 负责管理子弹的拖尾效果
- */
- @ccclass('BulletTrailController')
- export class BulletTrailController extends Component {
-
- @property({
- type: MotionStreak,
- tooltip: '拖尾组件引用'
- })
- public motionStreak: MotionStreak | null = null;
-
- // 拖尾配置
- private trailConfig = {
- fadeTime: 0.5, // 拖尾消失时间
- minSeg: 1, // 最小分段
- stroke: 30, // 拖尾宽度
- fastMode: false // 快速模式
- };
-
- // 预设颜色
- private trailColors = {
- default: new Color(255, 255, 0, 255), // 黄色
- white: new Color(255, 255, 255, 255), // 白色
- fire: new Color(255, 100, 0, 255), // 橙红色
- ice: new Color(0, 200, 255, 255), // 冰蓝色
- poison: new Color(100, 255, 0, 255), // 毒绿色
- electric: new Color(200, 0, 255, 255) // 紫色
- };
-
- onLoad() {
- this.initializeTrail();
- }
-
- /**
- * 初始化拖尾效果
- */
- private initializeTrail() {
- if (!this.motionStreak) {
- // 尝试从子节点中查找MotionStreak组件
- this.motionStreak = this.getComponentInChildren(MotionStreak);
-
- if (!this.motionStreak) {
- // 尝试从当前节点获取
- this.motionStreak = this.getComponent(MotionStreak);
- }
- }
-
- if (this.motionStreak) {
- this.applyTrailConfig();
- }
- }
-
- /**
- * 应用拖尾配置
- */
- private applyTrailConfig() {
- if (!this.motionStreak) return;
-
- this.motionStreak.fadeTime = this.trailConfig.fadeTime;
- this.motionStreak.minSeg = this.trailConfig.minSeg;
- this.motionStreak.stroke = this.trailConfig.stroke;
- this.motionStreak.fastMode = this.trailConfig.fastMode;
- this.motionStreak.color = this.trailColors.white;
- }
-
- /**
- * 设置拖尾颜色
- * @param colorType 颜色类型
- */
- public setTrailColor(colorType: 'default' | 'white' | 'fire' | 'ice' | 'poison' | 'electric') {
- if (!this.motionStreak) return;
-
- const color = this.trailColors[colorType];
- if (color) {
- this.motionStreak.color = color;
- }
- }
-
- /**
- * 设置自定义拖尾颜色
- * @param color 颜色
- */
- public setCustomTrailColor(color: Color) {
- if (!this.motionStreak) return;
-
- this.motionStreak.color = color;
- }
-
- /**
- * 设置拖尾宽度
- * @param width 宽度
- */
- public setTrailWidth(width: number) {
- if (!this.motionStreak) return;
-
- this.trailConfig.stroke = width;
- this.motionStreak.stroke = width;
- }
-
- /**
- * 设置拖尾消失时间
- * @param fadeTime 消失时间
- */
- public setTrailFadeTime(fadeTime: number) {
- if (!this.motionStreak) return;
-
- this.trailConfig.fadeTime = fadeTime;
- this.motionStreak.fadeTime = fadeTime;
- }
-
- /**
- * 设置拖尾纹理
- * @param texture 纹理
- */
- public setTrailTexture(texture: Texture2D) {
- if (!this.motionStreak) return;
-
- this.motionStreak.texture = texture;
- }
-
- /**
- * 启用拖尾效果
- */
- public enableTrail() {
- if (this.motionStreak) {
- this.motionStreak.enabled = true;
- }
- }
-
- /**
- * 禁用拖尾效果
- */
- public disableTrail() {
- if (this.motionStreak) {
- this.motionStreak.enabled = false;
- }
- }
-
- /**
- * 重置拖尾效果
- */
- public resetTrail() {
- if (this.motionStreak) {
- this.motionStreak.reset();
- }
- }
- onDestroy() {
- // 清理资源
- this.motionStreak = null;
- }
- }
|