PrepareMediator.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import { ButtonComponent, EventTouch, find, instantiate, LabelComponent, LayoutComponent, loader, ModelComponent, Node, Prefab, Quat, SpriteComponent, SpriteFrame, SystemEventType, Touch, Vec2, view, _decorator } from 'cc';
  2. import { GUIManager } from '../../../engines/gui/GUIManager';
  3. import { GUIMediator } from '../../../engines/gui/GUIMediator';
  4. import { DataModelEventType } from '../../../engines/models/DataModelEventType';
  5. import { NoticeManager } from '../../../engines/notices/NoticeManager';
  6. import { SceneManager } from '../../../engines/scenes/SceneManager';
  7. import { SoundManager } from '../../../engines/sounds/SoundManager';
  8. import { PlatformManager } from '../../../Platform/PlatformManager';
  9. import GameConfigManager from '../../models/GameConfigManager';
  10. import { GameModel } from '../../models/GameModel';
  11. import { GamePropertys } from '../../models/GamePropertys';
  12. import { UIConst } from '../UIConst';
  13. import { WeaponCellListView } from './WeaponCellListView';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('PrepareMediator')
  16. export class PrepareMediator extends GUIMediator {
  17. @property({
  18. type: LabelComponent
  19. })
  20. glodLabel: LabelComponent = null;
  21. @property({
  22. type: LabelComponent
  23. })
  24. glodLabel1: LabelComponent = null;
  25. @property({
  26. type: LabelComponent
  27. })
  28. diamondLabel: LabelComponent = null;
  29. @property({
  30. type: LabelComponent
  31. })
  32. gunNameLabel: LabelComponent = null;
  33. @property({
  34. type: LabelComponent
  35. })
  36. levelLabel: LabelComponent = null;
  37. /**
  38. * 商城按钮
  39. */
  40. @property({
  41. type: ButtonComponent
  42. })
  43. shopButton: ButtonComponent = null;
  44. /**
  45. * 快捷购买武器按钮
  46. */
  47. @property({
  48. type: ButtonComponent
  49. })
  50. quickBuyButton: ButtonComponent = null;
  51. /**
  52. * 快捷购买武器ICON
  53. */
  54. @property({
  55. type: SpriteComponent
  56. })
  57. quickBuyWeaponIcon: SpriteComponent = null;
  58. /**
  59. * 快捷购买武器价格
  60. */
  61. @property({
  62. type: LabelComponent
  63. })
  64. quickBuyWeaponPriceLabel: LabelComponent = null;
  65. /**
  66. * 武器拖拽Icon
  67. */
  68. @property({
  69. type: SpriteComponent
  70. })
  71. weaponDragIcon: SpriteComponent = null;
  72. /**
  73. * 删除按钮节点(用于拖拽判断)
  74. */
  75. @property({
  76. type: Node
  77. })
  78. deleteWeaponNode: Node = null;
  79. /**
  80. * 装配武器节点(用于拖拽判断)
  81. */
  82. @property({
  83. type: Node
  84. })
  85. equipWeaponNode: Node = null;
  86. /**
  87. * 装配格子预制体
  88. */
  89. @property({
  90. type: Prefab
  91. })
  92. WeaponCellPrefab: Prefab = null;
  93. /**
  94. * 武器列表
  95. */
  96. @property({
  97. type: LayoutComponent
  98. })
  99. weaponList: LayoutComponent = null;
  100. private modelView: ModelComponent;
  101. private modelPrefab: Prefab;
  102. private prefabInstance: Node;
  103. private prefabModelComponent: ModelComponent;
  104. private weaponCellListView: WeaponCellListView;
  105. onLoad(): void {
  106. this.weaponCellListView = new WeaponCellListView(this);
  107. }
  108. OnShow(data?: any): void {
  109. super.OnShow(data);
  110. this.RefreshGunModel();
  111. this.RefreshGlod();
  112. this.RefreshDiamond();
  113. this.RefreshLevel();
  114. this.RefreshQuickBuy();
  115. this.AddEvents();
  116. this.weaponCellListView.OnShow();
  117. SoundManager.single.PlayMusic("sounds/main");
  118. PlatformManager.showBanner();
  119. }
  120. /**
  121. * 初始化枪的模型
  122. */
  123. private RefreshGunModel(): void {
  124. if (this.modelView == null) {
  125. let modelNode: Node = find("ModelView", this.node);
  126. this.modelView = modelNode.getComponent(ModelComponent);
  127. }
  128. this.modelView.node.active = true;
  129. let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
  130. if (this.modelPrefab) {
  131. this.modelView.mesh = null;
  132. let target=this.modelView.materials[0];
  133. target.setProperty("mainTexture", null);
  134. this.prefabInstance.destroy();
  135. this.prefabInstance=null;
  136. let deps:string[]=loader.getDependsRecursively(this.modelPrefab);
  137. loader.release(deps);
  138. this.modelPrefab=null;
  139. }
  140. loader.loadRes(weaponConfig.prefab, Prefab, (err, prefab) => {
  141. if (err) {
  142. console.error("加载武器出错");
  143. }
  144. this.modelPrefab = prefab;
  145. this.prefabInstance = instantiate(prefab);
  146. this.prefabModelComponent = this.prefabInstance.getComponentInChildren(ModelComponent);
  147. //更换贴图
  148. let source = this.prefabModelComponent.materials[0];
  149. let target = this.modelView.materials[0];
  150. let texture = source.getProperty("mainTexture");
  151. target.setProperty("mainTexture", texture);
  152. //更换模型
  153. this.modelView.mesh = this.prefabModelComponent.mesh;
  154. });
  155. if (this.gunNameLabel != null) {
  156. this.gunNameLabel.string = weaponConfig.name;
  157. }
  158. }
  159. OnHide(): void {
  160. this.RemoveEvents();
  161. this.modelView.mesh = null;
  162. let target=this.modelView.materials[0];
  163. target.setProperty("mainTexture", null);
  164. if (this.prefabInstance) {
  165. this.prefabInstance.destroy();
  166. }
  167. if (this.modelPrefab != null) {
  168. var deps = loader.getDependsRecursively(this.modelPrefab);
  169. loader.release(deps);
  170. }
  171. this.modelView.node.active = false;
  172. this.weaponCellListView.onHide();
  173. }
  174. private AddEvents(): void {
  175. this.equipWeaponNode.on(Node.EventType.TOUCH_START, this.TouchStartHandler, this);
  176. this.equipWeaponNode.on(Node.EventType.TOUCH_MOVE, this.TouchMoveHandler, this);
  177. this.equipWeaponNode.on(Node.EventType.TOUCH_END, this.TouchEndHandler, this);
  178. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
  179. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
  180. this.shopButton.node.on(ButtonComponent.EventType.CLICK, this.ShopButtonClickHandler, this);
  181. }
  182. private RemoveEvents(): void {
  183. this.equipWeaponNode.off(SystemEventType.TOUCH_START, this.TouchStartHandler, this);
  184. this.equipWeaponNode.off(SystemEventType.TOUCH_MOVE, this.TouchMoveHandler, this);
  185. this.equipWeaponNode.off(SystemEventType.TOUCH_END, this.TouchEndHandler, this);
  186. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
  187. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
  188. }
  189. private GameModelPropertyChanged(key: string): void {
  190. switch (key) {
  191. case GamePropertys.gold:
  192. this.CallNextFrame(this.RefreshGlod.bind(this));
  193. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  194. break;
  195. case GamePropertys.diamond:
  196. this.CallNextFrame(this.RefreshDiamond.bind(this));
  197. break;
  198. case GamePropertys.currentWeaponId:
  199. this.CallNextFrame(this.RefreshGunModel.bind(this));
  200. break;
  201. case GamePropertys.currentLevel:
  202. this.CallNextFrame(this.RefreshLevel.bind(this));
  203. break;
  204. case GamePropertys.synthesisMaxWeaponId:
  205. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  206. break;
  207. }
  208. }
  209. private RefreshLevel(): void {
  210. this.levelLabel.string = "第" + GameModel.single.currentLevel.toString() + "关";
  211. }
  212. private RefreshGlod(): void {
  213. if (this.glodLabel != null) {
  214. this.glodLabel.string = GameModel.single.gold.toString();
  215. }
  216. this.glodLabel1.string = GameModel.single.fullEarnings.toString() + "/秒";
  217. }
  218. private RefreshDiamond(): void {
  219. if (this.diamondLabel != null) {
  220. this.diamondLabel.string = GameModel.single.diamond.toString();
  221. }
  222. }
  223. private weaponIcon: SpriteFrame;
  224. /**
  225. * 刷新快速购买按钮
  226. */
  227. private RefreshQuickBuy(): void {
  228. this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon;
  229. //价格
  230. let price: number = GameModel.single.GetQuickBuyPrice();
  231. // if(GameModel.single.gold<price){
  232. // this.quickBuyWeaponPriceLabel.color.set(Color.RED);
  233. // }else{
  234. // this.quickBuyWeaponPriceLabel.color.set(Color.WHITE);
  235. // }
  236. this.quickBuyWeaponPriceLabel.string = GameModel.single.GetQuickBuyPrice().toString();
  237. let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.CurrentQuickBuyWeaponId);
  238. loader.loadRes(weaponConfig.icon + "/spriteFrame", SpriteFrame, (err: Error, asset: SpriteFrame) => {
  239. if (err != null) {
  240. console.error("加载武器ICON出错!");
  241. }
  242. this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon = asset;
  243. })
  244. }
  245. /**
  246. * 购买武器
  247. */
  248. private QickBuyButtonClickHandler(): void {
  249. if (GameModel.single.FindWeaponEmptyCell() == null) {
  250. NoticeManager.ShowPrompt("没有武器槽位了!");
  251. return;
  252. }
  253. //价格
  254. let price: number = GameModel.single.GetQuickBuyPrice();
  255. //钱不够
  256. if (GameModel.single.gold < price) {
  257. NoticeManager.ShowPrompt("金币不足");
  258. return;
  259. }
  260. GameModel.single.BuyWeapon(0, GameModel.single.CurrentQuickBuyWeaponId);
  261. }
  262. private ShopButtonClickHandler(): void {
  263. GUIManager.single.Show(UIConst.SHOP_UI);
  264. this.HideSelf();
  265. }
  266. private startRotation: Quat = new Quat();
  267. private currentRotation: Quat = new Quat();
  268. private startPoint: Vec2 = new Vec2();
  269. private currentPoint: Vec2 = new Vec2();
  270. private TouchStartHandler(touch: Touch, touchEvent): void {
  271. let pos: Vec2 = touch.getUILocation();
  272. console.log(pos);
  273. this.startPoint.set(pos.x, pos.y);
  274. this.startRotation.x = this.modelView.node.rotation.x;
  275. this.startRotation.set(this.modelView.node.rotation.x, this.modelView.node.rotation.y, this.modelView.node.rotation.z, this.modelView.node.rotation.w);
  276. }
  277. private TouchMoveHandler(touch: EventTouch, touchEvent): void {
  278. let pos: Vec2 = touch.getUILocation();
  279. this.currentPoint.set(pos.x, pos.y);
  280. let dis: number = Vec2.distance(this.currentPoint, this.startPoint);
  281. let value: number = dis / view.getCanvasSize().width;
  282. if (this.currentPoint.x < this.startPoint.x) {
  283. value = -value;
  284. }
  285. Quat.rotateY(this.currentRotation, this.startRotation, value);
  286. Quat.normalize(this.currentRotation, this.currentRotation);
  287. this.modelView.node.setRotation(this.currentRotation);
  288. }
  289. private TouchEndHandler(...arg): void {
  290. console.log("拖拽模型结束");
  291. }
  292. StartGame(): void {
  293. this.HideSelf();
  294. PlatformManager.hideBanner();
  295. SceneManager.single.Swicth("FightingScene");
  296. }
  297. /**
  298. * 免费金币
  299. */
  300. getAdGold(): void {
  301. PlatformManager.showRewardedVideo(()=>{
  302. GameModel.single.gold += GameModel.single.fullEarnings * 3600;
  303. NoticeManager.ShowPrompt(`获得${GameModel.single.fullEarnings * 3600}金币`);
  304. },()=>{
  305. NoticeManager.ShowPrompt(`未看完广告,无奖励`);
  306. })
  307. }
  308. }