BattleFriendItem.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { _decorator, Component, Sprite, Label, Button, SpriteFrame, ImageAsset, Texture2D } from 'cc';
  2. import { Http } from '../core/net/Http';
  3. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  4. import { OpenWindow } from '../core/ui/window/OpenWindow';
  5. import { WindowManager } from '../core/ui/window/WindowManager';
  6. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  7. import { Data } from '../Data/Data';
  8. import { g } from '../Data/g';
  9. import { ISJSB } from '../Data/platform';
  10. import { FsUtils } from '../FsUtils/FsUtils';
  11. import { BattleItemRedPacket } from '../UI/Battle/BattleItemRedPacket';
  12. import { BattleType } from '../UI/Battle/BattleUIWindow';
  13. const { ccclass, property } = _decorator;
  14. /**切磋列表ITEM */
  15. @ccclass('BattleFriendItem')
  16. export class BattleFriendItem extends Component {
  17. @property({ type: Sprite, tooltip: "玩家头像" })
  18. headSprite: Sprite;
  19. @property({ type: Sprite, tooltip: "关系标识" })
  20. relationSprite: Sprite;
  21. @property({ type: Label, tooltip: "玩家名称" })
  22. nameLabel: Label;
  23. @property({ type: Label, tooltip: "玩家等级" })
  24. lvLabel: Label;
  25. @property({ type: BattleItemRedPacket, tooltip: "切磋红包" })
  26. redPacket: BattleItemRedPacket;
  27. @property({ type: Button, tooltip: "攻打按钮" })
  28. attackButton: Button;
  29. @property({ type: Button, tooltip: "查看按钮" })
  30. watchButton: Button;
  31. @property({ type: Button, tooltip: "抢红包按钮" })
  32. robRedPacketButton: Button;
  33. @property({ type: Http, tooltip: "消息控制" })
  34. http: Http;
  35. private isRandomUser = false;
  36. private state = ItemState.Normal;
  37. private emenyData: any;
  38. start() {
  39. }
  40. public async setData(data: { userID: number, lv: number, nickName: string, avator: string, isFriend: boolean, hasRedPacket: boolean, time: number, redPacketID: number }, isFirst: boolean = false) {
  41. this.emenyData = data;
  42. if (isFirst) {
  43. this.isRandomUser = true;
  44. this.lvLabel.string = `lv.???`;
  45. this.nameLabel.string = `随机玩家`;
  46. } else {
  47. this.lvLabel.string = `lv.` + data.lv;
  48. this.nameLabel.string = FsUtils.beautySub(data.nickName, 5);
  49. if (ISJSB && this.emenyData.avator) {
  50. let img = await ResourcesUtils.loadRemote<ImageAsset>(this.emenyData.avator, { ext: ".png" }, this);
  51. const spriteFrame = new SpriteFrame();
  52. const texture = new Texture2D();
  53. texture.image = img;
  54. spriteFrame.texture = texture;
  55. this.headSprite.spriteFrame = spriteFrame;
  56. }
  57. }
  58. //好友关系
  59. if (data.isFriend) {
  60. this.relationSprite.node.active = true;
  61. }
  62. if (data.hasRedPacket) {
  63. this.redPacket.node.active = true;
  64. this.redPacket.getComponent(BattleItemRedPacket).setData(data.time * 1000);
  65. if (data.time * 1000 > Date.now()) {
  66. this.setItemButton(ItemState.Watch);
  67. } else {
  68. this.setItemButton(ItemState.Finish);
  69. }
  70. } else {
  71. this.redPacket.node.active = false;
  72. this.setItemButton(ItemState.Normal);
  73. }
  74. }
  75. /**红包倒计时结束 */
  76. public onRedpacketFinish() {
  77. this.setItemButton(ItemState.Finish);
  78. }
  79. private setItemButton(state: number) {
  80. this.state = state;
  81. switch (state) {
  82. case ItemState.Normal:
  83. this.attackButton.node.active = true;
  84. this.watchButton.node.active = false;
  85. this.robRedPacketButton.node.active = false;
  86. break;
  87. case ItemState.Watch:
  88. this.attackButton.node.active = false;
  89. this.watchButton.node.active = true;
  90. this.robRedPacketButton.node.active = false;
  91. break;
  92. case ItemState.Finish:
  93. this.attackButton.node.active = false;
  94. this.watchButton.node.active = false;
  95. this.robRedPacketButton.node.active = true;
  96. break;
  97. }
  98. }
  99. private isTouch = false;
  100. private async onButtonTouch() {
  101. if (this.isTouch) {
  102. return;
  103. }
  104. this.isTouch = true;
  105. let result;
  106. switch (this.state) {
  107. case ItemState.Normal:
  108. if (g.gameData.battleRemainder + g.gameData.RemainderHadAddTimes == 0) {
  109. if (!g.gameData.RemainderAddTimes) {
  110. WindowManager.showTips("今日次数已用完");
  111. this.isTouch = false;
  112. return;
  113. }
  114. WindowManager.open("Prefabs/Windows/DialogWindow", WindowOpenMode.NotCloseAndAdd, { num: 0, content: "切磋次数不足,是否领取", type: 2 });
  115. this.isTouch = false;
  116. return;
  117. }
  118. if (this.isRandomUser) {
  119. result = await this.http.send("/api/battle/ReadyBattle", { type: BattleType.RandomBattle, enemyID: 0 });
  120. } else {
  121. result = await this.http.send("/api/battle/ReadyBattle", { type: BattleType.FriendBattle, enemyID: this.emenyData.userID });
  122. }
  123. if (!result.code) {
  124. g.battleData.setData(result.data);
  125. WindowManager.open("Prefabs/Battle/BattleWindow", WindowOpenMode.NotCloseAndAdd, { type: result.data.type });//打开战斗准备窗口
  126. } else {
  127. WindowManager.showTips(g.CodeMsg[result.code]);
  128. }
  129. break;
  130. case ItemState.Watch:
  131. WindowManager.open("Prefabs/Battle/BattleRedPacketWindow", WindowOpenMode.NotCloseAndAdd, { data: this.emenyData });//打开战斗准备窗口
  132. break;
  133. case ItemState.Finish:
  134. let data = await this.http.send("/api/redPacket/OpenRedPacket", { redPacketID: this.emenyData.redPacketID });
  135. if (data.code == 0) {
  136. for (let i = 0; i < g.gameData.myRedPacketDatas.length; i++) {
  137. if (this.emenyData.redPacketID == g.gameData.myRedPacketDatas[i].id) {
  138. g.gameData.myRedPacketDatas.splice(i, 1);
  139. break;
  140. }
  141. }
  142. if (data.data.openID == g.userData.id) {//打开红包
  143. this.redPacket.node.active = false;
  144. this.emenyData.hasRedPacket = false;
  145. g.gameData.hasNewRedPacketUpdate = true;
  146. let _window = this.getComponent(OpenWindow);
  147. _window.prefabPath = "Prefabs/Windows/切磋红包奖品";
  148. _window.open([data.data.prizes]);
  149. } else {//红包被抢
  150. console.log("红包被抢");
  151. }
  152. } else {
  153. WindowManager.showTips(g.CodeMsg[data.code]);
  154. }
  155. break;
  156. }
  157. this.isTouch = false;
  158. }
  159. }
  160. /**
  161. * 常规(攻打),查看,可领取(抢红包)
  162. */
  163. export enum ItemState {
  164. Normal,
  165. Watch,
  166. Finish
  167. }