MainUIController.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import { _decorator, Component, Node, Label, Button, find } from 'cc';
  2. import { SaveDataManager } from './SaveDataManager';
  3. import { GameManager } from './GameManager';
  4. const { ccclass, property } = _decorator;
  5. /**
  6. * 主界面控制器
  7. * 负责显示玩家信息和处理主界面交互
  8. */
  9. @ccclass('MainUIController')
  10. export class MainUIController extends Component {
  11. @property({
  12. type: Node,
  13. tooltip: '玩家等级显示节点'
  14. })
  15. public playerLevelLabel: Node = null;
  16. @property({
  17. type: Node,
  18. tooltip: '当前关卡显示节点'
  19. })
  20. public currentLevelLabel: Node = null;
  21. @property({
  22. type: Node,
  23. tooltip: '金币显示节点'
  24. })
  25. public coinsLabel: Node = null;
  26. @property({
  27. type: Node,
  28. tooltip: '钻石显示节点'
  29. })
  30. public diamondsLabel: Node = null;
  31. @property({
  32. type: Node,
  33. tooltip: '宝石显示节点'
  34. })
  35. public gemsLabel: Node = null;
  36. @property({
  37. type: Node,
  38. tooltip: '战斗按钮节点'
  39. })
  40. public battleButton: Node = null;
  41. @property({
  42. type: Node,
  43. tooltip: '商店按钮节点'
  44. })
  45. public shopButton: Node = null;
  46. @property({
  47. type: Node,
  48. tooltip: '设置按钮节点'
  49. })
  50. public settingsButton: Node = null;
  51. private saveDataManager: SaveDataManager = null;
  52. start() {
  53. // 初始化存档管理器
  54. this.saveDataManager = SaveDataManager.getInstance();
  55. this.saveDataManager.initialize();
  56. // 自动查找UI节点(如果没有手动拖拽设置)
  57. this.findUINodes();
  58. // 更新UI显示
  59. this.updateUI();
  60. // 设置按钮事件
  61. this.setupButtons();
  62. }
  63. /**
  64. * 自动查找UI节点
  65. */
  66. private findUINodes() {
  67. if (!this.playerLevelLabel) {
  68. this.playerLevelLabel = find('Canvas/MainUI/PlayerInfo/LevelLabel');
  69. }
  70. if (!this.currentLevelLabel) {
  71. this.currentLevelLabel = find('Canvas/MainUI/LevelInfo/CurrentLevelLabel');
  72. }
  73. if (!this.coinsLabel) {
  74. this.coinsLabel = find('Canvas/MainUI/Currency/CoinsLabel');
  75. }
  76. if (!this.diamondsLabel) {
  77. this.diamondsLabel = find('Canvas/MainUI/Currency/DiamondsLabel');
  78. }
  79. if (!this.gemsLabel) {
  80. this.gemsLabel = find('Canvas/MainUI/Currency/GemsLabel');
  81. }
  82. if (!this.battleButton) {
  83. this.battleButton = find('Canvas/MainUI/BattleButtonNode');
  84. }
  85. if (!this.shopButton) {
  86. this.shopButton = find('Canvas/MainUI/ShopButton');
  87. }
  88. if (!this.settingsButton) {
  89. this.settingsButton = find('Canvas/MainUI/SettingsButton');
  90. }
  91. }
  92. /**
  93. * 更新UI显示
  94. */
  95. public updateUI() {
  96. if (!this.saveDataManager) return;
  97. // 更新玩家等级显示
  98. if (this.playerLevelLabel) {
  99. const label = this.playerLevelLabel.getComponent(Label);
  100. if (label) {
  101. const playerLevel = this.saveDataManager.getPlayerLevel();
  102. label.string = `Lv.${playerLevel}`;
  103. }
  104. }
  105. // 更新当前关卡显示
  106. if (this.currentLevelLabel) {
  107. const label = this.currentLevelLabel.getComponent(Label);
  108. if (label) {
  109. const currentLevel = this.saveDataManager.getCurrentLevel();
  110. const maxUnlocked = this.saveDataManager.getMaxUnlockedLevel();
  111. label.string = `关卡 ${currentLevel} (最高 ${maxUnlocked})`;
  112. }
  113. }
  114. // 更新货币显示
  115. this.updateCurrencyDisplay();
  116. // 更新按钮状态
  117. this.updateButtonStates();
  118. }
  119. /**
  120. * 更新货币显示
  121. */
  122. private updateCurrencyDisplay() {
  123. if (!this.saveDataManager) return;
  124. // 金币
  125. if (this.coinsLabel) {
  126. const label = this.coinsLabel.getComponent(Label);
  127. if (label) {
  128. const coins = this.saveDataManager.getCoins();
  129. label.string = this.formatNumber(coins);
  130. }
  131. }
  132. // 钻石
  133. if (this.diamondsLabel) {
  134. const label = this.diamondsLabel.getComponent(Label);
  135. if (label) {
  136. const diamonds = this.saveDataManager.getDiamonds();
  137. label.string = this.formatNumber(diamonds);
  138. }
  139. }
  140. // 宝石
  141. if (this.gemsLabel) {
  142. const label = this.gemsLabel.getComponent(Label);
  143. if (label) {
  144. const gems = this.saveDataManager.getGems();
  145. label.string = this.formatNumber(gems);
  146. }
  147. }
  148. }
  149. /**
  150. * 更新按钮状态
  151. */
  152. private updateButtonStates() {
  153. // 这里可以根据玩家状态启用/禁用某些按钮
  154. // 例如:如果没有解锁某些功能,可以禁用相应按钮
  155. }
  156. /**
  157. * 设置按钮事件
  158. */
  159. private setupButtons() {
  160. // 战斗按钮
  161. if (this.battleButton) {
  162. const button = this.battleButton.getComponent(Button);
  163. if (button) {
  164. button.node.on(Button.EventType.CLICK, this.onBattleButtonClick, this);
  165. }
  166. }
  167. // 商店按钮
  168. if (this.shopButton) {
  169. const button = this.shopButton.getComponent(Button);
  170. if (button) {
  171. button.node.on(Button.EventType.CLICK, this.onShopButtonClick, this);
  172. }
  173. }
  174. // 设置按钮
  175. if (this.settingsButton) {
  176. const button = this.settingsButton.getComponent(Button);
  177. if (button) {
  178. button.node.on(Button.EventType.CLICK, this.onSettingsButtonClick, this);
  179. }
  180. }
  181. }
  182. /**
  183. * 战斗按钮点击事件
  184. */
  185. private onBattleButtonClick() {
  186. // 保存数据
  187. if (this.saveDataManager) {
  188. this.saveDataManager.savePlayerData();
  189. }
  190. // 切换 UI:隐藏主界面,显示关卡界面
  191. const mainUI = find('Canvas/MainUI');
  192. const gameLevelUI = find('Canvas/GameLevelUI');
  193. if (mainUI) mainUI.active = false;
  194. if (gameLevelUI) gameLevelUI.active = true;
  195. // 重置并加载关卡
  196. const gmNode = find('Canvas/GameLevelUI/GameManager');
  197. if (gmNode) {
  198. const gm = gmNode.getComponent(GameManager);
  199. if (gm) {
  200. gm.restartGame(); // 清理上一局数据
  201. gm.loadCurrentLevelConfig(); // 加载当前(或下一)关配置
  202. }
  203. }
  204. }
  205. /**
  206. * 商店按钮点击事件
  207. */
  208. private onShopButtonClick() {
  209. // UI 切换:隐藏主界面,显示商城界面(如果存在)
  210. const mainUI = find('Canvas/MainUI');
  211. const shopUI = find('Canvas/ShopUI');
  212. if (mainUI) mainUI.active = false;
  213. if (shopUI) {
  214. shopUI.active = true;
  215. } else {
  216. console.warn('未找到商城界面节点 Canvas/ShopUI');
  217. }
  218. }
  219. /**
  220. * 设置按钮点击事件
  221. */
  222. private onSettingsButtonClick() {
  223. // 这里可以打开设置界面
  224. // 暂时只显示调试信息
  225. this.showCurrentLevelInfo();
  226. }
  227. /**
  228. * 显示当前关卡信息(调试用)
  229. */
  230. private showCurrentLevelInfo() {
  231. if (!this.saveDataManager) return;
  232. const currentLevel = this.saveDataManager.getCurrentLevel();
  233. const isCompleted = this.saveDataManager.isLevelCompleted(currentLevel);
  234. if (isCompleted) {
  235. const progress = this.saveDataManager.getLevelProgress(currentLevel);
  236. if (progress) {
  237. // 显示关卡完成信息
  238. }
  239. }
  240. }
  241. /**
  242. * 格式化数字显示
  243. */
  244. private formatNumber(num: number): string {
  245. if (num >= 1000000) {
  246. return (num / 1000000).toFixed(1) + 'M';
  247. } else if (num >= 1000) {
  248. return (num / 1000).toFixed(1) + 'K';
  249. }
  250. return num.toString();
  251. }
  252. /**
  253. * 添加货币(用于测试)
  254. */
  255. public addTestCoins(amount: number = 1000) {
  256. if (this.saveDataManager) {
  257. this.saveDataManager.addCoins(amount, 'test');
  258. this.updateUI();
  259. }
  260. }
  261. /**
  262. * 添加钻石(用于测试)
  263. */
  264. public addTestDiamonds(amount: number = 100) {
  265. if (this.saveDataManager) {
  266. this.saveDataManager.addDiamonds(amount, 'test');
  267. this.updateUI();
  268. }
  269. }
  270. /**
  271. * 解锁关卡(用于测试)
  272. */
  273. public unlockLevel(level: number) {
  274. if (this.saveDataManager) {
  275. // 直接设置玩家数据中的最大解锁关卡
  276. const playerData = this.saveDataManager.getPlayerData();
  277. if (level > playerData.maxUnlockedLevel) {
  278. playerData.maxUnlockedLevel = level;
  279. this.saveDataManager.savePlayerData();
  280. this.updateUI();
  281. }
  282. }
  283. }
  284. /**
  285. * 重置存档(用于测试)
  286. */
  287. public resetSaveData() {
  288. if (this.saveDataManager) {
  289. this.saveDataManager.resetAllData();
  290. this.updateUI();
  291. }
  292. }
  293. /**
  294. * 获取存档管理器(供其他脚本使用)
  295. */
  296. public getSaveDataManager(): SaveDataManager {
  297. return this.saveDataManager;
  298. }
  299. onDestroy() {
  300. // 清理按钮事件
  301. if (this.battleButton) {
  302. const button = this.battleButton.getComponent(Button);
  303. if (button) {
  304. button.node.off(Button.EventType.CLICK, this.onBattleButtonClick, this);
  305. }
  306. }
  307. if (this.shopButton) {
  308. const button = this.shopButton.getComponent(Button);
  309. if (button) {
  310. button.node.off(Button.EventType.CLICK, this.onShopButtonClick, this);
  311. }
  312. }
  313. if (this.settingsButton) {
  314. const button = this.settingsButton.getComponent(Button);
  315. if (button) {
  316. button.node.off(Button.EventType.CLICK, this.onSettingsButtonClick, this);
  317. }
  318. }
  319. }
  320. }