BulletTrailController.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { _decorator, Component, Node, MotionStreak, Color, Texture2D } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * 子弹拖尾控制器
  5. * 负责管理子弹的拖尾效果
  6. */
  7. @ccclass('BulletTrailController')
  8. export class BulletTrailController extends Component {
  9. @property({
  10. type: MotionStreak,
  11. tooltip: '拖尾组件引用'
  12. })
  13. public motionStreak: MotionStreak | null = null;
  14. // 拖尾配置
  15. private trailConfig = {
  16. fadeTime: 0.5, // 拖尾消失时间
  17. minSeg: 1, // 最小分段
  18. stroke: 30, // 拖尾宽度
  19. fastMode: false // 快速模式
  20. };
  21. // 预设颜色
  22. private trailColors = {
  23. default: new Color(255, 255, 0, 255), // 黄色
  24. white: new Color(255, 255, 255, 255), // 白色
  25. fire: new Color(255, 100, 0, 255), // 橙红色
  26. ice: new Color(0, 200, 255, 255), // 冰蓝色
  27. poison: new Color(100, 255, 0, 255), // 毒绿色
  28. electric: new Color(200, 0, 255, 255) // 紫色
  29. };
  30. onLoad() {
  31. this.initializeTrail();
  32. }
  33. /**
  34. * 初始化拖尾效果
  35. */
  36. private initializeTrail() {
  37. if (!this.motionStreak) {
  38. // 尝试从子节点中查找MotionStreak组件
  39. this.motionStreak = this.getComponentInChildren(MotionStreak);
  40. if (!this.motionStreak) {
  41. // 尝试从当前节点获取
  42. this.motionStreak = this.getComponent(MotionStreak);
  43. }
  44. }
  45. if (this.motionStreak) {
  46. this.applyTrailConfig();
  47. }
  48. }
  49. /**
  50. * 应用拖尾配置
  51. */
  52. private applyTrailConfig() {
  53. if (!this.motionStreak) return;
  54. this.motionStreak.fadeTime = this.trailConfig.fadeTime;
  55. this.motionStreak.minSeg = this.trailConfig.minSeg;
  56. this.motionStreak.stroke = this.trailConfig.stroke;
  57. this.motionStreak.fastMode = this.trailConfig.fastMode;
  58. this.motionStreak.color = this.trailColors.white;
  59. }
  60. /**
  61. * 设置拖尾颜色
  62. * @param colorType 颜色类型
  63. */
  64. public setTrailColor(colorType: 'default' | 'white' | 'fire' | 'ice' | 'poison' | 'electric') {
  65. if (!this.motionStreak) return;
  66. const color = this.trailColors[colorType];
  67. if (color) {
  68. this.motionStreak.color = color;
  69. }
  70. }
  71. /**
  72. * 设置自定义拖尾颜色
  73. * @param color 颜色
  74. */
  75. public setCustomTrailColor(color: Color) {
  76. if (!this.motionStreak) return;
  77. this.motionStreak.color = color;
  78. }
  79. /**
  80. * 设置拖尾宽度
  81. * @param width 宽度
  82. */
  83. public setTrailWidth(width: number) {
  84. if (!this.motionStreak) return;
  85. this.trailConfig.stroke = width;
  86. this.motionStreak.stroke = width;
  87. }
  88. /**
  89. * 设置拖尾消失时间
  90. * @param fadeTime 消失时间
  91. */
  92. public setTrailFadeTime(fadeTime: number) {
  93. if (!this.motionStreak) return;
  94. this.trailConfig.fadeTime = fadeTime;
  95. this.motionStreak.fadeTime = fadeTime;
  96. }
  97. /**
  98. * 设置拖尾纹理
  99. * @param texture 纹理
  100. */
  101. public setTrailTexture(texture: Texture2D) {
  102. if (!this.motionStreak) return;
  103. this.motionStreak.texture = texture;
  104. }
  105. /**
  106. * 启用拖尾效果
  107. */
  108. public enableTrail() {
  109. if (this.motionStreak) {
  110. this.motionStreak.enabled = true;
  111. }
  112. }
  113. /**
  114. * 禁用拖尾效果
  115. */
  116. public disableTrail() {
  117. if (this.motionStreak) {
  118. this.motionStreak.enabled = false;
  119. }
  120. }
  121. /**
  122. * 重置拖尾效果
  123. */
  124. public resetTrail() {
  125. if (this.motionStreak) {
  126. this.motionStreak.reset();
  127. }
  128. }
  129. onDestroy() {
  130. // 清理资源
  131. this.motionStreak = null;
  132. }
  133. }