PrepareMediator.ts 15 KB

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