PrepareMediator.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. import { AnimationComponent, ButtonComponent, director, EventTouch, find, instantiate, LabelComponent, LayoutComponent, loader, ModelComponent, Node, Prefab, Quat, SpriteComponent, SpriteFrame, SystemEventType, Touch, Vec2, view, _decorator } from 'cc';
  2. import BufferManager from '../../../engines/buffers/BufferManager';
  3. import IBuffer from '../../../engines/buffers/IBuffer';
  4. import { GUIManager } from '../../../engines/gui/GUIManager';
  5. import { GUIMediator } from '../../../engines/gui/GUIMediator';
  6. import { DataModelEventType } from '../../../engines/models/DataModelEventType';
  7. import { NoticeManager } from '../../../engines/notices/NoticeManager';
  8. import { SoundManager } from '../../../engines/sounds/SoundManager';
  9. import StringUtils from '../../../engines/utils/StringUtils';
  10. import { LiangLiangSDK } from '../../../libs/liangliangSDK';
  11. import { PlatformManager } from '../../../Platform/PlatformManager';
  12. import { QQPlatform } from '../../../Platform/QQ/QQPlatform';
  13. import { branchIdType } from '../../../Platform/WeChat/branchIdType';
  14. import { WeChatPlatforms } from '../../../Platform/WeChat/WeChatPlatforms';
  15. import GameConfigManager from '../../models/GameConfigManager';
  16. import { GameModel } from '../../models/GameModel';
  17. import { GamePropertys } from '../../models/GamePropertys';
  18. import { UIConst } from '../UIConst';
  19. import { WeaponCellListView } from './WeaponCellListView';
  20. const { ccclass, property } = _decorator;
  21. @ccclass('PrepareMediator')
  22. export class PrepareMediator extends GUIMediator {
  23. @property({
  24. type: AnimationComponent
  25. })
  26. DropBoxAnimation: AnimationComponent = null;
  27. @property({
  28. type: Node
  29. })
  30. HandTip: Node = null;
  31. @property({
  32. type: Node
  33. })
  34. bufferNode: Node = null;
  35. @property({
  36. type: LabelComponent
  37. })
  38. glodLabel: LabelComponent = null;
  39. @property({
  40. type: LabelComponent
  41. })
  42. glodLabel1: LabelComponent = null;
  43. @property({
  44. type: LabelComponent
  45. })
  46. diamondLabel: LabelComponent = null;
  47. @property({
  48. type: LabelComponent
  49. })
  50. gunNameLabel: LabelComponent = null;
  51. @property({
  52. type: LabelComponent
  53. })
  54. levelLabel: LabelComponent = null;
  55. @property({
  56. type: LabelComponent
  57. })
  58. weaponInfoLabel: LabelComponent = null;
  59. /**
  60. * 商城按钮
  61. */
  62. @property({
  63. type: ButtonComponent
  64. })
  65. shopButton: ButtonComponent = null;
  66. /**
  67. * 自动合成文本
  68. */
  69. @property({
  70. type: LabelComponent
  71. })
  72. autoSynthesisLabel: LabelComponent = null;
  73. /**
  74. * 快捷购买武器按钮
  75. */
  76. @property({
  77. type: ButtonComponent
  78. })
  79. quickBuyButton: ButtonComponent = null;
  80. /**
  81. * 快捷购买武器ICON
  82. */
  83. @property({
  84. type: SpriteComponent
  85. })
  86. quickBuyWeaponIcon: SpriteComponent = null;
  87. /**
  88. * 快捷购买武器价格
  89. */
  90. @property({
  91. type: LabelComponent
  92. })
  93. quickBuyWeaponPriceLabel: LabelComponent = null;
  94. /**
  95. * 武器拖拽Icon
  96. */
  97. @property({
  98. type: SpriteComponent
  99. })
  100. weaponDragIcon: SpriteComponent = null;
  101. /**
  102. * 删除按钮节点(用于拖拽判断)
  103. */
  104. @property({
  105. type: Node
  106. })
  107. deleteWeaponNode: Node = null;
  108. /**
  109. * 装配武器节点(用于拖拽判断)
  110. */
  111. @property({
  112. type: Node
  113. })
  114. equipWeaponNode: Node = null;
  115. /**
  116. * 装配格子预制体
  117. */
  118. @property({
  119. type: Prefab
  120. })
  121. WeaponCellPrefab: Prefab = null;
  122. /**
  123. * 武器列表
  124. */
  125. @property({
  126. type: LayoutComponent
  127. })
  128. weaponList: LayoutComponent = null;
  129. private modelView: ModelComponent;
  130. private modelPrefab: Prefab;
  131. private prefabInstance: Node;
  132. private prefabModelComponent: ModelComponent;
  133. private weaponCellListView: WeaponCellListView;
  134. private isNew: boolean = true;
  135. private get isNewPlayer(): boolean {
  136. return this.isNew && GameModel.single.isNewPlayer;
  137. }
  138. onLoad(): void {
  139. this.weaponCellListView = new WeaponCellListView(this);
  140. }
  141. OnShow(data?: any): void {
  142. //清空试用栅栏
  143. GameModel.single.trialFenceId = -1;
  144. GameModel.single.trialWeaponId = -1;
  145. GameModel.single.useSuperWeapon = false;
  146. super.OnShow(data);
  147. this.RefreshGunModel();
  148. this.RefreshGlod();
  149. this.RefreshDiamond();
  150. this.RefreshLevel();
  151. this.RefreshQuickBuy();
  152. this.AddEvents();
  153. this.weaponCellListView.OnShow();
  154. SoundManager.single.PlayMusic("sounds/main");
  155. if (this.isNewPlayer) {
  156. this.HandTip.active = true;
  157. } else {
  158. this.HandTip.active = false;
  159. }
  160. if (LiangLiangSDK.CanWuChu() == true) {
  161. // GUIManager.single.Show(UIConst.ADVERTISING_UI)
  162. // PlatformManager.showBanner();
  163. } else {
  164. PlatformManager.showBanner();
  165. }
  166. //签到如果有奖励的话
  167. if (GameModel.single.GetSignAwardIndex() >= 0) {
  168. GUIManager.single.Show(UIConst.SIGN_UI);
  169. // this.HideSelf();
  170. }
  171. }
  172. /**
  173. * 初始化枪的模型
  174. */
  175. private RefreshGunModel(): void {
  176. if (this.modelView == null) {
  177. let modelNode: Node = find("ModelView", this.node);
  178. this.modelView = modelNode.getComponent(ModelComponent);
  179. }
  180. if (this.modelPrefab) {
  181. this.modelView.mesh = null;
  182. let target = this.modelView.materials[0];
  183. target.setProperty("mainTexture", null);
  184. this.prefabInstance.destroy();
  185. this.prefabInstance = null;
  186. let deps: string[] = loader.getDependsRecursively(this.modelPrefab);
  187. loader.release(deps);
  188. this.modelPrefab = null;
  189. }
  190. //武器为空
  191. if (GameModel.single.currentWeaponId < 0) {
  192. this.modelView.node.active = false;
  193. this.gunNameLabel.string = "";
  194. this.weaponInfoLabel.string = "";
  195. return;
  196. } else {
  197. this.modelView.node.active = true;
  198. let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
  199. loader.loadRes(weaponConfig.prefab, Prefab, (err, prefab) => {
  200. if (err) {
  201. console.error("加载武器出错");
  202. }
  203. this.modelPrefab = prefab;
  204. this.prefabInstance = instantiate(prefab);
  205. this.prefabModelComponent = this.prefabInstance.getComponentInChildren(ModelComponent);
  206. //更换贴图
  207. let source = this.prefabModelComponent.materials[0];
  208. let target = this.modelView.materials[0];
  209. let texture = source.getProperty("mainTexture");
  210. target.setProperty("mainTexture", texture);
  211. //更换模型
  212. this.modelView.mesh = this.prefabModelComponent.mesh;
  213. });
  214. if (this.gunNameLabel != null) {
  215. this.gunNameLabel.string = weaponConfig.name;
  216. }
  217. //描述
  218. this.weaponInfoLabel.string = weaponConfig.desc;
  219. }
  220. }
  221. OnHide(): void {
  222. this.RemoveEvents();
  223. this.modelView.mesh = null;
  224. let target = this.modelView.materials[0];
  225. target.setProperty("mainTexture", null);
  226. if (this.prefabInstance) {
  227. this.prefabInstance.destroy();
  228. }
  229. if (this.modelPrefab != null) {
  230. var deps = loader.getDependsRecursively(this.modelPrefab);
  231. loader.release(deps);
  232. }
  233. this.modelView.node.active = false;
  234. this.weaponCellListView.onHide();
  235. }
  236. private AddEvents(): void {
  237. this.equipWeaponNode.on(Node.EventType.TOUCH_START, this.TouchStartHandler, this);
  238. this.equipWeaponNode.on(Node.EventType.TOUCH_MOVE, this.TouchMoveHandler, this);
  239. this.equipWeaponNode.on(Node.EventType.TOUCH_END, this.TouchEndHandler, this);
  240. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
  241. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
  242. this.shopButton.node.on(ButtonComponent.EventType.CLICK, this.ShopButtonClickHandler, this);
  243. }
  244. private RemoveEvents(): void {
  245. this.equipWeaponNode.off(SystemEventType.TOUCH_START, this.TouchStartHandler, this);
  246. this.equipWeaponNode.off(SystemEventType.TOUCH_MOVE, this.TouchMoveHandler, this);
  247. this.equipWeaponNode.off(SystemEventType.TOUCH_END, this.TouchEndHandler, this);
  248. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
  249. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
  250. }
  251. private GameModelPropertyChanged(key: string): void {
  252. switch (key) {
  253. case GamePropertys.gold:
  254. this.CallNextFrame(this.RefreshGlod.bind(this));
  255. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  256. break;
  257. case GamePropertys.diamond:
  258. this.CallNextFrame(this.RefreshDiamond.bind(this));
  259. break;
  260. case GamePropertys.currentWeaponCellId:
  261. this.CallNextFrame(this.RefreshGunModel.bind(this));
  262. break;
  263. case GamePropertys.currentLevel:
  264. this.CallNextFrame(this.RefreshLevel.bind(this));
  265. break;
  266. case GamePropertys.synthesisMaxWeaponId:
  267. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  268. break;
  269. case GamePropertys.WeaponCell:
  270. this.HandTip.active = false;
  271. this.isNew = false;
  272. break;
  273. }
  274. }
  275. private RefreshLevel(): void {
  276. this.levelLabel.string = "第" + GameModel.single.currentLevel.toString() + "关";
  277. }
  278. private RefreshGlod(): void {
  279. if (this.glodLabel != null) {
  280. this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
  281. }
  282. this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
  283. }
  284. private RefreshDiamond(): void {
  285. if (this.diamondLabel != null) {
  286. this.diamondLabel.string = GameModel.single.diamond.toString();
  287. }
  288. }
  289. private weaponIcon: SpriteFrame;
  290. /**
  291. * 刷新快速购买按钮
  292. */
  293. private RefreshQuickBuy(): void {
  294. this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon;
  295. //价格
  296. let price: number = GameModel.single.GetQuickBuyPrice();
  297. // if(GameModel.single.gold<price){
  298. // this.quickBuyWeaponPriceLabel.color.set(Color.RED);
  299. // }else{
  300. // this.quickBuyWeaponPriceLabel.color.set(Color.WHITE);
  301. // }
  302. this.quickBuyWeaponPriceLabel.string = StringUtils.numberUtilsEn(GameModel.single.GetQuickBuyPrice());
  303. let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.CurrentQuickBuyWeaponId);
  304. loader.loadRes(weaponConfig.icon + "/spriteFrame", SpriteFrame, (err: Error, asset: SpriteFrame) => {
  305. if (err != null) {
  306. console.error("加载武器ICON出错!");
  307. }
  308. this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon = asset;
  309. })
  310. }
  311. /**
  312. * 购买武器
  313. */
  314. private QickBuyButtonClickHandler(): void {
  315. if (GameModel.single.FindWeaponEmptyCell() == null) {
  316. NoticeManager.ShowPrompt("没有武器槽位了!");
  317. return;
  318. }
  319. //价格
  320. let price: number = GameModel.single.GetQuickBuyPrice();
  321. //钱不够
  322. if (GameModel.single.gold < price) {
  323. NoticeManager.ShowPrompt("金币不足");
  324. return;
  325. }
  326. GameModel.single.BuyWeapon(0, GameModel.single.CurrentQuickBuyWeaponId);
  327. }
  328. private ShopButtonClickHandler(): void {
  329. GUIManager.single.Show(UIConst.SHOP_UI);
  330. this.HideSelf();
  331. }
  332. /**
  333. * 自动合成按钮点击
  334. */
  335. AutoSynthesisButtonClickHandler(): void {
  336. GUIManager.single.Show(UIConst.AUTO_SYNTHETIC_UI);
  337. this.HideSelf();
  338. }
  339. /**
  340. * 加速Buffer;
  341. */
  342. AccelerateButtonClickHandler(): void {
  343. GUIManager.single.Show(UIConst.ACCELERATE_UI);
  344. this.HideSelf();
  345. }
  346. /**
  347. * 兑换界面
  348. */
  349. ExchangeButtonClickHandler(): void {
  350. GUIManager.single.Show(UIConst.EXCHANGE_UI);
  351. this.HideSelf();
  352. }
  353. /**
  354. * 签到
  355. */
  356. SignButtonClickHandler(): void {
  357. GUIManager.single.Show(UIConst.SIGN_UI);
  358. }
  359. private startRotation: Quat = new Quat();
  360. private currentRotation: Quat = new Quat();
  361. private startPoint: Vec2 = new Vec2();
  362. private currentPoint: Vec2 = new Vec2();
  363. private TouchStartHandler(touch: Touch, touchEvent): void {
  364. let pos: Vec2 = touch.getUILocation();
  365. console.log(pos);
  366. this.startPoint.set(pos.x, pos.y);
  367. this.startRotation.x = this.modelView.node.rotation.x;
  368. this.startRotation.set(this.modelView.node.rotation.x, this.modelView.node.rotation.y, this.modelView.node.rotation.z, this.modelView.node.rotation.w);
  369. }
  370. private TouchMoveHandler(touch: EventTouch, touchEvent): void {
  371. let pos: Vec2 = touch.getUILocation();
  372. this.currentPoint.set(pos.x, pos.y);
  373. let dis: number = Vec2.distance(this.currentPoint, this.startPoint);
  374. let value: number = dis / view.getCanvasSize().width;
  375. if (this.currentPoint.x < this.startPoint.x) {
  376. value = -value;
  377. }
  378. Quat.rotateY(this.currentRotation, this.startRotation, value);
  379. Quat.normalize(this.currentRotation, this.currentRotation);
  380. this.modelView.node.setRotation(this.currentRotation);
  381. }
  382. private TouchEndHandler(...arg): void {
  383. console.log("拖拽模型结束");
  384. }
  385. StartGame(): void {
  386. if (GameModel.single.currentWeaponCell == null || GameModel.single.currentWeaponId < 0) {
  387. NoticeManager.ShowPrompt("请装配武器后再试!");
  388. return;
  389. }
  390. if (LiangLiangSDK.CanWuChu()) {
  391. GUIManager.single.Show(UIConst.FREE_USE_GUN_UI);
  392. } else {
  393. GUIManager.single.Show(UIConst.FREE_FENCE_UI);
  394. }
  395. }
  396. /**
  397. * 免费金币
  398. */
  399. getAdGold(): void {
  400. PlatformManager.showRewardedVideo(() => {
  401. GameModel.single.gold += GameModel.single.fullEarnings * 600;
  402. NoticeManager.ShowPrompt(`获得${StringUtils.numberUtilsEn(GameModel.single.fullEarnings * 600)}金币`);
  403. let weChat = PlatformManager.impl as WeChatPlatforms;
  404. if (weChat instanceof WeChatPlatforms) {
  405. weChat.branchAnalytics(branchIdType.FreeGold)
  406. }
  407. }, () => {
  408. NoticeManager.ShowPrompt(`未看完广告,无奖励`);
  409. })
  410. }
  411. /**
  412. * 心跳
  413. * @param dt
  414. */
  415. update(dt: number): void {
  416. super.update(dt);
  417. //空投
  418. let currentTime: number = director.getCurrentTime();
  419. if (currentTime - GameModel.single.lastDropBoxTime > 30000) {
  420. this.DropBoxAnimation.node.active = true;
  421. this.DropBoxAnimation.play("KongtouTip2");
  422. GameModel.single.lastDropBoxTime = currentTime;
  423. }
  424. //自动合成Buffer剩余时间
  425. let buffers: IBuffer[] = BufferManager.GetBufferGroup("AutoSynthesis");
  426. if (buffers != null && buffers.length > 0) {
  427. let buffer: IBuffer = buffers[0];
  428. let endTime: number = buffer.GetTimeRemaining();
  429. this.autoSynthesisLabel.string = StringUtils.TimeFormatting(endTime, ":", ":", ":", ":", ":", "");
  430. } else {
  431. this.autoSynthesisLabel.string = "自动合成";
  432. }
  433. //加速Buffer
  434. buffers = BufferManager.GetBufferGroup("Accelerate");
  435. if (buffers != null && buffers.length > 0) {
  436. this.bufferNode.active = true;
  437. } else {
  438. this.bufferNode.active = false;
  439. }
  440. }
  441. /**
  442. * 获取空投
  443. */
  444. GetDropBox(): void {
  445. this.DropBoxAnimation.node.active = false;
  446. GUIManager.single.Show(UIConst.DROP_BOX_UI, false);
  447. this.HideSelf();
  448. }
  449. /**
  450. * 更多游戏
  451. */
  452. onClickMoreButton(): void {
  453. let qq = PlatformManager.impl as QQPlatform;
  454. if (qq instanceof QQPlatform) {
  455. qq.showAppBox();
  456. }
  457. }
  458. }