PrepareMediator.ts 15 KB

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