GameBlockSelection.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. import { _decorator, Component, Node, Button, Label, find } from 'cc';
  2. import { LevelSessionManager } from '../../Core/LevelSessionManager';
  3. import { BallController } from '../BallController';
  4. import { BlockManager } from '../BlockManager';
  5. import { BlockTag } from './BlockTag';
  6. import { SkillManager } from '../SkillSelection/SkillManager';
  7. import EventBus, { GameEvents } from '../../Core/EventBus';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('GameBlockSelection')
  10. export class GameBlockSelection extends Component {
  11. @property({
  12. type: Node,
  13. tooltip: '拖拽diban/ann001按钮节点到这里'
  14. })
  15. public addBallButton: Node = null;
  16. @property({
  17. type: Node,
  18. tooltip: '拖拽diban/ann002按钮节点到这里'
  19. })
  20. public addCoinButton: Node = null;
  21. @property({
  22. type: Node,
  23. tooltip: '拖拽diban/ann003按钮节点到这里'
  24. })
  25. public refreshButton: Node = null;
  26. @property({
  27. type: Node,
  28. tooltip: '拖拽Canvas-001/TopArea/CoinNode/CoinLabel节点到这里'
  29. })
  30. public coinLabelNode: Node = null;
  31. // 移除toastNode属性,改用事件机制
  32. // @property({
  33. // type: Node,
  34. // tooltip: '拖拽Canvas/Toast节点到这里'
  35. // })
  36. // public toastNode: Node = null;
  37. @property({
  38. type: Node,
  39. tooltip: '拖拽Canvas/GameLevelUI/BallController节点到这里'
  40. })
  41. public ballControllerNode: Node = null;
  42. @property({
  43. type: Node,
  44. tooltip: '拖拽Canvas/GameLevelUI/BlockController节点到这里'
  45. })
  46. public blockManagerNode: Node = null;
  47. @property({
  48. type: Node,
  49. tooltip: '拖拽Canvas/GameLevelUI/GameArea/GridContainer节点到这里'
  50. })
  51. public gridContainer: Node = null;
  52. @property({
  53. type: Node,
  54. tooltip: '拖拽confirm按钮节点到这里'
  55. })
  56. public confirmButton: Node = null;
  57. @property({
  58. type: Node,
  59. tooltip: '拖拽Canvas/GameLevelUI/InGameManager节点到这里'
  60. })
  61. public inGameManagerNode: Node = null;
  62. @property({
  63. type: Node,
  64. tooltip: '拖拽Canvas/Camera节点到这里'
  65. })
  66. public cameraNode: Node = null;
  67. // 按钮费用配置
  68. private readonly ADD_BALL_COST = 80;
  69. private readonly ADD_COIN_AMOUNT = 80;
  70. private readonly REFRESH_COST = 5;
  71. private session: LevelSessionManager = null;
  72. private ballController: BallController = null;
  73. private blockManager: BlockManager = null;
  74. // 回调函数,用于通知GameManager
  75. public onConfirmCallback: () => void = null;
  76. // 标记是否已初始化
  77. private isInitialized: boolean = false;
  78. // 标记是否应该生成方块(只有在onBattle触发后才为true)
  79. private shouldGenerateBlocks: boolean = false;
  80. onEnable() {
  81. console.log('[GameBlockSelection] onEnable() 节点被激活');
  82. // 如果还未初始化,则进行初始化
  83. if (!this.isInitialized) {
  84. this.initializeComponent();
  85. }
  86. }
  87. start() {
  88. console.log('[GameBlockSelection] start() 被调用');
  89. // 如果还未初始化,则进行初始化
  90. if (!this.isInitialized) {
  91. this.initializeComponent();
  92. }
  93. }
  94. private initializeComponent() {
  95. console.log('[GameBlockSelection] initializeComponent() 开始初始化');
  96. console.log('[GameBlockSelection] 组件节点名称:', this.node.name);
  97. console.log('[GameBlockSelection] 组件节点激活状态:', this.node.active);
  98. // 获取管理器实例
  99. this.session = LevelSessionManager.inst;
  100. // 获取BallController
  101. if (this.ballControllerNode) {
  102. this.ballController = this.ballControllerNode.getComponent(BallController);
  103. } else {
  104. console.warn('BallController节点未绑定,请在Inspector中拖拽Canvas/GameLevelUI/BallController节点');
  105. }
  106. // 获取BlockManager
  107. if (this.blockManagerNode) {
  108. this.blockManager = this.blockManagerNode.getComponent(BlockManager);
  109. } else {
  110. console.warn('BlockManager节点未绑定,请在Inspector中拖拽Canvas/GameLevelUI/BlockController节点');
  111. }
  112. // 如果没有指定coinLabelNode,尝试找到它
  113. if (!this.coinLabelNode) {
  114. this.coinLabelNode = find('Canvas-001/TopArea/CoinNode/CoinLabel');
  115. }
  116. // 初始化金币显示
  117. this.updateCoinDisplay();
  118. // 绑定按钮事件
  119. this.bindButtonEvents();
  120. // 设置事件监听器
  121. this.setupEventListeners();
  122. // 标记为已初始化
  123. this.isInitialized = true;
  124. console.log('GameBlockSelection.initializeComponent() 初始化完成');
  125. }
  126. // 设置事件监听器
  127. private setupEventListeners() {
  128. console.log('[GameBlockSelection] 开始设置事件监听器');
  129. const eventBus = EventBus.getInstance();
  130. console.log('[GameBlockSelection] EventBus实例获取结果:', !!eventBus);
  131. // 监听重置方块选择事件
  132. eventBus.on(GameEvents.RESET_BLOCK_SELECTION, this.onResetBlockSelectionEvent, this);
  133. console.log('[GameBlockSelection] RESET_BLOCK_SELECTION事件监听器已设置');
  134. // 监听显示方块选择事件
  135. // 监听游戏开始事件,用于标记可以开始生成方块
  136. eventBus.on(GameEvents.GAME_START, this.onGameStartEvent, this);
  137. console.log('[GameBlockSelection] GAME_START事件监听器已设置');
  138. console.log('[GameBlockSelection] 事件监听器设置完成,组件节点:', this.node.name);
  139. console.log('[GameBlockSelection] 当前节点状态:', this.node.active);
  140. }
  141. // 处理重置方块选择事件
  142. private onResetBlockSelectionEvent() {
  143. console.log('[GameBlockSelection] 接收到重置方块选择事件');
  144. this.resetSelection();
  145. }
  146. // 处理游戏开始事件
  147. private onGameStartEvent() {
  148. console.log('[GameBlockSelection] 接收到游戏开始事件,允许生成方块');
  149. this.shouldGenerateBlocks = true;
  150. }
  151. // 绑定按钮事件
  152. private bindButtonEvents() {
  153. // 绑定新增小球按钮
  154. if (this.addBallButton) {
  155. const btn = this.addBallButton.getComponent(Button);
  156. if (btn) {
  157. this.addBallButton.on(Button.EventType.CLICK, this.onAddBallClicked, this);
  158. } else {
  159. this.addBallButton.on(Node.EventType.TOUCH_END, this.onAddBallClicked, this);
  160. }
  161. }
  162. // 绑定增加金币按钮
  163. if (this.addCoinButton) {
  164. const btn = this.addCoinButton.getComponent(Button);
  165. if (btn) {
  166. this.addCoinButton.on(Button.EventType.CLICK, this.onAddCoinClicked, this);
  167. } else {
  168. this.addCoinButton.on(Node.EventType.TOUCH_END, this.onAddCoinClicked, this);
  169. }
  170. }
  171. // 绑定刷新方块按钮
  172. if (this.refreshButton) {
  173. const btn = this.refreshButton.getComponent(Button);
  174. if (btn) {
  175. this.refreshButton.on(Button.EventType.CLICK, this.onRefreshClicked, this);
  176. } else {
  177. this.refreshButton.on(Node.EventType.TOUCH_END, this.onRefreshClicked, this);
  178. }
  179. }
  180. // 绑定确认按钮
  181. if (this.confirmButton) {
  182. const btn = this.confirmButton.getComponent(Button);
  183. if (btn) {
  184. this.confirmButton.on(Button.EventType.CLICK, this.onConfirmButtonClicked, this);
  185. } else {
  186. this.confirmButton.on(Node.EventType.TOUCH_END, this.onConfirmButtonClicked, this);
  187. }
  188. }
  189. }
  190. // 新增小球按钮点击
  191. private onAddBallClicked() {
  192. // 应用便宜技能效果计算实际费用
  193. const actualCost = this.getActualCost(this.ADD_BALL_COST);
  194. if (!this.canSpendCoins(actualCost)) {
  195. this.showInsufficientCoinsUI();
  196. return;
  197. }
  198. // 扣除金币
  199. if (this.session.spendCoins(actualCost)) {
  200. this.updateCoinDisplay();
  201. // 通过事件系统创建新的小球
  202. const eventBus = EventBus.getInstance();
  203. eventBus.emit(GameEvents.BALL_CREATE_ADDITIONAL);
  204. console.log(`新增小球成功,扣除${actualCost}金币`);
  205. }
  206. }
  207. // 增加金币按钮点击
  208. private onAddCoinClicked() {
  209. // 免费增加金币(模拟看广告获得奖励)
  210. const coinsToAdd = 80; // 免费获得的金币数量
  211. this.session.addCoins(coinsToAdd);
  212. console.log(`通过观看广告免费获得${coinsToAdd}金币`);
  213. // 更新显示
  214. this.updateCoinDisplay();
  215. // 可以在这里添加播放奖励动画的逻辑
  216. // MoneyAni.playReward(coinsToAdd, 0);
  217. }
  218. // 刷新方块按钮点击
  219. private onRefreshClicked() {
  220. // 应用便宜技能效果计算实际费用
  221. const actualCost = this.getActualCost(this.REFRESH_COST);
  222. if (!this.canSpendCoins(actualCost)) {
  223. this.showInsufficientCoinsUI();
  224. return;
  225. }
  226. // 扣除金币
  227. if (this.session.spendCoins(actualCost)) {
  228. this.updateCoinDisplay();
  229. // 刷新方块
  230. if (this.blockManager) {
  231. // 找到PlacedBlocks容器
  232. const placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
  233. if (placedBlocksContainer) {
  234. // 移除已放置方块的标签
  235. BlockTag.removeTagsInContainer(placedBlocksContainer);
  236. }
  237. // 刷新方块
  238. this.blockManager.refreshBlocks();
  239. console.log(`刷新方块成功,扣除${actualCost}金币`);
  240. } else {
  241. console.error('找不到BlockManager,无法刷新方块');
  242. }
  243. }
  244. }
  245. // 确认按钮点击
  246. public onConfirmButtonClicked() {
  247. // 检查是否有上阵方块
  248. const hasBlocks = this.hasPlacedBlocks();
  249. console.log(`[GameBlockSelection] 检查上阵方块: ${hasBlocks ? '有' : '无'}`);
  250. if (!hasBlocks) {
  251. this.showNoPlacedBlocksToast();
  252. return;
  253. }
  254. // 保存已放置的方块
  255. this.preservePlacedBlocks();
  256. // 清理kuang区域的方块(用户期望的行为)
  257. if (this.blockManager) {
  258. this.blockManager.clearBlocks();
  259. console.log('[GameBlockSelection] 已清理kuang区域的方块');
  260. }
  261. // 先回调通知GameManager,让它处理波次逻辑
  262. if (this.onConfirmCallback) {
  263. this.onConfirmCallback();
  264. }
  265. // 播放下滑diban动画,结束备战进入playing状态
  266. this.playDibanSlideDownAnimation();
  267. }
  268. // 播放diban下滑动画
  269. private playDibanSlideDownAnimation() {
  270. console.log('[GameBlockSelection] 开始播放diban下滑动画');
  271. // 使用装饰器属性获取Camera节点上的GameStartMove组件
  272. if (!this.cameraNode) {
  273. console.warn('[GameBlockSelection] Camera节点未设置,请在Inspector中拖拽Canvas/Camera节点');
  274. return;
  275. }
  276. const gameStartMove = this.cameraNode.getComponent('GameStartMove');
  277. if (!gameStartMove) {
  278. console.warn('[GameBlockSelection] GameStartMove组件未找到');
  279. return;
  280. }
  281. // 调用GameStartMove的下滑动画方法
  282. (gameStartMove as any).slideDibanDownAndHide(300, 0.3);
  283. console.log('[GameBlockSelection] 已调用GameStartMove的diban下滑动画');
  284. }
  285. // 保存已放置的方块(从GameManager迁移)
  286. private preservePlacedBlocks() {
  287. if (this.blockManager) {
  288. this.blockManager.onGameStart();
  289. }
  290. }
  291. // 检查是否有足够金币
  292. private canSpendCoins(amount: number): boolean {
  293. return this.session.getCoins() >= amount;
  294. }
  295. // 计算应用便宜技能效果后的实际费用
  296. private getActualCost(baseCost: number): number {
  297. const skillManager = SkillManager.getInstance();
  298. if (skillManager) {
  299. const cheaperSkillLevel = skillManager.getSkillLevel('cheaper_units');
  300. return Math.ceil(SkillManager.calculateCheaperUnitsPrice(baseCost, cheaperSkillLevel));
  301. }
  302. return baseCost;
  303. }
  304. // 更新金币显示
  305. private updateCoinDisplay() {
  306. if (this.coinLabelNode) {
  307. const label = this.coinLabelNode.getComponent(Label);
  308. if (label) {
  309. const coins = this.session.getCoins();
  310. label.string = coins.toString();
  311. console.log(`[GameBlockSelection] 更新金币显示: ${coins}`);
  312. } else {
  313. console.warn('[GameBlockSelection] coinLabelNode缺少Label组件');
  314. }
  315. } else {
  316. console.warn('[GameBlockSelection] coinLabelNode未找到');
  317. }
  318. }
  319. // 显示金币不足UI
  320. private showInsufficientCoinsUI() {
  321. // 使用事件机制显示Toast
  322. EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
  323. message: '金币不足!',
  324. duration: 3.0
  325. });
  326. console.log('金币不足!');
  327. }
  328. // === 公共方法:供GameManager调用 ===
  329. // 生成方块选择(不再控制UI显示,只负责生成方块)
  330. public generateBlockSelection() {
  331. console.log('[GameBlockSelection] generateBlockSelection开始执行');
  332. // 直接生成方块,不再依赖shouldGenerateBlocks标志
  333. if (this.blockManager) {
  334. this.blockManager.refreshBlocks();
  335. console.log('[GameBlockSelection] 生成新的随机方块');
  336. } else {
  337. console.warn('[GameBlockSelection] BlockManager未找到,无法生成随机方块');
  338. }
  339. // 触发进入备战状态事件
  340. EventBus.getInstance().emit(GameEvents.ENTER_BATTLE_PREPARATION);
  341. console.log('[GameBlockSelection] 方块生成完成');
  342. }
  343. // 设置确认回调
  344. public setConfirmCallback(callback: () => void) {
  345. this.onConfirmCallback = callback;
  346. }
  347. // 重置方块选择状态
  348. public resetSelection() {
  349. console.log('[GameBlockSelection] 重置方块选择状态');
  350. // 重置方块生成标志,等待下次onBattle触发
  351. this.shouldGenerateBlocks = false;
  352. console.log('[GameBlockSelection] 重置方块生成标志');
  353. // 注意:不再直接调用blockManager.onGameReset(),因为StartGame.clearGameStates()
  354. // 已经通过RESET_BLOCK_MANAGER事件触发了BlockManager的重置,避免重复生成方块
  355. console.log('[GameBlockSelection] BlockManager将通过事件系统自动重置');
  356. // 清理所有方块标签
  357. BlockTag.clearAllTags();
  358. console.log('[GameBlockSelection] 方块标签已清理');
  359. // 更新金币显示
  360. this.updateCoinDisplay();
  361. console.log('[GameBlockSelection] 金币显示已更新');
  362. }
  363. // 检查是否有上阵方块
  364. private hasPlacedBlocks(): boolean {
  365. // 优先使用BlockManager的方法检查
  366. if (this.blockManager) {
  367. return this.blockManager.hasPlacedBlocks();
  368. }
  369. // 备用方案:直接检查PlacedBlocks容器
  370. const placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
  371. if (!placedBlocksContainer) {
  372. console.warn('找不到PlacedBlocks容器');
  373. return false;
  374. }
  375. // 检查容器中是否有子节点(方块)
  376. return placedBlocksContainer.children.length > 0;
  377. }
  378. // 显示没有上阵方块的Toast提示
  379. private showNoPlacedBlocksToast() {
  380. console.log('[GameBlockSelection] 显示未上阵植物提示');
  381. // 使用事件机制显示Toast
  382. EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
  383. message: '请至少上阵一个植物!',
  384. duration: 3.0
  385. });
  386. console.log('[GameBlockSelection] 请至少上阵一个植物!');
  387. }
  388. onDestroy() {
  389. // 清理事件监听
  390. const eventBus = EventBus.getInstance();
  391. eventBus.off(GameEvents.RESET_BLOCK_SELECTION, this.onResetBlockSelectionEvent, this);
  392. eventBus.off(GameEvents.GAME_START, this.onGameStartEvent, this);
  393. }
  394. }