MainUIController.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. console.log('🏠 主界面初始化完成');
  63. console.log('存档摘要:', this.saveDataManager.getSaveSummary());
  64. }
  65. /**
  66. * 自动查找UI节点
  67. */
  68. private findUINodes() {
  69. if (!this.playerLevelLabel) {
  70. this.playerLevelLabel = find('Canvas/MainUI/PlayerInfo/LevelLabel');
  71. }
  72. if (!this.currentLevelLabel) {
  73. this.currentLevelLabel = find('Canvas/MainUI/LevelInfo/CurrentLevelLabel');
  74. }
  75. if (!this.coinsLabel) {
  76. this.coinsLabel = find('Canvas/MainUI/Currency/CoinsLabel');
  77. }
  78. if (!this.diamondsLabel) {
  79. this.diamondsLabel = find('Canvas/MainUI/Currency/DiamondsLabel');
  80. }
  81. if (!this.gemsLabel) {
  82. this.gemsLabel = find('Canvas/MainUI/Currency/GemsLabel');
  83. }
  84. if (!this.battleButton) {
  85. this.battleButton = find('Canvas/MainUI/BattleButtonNode');
  86. }
  87. if (!this.shopButton) {
  88. this.shopButton = find('Canvas/MainUI/ShopButton');
  89. }
  90. if (!this.settingsButton) {
  91. this.settingsButton = find('Canvas/MainUI/SettingsButton');
  92. }
  93. }
  94. /**
  95. * 更新UI显示
  96. */
  97. public updateUI() {
  98. if (!this.saveDataManager) return;
  99. // 更新玩家等级显示
  100. if (this.playerLevelLabel) {
  101. const label = this.playerLevelLabel.getComponent(Label);
  102. if (label) {
  103. const playerLevel = this.saveDataManager.getPlayerLevel();
  104. label.string = `Lv.${playerLevel}`;
  105. }
  106. }
  107. // 更新当前关卡显示
  108. if (this.currentLevelLabel) {
  109. const label = this.currentLevelLabel.getComponent(Label);
  110. if (label) {
  111. const currentLevel = this.saveDataManager.getCurrentLevel();
  112. const maxUnlocked = this.saveDataManager.getMaxUnlockedLevel();
  113. label.string = `关卡 ${currentLevel} (最高 ${maxUnlocked})`;
  114. }
  115. }
  116. // 更新货币显示
  117. this.updateCurrencyDisplay();
  118. // 更新按钮状态
  119. this.updateButtonStates();
  120. }
  121. /**
  122. * 更新货币显示
  123. */
  124. private updateCurrencyDisplay() {
  125. if (!this.saveDataManager) return;
  126. // 金币
  127. if (this.coinsLabel) {
  128. const label = this.coinsLabel.getComponent(Label);
  129. if (label) {
  130. const coins = this.saveDataManager.getCoins();
  131. label.string = this.formatNumber(coins);
  132. }
  133. }
  134. // 钻石
  135. if (this.diamondsLabel) {
  136. const label = this.diamondsLabel.getComponent(Label);
  137. if (label) {
  138. const diamonds = this.saveDataManager.getDiamonds();
  139. label.string = this.formatNumber(diamonds);
  140. }
  141. }
  142. // 宝石
  143. if (this.gemsLabel) {
  144. const label = this.gemsLabel.getComponent(Label);
  145. if (label) {
  146. const gems = this.saveDataManager.getGems();
  147. label.string = this.formatNumber(gems);
  148. }
  149. }
  150. }
  151. /**
  152. * 更新按钮状态
  153. */
  154. private updateButtonStates() {
  155. // 这里可以根据玩家状态启用/禁用某些按钮
  156. // 例如:如果没有解锁某些功能,可以禁用相应按钮
  157. }
  158. /**
  159. * 设置按钮事件
  160. */
  161. private setupButtons() {
  162. // 战斗按钮
  163. if (this.battleButton) {
  164. const button = this.battleButton.getComponent(Button);
  165. if (button) {
  166. button.node.on(Button.EventType.CLICK, this.onBattleButtonClick, this);
  167. }
  168. }
  169. // 商店按钮
  170. if (this.shopButton) {
  171. const button = this.shopButton.getComponent(Button);
  172. if (button) {
  173. button.node.on(Button.EventType.CLICK, this.onShopButtonClick, this);
  174. }
  175. }
  176. // 设置按钮
  177. if (this.settingsButton) {
  178. const button = this.settingsButton.getComponent(Button);
  179. if (button) {
  180. button.node.on(Button.EventType.CLICK, this.onSettingsButtonClick, this);
  181. }
  182. }
  183. }
  184. /**
  185. * 战斗按钮点击事件
  186. */
  187. private onBattleButtonClick() {
  188. console.log('🎮 进入战斗!');
  189. // 保存数据
  190. if (this.saveDataManager) {
  191. this.saveDataManager.savePlayerData();
  192. }
  193. // 切换 UI:隐藏主界面,显示关卡界面
  194. const mainUI = find('Canvas/MainUI');
  195. const gameLevelUI = find('Canvas/GameLevelUI');
  196. if (mainUI) mainUI.active = false;
  197. if (gameLevelUI) gameLevelUI.active = true;
  198. // 重置并加载关卡
  199. const gmNode = find('Canvas/GameLevelUI/GameManager');
  200. if (gmNode) {
  201. const gm = gmNode.getComponent(GameManager);
  202. if (gm) {
  203. gm.restartGame(); // 清理上一局数据
  204. gm.loadCurrentLevelConfig(); // 加载当前(或下一)关配置
  205. }
  206. }
  207. }
  208. /**
  209. * 商店按钮点击事件
  210. */
  211. private onShopButtonClick() {
  212. console.log('🛒 打开商店');
  213. // UI 切换:隐藏主界面,显示商城界面(如果存在)
  214. const mainUI = find('Canvas/MainUI');
  215. const shopUI = find('Canvas/ShopUI');
  216. if (mainUI) mainUI.active = false;
  217. if (shopUI) {
  218. shopUI.active = true;
  219. } else {
  220. console.warn('未找到商城界面节点 Canvas/ShopUI');
  221. }
  222. }
  223. /**
  224. * 设置按钮点击事件
  225. */
  226. private onSettingsButtonClick() {
  227. console.log('⚙️ 打开设置');
  228. // 这里可以打开设置界面
  229. }
  230. /**
  231. * 显示当前关卡信息(调试用)
  232. */
  233. private showCurrentLevelInfo() {
  234. if (!this.saveDataManager) return;
  235. const currentLevel = this.saveDataManager.getCurrentLevel();
  236. const progress = this.saveDataManager.getLevelProgress(currentLevel);
  237. const isCompleted = this.saveDataManager.isLevelCompleted(currentLevel);
  238. const isUnlocked = this.saveDataManager.isLevelUnlocked(currentLevel);
  239. console.log('=== 当前关卡信息 ===');
  240. console.log(`关卡: ${currentLevel}`);
  241. console.log(`已解锁: ${isUnlocked}`);
  242. console.log(`已完成: ${isCompleted}`);
  243. if (progress) {
  244. console.log(`最佳得分: ${progress.bestScore}`);
  245. console.log(`最佳时间: ${progress.bestTime}秒`);
  246. console.log(`星级: ${progress.stars}`);
  247. console.log(`尝试次数: ${progress.attempts}`);
  248. }
  249. }
  250. /**
  251. * 格式化数字显示
  252. */
  253. private formatNumber(num: number): string {
  254. if (num >= 1000000) {
  255. return (num / 1000000).toFixed(1) + 'M';
  256. } else if (num >= 1000) {
  257. return (num / 1000).toFixed(1) + 'K';
  258. }
  259. return num.toString();
  260. }
  261. /**
  262. * 添加货币(用于测试)
  263. */
  264. public addTestCoins(amount: number = 1000) {
  265. if (this.saveDataManager) {
  266. this.saveDataManager.addCoins(amount, 'test');
  267. this.updateUI();
  268. }
  269. }
  270. /**
  271. * 添加钻石(用于测试)
  272. */
  273. public addTestDiamonds(amount: number = 100) {
  274. if (this.saveDataManager) {
  275. this.saveDataManager.addDiamonds(amount, 'test');
  276. this.updateUI();
  277. }
  278. }
  279. /**
  280. * 解锁关卡(用于测试)
  281. */
  282. public unlockLevel(level: number) {
  283. if (this.saveDataManager) {
  284. const playerData = this.saveDataManager.getPlayerData();
  285. if (level > playerData.maxUnlockedLevel) {
  286. playerData.maxUnlockedLevel = level;
  287. this.saveDataManager.savePlayerData();
  288. this.updateUI();
  289. console.log(`🔓 解锁关卡 ${level}`);
  290. }
  291. }
  292. }
  293. /**
  294. * 重置存档(用于测试)
  295. */
  296. public resetSaveData() {
  297. if (this.saveDataManager) {
  298. this.saveDataManager.resetAllData();
  299. this.updateUI();
  300. console.log('🔄 存档已重置');
  301. }
  302. }
  303. /**
  304. * 获取存档管理器(供其他脚本使用)
  305. */
  306. public getSaveDataManager(): SaveDataManager {
  307. return this.saveDataManager;
  308. }
  309. onDestroy() {
  310. // 清理按钮事件
  311. if (this.battleButton) {
  312. const button = this.battleButton.getComponent(Button);
  313. if (button) {
  314. button.node.off(Button.EventType.CLICK, this.onBattleButtonClick, this);
  315. }
  316. }
  317. if (this.shopButton) {
  318. const button = this.shopButton.getComponent(Button);
  319. if (button) {
  320. button.node.off(Button.EventType.CLICK, this.onShopButtonClick, this);
  321. }
  322. }
  323. if (this.settingsButton) {
  324. const button = this.settingsButton.getComponent(Button);
  325. if (button) {
  326. button.node.off(Button.EventType.CLICK, this.onSettingsButtonClick, this);
  327. }
  328. }
  329. }
  330. }