GameItem.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. protected onDestroy(): void {
  24. removeBtnClick(this.node, this.onClick, this);
  25. removeBtnClick(this.btnFavo, this.onBtnFavoClick, this);
  26. }
  27. public updateItem(data: IGameInfo, index: number) {
  28. this._data = data;
  29. this._dataIdx = index;
  30. if (!this.node.active) this.node.active = true;
  31. if (this.lblGameName) this.lblGameName.string = data.gameName;
  32. setAlpha(this.favoFlag, data.isFavorite);
  33. setSpriteFrame(this.gameIcon, "https://cdn.usgamestatic.com/24680/game/lw/bankersbreachcollisionways@1x.webp");
  34. }
  35. private onClick() {
  36. AppKernel.appLobby.openGame(this._data);
  37. // AppKernel.appLobby.openGame(this._gameConfig.id);
  38. }
  39. private async onBtnFavoClick() {
  40. const gameStore = AppKernel.stores.gameStore;
  41. if (gameStore.favoriting) return;
  42. const index = this._dataIdx;
  43. const data = this._data;
  44. const isFavorite = data.isFavorite;
  45. const game = isFavorite ? await gameStore.reqFavoRemove(data.gameID)
  46. : await gameStore.reqFavoAdd(data.gameID);
  47. if (!!game && this._dataIdx === index) {
  48. this.updateItem(game, index);
  49. }
  50. }
  51. }