WeaponBlockExample.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { _decorator, Component, Node, Prefab, instantiate, Vec3, Sprite, SpriteFrame, resources } from 'cc';
  2. import { ConfigManager, WeaponConfig } from './ConfigManager';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 武器方块示例脚本
  6. * 展示如何使用ConfigManager来创建带有武器配置的方块
  7. */
  8. @ccclass('WeaponBlockExample')
  9. export class WeaponBlockExample extends Component {
  10. @property({
  11. type: Prefab,
  12. tooltip: '基础方块预制体'
  13. })
  14. public blockPrefab: Prefab = null;
  15. @property({
  16. type: Node,
  17. tooltip: '方块容器节点'
  18. })
  19. public blockContainer: Node = null;
  20. private configManager: ConfigManager = null;
  21. start() {
  22. console.log('WeaponBlockExample start()');
  23. // 获取配置管理器实例
  24. this.configManager = ConfigManager.getInstance();
  25. if (!this.configManager) {
  26. console.error('ConfigManager实例获取失败');
  27. return;
  28. }
  29. // 等待配置加载完成后创建示例方块
  30. this.scheduleOnce(() => {
  31. this.createExampleWeaponBlocks();
  32. }, 2.0); // 增加等待时间
  33. }
  34. // 创建示例武器方块
  35. private createExampleWeaponBlocks() {
  36. console.log('开始创建示例武器方块');
  37. if (!this.configManager) {
  38. console.error('配置管理器未初始化');
  39. return;
  40. }
  41. if (!this.configManager.isConfigLoaded()) {
  42. console.warn('配置尚未加载完成,延迟创建');
  43. this.scheduleOnce(() => {
  44. this.createExampleWeaponBlocks();
  45. }, 1.0);
  46. return;
  47. }
  48. // 检查必要的组件
  49. if (!this.blockPrefab) {
  50. console.error('方块预制体未设置,请在编辑器中设置blockPrefab');
  51. return;
  52. }
  53. console.log('开始创建武器方块...');
  54. // 创建几个不同稀有度的武器方块
  55. this.createWeaponBlock('common', new Vec3(-200, 0, 0));
  56. this.createWeaponBlock('uncommon', new Vec3(-100, 0, 0));
  57. this.createWeaponBlock('rare', new Vec3(0, 0, 0));
  58. this.createWeaponBlock('epic', new Vec3(100, 0, 0));
  59. // 创建随机武器方块
  60. this.createRandomWeaponBlock(new Vec3(200, 0, 0));
  61. }
  62. // 创建指定稀有度的武器方块
  63. private createWeaponBlock(rarity: string, position: Vec3) {
  64. const weaponConfig = this.configManager.getRandomWeapon(rarity);
  65. if (!weaponConfig) {
  66. console.warn(`无法获取稀有度为 ${rarity} 的武器配置`);
  67. return;
  68. }
  69. this.createBlockWithWeapon(weaponConfig, position);
  70. }
  71. // 创建随机武器方块
  72. private createRandomWeaponBlock(position: Vec3) {
  73. const weaponConfig = this.configManager.getRandomWeapon();
  74. if (!weaponConfig) {
  75. console.warn('无法获取随机武器配置');
  76. return;
  77. }
  78. this.createBlockWithWeapon(weaponConfig, position);
  79. }
  80. // 根据武器配置创建方块
  81. private createBlockWithWeapon(weaponConfig: WeaponConfig, position: Vec3) {
  82. if (!this.blockPrefab) {
  83. console.error('方块预制体未设置');
  84. return;
  85. }
  86. // 实例化方块
  87. const blockNode = instantiate(this.blockPrefab);
  88. if (!blockNode) {
  89. console.error('方块实例化失败');
  90. return;
  91. }
  92. // 设置位置
  93. blockNode.setPosition(position);
  94. // 添加到容器
  95. if (this.blockContainer) {
  96. this.blockContainer.addChild(blockNode);
  97. } else {
  98. this.node.addChild(blockNode);
  99. }
  100. // 设置方块的武器配置
  101. this.setupBlockWeapon(blockNode, weaponConfig);
  102. console.log(`创建武器方块: ${weaponConfig.name} (${weaponConfig.rarity})`);
  103. }
  104. // 设置方块的武器配置
  105. private setupBlockWeapon(blockNode: Node, weaponConfig: WeaponConfig) {
  106. // 暂时不添加WeaponComponent,避免组件重复问题
  107. // 直接在节点上存储武器配置数据
  108. (blockNode as any).weaponConfig = weaponConfig;
  109. // 设置方块外观
  110. this.setupBlockVisual(blockNode, weaponConfig);
  111. // 设置方块名称
  112. blockNode.name = `WeaponBlock_${weaponConfig.id}`;
  113. }
  114. // 设置方块外观
  115. private setupBlockVisual(blockNode: Node, weaponConfig: WeaponConfig) {
  116. const spriteComponent = blockNode.getComponent(Sprite);
  117. if (!spriteComponent) {
  118. console.warn('方块节点没有Sprite组件');
  119. return;
  120. }
  121. // 根据稀有度设置颜色
  122. const rarityColors = {
  123. 'common': { r: 255, g: 255, b: 255, a: 255 }, // 白色
  124. 'uncommon': { r: 0, g: 255, b: 0, a: 255 }, // 绿色
  125. 'rare': { r: 0, g: 100, b: 255, a: 255 }, // 蓝色
  126. 'epic': { r: 160, g: 32, b: 240, a: 255 } // 紫色
  127. };
  128. const color = rarityColors[weaponConfig.rarity] || rarityColors['common'];
  129. spriteComponent.color = spriteComponent.color.set(color.r, color.g, color.b, color.a);
  130. // 尝试加载武器图标
  131. this.loadWeaponSprite(spriteComponent, weaponConfig);
  132. }
  133. // 加载武器图标
  134. private loadWeaponSprite(spriteComponent: Sprite, weaponConfig: WeaponConfig) {
  135. // 默认使用1x1尺寸的图标
  136. const spritePath = weaponConfig.visualConfig.weaponSprites['1x1'];
  137. if (!spritePath) {
  138. console.warn(`武器 ${weaponConfig.name} 没有1x1尺寸的图标`);
  139. return;
  140. }
  141. // 加载图标资源
  142. resources.load(spritePath, SpriteFrame, (err, spriteFrame) => {
  143. if (err) {
  144. console.warn(`加载武器图标失败: ${spritePath}`, err);
  145. return;
  146. }
  147. spriteComponent.spriteFrame = spriteFrame;
  148. });
  149. }
  150. // 获取方块的武器配置(供其他脚本调用)
  151. public static getBlockWeaponConfig(blockNode: Node): WeaponConfig | null {
  152. return (blockNode as any).weaponConfig || null;
  153. }
  154. // 创建指定ID的武器方块
  155. public createWeaponBlockById(weaponId: string, position: Vec3): Node | null {
  156. const weaponConfig = this.configManager.getWeaponById(weaponId);
  157. if (!weaponConfig) {
  158. console.warn(`找不到ID为 ${weaponId} 的武器配置`);
  159. return null;
  160. }
  161. this.createBlockWithWeapon(weaponConfig, position);
  162. return null; // 实际项目中应该返回创建的节点
  163. }
  164. // 获取所有可用的武器ID列表
  165. public getAvailableWeaponIds(): string[] {
  166. if (!this.configManager || !this.configManager.isConfigLoaded()) {
  167. return [];
  168. }
  169. return this.configManager.getAllWeapons().map(weapon => weapon.id);
  170. }
  171. // 获取指定稀有度的武器数量
  172. public getWeaponCountByRarity(rarity: string): number {
  173. if (!this.configManager || !this.configManager.isConfigLoaded()) {
  174. return 0;
  175. }
  176. return this.configManager.getWeaponsByRarity(rarity).length;
  177. }
  178. }