PrepareMediator.ts 15 KB

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