PrepareMediator.ts 15 KB

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