PrepareMediator.ts 16 KB

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