GameItem.ts 1.8 KB

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