WeaponBlockExample.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { _decorator, Component, Node, Prefab, instantiate, Vec3, Sprite, SpriteFrame, resources, Texture2D, Vec2, Size, Rect, Color } 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. return;
  27. }
  28. // 等待配置加载完成后创建方块
  29. this.scheduleOnce(() => {
  30. this.initializeBlocks();
  31. }, 0.5);
  32. }
  33. private initializeBlocks() {
  34. // 检查配置管理器是否已初始化
  35. if (!this.configManager) {
  36. return;
  37. }
  38. // 检查配置是否已加载完成
  39. if (!this.configManager.isConfigLoaded()) {
  40. // 如果配置尚未加载完成,延迟创建
  41. this.scheduleOnce(() => {
  42. this.initializeBlocks();
  43. }, 1.0);
  44. return;
  45. }
  46. // 检查方块预制体是否已设置
  47. if (!this.blockPrefab) {
  48. return;
  49. }
  50. // 创建5个不同稀有度的武器方块
  51. this.createWeaponBlock('common');
  52. this.createWeaponBlock('uncommon');
  53. this.createWeaponBlock('rare');
  54. this.createWeaponBlock('epic');
  55. this.createRandomWeaponBlock();
  56. }
  57. private createWeaponBlock(rarity: string) {
  58. // 获取指定稀有度的武器
  59. const weapons = this.configManager.getWeaponsByRarity(rarity);
  60. if (weapons.length === 0) {
  61. return;
  62. }
  63. // 随机选择一个武器
  64. const weapon = weapons[Math.floor(Math.random() * weapons.length)];
  65. this.createBlockWithWeapon(weapon);
  66. }
  67. private createRandomWeaponBlock() {
  68. // 获取随机武器
  69. const weapon = this.configManager.getRandomWeapon();
  70. if (!weapon) {
  71. return;
  72. }
  73. this.createBlockWithWeapon(weapon);
  74. }
  75. private createBlockWithWeapon(weaponConfig: any) {
  76. // 检查方块预制体
  77. if (!this.blockPrefab) {
  78. return;
  79. }
  80. // 实例化方块
  81. const blockNode = instantiate(this.blockPrefab);
  82. if (!blockNode) {
  83. return;
  84. }
  85. // 设置方块名称
  86. blockNode.name = `WeaponBlock_${weaponConfig.id}`;
  87. // 添加到场景中
  88. this.node.addChild(blockNode);
  89. // 设置方块位置(简单的网格布局)
  90. const index = this.node.children.length - 1;
  91. const x = (index % 5) * 120 - 240; // 5列布局,间距120
  92. const y = Math.floor(index / 5) * 120; // 行间距120
  93. blockNode.position = new Vec3(x, y, 0);
  94. // 设置武器配置到方块
  95. this.setupBlockWeapon(blockNode, weaponConfig);
  96. console.log(`生成武器方块: ${weaponConfig.name} (${weaponConfig.rarity})`);
  97. }
  98. // 设置方块的武器配置
  99. private setupBlockWeapon(blockNode: Node, weaponConfig: WeaponConfig) {
  100. // 设置方块的武器ID
  101. blockNode['weaponId'] = weaponConfig.id;
  102. // 设置方块的稀有度颜色
  103. this.setBlockRarityColor(blockNode, weaponConfig.rarity);
  104. // 加载武器图标
  105. this.loadWeaponIcon(blockNode, weaponConfig);
  106. }
  107. // 设置方块稀有度颜色
  108. private setBlockRarityColor(blockNode: Node, rarity: string) {
  109. const sprite = blockNode.getComponent(Sprite);
  110. if (!sprite) {
  111. return;
  112. }
  113. // 根据稀有度设置颜色
  114. let color: Color;
  115. switch (rarity) {
  116. case 'common':
  117. color = new Color(255, 255, 255); // 白色
  118. break;
  119. case 'uncommon':
  120. color = new Color(0, 255, 0); // 绿色
  121. break;
  122. case 'rare':
  123. color = new Color(0, 100, 255); // 蓝色
  124. break;
  125. case 'epic':
  126. color = new Color(160, 32, 240); // 紫色
  127. break;
  128. case 'legendary':
  129. color = new Color(255, 165, 0); // 橙色
  130. break;
  131. default:
  132. color = new Color(255, 255, 255); // 默认白色
  133. }
  134. sprite.color = color;
  135. }
  136. // 加载武器图标
  137. private loadWeaponIcon(blockNode: Node, weaponConfig: WeaponConfig) {
  138. // 这里应该根据weaponConfig加载对应的武器图标
  139. // 由于没有实际的图标资源,这里只是示例代码
  140. // 寻找方块中的图标节点
  141. const iconNode = blockNode.getChildByName('Icon');
  142. if (!iconNode) {
  143. return;
  144. }
  145. const iconSprite = iconNode.getComponent(Sprite);
  146. if (!iconSprite) {
  147. return;
  148. }
  149. // 根据武器类型加载不同的图标
  150. // 这里需要根据实际的资源路径来加载
  151. const iconPath = `images/PlantsSprite/${weaponConfig.id}`;
  152. resources.load(iconPath, SpriteFrame, (err, spriteFrame) => {
  153. if (err) {
  154. // 如果加载失败,使用默认图标或保持原样
  155. return;
  156. }
  157. if (iconSprite && spriteFrame) {
  158. iconSprite.spriteFrame = spriteFrame;
  159. }
  160. });
  161. }
  162. // 根据武器ID获取武器配置
  163. public getWeaponConfigById(weaponId: string): WeaponConfig | null {
  164. if (!this.configManager) {
  165. return null;
  166. }
  167. const weaponConfig = this.configManager.getWeaponById(weaponId);
  168. if (!weaponConfig) {
  169. return null;
  170. }
  171. return weaponConfig;
  172. }
  173. // 获取方块的武器配置(供其他脚本调用)
  174. public static getBlockWeaponConfig(blockNode: Node): WeaponConfig | null {
  175. return (blockNode as any).weaponConfig || null;
  176. }
  177. // 创建指定ID的武器方块
  178. public createWeaponBlockById(weaponId: string, position: Vec3): Node | null {
  179. const weaponConfig = this.configManager.getWeaponById(weaponId);
  180. if (!weaponConfig) {
  181. console.warn(`找不到ID为 ${weaponId} 的武器配置`);
  182. return null;
  183. }
  184. this.createBlockWithWeapon(weaponConfig);
  185. return null; // 实际项目中应该返回创建的节点
  186. }
  187. // 获取所有可用的武器ID列表
  188. public getAvailableWeaponIds(): string[] {
  189. if (!this.configManager || !this.configManager.isConfigLoaded()) {
  190. return [];
  191. }
  192. return this.configManager.getAllWeapons().map(weapon => weapon.id);
  193. }
  194. // 获取指定稀有度的武器数量
  195. public getWeaponCountByRarity(rarity: string): number {
  196. if (!this.configManager || !this.configManager.isConfigLoaded()) {
  197. return 0;
  198. }
  199. return this.configManager.getWeaponsByRarity(rarity).length;
  200. }
  201. }