GameItem.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { ShowNode } from '@scripts/components/loopList/LoopList';
  3. import { IGameInfo } from '@scripts/store/gameStore/data';
  4. import { setSpriteFrame } from '@scripts/utils/ResHelper';
  5. import { addBtnClick, removeBtnClick } from '@scripts/utils/UIHelper';
  6. import { _decorator, Button, Color, Component, Label, Node, Size, Sprite } from 'cc';
  7. const { ccclass, property } = _decorator;
  8. interface IGameData extends IGameInfo {
  9. index?: number,
  10. size?: Size,
  11. }
  12. @ccclass('GameItem')
  13. export class GameItem extends ShowNode<IGameData> {
  14. @property(Sprite)
  15. gameIcon: Sprite;
  16. @property(Sprite)
  17. btnFavo: Sprite;
  18. @property(Label)
  19. lblGameName: Label;
  20. protected onLoad(): void {
  21. addBtnClick(this.node, this.onClick, this);
  22. addBtnClick(this.btnFavo.node, this.onBtnFavoClick, this);
  23. }
  24. protected onDestroy(): void {
  25. removeBtnClick(this.node, this.onClick, this);
  26. removeBtnClick(this.btnFavo.node, this.onBtnFavoClick, this);
  27. }
  28. public initShow() {
  29. if (!this.node.active) this.node.active = true;
  30. this.lblGameName.string = this.data.gameName;
  31. this.btnFavo.color = this.data.isFavorite ? Color.WHITE : Color.GRAY;
  32. setSpriteFrame(this.gameIcon, "https://cdn.usgamestatic.com/24680/game/lw/bankersbreachcollisionways@1x.webp");
  33. }
  34. private onClick() {
  35. // AppKernel.appLobby.openGame(this._gameConfig.id);
  36. }
  37. private async onBtnFavoClick() {
  38. const gameStore = AppKernel.stores.gameStore;
  39. if (gameStore.favoriting) return;
  40. const isFavorite = this.data.isFavorite;
  41. const game = isFavorite ? await gameStore.reqFavoRemove(this.data.gameID)
  42. : await gameStore.reqFavoAdd(this.data.gameID);
  43. if (!!game) {
  44. this.data = game;
  45. this.initShow();
  46. }
  47. }
  48. }