HomeView.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. const { ccclass, property } = _decorator;
  8. @ccclass('HomeView')
  9. export class HomeView extends UIView {
  10. readonly viewId = UIViewID.HomeView;
  11. @property(Label)
  12. lblNickName: Label;
  13. @property(GameItem)
  14. gamePrefab: GameItem;
  15. @property(Node)
  16. gameRoot: Node;
  17. protected onCreate(): void | Promise<void> {
  18. AppKernel.events.on('login:success', this.updateView, this);
  19. }
  20. protected onOpen(_params: unknown): void | Promise<void> {
  21. this.updateView();
  22. }
  23. private updateView() {
  24. const mainStore = AppKernel.stores.mainStore;
  25. this.lblNickName.string = mainStore.userInfo?.NickName || "";
  26. const pageStore = AppKernel.stores.pageStore;
  27. const gameList = pageStore.gameList || [];
  28. if (gameList.length > 0) {
  29. const index = 0;//Math.floor(Math.random() * gameList.length);
  30. this.initGameItem(gameList[index]);
  31. }
  32. }
  33. private async initGameItem(game: IGameInfo) {
  34. const gameNode = instantiate(this.gamePrefab.node).getComponent(GameItem);
  35. gameNode.node.setParent(this.gameRoot);
  36. gameNode.node.active = true;
  37. gameNode.initView(game);
  38. }
  39. }