PrepareMediator.ts 14 KB

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