PrepareMediator.ts 11 KB

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