RoosterTakeGoblet.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { _decorator, Component, ERaycast2DType, find, Label, Node, NodeEventType, PhysicsSystem2D, Tween, tween, v2, v3, Vec2, Vec3 } from 'cc';
  2. import { EventDispatcher } from '../core_tgx/easy_ui_framework/EventDispatcher';
  3. import { TakeGobletGlobalInstance } from './Script/TakeGobletGlobalInstance';
  4. import { GameEvent } from './Script/Enum/GameEvent';
  5. import { LevelManager } from './Script/Manager/LevelMgr';
  6. import { GameUtil } from './Script/GameUtil';
  7. import { tgxUIMgr } from '../core_tgx/tgx';
  8. import { UI_Setting } from '../scripts/UIDef';
  9. import { TakeGobletAudioMgr } from './Script/Manager/TakeGobletAudioMgr';
  10. import { LevelAction } from './Script/LevelAction';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('RoosterTakeGoblet')
  13. export class RoosterTakeGoblet extends Component {
  14. onLoad() {
  15. TakeGobletAudioMgr.initilize();
  16. TakeGobletAudioMgr.play(TakeGobletAudioMgr.getMusicIdName(1), 1.0);
  17. LevelManager.instance.initilizeModel();
  18. TakeGobletGlobalInstance.instance.levels = find('Canvas/Scene/Levels')!;
  19. this.registerListener();
  20. }
  21. protected start(): void {
  22. this.startGame();
  23. }
  24. async startGame() {
  25. await LevelManager.instance.gameStart();
  26. this.refreshCupCount();
  27. this.onUpdateLvl();
  28. }
  29. registerListener() {
  30. EventDispatcher.instance.on(GameEvent.EVENT_REFRESH_REMAIN_CUP, this.refreshCupCount, this);
  31. EventDispatcher.instance.on(GameEvent.EVENT_BATTLE_SUCCESS_LEVEL_UP, this.onUpdateLvl, this);
  32. const btnRefresh = find('Canvas/GameUI/TopLeft/BtnRefresh')!;
  33. const btnSet = find('Canvas/GameUI/TopLeft/BtnSet')!;
  34. btnRefresh.on(NodeEventType.TOUCH_END, () => this.onClickRefresh(), this);
  35. btnSet.on(NodeEventType.TOUCH_END, () => this.onClickSet(), this);
  36. }
  37. private async onClickRefresh(): Promise<void> {
  38. TakeGobletAudioMgr.playOneShot(TakeGobletAudioMgr.getMusicIdName(2), 1.0);
  39. Tween.stopAll();
  40. EventDispatcher.instance.emit(GameEvent.EVENT_BATTLE_FAIL_LEVEL_RESET);
  41. }
  42. private onClickSet(): void {
  43. TakeGobletAudioMgr.playOneShot(TakeGobletAudioMgr.getMusicIdName(2), 1.0);
  44. const show = tgxUIMgr.inst.isShowing(UI_Setting);
  45. if (!show) {
  46. tgxUIMgr.inst.showUI(UI_Setting);
  47. }
  48. }
  49. onUpdateLvl() {
  50. const lbScrews = find('Canvas/GameUI/TitleLvl/LbLvl')!.getComponent(Label);
  51. const { level } = LevelManager.instance.levelModel;
  52. lbScrews.string = `Level:${level}`;
  53. }
  54. //刷新剩余杯子的数量
  55. private refreshCupCount() {
  56. const lbRemain = find('Canvas/Scene/Pai/LbRemaining')!.getComponent(Label);
  57. const lvlAction = find('Canvas/Scene/Levels')!.children[0].getComponent(LevelAction);
  58. const waitLength = lvlAction.waitArea!.getCups().length;
  59. const outLength = lvlAction.outArea!.getCups().length;
  60. lbRemain.string = `${waitLength + outLength} Cup`;
  61. }
  62. }