FriendChallengeItem.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { _decorator, Component, Node, Sprite, Label, SpriteFrame, ImageAsset, Texture2D } from 'cc';
  2. import { Http } from '../core/net/Http';
  3. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  4. import { WindowManager } from '../core/ui/window/WindowManager';
  5. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  6. import { Utils } from '../core/utils/Utils';
  7. import { g } from '../Data/g';
  8. import { ISJSB } from '../Data/platform';
  9. import { FsUtils } from '../FsUtils/FsUtils';
  10. import { BattleType } from '../UI/Battle/BattleUIWindow';
  11. const { ccclass, property } = _decorator;
  12. /**好友战斗 */
  13. @ccclass('FriendChallengeItem')
  14. export class FriendChallengeItem extends Component {
  15. @property({ type: Sprite, tooltip: "好友头像" })
  16. headSprite: Sprite;
  17. @property({ type: Label, tooltip: "好友名字文本" })
  18. nameLabel: Label;
  19. @property({ type: Node, tooltip: "挑战按钮节点" })
  20. challengeButtonNode: Node;
  21. @property({ type: Label, tooltip: "胜利文本" })
  22. winLabel: Label;
  23. @property({ type: Http, tooltip: "Http服务" })
  24. http: Http;
  25. private data: any;
  26. start() {
  27. }
  28. public async setData(data: any, logs: any) {
  29. this.data = data;
  30. this.nameLabel.string = FsUtils.beautySub(data.nickName, 4);
  31. // 找出当前用户ID今天的挑战红包判断是否挑战胜利 this.headSprite.spriteFrame = img;
  32. if (ISJSB && data.avator) {
  33. let img = await ResourcesUtils.loadRemote<ImageAsset>(data.avator, { ext: ".png" }, this);
  34. const spriteFrame = new SpriteFrame();
  35. const texture = new Texture2D();
  36. texture.image = img;
  37. spriteFrame.texture = texture;
  38. this.headSprite.spriteFrame = spriteFrame;
  39. }
  40. for (let i = 0; i < logs.length; i++) {
  41. if (logs[i].win && logs[i].beAttackUserID == data.id && logs[i].createTime > FsUtils.getTodayTime()) { //今天已经挑战过胜利了
  42. this.winLabel.node.active = true;
  43. this.challengeButtonNode.active = false;
  44. return;
  45. }
  46. }
  47. this.winLabel.node.active = false;
  48. this.challengeButtonNode.active = true;
  49. }
  50. /**挑战 */
  51. private async onChallengeButton() {
  52. let result = await this.http.send("/api/battle/ReadyBattle", { type: BattleType.ChallengeBattle, enemyID: this.data.id });
  53. if (!result.code) {
  54. g.battleData.setData(result.data);
  55. WindowManager.open("Prefabs/Battle/BattleWindow", WindowOpenMode.NotCloseAndAdd, { type: BattleType.ChallengeBattle });//打开战斗准备窗口
  56. }
  57. }
  58. }