PrepareMediator.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. super.OnShow(data);
  144. this.RefreshGunModel();
  145. this.RefreshGlod();
  146. this.RefreshDiamond();
  147. this.RefreshLevel();
  148. this.RefreshQuickBuy();
  149. this.AddEvents();
  150. this.weaponCellListView.OnShow();
  151. SoundManager.single.PlayMusic("sounds/main");
  152. if(this.isNewPlayer){
  153. this.HandTip.active=true;
  154. }else{
  155. this.HandTip.active=false;
  156. }
  157. if(LiangLiangSDK.CanWuChu() == true){
  158. // GUIManager.single.Show(UIConst.ADVERTISING_UI)
  159. PlatformManager.showBanner();
  160. }else{
  161. PlatformManager.showBanner();
  162. }
  163. //签到如果有奖励的话
  164. if(GameModel.single.GetSignAwardIndex()>=0){
  165. GUIManager.single.Show(UIConst.SIGN_UI);
  166. this.HideSelf();
  167. }
  168. }
  169. /**
  170. * 初始化枪的模型
  171. */
  172. private RefreshGunModel(): void {
  173. if (this.modelView == null) {
  174. let modelNode: Node = find("ModelView", this.node);
  175. this.modelView = modelNode.getComponent(ModelComponent);
  176. }
  177. if (this.modelPrefab) {
  178. this.modelView.mesh = null;
  179. let target=this.modelView.materials[0];
  180. target.setProperty("mainTexture", null);
  181. this.prefabInstance.destroy();
  182. this.prefabInstance=null;
  183. let deps:string[]=loader.getDependsRecursively(this.modelPrefab);
  184. loader.release(deps);
  185. this.modelPrefab=null;
  186. }
  187. //武器为空
  188. if(GameModel.single.currentWeaponId<0){
  189. this.modelView.node.active = false;
  190. this.gunNameLabel.string="";
  191. this.weaponInfoLabel.string="";
  192. return;
  193. }else{
  194. this.modelView.node.active = true;
  195. let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
  196. loader.loadRes(weaponConfig.prefab, Prefab, (err, prefab) => {
  197. if (err) {
  198. console.error("加载武器出错");
  199. }
  200. this.modelPrefab = prefab;
  201. this.prefabInstance = instantiate(prefab);
  202. this.prefabModelComponent = this.prefabInstance.getComponentInChildren(ModelComponent);
  203. //更换贴图
  204. let source = this.prefabModelComponent.materials[0];
  205. let target = this.modelView.materials[0];
  206. let texture = source.getProperty("mainTexture");
  207. target.setProperty("mainTexture", texture);
  208. //更换模型
  209. this.modelView.mesh = this.prefabModelComponent.mesh;
  210. });
  211. if (this.gunNameLabel != null) {
  212. this.gunNameLabel.string = weaponConfig.name;
  213. }
  214. //描述
  215. this.weaponInfoLabel.string=weaponConfig.desc;
  216. }
  217. }
  218. OnHide(): void {
  219. this.RemoveEvents();
  220. this.modelView.mesh = null;
  221. let target=this.modelView.materials[0];
  222. target.setProperty("mainTexture", null);
  223. if (this.prefabInstance) {
  224. this.prefabInstance.destroy();
  225. }
  226. if (this.modelPrefab != null) {
  227. var deps = loader.getDependsRecursively(this.modelPrefab);
  228. loader.release(deps);
  229. }
  230. this.modelView.node.active = false;
  231. this.weaponCellListView.onHide();
  232. }
  233. private AddEvents(): void {
  234. this.equipWeaponNode.on(Node.EventType.TOUCH_START, this.TouchStartHandler, this);
  235. this.equipWeaponNode.on(Node.EventType.TOUCH_MOVE, this.TouchMoveHandler, this);
  236. this.equipWeaponNode.on(Node.EventType.TOUCH_END, this.TouchEndHandler, this);
  237. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
  238. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
  239. this.shopButton.node.on(ButtonComponent.EventType.CLICK, this.ShopButtonClickHandler, this);
  240. }
  241. private RemoveEvents(): void {
  242. this.equipWeaponNode.off(SystemEventType.TOUCH_START, this.TouchStartHandler, this);
  243. this.equipWeaponNode.off(SystemEventType.TOUCH_MOVE, this.TouchMoveHandler, this);
  244. this.equipWeaponNode.off(SystemEventType.TOUCH_END, this.TouchEndHandler, this);
  245. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
  246. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
  247. }
  248. private GameModelPropertyChanged(key: string): void {
  249. switch (key) {
  250. case GamePropertys.gold:
  251. this.CallNextFrame(this.RefreshGlod.bind(this));
  252. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  253. break;
  254. case GamePropertys.diamond:
  255. this.CallNextFrame(this.RefreshDiamond.bind(this));
  256. break;
  257. case GamePropertys.currentWeaponCellId:
  258. this.CallNextFrame(this.RefreshGunModel.bind(this));
  259. break;
  260. case GamePropertys.currentLevel:
  261. this.CallNextFrame(this.RefreshLevel.bind(this));
  262. break;
  263. case GamePropertys.synthesisMaxWeaponId:
  264. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  265. break;
  266. case GamePropertys.WeaponCell:
  267. this.HandTip.active=false;
  268. this.isNew=false;
  269. break;
  270. }
  271. }
  272. private RefreshLevel(): void {
  273. this.levelLabel.string = "第" + GameModel.single.currentLevel.toString() + "关";
  274. }
  275. private RefreshGlod(): void {
  276. if (this.glodLabel != null) {
  277. this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
  278. }
  279. this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
  280. }
  281. private RefreshDiamond(): void {
  282. if (this.diamondLabel != null) {
  283. this.diamondLabel.string = GameModel.single.diamond.toString();
  284. }
  285. }
  286. private weaponIcon: SpriteFrame;
  287. /**
  288. * 刷新快速购买按钮
  289. */
  290. private RefreshQuickBuy(): void {
  291. this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon;
  292. //价格
  293. let price: number = GameModel.single.GetQuickBuyPrice();
  294. // if(GameModel.single.gold<price){
  295. // this.quickBuyWeaponPriceLabel.color.set(Color.RED);
  296. // }else{
  297. // this.quickBuyWeaponPriceLabel.color.set(Color.WHITE);
  298. // }
  299. this.quickBuyWeaponPriceLabel.string = StringUtils.numberUtilsEn(GameModel.single.GetQuickBuyPrice());
  300. let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.CurrentQuickBuyWeaponId);
  301. loader.loadRes(weaponConfig.icon + "/spriteFrame", SpriteFrame, (err: Error, asset: SpriteFrame) => {
  302. if (err != null) {
  303. console.error("加载武器ICON出错!");
  304. }
  305. this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon = asset;
  306. })
  307. }
  308. /**
  309. * 购买武器
  310. */
  311. private QickBuyButtonClickHandler(): void {
  312. if (GameModel.single.FindWeaponEmptyCell() == null) {
  313. NoticeManager.ShowPrompt("没有武器槽位了!");
  314. return;
  315. }
  316. //价格
  317. let price: number = GameModel.single.GetQuickBuyPrice();
  318. //钱不够
  319. if (GameModel.single.gold < price) {
  320. NoticeManager.ShowPrompt("金币不足");
  321. return;
  322. }
  323. GameModel.single.BuyWeapon(0, GameModel.single.CurrentQuickBuyWeaponId);
  324. }
  325. private ShopButtonClickHandler(): void {
  326. GUIManager.single.Show(UIConst.SHOP_UI);
  327. this.HideSelf();
  328. }
  329. /**
  330. * 自动合成按钮点击
  331. */
  332. AutoSynthesisButtonClickHandler():void{
  333. GUIManager.single.Show(UIConst.AUTO_SYNTHETIC_UI);
  334. this.HideSelf();
  335. }
  336. /**
  337. * 加速Buffer;
  338. */
  339. AccelerateButtonClickHandler():void{
  340. GUIManager.single.Show(UIConst.ACCELERATE_UI);
  341. this.HideSelf();
  342. }
  343. /**
  344. * 兑换界面
  345. */
  346. ExchangeButtonClickHandler():void{
  347. GUIManager.single.Show(UIConst.EXCHANGE_UI);
  348. this.HideSelf();
  349. }
  350. /**
  351. * 签到
  352. */
  353. SignButtonClickHandler():void{
  354. GUIManager.single.Show(UIConst.SIGN_UI);
  355. this.HideSelf();
  356. }
  357. private startRotation: Quat = new Quat();
  358. private currentRotation: Quat = new Quat();
  359. private startPoint: Vec2 = new Vec2();
  360. private currentPoint: Vec2 = new Vec2();
  361. private TouchStartHandler(touch: Touch, touchEvent): void {
  362. let pos: Vec2 = touch.getUILocation();
  363. console.log(pos);
  364. this.startPoint.set(pos.x, pos.y);
  365. this.startRotation.x = this.modelView.node.rotation.x;
  366. this.startRotation.set(this.modelView.node.rotation.x, this.modelView.node.rotation.y, this.modelView.node.rotation.z, this.modelView.node.rotation.w);
  367. }
  368. private TouchMoveHandler(touch: EventTouch, touchEvent): void {
  369. let pos: Vec2 = touch.getUILocation();
  370. this.currentPoint.set(pos.x, pos.y);
  371. let dis: number = Vec2.distance(this.currentPoint, this.startPoint);
  372. let value: number = dis / view.getCanvasSize().width;
  373. if (this.currentPoint.x < this.startPoint.x) {
  374. value = -value;
  375. }
  376. Quat.rotateY(this.currentRotation, this.startRotation, value);
  377. Quat.normalize(this.currentRotation, this.currentRotation);
  378. this.modelView.node.setRotation(this.currentRotation);
  379. }
  380. private TouchEndHandler(...arg): void {
  381. console.log("拖拽模型结束");
  382. }
  383. StartGame(): void {
  384. if(GameModel.single.currentWeaponCell==null||GameModel.single.currentWeaponId<0){
  385. NoticeManager.ShowPrompt("请装配武器后再试!");
  386. return;
  387. }
  388. GUIManager.single.Show(UIConst.FREE_FENCE_UI);
  389. // if(GameConfigManager.GetNextFenceID(GameModel.single.currentFenceId)>0){
  390. // GUIManager.single.Show(UIConst.FREE_FENCE_UI);
  391. // return;
  392. // }
  393. // //隐藏
  394. // this.HideSelf();
  395. // PlatformManager.hideBanner();
  396. // SceneManager.single.Swicth("FightingScene");
  397. // let weChat = PlatformManager.impl as WeChatPlatform
  398. // if(weChat instanceof WeChatPlatform){
  399. // weChat.startBranchAnalytics(String(GameModel.single.currentLevel));
  400. // }
  401. }
  402. /**
  403. * 免费金币
  404. */
  405. getAdGold(): void {
  406. PlatformManager.showRewardedVideo(()=>{
  407. GameModel.single.gold += GameModel.single.fullEarnings * 600;
  408. NoticeManager.ShowPrompt(`获得${StringUtils.numberUtilsEn(GameModel.single.fullEarnings * 600)}金币`);
  409. let weChat = PlatformManager.impl as WeChatPlatform;
  410. if(weChat instanceof WeChatPlatform){
  411. weChat.branchAnalytics(branchIdType.FreeGold)
  412. }
  413. },()=>{
  414. NoticeManager.ShowPrompt(`未看完广告,无奖励`);
  415. })
  416. }
  417. /**
  418. * 心跳
  419. * @param dt
  420. */
  421. update(dt:number):void{
  422. super.update(dt);
  423. //空投
  424. let currentTime:number=director.getCurrentTime();
  425. if(currentTime-GameModel.single.lastDropBoxTime>30000){
  426. this.DropBoxAnimation.node.active=true;
  427. this.DropBoxAnimation.play("KongtouTip2");
  428. GameModel.single.lastDropBoxTime=currentTime;
  429. }
  430. //自动合成Buffer剩余时间
  431. let buffers:IBuffer[]=BufferManager.GetBufferGroup("AutoSynthesis");
  432. if(buffers!=null&&buffers.length>0){
  433. let buffer:IBuffer=buffers[0];
  434. let endTime:number=buffer.GetTimeRemaining();
  435. this.autoSynthesisLabel.string=StringUtils.TimeFormatting(endTime,":",":",":",":",":","");
  436. }else{
  437. this.autoSynthesisLabel.string="自动合成";
  438. }
  439. //加速Buffer
  440. buffers=BufferManager.GetBufferGroup("Accelerate");
  441. if(buffers!=null&&buffers.length>0){
  442. this.bufferNode.active=true;
  443. }else{
  444. this.bufferNode.active=false;
  445. }
  446. }
  447. /**
  448. * 获取空投
  449. */
  450. GetDropBox():void{
  451. this.DropBoxAnimation.node.active=false;
  452. GUIManager.single.Show(UIConst.DROP_BOX_UI,false);
  453. this.HideSelf();
  454. }
  455. }