PrepareMediator.ts 14 KB

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