PrepareMediator.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. super.OnShow(data);
  127. this.RefreshGunModel();
  128. this.RefreshGlod();
  129. this.RefreshDiamond();
  130. this.RefreshLevel();
  131. this.RefreshQuickBuy();
  132. this.AddEvents();
  133. this.weaponCellListView.OnShow();
  134. SoundManager.single.PlayMusic("sounds/main");
  135. PlatformManager.showBanner();
  136. }
  137. /**
  138. * 初始化枪的模型
  139. */
  140. private RefreshGunModel(): void {
  141. if (this.modelView == null) {
  142. let modelNode: Node = find("ModelView", this.node);
  143. this.modelView = modelNode.getComponent(ModelComponent);
  144. }
  145. this.modelView.node.active = true;
  146. let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
  147. if (this.modelPrefab) {
  148. this.modelView.mesh = null;
  149. let target=this.modelView.materials[0];
  150. target.setProperty("mainTexture", null);
  151. this.prefabInstance.destroy();
  152. this.prefabInstance=null;
  153. let deps:string[]=loader.getDependsRecursively(this.modelPrefab);
  154. loader.release(deps);
  155. this.modelPrefab=null;
  156. }
  157. loader.loadRes(weaponConfig.prefab, Prefab, (err, prefab) => {
  158. if (err) {
  159. console.error("加载武器出错");
  160. }
  161. this.modelPrefab = prefab;
  162. this.prefabInstance = instantiate(prefab);
  163. this.prefabModelComponent = this.prefabInstance.getComponentInChildren(ModelComponent);
  164. //更换贴图
  165. let source = this.prefabModelComponent.materials[0];
  166. let target = this.modelView.materials[0];
  167. let texture = source.getProperty("mainTexture");
  168. target.setProperty("mainTexture", texture);
  169. //更换模型
  170. this.modelView.mesh = this.prefabModelComponent.mesh;
  171. });
  172. if (this.gunNameLabel != null) {
  173. this.gunNameLabel.string = weaponConfig.name;
  174. }
  175. }
  176. OnHide(): void {
  177. this.RemoveEvents();
  178. this.modelView.mesh = null;
  179. let target=this.modelView.materials[0];
  180. target.setProperty("mainTexture", null);
  181. if (this.prefabInstance) {
  182. this.prefabInstance.destroy();
  183. }
  184. if (this.modelPrefab != null) {
  185. var deps = loader.getDependsRecursively(this.modelPrefab);
  186. loader.release(deps);
  187. }
  188. this.modelView.node.active = false;
  189. this.weaponCellListView.onHide();
  190. }
  191. private AddEvents(): void {
  192. this.equipWeaponNode.on(Node.EventType.TOUCH_START, this.TouchStartHandler, this);
  193. this.equipWeaponNode.on(Node.EventType.TOUCH_MOVE, this.TouchMoveHandler, this);
  194. this.equipWeaponNode.on(Node.EventType.TOUCH_END, this.TouchEndHandler, this);
  195. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
  196. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
  197. this.shopButton.node.on(ButtonComponent.EventType.CLICK, this.ShopButtonClickHandler, this);
  198. }
  199. private RemoveEvents(): void {
  200. this.equipWeaponNode.off(SystemEventType.TOUCH_START, this.TouchStartHandler, this);
  201. this.equipWeaponNode.off(SystemEventType.TOUCH_MOVE, this.TouchMoveHandler, this);
  202. this.equipWeaponNode.off(SystemEventType.TOUCH_END, this.TouchEndHandler, this);
  203. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
  204. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
  205. }
  206. private GameModelPropertyChanged(key: string): void {
  207. switch (key) {
  208. case GamePropertys.gold:
  209. this.CallNextFrame(this.RefreshGlod.bind(this));
  210. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  211. break;
  212. case GamePropertys.diamond:
  213. this.CallNextFrame(this.RefreshDiamond.bind(this));
  214. break;
  215. case GamePropertys.currentWeaponId:
  216. this.CallNextFrame(this.RefreshGunModel.bind(this));
  217. break;
  218. case GamePropertys.currentLevel:
  219. this.CallNextFrame(this.RefreshLevel.bind(this));
  220. break;
  221. case GamePropertys.synthesisMaxWeaponId:
  222. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  223. break;
  224. }
  225. }
  226. private RefreshLevel(): void {
  227. this.levelLabel.string = "第" + GameModel.single.currentLevel.toString() + "关";
  228. }
  229. private RefreshGlod(): void {
  230. if (this.glodLabel != null) {
  231. this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
  232. }
  233. this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
  234. }
  235. private RefreshDiamond(): void {
  236. if (this.diamondLabel != null) {
  237. this.diamondLabel.string = GameModel.single.diamond.toString();
  238. }
  239. }
  240. private weaponIcon: SpriteFrame;
  241. /**
  242. * 刷新快速购买按钮
  243. */
  244. private RefreshQuickBuy(): void {
  245. this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon;
  246. //价格
  247. let price: number = GameModel.single.GetQuickBuyPrice();
  248. // if(GameModel.single.gold<price){
  249. // this.quickBuyWeaponPriceLabel.color.set(Color.RED);
  250. // }else{
  251. // this.quickBuyWeaponPriceLabel.color.set(Color.WHITE);
  252. // }
  253. this.quickBuyWeaponPriceLabel.string = StringUtils.numberUtilsEn(GameModel.single.GetQuickBuyPrice());
  254. let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.CurrentQuickBuyWeaponId);
  255. loader.loadRes(weaponConfig.icon + "/spriteFrame", SpriteFrame, (err: Error, asset: SpriteFrame) => {
  256. if (err != null) {
  257. console.error("加载武器ICON出错!");
  258. }
  259. this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon = asset;
  260. })
  261. }
  262. /**
  263. * 购买武器
  264. */
  265. private QickBuyButtonClickHandler(): void {
  266. if (GameModel.single.FindWeaponEmptyCell() == null) {
  267. NoticeManager.ShowPrompt("没有武器槽位了!");
  268. return;
  269. }
  270. //价格
  271. let price: number = GameModel.single.GetQuickBuyPrice();
  272. //钱不够
  273. if (GameModel.single.gold < price) {
  274. NoticeManager.ShowPrompt("金币不足");
  275. return;
  276. }
  277. GameModel.single.BuyWeapon(0, GameModel.single.CurrentQuickBuyWeaponId);
  278. }
  279. private ShopButtonClickHandler(): void {
  280. GUIManager.single.Show(UIConst.SHOP_UI);
  281. this.HideSelf();
  282. }
  283. /**
  284. * 自动合成按钮点击
  285. */
  286. AutoSynthesisButtonClickHandler():void{
  287. GUIManager.single.Show(UIConst.AUTO_SYNTHETIC_UI);
  288. this.HideSelf();
  289. }
  290. /**
  291. * 加速Buffer;
  292. */
  293. AccelerateButtonClickHandler():void{
  294. GUIManager.single.Show(UIConst.ACCELERATE_UI);
  295. this.HideSelf();
  296. }
  297. /**
  298. * 兑换界面
  299. */
  300. ExchangeButtonClickHandler():void{
  301. GUIManager.single.Show(UIConst.EXCHANGE_UI);
  302. this.HideSelf();
  303. }
  304. private startRotation: Quat = new Quat();
  305. private currentRotation: Quat = new Quat();
  306. private startPoint: Vec2 = new Vec2();
  307. private currentPoint: Vec2 = new Vec2();
  308. private TouchStartHandler(touch: Touch, touchEvent): void {
  309. let pos: Vec2 = touch.getUILocation();
  310. console.log(pos);
  311. this.startPoint.set(pos.x, pos.y);
  312. this.startRotation.x = this.modelView.node.rotation.x;
  313. this.startRotation.set(this.modelView.node.rotation.x, this.modelView.node.rotation.y, this.modelView.node.rotation.z, this.modelView.node.rotation.w);
  314. }
  315. private TouchMoveHandler(touch: EventTouch, touchEvent): void {
  316. let pos: Vec2 = touch.getUILocation();
  317. this.currentPoint.set(pos.x, pos.y);
  318. let dis: number = Vec2.distance(this.currentPoint, this.startPoint);
  319. let value: number = dis / view.getCanvasSize().width;
  320. if (this.currentPoint.x < this.startPoint.x) {
  321. value = -value;
  322. }
  323. Quat.rotateY(this.currentRotation, this.startRotation, value);
  324. Quat.normalize(this.currentRotation, this.currentRotation);
  325. this.modelView.node.setRotation(this.currentRotation);
  326. }
  327. private TouchEndHandler(...arg): void {
  328. console.log("拖拽模型结束");
  329. }
  330. StartGame(): void {
  331. this.HideSelf();
  332. PlatformManager.hideBanner();
  333. SceneManager.single.Swicth("FightingScene");
  334. let weChat = PlatformManager.impl as WeChatPlatform
  335. if(weChat instanceof WeChatPlatform){
  336. weChat.startBranchAnalytics(String(GameModel.single.currentLevel));
  337. }
  338. }
  339. /**
  340. * 免费金币
  341. */
  342. getAdGold(): void {
  343. PlatformManager.showRewardedVideo(()=>{
  344. GameModel.single.gold += GameModel.single.fullEarnings * 600;
  345. NoticeManager.ShowPrompt(`获得${StringUtils.numberUtilsEn(GameModel.single.fullEarnings * 600)}金币`);
  346. },()=>{
  347. NoticeManager.ShowPrompt(`未看完广告,无奖励`);
  348. })
  349. }
  350. /**
  351. * 心跳
  352. * @param dt
  353. */
  354. update(dt:number):void{
  355. super.update(dt);
  356. //自动合成Buffer剩余时间
  357. let buffers:IBuffer[]=BufferManager.GetBufferGroup("AutoSynthesis");
  358. if(buffers!=null&&buffers.length>0){
  359. let buffer:IBuffer=buffers[0];
  360. let endTime:number=buffer.GetTimeRemaining();
  361. this.autoSynthesisLabel.string=StringUtils.TimeFormatting(endTime,":",":",":",":",":","");
  362. }else{
  363. this.autoSynthesisLabel.string="自动合成";
  364. }
  365. //加速Buffer
  366. buffers=BufferManager.GetBufferGroup("Accelerate");
  367. if(buffers!=null&&buffers.length>0){
  368. this.bufferNode.active=true;
  369. }else{
  370. this.bufferNode.active=false;
  371. }
  372. }
  373. }