BlockInfo.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { _decorator, Component, Node, Label, Sprite, SpriteFrame, Color, Vec2, Enum } from 'cc';
  2. import { WeaponConfig } from '../../Core/ConfigManager';
  3. // 导入cc命名空间以支持旧版本API
  4. const cc = { Color, Vec2, Enum };
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * 方块信息组件
  8. * 用于在属性检查器中显示和管理方块的各种信息
  9. * 包括稀有度、价格、标签、武器配置等
  10. */
  11. @ccclass('BlockInfo')
  12. export class BlockInfo extends Component {
  13. // 方块基础信息
  14. @property({
  15. displayName: '方块名称',
  16. tooltip: '方块的显示名称'
  17. })
  18. public blockName: string = '';
  19. @property({
  20. displayName: '方块ID',
  21. tooltip: '方块的唯一标识符'
  22. })
  23. public blockId: string = '';
  24. // 稀有度相关
  25. @property({
  26. type: cc.Enum({
  27. common: 0,
  28. uncommon: 1,
  29. rare: 2,
  30. epic: 3
  31. }),
  32. displayName: '稀有度',
  33. tooltip: '方块的稀有度等级'
  34. })
  35. public rarity: number = 0; // 0: common, 1: uncommon, 2: rare, 3: epic
  36. @property({
  37. displayName: '稀有度颜色',
  38. tooltip: '稀有度对应的颜色'
  39. })
  40. public rarityColor: Color = Color.WHITE;
  41. // 武器相关
  42. @property({
  43. displayName: '武器类型',
  44. tooltip: '方块对应的武器类型'
  45. })
  46. public weaponType: string = '';
  47. @property({
  48. displayName: '武器ID',
  49. tooltip: '武器的唯一标识符'
  50. })
  51. public weaponId: string = '';
  52. @property({
  53. displayName: '伤害值',
  54. tooltip: '武器的伤害值'
  55. })
  56. public damage: number = 0;
  57. @property({
  58. displayName: '攻击范围',
  59. tooltip: '武器的攻击范围'
  60. })
  61. public range: number = 0;
  62. @property({
  63. displayName: '攻击速度',
  64. tooltip: '武器的攻击速度'
  65. })
  66. public attackSpeed: number = 1.0;
  67. // 方块标签
  68. @property({
  69. displayName: '方块标签',
  70. tooltip: '方块的标签列表'
  71. })
  72. public blockTags: string[] = [];
  73. // 形状相关
  74. @property({
  75. displayName: '形状ID',
  76. tooltip: '方块的形状标识符',
  77. readonly: true
  78. })
  79. public shapeId: string = '';
  80. @property({
  81. displayName: '形状价格',
  82. tooltip: '从武器配置中读取的形状对应价格',
  83. readonly: true
  84. })
  85. public shapePrice: number = 0;
  86. @property({
  87. displayName: '形状大小',
  88. tooltip: '方块形状的尺寸(宽x高)'
  89. })
  90. public shapeSize: Vec2 = new Vec2(1, 1);
  91. // 位置信息
  92. @property({
  93. displayName: '所在区域',
  94. tooltip: '方块当前所在的区域(kuang/block1/block2/block3/grid)'
  95. })
  96. public currentLocation: string = 'kuang';
  97. @property({
  98. displayName: '网格位置',
  99. tooltip: '方块在网格中的位置'
  100. })
  101. public gridPosition: Vec2 = new Vec2(-1, -1);
  102. // 状态信息
  103. @property({
  104. displayName: '是否已放置',
  105. tooltip: '方块是否已经放置在网格中'
  106. })
  107. public isPlaced: boolean = false;
  108. @property({
  109. displayName: '是否可拖拽',
  110. tooltip: '方块是否可以被拖拽'
  111. })
  112. public isDraggable: boolean = true;
  113. @property({
  114. displayName: '是否可合并',
  115. tooltip: '方块是否可以与其他方块合并'
  116. })
  117. public isMergeable: boolean = true;
  118. // 引用节点
  119. @property({
  120. type: Sprite,
  121. displayName: '稀有度图标',
  122. tooltip: '显示稀有度的Sprite组件'
  123. })
  124. public raritySprite: Sprite = null;
  125. @property({
  126. type: Sprite,
  127. displayName: '武器图标',
  128. tooltip: '显示武器的Sprite组件'
  129. })
  130. public weaponSprite: Sprite = null;
  131. @property({
  132. type: [SpriteFrame],
  133. displayName: '稀有度图标数组',
  134. tooltip: '按稀有度顺序排列的图标数组:0-common, 1-uncommon, 2-rare, 3-epic'
  135. })
  136. public rarityIcons: SpriteFrame[] = [];
  137. // 私有属性:武器配置
  138. private weaponConfig: WeaponConfig = null;
  139. /**
  140. * 获取稀有度名称
  141. */
  142. public getRarityName(): string {
  143. const rarityNames = ['common', 'uncommon', 'rare', 'epic'];
  144. return rarityNames[this.rarity] || 'common';
  145. }
  146. /**
  147. * 获取当前稀有度对应的图标
  148. */
  149. public getRarityIcon(): SpriteFrame | null {
  150. if (this.rarityIcons && this.rarityIcons.length > this.rarity && this.rarity >= 0) {
  151. return this.rarityIcons[this.rarity];
  152. }
  153. return null;
  154. }
  155. /**
  156. * 设置武器配置
  157. */
  158. public setWeaponConfig(config: WeaponConfig): void {
  159. this.weaponConfig = config;
  160. if (config) {
  161. this.weaponType = config.name;
  162. this.weaponId = config.id || config.name;
  163. this.damage = config.stats?.damage || 0;
  164. this.range = config.stats?.range || 0;
  165. this.attackSpeed = config.stats?.fireRate || 1.0;
  166. // 如果配置中有稀有度信息,更新稀有度
  167. if (config.rarity) {
  168. const rarityMap = { 'common': 0, 'uncommon': 1, 'rare': 2, 'epic': 3 };
  169. this.rarity = rarityMap[config.rarity] || 0;
  170. }
  171. // 设置形状价格
  172. if (config.inGameCostConfig && config.inGameCostConfig.shapeCosts && this.shapeId) {
  173. this.shapePrice = config.inGameCostConfig.shapeCosts[this.shapeId] || 0;
  174. }
  175. }
  176. }
  177. /**
  178. * 获取武器配置
  179. */
  180. public getWeaponConfig(): WeaponConfig {
  181. return this.weaponConfig;
  182. }
  183. /**
  184. * 更新价格显示
  185. */
  186. public updatePriceDisplay(): void {
  187. // 价格显示功能已移除
  188. }
  189. /**
  190. * 更新稀有度显示
  191. */
  192. public updateRarityDisplay(): void {
  193. if (this.raritySprite) {
  194. // 根据稀有度设置颜色
  195. this.raritySprite.color = this.rarityColor;
  196. }
  197. }
  198. /**
  199. * 设置方块标签
  200. */
  201. public setBlockTags(tags: string[]): void {
  202. this.blockTags = [...tags];
  203. }
  204. /**
  205. * 添加方块标签
  206. */
  207. public addBlockTag(tag: string): void {
  208. if (this.blockTags.indexOf(tag) === -1) {
  209. this.blockTags.push(tag);
  210. }
  211. }
  212. /**
  213. * 移除方块标签
  214. */
  215. public removeBlockTag(tag: string): void {
  216. const index = this.blockTags.indexOf(tag);
  217. if (index > -1) {
  218. this.blockTags.splice(index, 1);
  219. }
  220. }
  221. /**
  222. * 检查是否有指定标签
  223. */
  224. public hasTag(tag: string): boolean {
  225. return this.blockTags.indexOf(tag) !== -1;
  226. }
  227. /**
  228. * 更新网格位置
  229. */
  230. public updateGridPosition(row: number, col: number): void {
  231. this.gridPosition.set(col, row);
  232. }
  233. /**
  234. * 设置放置状态
  235. */
  236. public setPlacedState(placed: boolean, location: string = ''): void {
  237. this.isPlaced = placed;
  238. if (location) {
  239. this.currentLocation = location;
  240. }
  241. }
  242. /**
  243. * 根据稀有度计算价格
  244. */
  245. /**
  246. * 获取方块信息摘要
  247. */
  248. public getBlockSummary(): string {
  249. return `${this.blockName} (${this.getRarityName()}) - 0金币 - ${this.weaponType}`;
  250. }
  251. start() {
  252. // 初始化时更新显示
  253. this.updatePriceDisplay();
  254. this.updateRarityDisplay();
  255. }
  256. /**
  257. * 在编辑器中更新属性时调用
  258. */
  259. onLoad() {
  260. // 初始化组件
  261. }
  262. }