import { _decorator, Component, Node, Label, ProgressBar, Button, Vec3, Sprite, SpriteFrame, systemEvent, SystemEventType, EventTouch, tween, UITransform, Vec2 } from 'cc'; import { Data } from '../core/data/Data'; import { DataSystem } from '../core/data/DataSystem'; import { Http, HttpResponseCode } from '../core/net/Http'; import { Sound } from '../core/sound/Sound'; import { UITween } from '../core/ui/tween/UITween'; import { WindowOpenMode } from '../core/ui/window/WindowOpenMode'; import { WindowSystem } from '../core/ui/window/WindowSystem'; import { StringUtils } from '../core/utils/StringUtils'; import { Utils } from '../core/utils/Utils'; import { UserData } from '../data/UserData'; import { Guide, GuideType } from '../guide/Guide'; import { FormationData } from '../hero/formation/FormationData'; import { ReportThinking } from '../ReportThinking'; import { Trajectory } from '../turntable/Trajectory'; import { Task } from './Task'; import { TaskData } from './TaskData'; import { TaskInfoData } from './TaskInfoData'; const { ccclass, property, requireComponent } = _decorator; /** * 任务Item * @author 孙雄 */ @ccclass('TaskItem') @requireComponent(Http) export class TaskItem extends Component { @property({ type: Node, tooltip: "item内容节点" }) itemContentNode: Node; @property({ type: Label, tooltip: "奖励数量文本" }) numLabel: Label; @property({ type: Label, tooltip: "任务描述文本" }) desLabel: Label; @property({ type: Label, tooltip: "任务进度文本" }) progressLabel: Label; @property({ type: ProgressBar, tooltip: "任务进度条" }) taskPorgress: ProgressBar; @property({ type: Node, tooltip: "领取按钮节点" }) getNode: Node; @property({ type: Node, tooltip: "已领取按钮节点" }) receivedNode: Node; @property({ type: Node, tooltip: "前往节点" }) gotoNode: Node; @property({ type: Node, tooltip: "未完成节点" }) incompleteNode: Node; @property({ type: Node, tooltip: "背景节点" }) bgNode: Node; @property({ type: SpriteFrame, tooltip: "特效资源" }) effectSpriteFrame: SpriteFrame; @property({ type: UITween, tooltip: "领取1级任务特效" }) rewardTween: UITween; @property({ type: Node, tooltip: "特效开始位置节点" }) starNode: Node; @property({ type: Sound, tooltip: "飞行音效" }) flySound: Sound; private rewardType = 1;//1段任务,2段任务 private data: TaskInfoData; private targetNode: Node; private effectNode: Node; private startWolrdPostion; private addNums = []; start() { this.startWolrdPostion = this.node.worldPosition.clone(); } public onDataChange(data: TaskInfoData, targetNode: Node, bgNode: Node) { this.data = data; this.effectNode = bgNode; this.targetNode = targetNode; this.setView(); } private setView() { if (this.data.isReceive1 && this.data.isReceive2) {//全部领取 this.receivedNode.active = true; this.getNode.active = this.gotoNode.active = this.incompleteNode.active = false; this.rewardType = 2; } else { if (this.data.isReceive1) { this.rewardType = 2; } else { this.rewardType = 1; } if (this.data.count1 <= this.data.serverCount && this.rewardType == 1 || this.data.count2 <= this.data.serverCount && this.rewardType == 2) {//可领取 this.getNode.active = true; this.receivedNode.active = this.gotoNode.active = this.incompleteNode.active = false; } else { if (this.data.openId != -1) { this.gotoNode.active = true; this.receivedNode.active = this.getNode.active = this.incompleteNode.active = false; } else { this.incompleteNode.active = true; this.receivedNode.active = this.getNode.active = this.gotoNode.active = false; } } } this.numLabel.string = this.data['activeValue' + this.rewardType]; this.desLabel.string = StringUtils.format(this.data.des, this.data['count' + this.rewardType]); this.taskPorgress.progress = this.data.serverCount / this.data['count' + this.rewardType]; this.progressLabel.string = (this.data.serverCount > this.data['count' + this.rewardType] ? this.data['count' + this.rewardType] : this.data.serverCount) + '/' + this.data['count' + this.rewardType]; } /**前往 */ private goto(e: EventTouch) { let windowList = WindowSystem.getWindowList(); for (let i = 0; i < windowList.length; i++) { if (windowList[i].getComponent(Task)) { windowList[i].close(); } } switch (this.data.openId) { case 0: { Guide.create(GuideType.Train); break; } case 5: WindowSystem.open('prefabs/ui/recruit/recruit', WindowOpenMode.NotCloseAndAdd); break; case 6: { if (DataSystem.getData(UserData).level >= 2) { WindowSystem.open('prefabs/ui/turntable/turntable', WindowOpenMode.NotCloseAndAdd); } else { WindowSystem.showTips('关卡达到2级开启'); } break }; case 7: { if (DataSystem.getData(UserData).level >= 3) { WindowSystem.open('prefabs/ui/expropriationGrain/expropriationGrain', WindowOpenMode.NotCloseAndAdd); } else { WindowSystem.showTips('关卡达到3级开启'); } break }; case 1: { if (DataSystem.getData(FormationData).has(5) && DataSystem.getData(FormationData).get(5)) { Guide.create(GuideType.Marshal); } else { WindowSystem.showTips('请先上阵统帅武将'); } break; } } } private isTouch = false; private async onGet(e: EventTouch) { if (this.isTouch) { return; } this.isTouch = true; let result = await this.getComponent(Http).send('/api/task/ReceiveTask', { id: this.data.id, rewardType: this.rewardType }) this.isTouch = false; if (result && result.code == HttpResponseCode.Success) { ReportThinking.task(this.desLabel.string); this.getNode.active = false; let taskData = DataSystem.getData(TaskData); this.addNums.push(result.data); for (let i = 0; i < taskData.tasks.length; i++) { if (taskData.tasks[i].id == this.data.id) { taskData.tasks[i]['isReceive' + this.rewardType] = true; this.data['isReceive' + this.rewardType] = true; break; } } if (this.rewardType == 1) { this.rewardType = 2; this.showEffect(); this.rewardTween.play(); } else { this.showEffect(); DataSystem.getData(TaskData).getTaskGiftNum++; } } } private itemReFrush() { this.bgNode.position = new Vec3(800, 0, 0); this.data.isReceive1 = true; DataSystem.getData(TaskData).getTaskGiftNum++; this.setView(); } private showEffect() { this.flySound.play(); this.scheduleOnce(() => { this.addNums.length > 0 && (DataSystem.getData(TaskData).activityNum += this.addNums.shift()); }, 1); for (let i = 0; i < this.data['activeValue' + this.rewardType]; i++) { let node = new Node(); node.addComponent(Sprite); node.getComponent(Sprite)!.spriteFrame = this.effectSpriteFrame; let pos = this.starNode.getWorldPosition(); let targetY = 380 + this.node.position.y + this.node.worldPosition.y - this.startWolrdPostion.y; node.setPosition(new Vec3(-238, targetY)); this.effectNode.addChild(node); let trajectory = node.addComponent(Trajectory); trajectory.startPoint = new Vec3(-238, targetY); trajectory.ctrl1Point = new Vec3(Utils.random_both(80, -80), Utils.random_both(0, -200)); trajectory.ctrl2Point = new Vec3(Utils.random_both(50, -50), Utils.random_both(100, -100)); trajectory.endPoint = this.targetNode.position; } } }