PrepareMediator.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. import { AnimationComponent, 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 { 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:AnimationComponent
  23. })
  24. DropBoxAnimation:AnimationComponent=null;
  25. @property({
  26. type: Node
  27. })
  28. HandTip: Node = null;
  29. @property({
  30. type: Node
  31. })
  32. bufferNode: Node = null;
  33. @property({
  34. type: LabelComponent
  35. })
  36. glodLabel: LabelComponent = null;
  37. @property({
  38. type: LabelComponent
  39. })
  40. glodLabel1: LabelComponent = null;
  41. @property({
  42. type: LabelComponent
  43. })
  44. diamondLabel: LabelComponent = null;
  45. @property({
  46. type: LabelComponent
  47. })
  48. gunNameLabel: LabelComponent = null;
  49. @property({
  50. type: LabelComponent
  51. })
  52. levelLabel: LabelComponent = null;
  53. @property({
  54. type: LabelComponent
  55. })
  56. weaponInfoLabel: LabelComponent = null;
  57. /**
  58. * 商城按钮
  59. */
  60. @property({
  61. type: ButtonComponent
  62. })
  63. shopButton: ButtonComponent = null;
  64. /**
  65. * 自动合成文本
  66. */
  67. @property({
  68. type: LabelComponent
  69. })
  70. autoSynthesisLabel: LabelComponent = null;
  71. /**
  72. * 快捷购买武器按钮
  73. */
  74. @property({
  75. type: ButtonComponent
  76. })
  77. quickBuyButton: ButtonComponent = null;
  78. /**
  79. * 快捷购买武器ICON
  80. */
  81. @property({
  82. type: SpriteComponent
  83. })
  84. quickBuyWeaponIcon: SpriteComponent = null;
  85. /**
  86. * 快捷购买武器价格
  87. */
  88. @property({
  89. type: LabelComponent
  90. })
  91. quickBuyWeaponPriceLabel: LabelComponent = null;
  92. /**
  93. * 武器拖拽Icon
  94. */
  95. @property({
  96. type: SpriteComponent
  97. })
  98. weaponDragIcon: SpriteComponent = null;
  99. /**
  100. * 删除按钮节点(用于拖拽判断)
  101. */
  102. @property({
  103. type: Node
  104. })
  105. deleteWeaponNode: Node = null;
  106. /**
  107. * 装配武器节点(用于拖拽判断)
  108. */
  109. @property({
  110. type: Node
  111. })
  112. equipWeaponNode: Node = null;
  113. /**
  114. * 装配格子预制体
  115. */
  116. @property({
  117. type: Prefab
  118. })
  119. WeaponCellPrefab: Prefab = null;
  120. /**
  121. * 武器列表
  122. */
  123. @property({
  124. type: LayoutComponent
  125. })
  126. weaponList: LayoutComponent = null;
  127. private modelView: ModelComponent;
  128. private modelPrefab: Prefab;
  129. private prefabInstance: Node;
  130. private prefabModelComponent: ModelComponent;
  131. private weaponCellListView: WeaponCellListView;
  132. private isNew:boolean=true;
  133. private get isNewPlayer():boolean{
  134. return this.isNew&&GameModel.single.isNewPlayer;
  135. }
  136. onLoad(): void {
  137. this.weaponCellListView = new WeaponCellListView(this);
  138. }
  139. OnShow(data?: any): void {
  140. //清空试用栅栏
  141. GameModel.single.trialFenceId=-1;
  142. super.OnShow(data);
  143. this.RefreshGunModel();
  144. this.RefreshGlod();
  145. this.RefreshDiamond();
  146. this.RefreshLevel();
  147. this.RefreshQuickBuy();
  148. this.AddEvents();
  149. this.weaponCellListView.OnShow();
  150. SoundManager.single.PlayMusic("sounds/main");
  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.currentWeaponCellId:
  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. /**
  339. * 签到
  340. */
  341. SignButtonClickHandler():void{
  342. GUIManager.single.Show(UIConst.SIGN_UI);
  343. this.HideSelf();
  344. }
  345. private startRotation: Quat = new Quat();
  346. private currentRotation: Quat = new Quat();
  347. private startPoint: Vec2 = new Vec2();
  348. private currentPoint: Vec2 = new Vec2();
  349. private TouchStartHandler(touch: Touch, touchEvent): void {
  350. let pos: Vec2 = touch.getUILocation();
  351. console.log(pos);
  352. this.startPoint.set(pos.x, pos.y);
  353. this.startRotation.x = this.modelView.node.rotation.x;
  354. this.startRotation.set(this.modelView.node.rotation.x, this.modelView.node.rotation.y, this.modelView.node.rotation.z, this.modelView.node.rotation.w);
  355. }
  356. private TouchMoveHandler(touch: EventTouch, touchEvent): void {
  357. let pos: Vec2 = touch.getUILocation();
  358. this.currentPoint.set(pos.x, pos.y);
  359. let dis: number = Vec2.distance(this.currentPoint, this.startPoint);
  360. let value: number = dis / view.getCanvasSize().width;
  361. if (this.currentPoint.x < this.startPoint.x) {
  362. value = -value;
  363. }
  364. Quat.rotateY(this.currentRotation, this.startRotation, value);
  365. Quat.normalize(this.currentRotation, this.currentRotation);
  366. this.modelView.node.setRotation(this.currentRotation);
  367. }
  368. private TouchEndHandler(...arg): void {
  369. console.log("拖拽模型结束");
  370. }
  371. StartGame(): void {
  372. if(GameModel.single.currentWeaponCell==null||GameModel.single.currentWeaponId<0){
  373. NoticeManager.ShowPrompt("请装配武器后再试!");
  374. return;
  375. }
  376. GUIManager.single.Show(UIConst.FREE_FENCE_UI);
  377. // if(GameConfigManager.GetNextFenceID(GameModel.single.currentFenceId)>0){
  378. // GUIManager.single.Show(UIConst.FREE_FENCE_UI);
  379. // return;
  380. // }
  381. // //隐藏
  382. // this.HideSelf();
  383. // PlatformManager.hideBanner();
  384. // SceneManager.single.Swicth("FightingScene");
  385. // let weChat = PlatformManager.impl as WeChatPlatform
  386. // if(weChat instanceof WeChatPlatform){
  387. // weChat.startBranchAnalytics(String(GameModel.single.currentLevel));
  388. // }
  389. }
  390. /**
  391. * 免费金币
  392. */
  393. getAdGold(): void {
  394. PlatformManager.showRewardedVideo(()=>{
  395. GameModel.single.gold += GameModel.single.fullEarnings * 600;
  396. NoticeManager.ShowPrompt(`获得${StringUtils.numberUtilsEn(GameModel.single.fullEarnings * 600)}金币`);
  397. let weChat = PlatformManager.impl as WeChatPlatform;
  398. if(weChat instanceof WeChatPlatform){
  399. weChat.branchAnalytics(branchIdType.FreeGold)
  400. }
  401. },()=>{
  402. NoticeManager.ShowPrompt(`未看完广告,无奖励`);
  403. })
  404. }
  405. /**
  406. * 心跳
  407. * @param dt
  408. */
  409. update(dt:number):void{
  410. super.update(dt);
  411. //空投
  412. let currentTime:number=director.getCurrentTime();
  413. if(currentTime-GameModel.single.lastDropBoxTime>30000){
  414. this.DropBoxAnimation.node.active=true;
  415. this.DropBoxAnimation.play("KongtouTip2");
  416. GameModel.single.lastDropBoxTime=currentTime;
  417. }
  418. //自动合成Buffer剩余时间
  419. let buffers:IBuffer[]=BufferManager.GetBufferGroup("AutoSynthesis");
  420. if(buffers!=null&&buffers.length>0){
  421. let buffer:IBuffer=buffers[0];
  422. let endTime:number=buffer.GetTimeRemaining();
  423. this.autoSynthesisLabel.string=StringUtils.TimeFormatting(endTime,":",":",":",":",":","");
  424. }else{
  425. this.autoSynthesisLabel.string="自动合成";
  426. }
  427. //加速Buffer
  428. buffers=BufferManager.GetBufferGroup("Accelerate");
  429. if(buffers!=null&&buffers.length>0){
  430. this.bufferNode.active=true;
  431. }else{
  432. this.bufferNode.active=false;
  433. }
  434. }
  435. /**
  436. * 获取空投
  437. */
  438. GetDropBox():void{
  439. this.DropBoxAnimation.node.active=false;
  440. GUIManager.single.Show(UIConst.DROP_BOX_UI,false);
  441. this.HideSelf();
  442. }
  443. }