Train.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { _decorator, Component, Node, Label, ProgressBar, Sprite, Vec3, SpriteFrame, animation, Animation, sp, instantiate, Skeleton, Prefab, tween, EventHandler } from 'cc';
  2. import { ADHelper } from '../ad/ADHelper';
  3. import { RewardVideoSystem } from '../ad/RewardVideoSystem';
  4. import { BattleData } from '../battle/BattleData';
  5. import { DataSystem } from '../core/data/DataSystem';
  6. import { Http, HttpResponseCode } from '../core/net/Http';
  7. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  8. import { WindowSystem } from '../core/ui/window/WindowSystem';
  9. import { Utils } from '../core/utils/Utils';
  10. import { ConfigData } from '../data/ConfigData';
  11. import { platform } from '../data/jsb/platform';
  12. import { UserData } from '../data/UserData';
  13. import { Guide, GuideType } from '../guide/Guide';
  14. import { NoviceGuideData } from '../guide/NoviceGuideData';
  15. import { FormationData } from '../hero/formation/FormationData';
  16. import { UserHeroData } from '../hero/UserHeroData';
  17. import { ReportThinking } from '../ReportThinking';
  18. import { Trajectory } from '../turntable/Trajectory';
  19. import { TrainAni } from './TrainAni';
  20. const { ccclass, property, requireComponent } = _decorator;
  21. /**粮草训练 */
  22. @ccclass('Train')
  23. @requireComponent(Http)
  24. export class Train extends Component {
  25. @property({ type: Node, tooltip: "训练显示节点" }) trainNode: Node;
  26. @property({ type: Node, tooltip: "看视频节点" }) trainCDNode: Node;
  27. @property({ type: Label, tooltip: "兵营等级" }) campLvLabel: Label;
  28. // @property({ type: Label, tooltip: "粮草进度" }) progressLabel: Label;
  29. // @property({ type: ProgressBar, tooltip: "粮草进度" }) progress: ProgressBar;
  30. @property({ type: Node, tooltip: "粮草特效起点" }) startNode: Node;
  31. @property({ type: Node, tooltip: "红包特效终点" }) endNode: Node;
  32. @property({ type: Node, tooltip: "特效节点" }) effectNode: Node;
  33. @property({ type: [SpriteFrame], tooltip: "粒子合集" }) partics: Array<SpriteFrame> = [];
  34. @property({ type: Animation, tooltip: "存钱罐增加红包币文本特效" }) animation: Animation;
  35. @property({ type: Label, tooltip: "增加粮草文本" }) addLabel: Label;
  36. // @property({ type: ProgressBar, tooltip: "回复CD" }) cdProgress: ProgressBar;
  37. // @property({ type: Label, tooltip: "回复CD文本" }) cdLabel: Label;
  38. @property({ type: [Prefab], tooltip: "粮草动画节点" }) spineFoods: Prefab[] = [];
  39. @property({ tooltip: "飞行动画结束回调", type: EventHandler }) tweenBack: EventHandler;
  40. @property({ type: ADHelper, tooltip: "粮草动画节点" }) adHelper: ADHelper;
  41. /**当前关卡粮他上限 */
  42. private maxCapacity: number;
  43. private missionConfig: any;
  44. /**单次训练消耗 */
  45. private trainConsume: number;
  46. /**训练目标武将ID */
  47. private trainHeroId: number;
  48. private addBouns: number = 0;
  49. start() {
  50. this.trainConsume = DataSystem.getData(ConfigData).get('serverConfig').trainConsume;
  51. this.setLevel();
  52. this.setMoney();
  53. }
  54. update() {
  55. DataSystem.watch(UserData, 'campLv') && this.setLevel();
  56. DataSystem.watch(UserData, 'money') && this.setMoney();
  57. DataSystem.watch(NoviceGuideData, 'isTrain') && this.onTrain();
  58. }
  59. private setLevel() {
  60. this.missionConfig = DataSystem.getData(ConfigData).get('mission');
  61. let level = DataSystem.getData(UserData).level;
  62. this.campLvLabel.string = 'Lv.' + DataSystem.getData(UserData).campLv;
  63. if (this.missionConfig[level]) {
  64. this.maxCapacity = this.missionConfig[level].maxCapacity;
  65. }
  66. }
  67. /**设置粮草进度 */
  68. private setMoney() {
  69. let money = DataSystem.getData(UserData).money;
  70. if (money >= this.trainConsume && !this.trainNode.active) {
  71. this.trainNode.active = money >= this.trainConsume;
  72. this.trainCDNode.active && (this.trainCDNode.active = false);
  73. }
  74. if (money < this.trainConsume) {
  75. this.trainCDNode.active = money < this.trainConsume;
  76. this.setCdInfo();
  77. this.trainNode.active && (this.trainNode.active = false);
  78. }
  79. }
  80. private setCdInfo() {
  81. let recover = this.missionConfig[DataSystem.getData(UserData).level].recover;
  82. let times = Math.floor((this.trainConsume - DataSystem.getData(UserData).money) / (recover / 60));
  83. // this.cdProgress.progress = DataSystem.getData(UserData).money / this.trainConsume;
  84. // this.cdLabel.string = times + 's';
  85. }
  86. private isTouch = false;
  87. /**训练 */
  88. private async onTrain() {
  89. if (this.isTouch) {
  90. return;
  91. }
  92. if (DataSystem.getData(UserData).money < this.trainConsume) {
  93. this.openGetGrain();
  94. return;
  95. }
  96. //判断阵上有没有武将
  97. let bool = false;
  98. for (let i = 1; i < 6; i++) {
  99. if (DataSystem.getData(FormationData).has(i)) {
  100. bool = true;
  101. break;
  102. }
  103. }
  104. if (!bool) {
  105. // this.tset();
  106. return;
  107. }
  108. this.isTouch = true;
  109. let result = await this.getComponent(Http).send('/api/hero/trainHero');
  110. if (result && result.code == HttpResponseCode.Success) {
  111. let userData = DataSystem.getData(UserData);
  112. ReportThinking.currency_decrease('money', userData.money, result.data.money, userData.money - result.data.money, 'train');
  113. Guide.close(GuideType.Train);
  114. if (result.data.needAd) {
  115. this.adHelper.show();
  116. }
  117. if (!localStorage.getItem("trainNum" + DataSystem.getData(UserData).account)) {
  118. platform.reportThinking("loading", JSON.stringify({ guide_id: 'first_train' }));
  119. localStorage.setItem("trainNum" + DataSystem.getData(UserData).account, '1');
  120. } else {
  121. if (JSON.parse(localStorage.getItem("trainNum" + DataSystem.getData(UserData).account)) == 1) {
  122. platform.reportThinking("loading", JSON.stringify({ guide_id: 'second_train' }));
  123. localStorage.setItem("trainNum" + DataSystem.getData(UserData).account, '2');
  124. }
  125. }
  126. let id = DataSystem.getData(FormationData).getIDByHero(result.data.id);
  127. let userHeroData = DataSystem.getData(UserHeroData);
  128. for (let i = 0; i < userHeroData.haveHeros.length; i++) {
  129. let hero = userHeroData.get(userHeroData.haveHeros[i])
  130. if (hero.server.id == result.data.id && hero.server.lv != result.data.lv) {
  131. hero.server.lv = result.data.lv;
  132. userHeroData.set(userHeroData.haveHeros[i], hero);
  133. ReportThinking.train(hero.client.name, hero.server.lv, this.trainConsume);
  134. }
  135. }
  136. let battleHeroData = DataSystem.getData(BattleData).heros.get(id);
  137. this.trainHeroId = battleHeroData.id;
  138. if (battleHeroData.id == result.data.id) {
  139. battleHeroData.lv = result.data.lv;
  140. battleHeroData.lvProgress = result.data.percent / 100;
  141. }
  142. this.isTouch = false;
  143. let endPoint = DataSystem.getData(BattleData).slots.get(id).node.position;
  144. DataSystem.getData(UserData).money -= result.data.money;
  145. this.showEffect(1, endPoint);
  146. if (result.data.bonus) {
  147. await this.showEffect(0, this.endNode.position);
  148. this.addBouns = result.data.bonus;
  149. }
  150. } else {
  151. if (result && result.code == 101) {
  152. WindowSystem.showTips('训练粮草不足');
  153. } else {
  154. WindowSystem.showTips('http返回null');
  155. }
  156. }
  157. }
  158. /**打开免费粮草获得 */
  159. private async openGetGrain() {
  160. let result = await this.getComponent(Http).send('/api/user/getMoneyTimes');
  161. if (result && result.code == HttpResponseCode.Success) {
  162. if (result.data) {
  163. this.getGrain(result.data);
  164. // WindowSystem.open('prefabs/ui/train/grain', WindowOpenMode.NotCloseAndCover, [result.data]);
  165. } else {
  166. WindowSystem.open('prefabs/ui/gain/gain', WindowOpenMode.NotCloseAndCover, [{ type: 1 }]);
  167. }
  168. }
  169. }
  170. private isOpening = false;
  171. /**看视频领取粮草 */
  172. private async getGrain(num: number) {
  173. if (this.isOpening) {
  174. return;
  175. }
  176. if (num) {
  177. this.isOpening = true;
  178. let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
  179. if (result && result.code == HttpResponseCode.Success) {
  180. ReportThinking.ad_init('grain_get');
  181. let adData = await RewardVideoSystem.show();
  182. if (adData) {//视频观看成功
  183. result = await this.getComponent(Http).send('/api/user/getMoney', { adData: adData.obj });
  184. this.isOpening = false;
  185. if (result && result.code == HttpResponseCode.Success) {
  186. ReportThinking.ad_end('grain_get', result.data);
  187. ReportThinking.currency_increase('money', DataSystem.getData(UserData).money, result.data, result.data + DataSystem.getData(UserData).money, 'getMoney');
  188. await WindowSystem.open("prefabs/ui/turntable/turntablePrizePopup", WindowOpenMode.NotCloseAndCover,
  189. [{ icon: 4, type: 1, num: result.data }]);
  190. // DataSystem.getData(UserData).money += result.data;
  191. // num--;
  192. // this.numLabel.string = this.num + '次';
  193. // WindowSystem.showTips('免费粮草领取成功');
  194. }
  195. // this.getComponent(Window).close();
  196. } else {
  197. this.isOpening = false;
  198. }
  199. } else if (result && result.code == 108) {
  200. this.isOpening = false;
  201. WindowSystem.showTips('视频CD中');
  202. }
  203. } else {
  204. WindowSystem.showTips('当日看视频领取免费粮草次数不足');
  205. }
  206. }
  207. private async tset() {
  208. await this.showEffect(0, this.endNode.position);
  209. this.addLabel.string = Math.floor(Math.random() * 100) + '';
  210. this.animation.play();
  211. }
  212. /**
  213. *
  214. * @param type 0,红包,1武将粮草
  215. * @param endPoint
  216. */
  217. private async showEffect(type: number, endPoint: Vec3) {
  218. let node: Node;
  219. if (type == 1) {
  220. let heroData = DataSystem.getData(UserHeroData).get(this.trainHeroId);
  221. let camp = parseInt(heroData.client.camp.toString());
  222. node = instantiate(this.spineFoods[camp - 1]);
  223. let tempStart = new Vec3(this.startNode.position.x + Utils.random_both(110, -110), this.startNode.position.y + Utils.random_both(0, 150));
  224. this.effectNode.addChild(node);
  225. let trainAni = node.addComponent(TrainAni);
  226. trainAni.initPosition = new Vec3(this.startNode.position.x, this.startNode.position.y);
  227. trainAni.targetPosition = tempStart;
  228. trainAni.startPosition = tempStart;
  229. trainAni.endPosition = endPoint;
  230. } else {
  231. node = new Node();
  232. node.addComponent(Sprite);
  233. node.getComponent(Sprite)!.spriteFrame = this.partics[type];
  234. node.addComponent(Trajectory);
  235. let trajectory = node.getComponent(Trajectory);
  236. trajectory.startPoint = this.startNode.position;
  237. trajectory.ctrl1Point = new Vec3(Utils.random_both(300, -300), Utils.random_both(0, -500));
  238. trajectory.ctrl2Point = new Vec3(Utils.random_both(100, -100), Utils.random_both(300, -300));
  239. trajectory.endPoint = endPoint;
  240. trajectory.backFn = this.tweenBack;
  241. node.setPosition(0, 0);
  242. this.effectNode.addChild(node);
  243. }
  244. }
  245. /**红包特效结束 */
  246. private onComplete() {
  247. this.addLabel.string = '+' + this.addBouns;
  248. this.animation.play();
  249. }
  250. }