PrepareMediator.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { ButtonComponent, Color, find, instantiate, LabelComponent, LayoutComponent, loader, 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 GameConfigManager from '../../models/GameConfigManager';
  8. import { GameModel } from '../../models/GameModel';
  9. import { GamePropertys } from '../../models/GamePropertys';
  10. import { WeaponCell } from '../../models/weapons/WeaponCell';
  11. import { UIConst } from '../UIConst';
  12. import { WeaponCellListView } from './WeaponCellListView';
  13. import { WeaponCellScript } from './WeaponCellScript';
  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:LayoutComponent
  91. })
  92. weaponList:LayoutComponent=null;
  93. private modelView:ModelComponent;
  94. private prefabInstance:Node;
  95. private prefabModelComponent:ModelComponent;
  96. private weaponCellListView:WeaponCellListView;
  97. onLoad():void{
  98. this.weaponCellListView=new WeaponCellListView(this);
  99. }
  100. OnShow(data?:any):void{
  101. super.OnShow(data);
  102. this.RefreshGunModel();
  103. this.RefreshGlod();
  104. this.RefreshDiamond();
  105. this.RefreshLevel();
  106. this.RefreshQuickBuy();
  107. this.AddEvents();
  108. this.weaponCellListView.OnShow();
  109. }
  110. /**
  111. * 初始化枪的模型
  112. */
  113. private RefreshGunModel():void{
  114. if(this.modelView==null){
  115. let modelNode:Node=find("ModelView",this.node);
  116. this.modelView=modelNode.getComponent(ModelComponent);
  117. }
  118. this.modelView.node.active=true;
  119. let weaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
  120. if(this.prefabInstance){
  121. this.modelView.mesh=null;
  122. this.modelView.setMaterial(null,0);
  123. this.prefabInstance.destroy();
  124. }
  125. loader.loadRes(weaponConfig.prefab,Prefab,(err,prefab)=>{
  126. if(err){
  127. console.error("加载武器出错");
  128. }
  129. this.prefabInstance=instantiate(prefab);
  130. this.prefabModelComponent=this.prefabInstance.getComponentInChildren(ModelComponent);
  131. this.modelView.mesh=this.prefabModelComponent.mesh;
  132. this.modelView.setMaterial(this.prefabModelComponent.getMaterial(0),0);
  133. });
  134. if(this.gunNameLabel!=null){
  135. this.gunNameLabel.string=weaponConfig.name;
  136. }
  137. }
  138. OnHide():void{
  139. this.RemoveEvents();
  140. this.modelView.mesh=null;
  141. this.modelView.setMaterial(null,0);
  142. this.prefabInstance.destroy();
  143. this.modelView.node.active=false;
  144. this.weaponCellListView.onHide();
  145. }
  146. private AddEvents():void{
  147. this.equipWeaponNode.on(Node.EventType.TOUCH_START,this.TouchStartHandler,this);
  148. this.equipWeaponNode.on(Node.EventType.TOUCH_MOVE,this.TouchMoveHandler,this);
  149. this.equipWeaponNode.on(Node.EventType.TOUCH_END,this.TouchEndHandler,this);
  150. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged,0);
  151. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK,this.QickBuyButtonClickHandler,this);
  152. this.shopButton.node.on(ButtonComponent.EventType.CLICK,this.ShopButtonClickHandler,this);
  153. }
  154. private RemoveEvents():void{
  155. this.equipWeaponNode.off(SystemEventType.TOUCH_START,this.TouchStartHandler,this);
  156. this.equipWeaponNode.off(SystemEventType.TOUCH_MOVE,this.TouchMoveHandler,this);
  157. this.equipWeaponNode.off(SystemEventType.TOUCH_END,this.TouchEndHandler,this);
  158. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged);
  159. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK,this.QickBuyButtonClickHandler,this);
  160. }
  161. private GameModelPropertyChanged(key:string):void{
  162. switch (key) {
  163. case GamePropertys.gold:
  164. this.CallNextFrame(this.RefreshGlod.bind(this));
  165. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  166. break;
  167. case GamePropertys.diamond:
  168. this.CallNextFrame(this.RefreshDiamond.bind(this));
  169. break;
  170. case GamePropertys.currentWeaponId:
  171. this.CallNextFrame(this.RefreshGunModel.bind(this));
  172. break;
  173. case GamePropertys.currentLevel:
  174. this.CallNextFrame(this.RefreshLevel.bind(this));
  175. break;
  176. case GamePropertys.synthesisMaxWeaponId:
  177. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  178. break;
  179. }
  180. }
  181. private RefreshLevel():void{
  182. this.levelLabel.string="第"+GameModel.single.currentLevel.toString()+"关";
  183. }
  184. private RefreshGlod():void{
  185. if(this.glodLabel!=null){
  186. this.glodLabel.string=GameModel.single.gold.toString();
  187. }
  188. this.glodLabel1.string=GameModel.single.fullEarnings.toString()+"/秒";
  189. }
  190. private RefreshDiamond():void{
  191. if(this.diamondLabel!=null){
  192. this.diamondLabel.string=GameModel.single.diamond.toString();
  193. }
  194. }
  195. private weaponIcon:SpriteFrame;
  196. /**
  197. * 刷新快速购买按钮
  198. */
  199. private RefreshQuickBuy():void{
  200. this.quickBuyWeaponIcon.spriteFrame=this.weaponIcon;
  201. //价格
  202. let price:number=GameModel.single.GetQuickBuyPrice();
  203. // if(GameModel.single.gold<price){
  204. // this.quickBuyWeaponPriceLabel.color.set(Color.RED);
  205. // }else{
  206. // this.quickBuyWeaponPriceLabel.color.set(Color.WHITE);
  207. // }
  208. this.quickBuyWeaponPriceLabel.string=GameModel.single.GetQuickBuyPrice().toString();
  209. let weaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.CurrentQuickBuyWeaponId);
  210. loader.loadRes(weaponConfig.icon+"/spriteFrame",SpriteFrame,(err:Error,asset:SpriteFrame)=>{
  211. if(err!=null){
  212. console.error("加载武器ICON出错!");
  213. }
  214. this.quickBuyWeaponIcon.spriteFrame=this.weaponIcon=asset;
  215. })
  216. }
  217. /**
  218. * 购买武器
  219. */
  220. private QickBuyButtonClickHandler():void{
  221. if(GameModel.single.FindWeaponEmptyCell()==null){
  222. NoticeManager.ShowPrompt("没有武器槽位了!");
  223. return;
  224. }
  225. //价格
  226. let price:number=GameModel.single.GetQuickBuyPrice();
  227. //钱不够
  228. if(GameModel.single.gold<price){
  229. NoticeManager.ShowPrompt("金币不足");
  230. return;
  231. }
  232. GameModel.single.BuyWeapon(0,GameModel.single.CurrentQuickBuyWeaponId);
  233. }
  234. private ShopButtonClickHandler():void{
  235. GUIManager.single.Show(UIConst.SHOP_UI);
  236. this.HideSelf();
  237. }
  238. private startRotation:Quat=new Quat();
  239. private currentRotation:Quat=new Quat();
  240. private startPoint:Vec2=new Vec2();
  241. private currentPoint:Vec2=new Vec2();
  242. private TouchStartHandler(touch:Touch,touchEvent):void{
  243. let pos:Vec2=touch.getLocation();
  244. console.log(pos);
  245. this.startPoint.set(pos.x,pos.y);
  246. this.startRotation.x=this.modelView.node.rotation.x;
  247. this.startRotation.set(this.modelView.node.rotation.x,this.modelView.node.rotation.y,this.modelView.node.rotation.z,this.modelView.node.rotation.w);
  248. }
  249. private TouchMoveHandler(touch,touchEvent):void{
  250. let pos:Vec2=touch.getLocation();
  251. this.currentPoint.set(pos.x,pos.y);
  252. let dis:number=Vec2.distance(this.currentPoint,this.startPoint);
  253. let value:number=dis/view.getCanvasSize().width;
  254. Quat.rotateY(this.currentRotation,this.startRotation,value);
  255. Quat.normalize(this.currentRotation,this.currentRotation);
  256. this.modelView.node.setRotation(this.currentRotation);
  257. }
  258. private TouchEndHandler(...arg):void{
  259. console.log("拖拽模型结束");
  260. }
  261. StartGame():void{
  262. this.HideSelf();
  263. SceneManager.single.Swicth("FightingScene");
  264. }
  265. }