| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import { _decorator, Component, Node, Label, ProgressBar, Sprite, Vec3, SpriteFrame, animation, Animation, sp, instantiate, Skeleton, Prefab, tween, EventHandler } from 'cc';
- import { ADHelper } from '../ad/ADHelper';
- import { RewardVideoSystem } from '../ad/RewardVideoSystem';
- import { BattleData } from '../battle/BattleData';
- import { DataSystem } from '../core/data/DataSystem';
- import { Http, HttpResponseCode } from '../core/net/Http';
- import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
- import { WindowSystem } from '../core/ui/window/WindowSystem';
- import { Utils } from '../core/utils/Utils';
- import { ConfigData } from '../data/ConfigData';
- import { platform } from '../data/jsb/platform';
- import { UserData } from '../data/UserData';
- import { Guide, GuideType } from '../guide/Guide';
- import { NoviceGuideData } from '../guide/NoviceGuideData';
- import { FormationData } from '../hero/formation/FormationData';
- import { UserHeroData } from '../hero/UserHeroData';
- import { ReportThinking } from '../ReportThinking';
- import { Trajectory } from '../turntable/Trajectory';
- import { TrainAni } from './TrainAni';
- const { ccclass, property, requireComponent } = _decorator;
- /**粮草训练 */
- @ccclass('Train')
- @requireComponent(Http)
- export class Train extends Component {
- @property({ type: Node, tooltip: "训练显示节点" }) trainNode: Node;
- @property({ type: Node, tooltip: "看视频节点" }) trainCDNode: Node;
- @property({ type: Label, tooltip: "兵营等级" }) campLvLabel: Label;
- // @property({ type: Label, tooltip: "粮草进度" }) progressLabel: Label;
- // @property({ type: ProgressBar, tooltip: "粮草进度" }) progress: ProgressBar;
- @property({ type: Node, tooltip: "粮草特效起点" }) startNode: Node;
- @property({ type: Node, tooltip: "红包特效终点" }) endNode: Node;
- @property({ type: Node, tooltip: "特效节点" }) effectNode: Node;
- @property({ type: [SpriteFrame], tooltip: "粒子合集" }) partics: Array<SpriteFrame> = [];
- @property({ type: Animation, tooltip: "存钱罐增加红包币文本特效" }) animation: Animation;
- @property({ type: Label, tooltip: "增加粮草文本" }) addLabel: Label;
- // @property({ type: ProgressBar, tooltip: "回复CD" }) cdProgress: ProgressBar;
- // @property({ type: Label, tooltip: "回复CD文本" }) cdLabel: Label;
- @property({ type: [Prefab], tooltip: "粮草动画节点" }) spineFoods: Prefab[] = [];
- @property({ tooltip: "飞行动画结束回调", type: EventHandler }) tweenBack: EventHandler;
- @property({ type: ADHelper, tooltip: "粮草动画节点" }) adHelper: ADHelper;
- /**当前关卡粮他上限 */
- private maxCapacity: number;
- private missionConfig: any;
- /**单次训练消耗 */
- private trainConsume: number;
- /**训练目标武将ID */
- private trainHeroId: number;
- private addBouns: number = 0;
- start() {
- this.trainConsume = DataSystem.getData(ConfigData).get('serverConfig').trainConsume;
- this.setLevel();
- this.setMoney();
- }
- update() {
- DataSystem.watch(UserData, 'campLv') && this.setLevel();
- DataSystem.watch(UserData, 'money') && this.setMoney();
- DataSystem.watch(NoviceGuideData, 'isTrain') && this.onTrain();
- }
- private setLevel() {
- this.missionConfig = DataSystem.getData(ConfigData).get('mission');
- let level = DataSystem.getData(UserData).level;
- this.campLvLabel.string = 'Lv.' + DataSystem.getData(UserData).campLv;
- if (this.missionConfig[level]) {
- this.maxCapacity = this.missionConfig[level].maxCapacity;
- }
- }
- /**设置粮草进度 */
- private setMoney() {
- let money = DataSystem.getData(UserData).money;
- if (money >= this.trainConsume && !this.trainNode.active) {
- this.trainNode.active = money >= this.trainConsume;
- this.trainCDNode.active && (this.trainCDNode.active = false);
- }
- if (money < this.trainConsume) {
- this.trainCDNode.active = money < this.trainConsume;
- this.setCdInfo();
- this.trainNode.active && (this.trainNode.active = false);
- }
- }
- private setCdInfo() {
- let recover = this.missionConfig[DataSystem.getData(UserData).level].recover;
- let times = Math.floor((this.trainConsume - DataSystem.getData(UserData).money) / (recover / 60));
- // this.cdProgress.progress = DataSystem.getData(UserData).money / this.trainConsume;
- // this.cdLabel.string = times + 's';
- }
- private isTouch = false;
- /**训练 */
- private async onTrain() {
- if (this.isTouch) {
- return;
- }
- if (DataSystem.getData(UserData).money < this.trainConsume) {
- this.openGetGrain();
- return;
- }
- //判断阵上有没有武将
- let bool = false;
- for (let i = 1; i < 6; i++) {
- if (DataSystem.getData(FormationData).has(i)) {
- bool = true;
- break;
- }
- }
- if (!bool) {
- // this.tset();
- return;
- }
- this.isTouch = true;
- let result = await this.getComponent(Http).send('/api/hero/trainHero');
- if (result && result.code == HttpResponseCode.Success) {
- let userData = DataSystem.getData(UserData);
- ReportThinking.currency_decrease('money', userData.money, result.data.money, userData.money - result.data.money, 'train');
- Guide.close(GuideType.Train);
- if (result.data.needAd) {
- this.adHelper.show();
- }
- if (!localStorage.getItem("trainNum" + DataSystem.getData(UserData).account)) {
- platform.reportThinking("loading", JSON.stringify({ guide_id: 'first_train' }));
- localStorage.setItem("trainNum" + DataSystem.getData(UserData).account, '1');
- } else {
- if (JSON.parse(localStorage.getItem("trainNum" + DataSystem.getData(UserData).account)) == 1) {
- platform.reportThinking("loading", JSON.stringify({ guide_id: 'second_train' }));
- localStorage.setItem("trainNum" + DataSystem.getData(UserData).account, '2');
- }
- }
- let id = DataSystem.getData(FormationData).getIDByHero(result.data.id);
- let userHeroData = DataSystem.getData(UserHeroData);
- for (let i = 0; i < userHeroData.haveHeros.length; i++) {
- let hero = userHeroData.get(userHeroData.haveHeros[i])
- if (hero.server.id == result.data.id && hero.server.lv != result.data.lv) {
- hero.server.lv = result.data.lv;
- userHeroData.set(userHeroData.haveHeros[i], hero);
- ReportThinking.train(hero.client.name, hero.server.lv, this.trainConsume);
- }
- }
- let battleHeroData = DataSystem.getData(BattleData).heros.get(id);
- this.trainHeroId = battleHeroData.id;
- if (battleHeroData.id == result.data.id) {
- battleHeroData.lv = result.data.lv;
- battleHeroData.lvProgress = result.data.percent / 100;
- }
- this.isTouch = false;
- let endPoint = DataSystem.getData(BattleData).slots.get(id).node.position;
- DataSystem.getData(UserData).money -= result.data.money;
- this.showEffect(1, endPoint);
- if (result.data.bonus) {
- await this.showEffect(0, this.endNode.position);
- this.addBouns = result.data.bonus;
- }
- } else {
- if (result && result.code == 101) {
- WindowSystem.showTips('训练粮草不足');
- } else {
- WindowSystem.showTips('http返回null');
- }
- }
- }
- /**打开免费粮草获得 */
- private async openGetGrain() {
- let result = await this.getComponent(Http).send('/api/user/getMoneyTimes');
- if (result && result.code == HttpResponseCode.Success) {
- if (result.data) {
- this.getGrain(result.data);
- // WindowSystem.open('prefabs/ui/train/grain', WindowOpenMode.NotCloseAndCover, [result.data]);
- } else {
- WindowSystem.open('prefabs/ui/gain/gain', WindowOpenMode.NotCloseAndCover, [{ type: 1 }]);
- }
- }
- }
- private isOpening = false;
- /**看视频领取粮草 */
- private async getGrain(num: number) {
- if (this.isOpening) {
- return;
- }
- if (num) {
- this.isOpening = true;
- let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
- if (result && result.code == HttpResponseCode.Success) {
- ReportThinking.ad_init('grain_get');
- let adData = await RewardVideoSystem.show();
- if (adData) {//视频观看成功
- result = await this.getComponent(Http).send('/api/user/getMoney', { adData: adData.obj });
- this.isOpening = false;
- if (result && result.code == HttpResponseCode.Success) {
- ReportThinking.ad_end('grain_get', result.data);
- ReportThinking.currency_increase('money', DataSystem.getData(UserData).money, result.data, result.data + DataSystem.getData(UserData).money, 'getMoney');
- await WindowSystem.open("prefabs/ui/turntable/turntablePrizePopup", WindowOpenMode.NotCloseAndCover,
- [{ icon: 4, type: 1, num: result.data }]);
- // DataSystem.getData(UserData).money += result.data;
- // num--;
- // this.numLabel.string = this.num + '次';
- // WindowSystem.showTips('免费粮草领取成功');
- }
- // this.getComponent(Window).close();
- } else {
- this.isOpening = false;
- }
- } else if (result && result.code == 108) {
- this.isOpening = false;
- WindowSystem.showTips('视频CD中');
- }
- } else {
- WindowSystem.showTips('当日看视频领取免费粮草次数不足');
- }
- }
- private async tset() {
- await this.showEffect(0, this.endNode.position);
- this.addLabel.string = Math.floor(Math.random() * 100) + '';
- this.animation.play();
- }
- /**
- *
- * @param type 0,红包,1武将粮草
- * @param endPoint
- */
- private async showEffect(type: number, endPoint: Vec3) {
- let node: Node;
- if (type == 1) {
- let heroData = DataSystem.getData(UserHeroData).get(this.trainHeroId);
- let camp = parseInt(heroData.client.camp.toString());
- node = instantiate(this.spineFoods[camp - 1]);
- let tempStart = new Vec3(this.startNode.position.x + Utils.random_both(110, -110), this.startNode.position.y + Utils.random_both(0, 150));
- this.effectNode.addChild(node);
- let trainAni = node.addComponent(TrainAni);
- trainAni.initPosition = new Vec3(this.startNode.position.x, this.startNode.position.y);
- trainAni.targetPosition = tempStart;
- trainAni.startPosition = tempStart;
- trainAni.endPosition = endPoint;
- } else {
- node = new Node();
- node.addComponent(Sprite);
- node.getComponent(Sprite)!.spriteFrame = this.partics[type];
- node.addComponent(Trajectory);
- let trajectory = node.getComponent(Trajectory);
- trajectory.startPoint = this.startNode.position;
- trajectory.ctrl1Point = new Vec3(Utils.random_both(300, -300), Utils.random_both(0, -500));
- trajectory.ctrl2Point = new Vec3(Utils.random_both(100, -100), Utils.random_both(300, -300));
- trajectory.endPoint = endPoint;
- trajectory.backFn = this.tweenBack;
- node.setPosition(0, 0);
- this.effectNode.addChild(node);
- }
- }
- /**红包特效结束 */
- private onComplete() {
- this.addLabel.string = '+' + this.addBouns;
- this.animation.play();
- }
- }
|