HomeView.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { _decorator, instantiate, Label, Node } from 'cc';
  2. import { AppKernel } from '@scripts/app/AppKernel';
  3. import { IGameInfo } from '@scripts/store/pageSlice/data';
  4. import { UIView } from '@scripts/framework/ui/UIView';
  5. import { UIViewID } from '@scripts/define/UIDefine';
  6. import { GameItem } from './GameItem';
  7. import { addBtnClick } from '@scripts/utils/UIHelper';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('HomeView')
  10. export class HomeView extends UIView {
  11. readonly viewId = UIViewID.HomeView;
  12. @property(Label)
  13. lblNickName: Label;
  14. @property(GameItem)
  15. gamePrefab: GameItem;
  16. @property(Node)
  17. gameRoot: Node;
  18. @property(Node)
  19. btnMail: Node;
  20. protected onCreate(): void | Promise<void> {
  21. AppKernel.events.on('login:success', this.updateView, this);
  22. addBtnClick(this.btnMail, this.onBtnMailClick, this);
  23. }
  24. protected onOpen(_params: unknown): void | Promise<void> {
  25. this.updateView();
  26. }
  27. private updateView() {
  28. const mainStore = AppKernel.stores.mainStore;
  29. this.lblNickName.string = mainStore.userInfo?.NickName || "";
  30. const pageStore = AppKernel.stores.pageStore;
  31. const gameList = pageStore.gameList || [];
  32. if (gameList.length > 0) {
  33. const index = 0;//Math.floor(Math.random() * gameList.length);
  34. this.initGameItem(gameList[index]);
  35. }
  36. }
  37. private async initGameItem(game: IGameInfo) {
  38. const gameNode = instantiate(this.gamePrefab.node).getComponent(GameItem);
  39. gameNode.node.setParent(this.gameRoot);
  40. gameNode.node.active = true;
  41. gameNode.initView(game);
  42. }
  43. private onBtnMailClick() {
  44. AppKernel.ui.open(UIViewID.MailView);
  45. }
  46. }