| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { _decorator, Component, Sprite, Label, Button, SpriteFrame, ImageAsset, Texture2D } from 'cc';
- import { Http } from '../core/net/Http';
- import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
- import { OpenWindow } from '../core/ui/window/OpenWindow';
- import { WindowManager } from '../core/ui/window/WindowManager';
- import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
- import { Data } from '../Data/Data';
- import { g } from '../Data/g';
- import { ISJSB } from '../Data/platform';
- import { FsUtils } from '../FsUtils/FsUtils';
- import { BattleItemRedPacket } from '../UI/Battle/BattleItemRedPacket';
- import { BattleType } from '../UI/Battle/BattleUIWindow';
- const { ccclass, property } = _decorator;
- /**切磋列表ITEM */
- @ccclass('BattleFriendItem')
- export class BattleFriendItem extends Component {
- @property({ type: Sprite, tooltip: "玩家头像" })
- headSprite: Sprite;
- @property({ type: Sprite, tooltip: "关系标识" })
- relationSprite: Sprite;
- @property({ type: Label, tooltip: "玩家名称" })
- nameLabel: Label;
- @property({ type: Label, tooltip: "玩家等级" })
- lvLabel: Label;
- @property({ type: BattleItemRedPacket, tooltip: "切磋红包" })
- redPacket: BattleItemRedPacket;
- @property({ type: Button, tooltip: "攻打按钮" })
- attackButton: Button;
- @property({ type: Button, tooltip: "查看按钮" })
- watchButton: Button;
- @property({ type: Button, tooltip: "抢红包按钮" })
- robRedPacketButton: Button;
- @property({ type: Http, tooltip: "消息控制" })
- http: Http;
- private isRandomUser = false;
- private state = ItemState.Normal;
- private emenyData: any;
- start() {
- }
- public async setData(data: { userID: number, lv: number, nickName: string, avator: string, isFriend: boolean, hasRedPacket: boolean, time: number, redPacketID: number }, isFirst: boolean = false) {
- this.emenyData = data;
- if (isFirst) {
- this.isRandomUser = true;
- this.lvLabel.string = `lv.???`;
- this.nameLabel.string = `随机玩家`;
- } else {
- this.lvLabel.string = `lv.` + data.lv;
- this.nameLabel.string = FsUtils.beautySub(data.nickName, 5);
- if (ISJSB && this.emenyData.avator) {
- let img = await ResourcesUtils.loadRemote<ImageAsset>(this.emenyData.avator, { ext: ".png" }, this);
- const spriteFrame = new SpriteFrame();
- const texture = new Texture2D();
- texture.image = img;
- spriteFrame.texture = texture;
- this.headSprite.spriteFrame = spriteFrame;
- }
- }
- //好友关系
- if (data.isFriend) {
- this.relationSprite.node.active = true;
- }
- if (data.hasRedPacket) {
- this.redPacket.node.active = true;
- this.redPacket.getComponent(BattleItemRedPacket).setData(data.time * 1000);
- if (data.time * 1000 > Date.now()) {
- this.setItemButton(ItemState.Watch);
- } else {
- this.setItemButton(ItemState.Finish);
- }
- } else {
- this.redPacket.node.active = false;
- this.setItemButton(ItemState.Normal);
- }
- }
- /**红包倒计时结束 */
- public onRedpacketFinish() {
- this.setItemButton(ItemState.Finish);
- }
- private setItemButton(state: number) {
- this.state = state;
- switch (state) {
- case ItemState.Normal:
- this.attackButton.node.active = true;
- this.watchButton.node.active = false;
- this.robRedPacketButton.node.active = false;
- break;
- case ItemState.Watch:
- this.attackButton.node.active = false;
- this.watchButton.node.active = true;
- this.robRedPacketButton.node.active = false;
- break;
- case ItemState.Finish:
- this.attackButton.node.active = false;
- this.watchButton.node.active = false;
- this.robRedPacketButton.node.active = true;
- break;
- }
- }
- private isTouch = false;
- private async onButtonTouch() {
- if (this.isTouch) {
- return;
- }
- this.isTouch = true;
- let result;
- switch (this.state) {
- case ItemState.Normal:
- if (g.gameData.battleRemainder + g.gameData.RemainderHadAddTimes == 0) {
- if (!g.gameData.RemainderAddTimes) {
- WindowManager.showTips("今日次数已用完");
- this.isTouch = false;
- return;
- }
- WindowManager.open("Prefabs/Windows/DialogWindow", WindowOpenMode.NotCloseAndAdd, { num: 0, content: "切磋次数不足,是否领取", type: 2 });
- this.isTouch = false;
- return;
- }
- if (this.isRandomUser) {
- result = await this.http.send("/api/battle/ReadyBattle", { type: BattleType.RandomBattle, enemyID: 0 });
- } else {
- result = await this.http.send("/api/battle/ReadyBattle", { type: BattleType.FriendBattle, enemyID: this.emenyData.userID });
- }
- if (!result.code) {
- g.battleData.setData(result.data);
- WindowManager.open("Prefabs/Battle/BattleWindow", WindowOpenMode.NotCloseAndAdd, { type: result.data.type });//打开战斗准备窗口
- } else {
- WindowManager.showTips(g.CodeMsg[result.code]);
- }
- break;
- case ItemState.Watch:
- WindowManager.open("Prefabs/Battle/BattleRedPacketWindow", WindowOpenMode.NotCloseAndAdd, { data: this.emenyData });//打开战斗准备窗口
- break;
- case ItemState.Finish:
- let data = await this.http.send("/api/redPacket/OpenRedPacket", { redPacketID: this.emenyData.redPacketID });
- if (data.code == 0) {
- for (let i = 0; i < g.gameData.myRedPacketDatas.length; i++) {
- if (this.emenyData.redPacketID == g.gameData.myRedPacketDatas[i].id) {
- g.gameData.myRedPacketDatas.splice(i, 1);
- break;
- }
- }
- if (data.data.openID == g.userData.id) {//打开红包
- this.redPacket.node.active = false;
- this.emenyData.hasRedPacket = false;
- g.gameData.hasNewRedPacketUpdate = true;
- let _window = this.getComponent(OpenWindow);
- _window.prefabPath = "Prefabs/Windows/切磋红包奖品";
- _window.open([data.data.prizes]);
- } else {//红包被抢
- console.log("红包被抢");
- }
- } else {
- WindowManager.showTips(g.CodeMsg[data.code]);
- }
- break;
- }
- this.isTouch = false;
- }
- }
- /**
- * 常规(攻打),查看,可领取(抢红包)
- */
- export enum ItemState {
- Normal,
- Watch,
- Finish
- }
|