import { _decorator, Component, Node, SpriteFrame, Animation, Sprite, Label, Button, RichText, Quat } from 'cc'; import { DataSystem } from '../core/data/DataSystem'; import { Http, HttpResponseCode } from '../core/net/Http'; import { OpenWindow } from '../core/ui/window/OpenWindow'; import { WindowOpenMode } from '../core/ui/window/WindowOpenMode'; import { WindowSystem } from '../core/ui/window/WindowSystem'; import { ConfigData } from '../data/ConfigData'; import { Config } from '../launch/Config'; import { ActivityInfoData } from './ActivityInfoData'; import { TaskData } from './TaskData'; const { ccclass, property, requireComponent } = _decorator; /***活跃度奖励 */ @ccclass('ActivityItem') @requireComponent(Http) export class ActivityItem extends Component { @property({ type: Animation, tooltip: "光效节点" }) animation: Animation; @property({ type: Node, tooltip: "红包节点" }) redPacketNode: Node; @property({ type: Node, tooltip: "宝箱节点" }) boxNode: Node; @property({ type: Label, tooltip: "宝箱节点" }) activityLabel: Label; @property({ type: [SpriteFrame], tooltip: "红包状态资源合集" }) redPacketStates: Array = []; @property({ type: [SpriteFrame], tooltip: "宝箱状态资源合集" }) boxStates: Array = []; @property({ type: OpenWindow, tooltip: "宝箱领取窗口" }) boxGiftWin: OpenWindow; @property({ type: Button, tooltip: "宝箱领取窗口" }) getBoxButton: Button; @property({ type: Button, tooltip: "红包领取窗口" }) getRedpackeButton: Button; @property({ type: RichText, tooltip: "红包领取窗口" }) tipsLabel: RichText; @property({ type: Node, tooltip: "气泡节点" }) tipNode: Node; @property({ tooltip: "活跃度奖励ID" }) activityId: number = 1; @property({ type: Sprite, tooltip: "汽泡图片" }) tipsIconSprite: Sprite; @property({ type: Animation, tooltip: "宝箱可领取" }) boxFinishedAni: Animation; private type = 1;//1宝箱 ,2红包 private activityConfig: any; private _state = -1; private currentNode: Node; private taskData: TaskData; /**是否领取过双倍奖励 */ private isReceiveDouble = false; private isReceiveSingle = false; onLoad() { this.animation.node.active = false; this.activityConfig = DataSystem.getData(ConfigData).get('taskActive'); this.taskData = DataSystem.getData(TaskData); this.type = this.activityConfig[this.activityId].icon; this.redPacketNode.active = this.type == 2; this.boxNode.active = this.type == 1; this.activityLabel.string = this.activityConfig[this.activityId].activevalue; if (this.type == 1) { this.tipsLabel.string = '随机数量粮草或红包币'; } else { this.tipsLabel.string = `${this.taskData.minNum}-${this.taskData.maxNum}的红包币`; } this.setBoxState(); } update() { DataSystem.watch(TaskData, 'activityNum') && this.setBoxState(); DataSystem.watch(TaskData, 'getActiveBoxGiftNum') && this.setBoxState(); } private setBoxState() { this.currentNode = this.boxNode.active ? this.boxNode : this.redPacketNode; let activevalue = this.activityConfig[this.activityId].activevalue; let nowAcitvityNum = this.taskData.activityNum; for (let i = 0; i < this.taskData.activitys.length; i++) { this.isReceiveDouble; if (this.taskData.activitys[i].id == this.activityId) { this.isReceiveDouble = this.taskData.activitys[i].isReceiveDouble; this.isReceiveSingle = this.taskData.activitys[i].isReceiveSingle; break; } } if (nowAcitvityNum >= activevalue) { if (this.activityConfig[this.activityId].isdouble) { if (!this.isReceiveDouble) { this.state = ActivityState.Finished; } else { this.state = ActivityState.Get; } } else { if (!this.isReceiveSingle) { this.state = ActivityState.Finished; } else { this.state = ActivityState.Get; } } } else { this.state = ActivityState.Lock; } } public set state(value: number) { if (this._state != value) { this._state = value; this.setShowState(); } } private setShowState() { switch (this._state) { case ActivityState.Lock: this.animation.node.active = false; this.currentNode.getComponent(Sprite).spriteFrame = this.type == 1 ? this.boxStates[0] : this.redPacketStates[0]; // this.getBoxButton.interactable = true; // this.getRedpackeButton.interactable = true; break; case ActivityState.Finished: this.currentNode.getComponent(Sprite).spriteFrame = this.type == 1 ? this.boxStates[0] : this.redPacketStates[1]; this.animation.node.active = true; this.animation.play(); this.getBoxButton.node.active = this.type == 1; this.getRedpackeButton.node.active = this.type == 2; // this.getBoxButton.interactable = true; // this.getRedpackeButton.interactable = true; this.boxFinishedAni.play(); break; case ActivityState.Get: this.boxFinishedAni.stop(); this.animation.stop(); this.animation.node.active = false; this.currentNode.getComponent(Sprite).spriteFrame = this.type == 1 ? this.boxStates[1] : this.redPacketStates[2]; this.currentNode.setRotation(new Quat(0, 0, 0, 0)); break; } } /**领取奖励 */ private onGet() { if (this._state == ActivityState.Finished) { let data: ActivityInfoData; for (let i = 0; i < this.taskData.activitys.length; i++) { if (this.taskData.activitys[i].id == this.activityId) { data = this.taskData.activitys[i]; break; } } if (this.activityConfig[this.activityId].icon == 1) {//1宝箱 this.boxGiftWin.open({ id: this.activityId }); } else {//2红包 this.getRedGift(); } } else if (this._state == ActivityState.Lock) { if (this.type == 2) { this.tipsIconSprite.spriteFrame = this.redPacketStates[0]; this.tipsLabel.string = `${this.taskData.minNum}-${DataSystem.getData(TaskData).maxNum} 的红包币`; } this.tipNode.active = true; this.scheduleOnce(() => { this.tipNode.active = false; }, 1); } else { WindowSystem.showTips('奖励已领取'); } } /*领取活跃红包 */ private async getRedGift() { let result = await this.getComponent(Http).send('/api/task/OpenTaskActive', { id: this.activityId }); if (result && result.code == HttpResponseCode.Success) { let type = -1; if (this.activityConfig[this.activityId].isdouble) { if (!this.isReceiveDouble && !this.isReceiveSingle) { type = 3; } else { type = 2; } } else { type = 1; } WindowSystem.open("prefabs/ui/redEnvelopes", WindowOpenMode.NotCloseAndCover, [{ type: type, num: result.data.rewardNum, id: this.activityId, isShowBtn: type == 3 }]); } } } /**0未解锁,1完成可领取,2已领取 */ export enum ActivityState { Lock, Finished, Get }