WeaponBlockExample.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { _decorator, Component, Node, Prefab, instantiate, Vec3, Sprite, SpriteFrame, resources, Texture2D, Vec2, Size, Rect } from 'cc';
  2. import { ConfigManager, WeaponConfig } from '../Core/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. if (!this.configManager) {
  37. console.error('配置管理器未初始化');
  38. return;
  39. }
  40. if (!this.configManager.isConfigLoaded()) {
  41. console.warn('配置尚未加载完成,延迟创建');
  42. this.scheduleOnce(() => {
  43. this.createExampleWeaponBlocks();
  44. }, 1.0);
  45. return;
  46. }
  47. // 检查必要的组件
  48. if (!this.blockPrefab) {
  49. console.error('方块预制体未设置,请在编辑器中设置blockPrefab');
  50. return;
  51. }
  52. // 创建几个不同稀有度的武器方块
  53. this.createWeaponBlock('common', new Vec3(-200, 0, 0));
  54. this.createWeaponBlock('uncommon', new Vec3(-100, 0, 0));
  55. this.createWeaponBlock('rare', new Vec3(0, 0, 0));
  56. this.createWeaponBlock('epic', new Vec3(100, 0, 0));
  57. // 创建随机武器方块
  58. this.createRandomWeaponBlock(new Vec3(200, 0, 0));
  59. }
  60. // 创建指定稀有度的武器方块
  61. private createWeaponBlock(rarity: string, position: Vec3) {
  62. const weaponConfig = this.configManager.getRandomWeapon(rarity);
  63. if (!weaponConfig) {
  64. console.warn(`无法获取稀有度为 ${rarity} 的武器配置`);
  65. return;
  66. }
  67. this.createBlockWithWeapon(weaponConfig, position);
  68. }
  69. // 创建随机武器方块
  70. private createRandomWeaponBlock(position: Vec3) {
  71. const weaponConfig = this.configManager.getRandomWeapon();
  72. if (!weaponConfig) {
  73. console.warn('无法获取随机武器配置');
  74. return;
  75. }
  76. this.createBlockWithWeapon(weaponConfig, position);
  77. }
  78. // 根据武器配置创建方块
  79. private createBlockWithWeapon(weaponConfig: WeaponConfig, position: Vec3) {
  80. if (!this.blockPrefab) {
  81. console.error('方块预制体未设置');
  82. return;
  83. }
  84. // 实例化方块
  85. const blockNode = instantiate(this.blockPrefab);
  86. if (!blockNode) {
  87. console.error('方块实例化失败');
  88. return;
  89. }
  90. // 设置位置
  91. blockNode.setPosition(position);
  92. // 添加到容器
  93. if (this.blockContainer) {
  94. this.blockContainer.addChild(blockNode);
  95. } else {
  96. this.node.addChild(blockNode);
  97. }
  98. // 设置方块的武器配置
  99. this.setupBlockWeapon(blockNode, weaponConfig);
  100. }
  101. // 设置方块的武器配置
  102. private setupBlockWeapon(blockNode: Node, weaponConfig: WeaponConfig) {
  103. // 暂时不添加WeaponComponent,避免组件重复问题
  104. // 直接在节点上存储武器配置数据
  105. (blockNode as any).weaponConfig = weaponConfig;
  106. // 设置方块外观
  107. this.setupBlockVisual(blockNode, weaponConfig);
  108. // 设置方块名称
  109. blockNode.name = `WeaponBlock_${weaponConfig.id}`;
  110. }
  111. // 设置方块外观
  112. private setupBlockVisual(blockNode: Node, weaponConfig: WeaponConfig) {
  113. const spriteComponent = blockNode.getComponent(Sprite);
  114. if (!spriteComponent) {
  115. console.warn('方块节点没有Sprite组件');
  116. return;
  117. }
  118. // 根据稀有度设置颜色
  119. const rarityColors = {
  120. 'common': { r: 200, g: 200, b: 200, a: 255 }, // 灰色
  121. 'uncommon': { r: 0, g: 255, b: 0, a: 255 }, // 绿色
  122. 'rare': { r: 0, g: 100, b: 255, a: 255 }, // 蓝色
  123. 'epic': { r: 160, g: 32, b: 240, a: 255 } // 紫色
  124. };
  125. const color = rarityColors[weaponConfig.rarity] || rarityColors['common'];
  126. spriteComponent.color = spriteComponent.color.set(color.r, color.g, color.b, color.a);
  127. // 添加武器名称标识到节点名称中
  128. blockNode.name = `WeaponBlock_${weaponConfig.id}_${weaponConfig.name}`;
  129. // 查找Weapon子节点并加载武器图标
  130. this.loadWeaponSpriteToWeaponNode(blockNode, weaponConfig);
  131. }
  132. // 将武器图标加载到Weapon子节点
  133. private loadWeaponSpriteToWeaponNode(blockNode: Node, weaponConfig: WeaponConfig) {
  134. // 查找B1子节点
  135. const b1Node = blockNode.getChildByName('B1');
  136. if (!b1Node) {
  137. return;
  138. }
  139. // 在B1子节点下查找Weapon子节点
  140. const weaponNode = b1Node.getChildByName('Weapon');
  141. if (!weaponNode) {
  142. return;
  143. }
  144. const weaponSprite = weaponNode.getComponent(Sprite);
  145. if (!weaponSprite) {
  146. return;
  147. }
  148. // 加载武器图标
  149. this.loadWeaponSprite(weaponSprite, weaponConfig);
  150. }
  151. // 加载武器图标
  152. private loadWeaponSprite(spriteComponent: Sprite, weaponConfig: WeaponConfig) {
  153. // 默认使用1x1尺寸的图标
  154. const spritePath = weaponConfig.visualConfig.weaponSprites['1x1'];
  155. if (!spritePath) {
  156. return;
  157. }
  158. // 从路径中提取文件名
  159. const fileName = spritePath.replace('images/PlantsSprite/', '');
  160. // 通过loadDir获取资源并直接分配
  161. resources.loadDir('images/PlantsSprite', (err, assets) => {
  162. if (err) return;
  163. const spriteFrames = assets.filter(asset => asset instanceof SpriteFrame);
  164. const matchingAsset = spriteFrames.find(asset => asset.name === fileName);
  165. if (matchingAsset) {
  166. spriteComponent.spriteFrame = matchingAsset;
  167. }
  168. });
  169. }
  170. // 获取方块的武器配置(供其他脚本调用)
  171. public static getBlockWeaponConfig(blockNode: Node): WeaponConfig | null {
  172. return (blockNode as any).weaponConfig || null;
  173. }
  174. // 创建指定ID的武器方块
  175. public createWeaponBlockById(weaponId: string, position: Vec3): Node | null {
  176. const weaponConfig = this.configManager.getWeaponById(weaponId);
  177. if (!weaponConfig) {
  178. console.warn(`找不到ID为 ${weaponId} 的武器配置`);
  179. return null;
  180. }
  181. this.createBlockWithWeapon(weaponConfig, position);
  182. return null; // 实际项目中应该返回创建的节点
  183. }
  184. // 获取所有可用的武器ID列表
  185. public getAvailableWeaponIds(): string[] {
  186. if (!this.configManager || !this.configManager.isConfigLoaded()) {
  187. return [];
  188. }
  189. return this.configManager.getAllWeapons().map(weapon => weapon.id);
  190. }
  191. // 获取指定稀有度的武器数量
  192. public getWeaponCountByRarity(rarity: string): number {
  193. if (!this.configManager || !this.configManager.isConfigLoaded()) {
  194. return 0;
  195. }
  196. return this.configManager.getWeaponsByRarity(rarity).length;
  197. }
  198. }