PrepareMediator.ts 16 KB

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