PrepareMediator.ts 11 KB

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