BattleAvengeItem.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { _decorator, Component, Sprite, Label, RichText, 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 { g } from '../Data/g';
  7. import { ISJSB } from '../Data/platform';
  8. import { FsUtils } from '../FsUtils/FsUtils';
  9. import { BattleType } from '../UI/Battle/BattleUIWindow';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('BattleAvengeItem')
  12. export class BattleAvengeItem extends Component {
  13. @property({ type: Sprite, tooltip: "玩家头像" })
  14. headSprite: Sprite;
  15. @property({ type: Label, tooltip: "战斗时间文本" })
  16. battleTimeLabel: Label;
  17. @property({ type: RichText, tooltip: "玩家文本" })
  18. userNameLabel: RichText;
  19. @property({ type: Http, tooltip: "消息控制" })
  20. http: Http;
  21. private data: any;//attackUserID,attackUserLv,attackUserName,attackUserAvator,createTime
  22. start() {
  23. }
  24. public async setData(data: any) {
  25. this.data = data;
  26. if (ISJSB && data.attackUserAvator) {
  27. let img = await ResourcesUtils.loadRemote<ImageAsset>(data.attackUserAvator, { ext: ".png" }, this);
  28. const spriteFrame = new SpriteFrame();
  29. const texture = new Texture2D();
  30. texture.image = img;
  31. spriteFrame.texture = texture;
  32. this.headSprite.spriteFrame = spriteFrame;
  33. }
  34. this.battleTimeLabel.string = FsUtils.formatBattleTime(Date.now() / 1000 - data.createTime) + '前';
  35. this.userNameLabel.string = `<color=#ff0000>${FsUtils.beautySub(data.attackUserName, 4)}</color><color=#b7331f>攻击了你的[队伍]</color>`;
  36. }
  37. /**复仇*/
  38. public async onRevengeTouch() {
  39. let result = await this.http.send("/api/battle/ReadyBattle", { type: BattleType.avengeBattle, enemyID: this.data.attackUserID });
  40. if (!result.code) {
  41. g.battleData.setData(result.data);
  42. WindowManager.open("Prefabs/Battle/BattleWindow", WindowOpenMode.NotCloseAndAdd, { type: BattleType.avengeBattle });//打开战斗准备窗口
  43. } else {
  44. WindowManager.showTips(g.CodeMsg[result.code]);
  45. }
  46. }
  47. }