PrepareMediator.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import { 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 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.prefabInstance.destroy();
  123. }
  124. loader.loadRes(weaponConfig.prefab,Prefab,(err,prefab)=>{
  125. if(err){
  126. console.error("加载武器出错");
  127. }
  128. this.prefabInstance=instantiate(prefab);
  129. this.prefabModelComponent=this.prefabInstance.getComponentInChildren(ModelComponent);
  130. //更换贴图
  131. let source=this.prefabModelComponent.materials[0];
  132. let target=this.modelView.materials[0];
  133. let texture=source.getProperty("mainTexture");
  134. target.setProperty("mainTexture",texture);
  135. //更换模型
  136. this.modelView.mesh=this.prefabModelComponent.mesh;
  137. });
  138. if(this.gunNameLabel!=null){
  139. this.gunNameLabel.string=weaponConfig.name;
  140. }
  141. }
  142. OnHide():void{
  143. this.RemoveEvents();
  144. this.modelView.mesh=null;
  145. this.modelView.setMaterial(null,0);
  146. this.prefabInstance.destroy();
  147. this.modelView.node.active=false;
  148. this.weaponCellListView.onHide();
  149. }
  150. private AddEvents():void{
  151. this.equipWeaponNode.on(Node.EventType.TOUCH_START,this.TouchStartHandler,this);
  152. this.equipWeaponNode.on(Node.EventType.TOUCH_MOVE,this.TouchMoveHandler,this);
  153. this.equipWeaponNode.on(Node.EventType.TOUCH_END,this.TouchEndHandler,this);
  154. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged,0);
  155. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK,this.QickBuyButtonClickHandler,this);
  156. this.shopButton.node.on(ButtonComponent.EventType.CLICK,this.ShopButtonClickHandler,this);
  157. }
  158. private RemoveEvents():void{
  159. this.equipWeaponNode.off(SystemEventType.TOUCH_START,this.TouchStartHandler,this);
  160. this.equipWeaponNode.off(SystemEventType.TOUCH_MOVE,this.TouchMoveHandler,this);
  161. this.equipWeaponNode.off(SystemEventType.TOUCH_END,this.TouchEndHandler,this);
  162. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged);
  163. this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK,this.QickBuyButtonClickHandler,this);
  164. }
  165. private GameModelPropertyChanged(key:string):void{
  166. switch (key) {
  167. case GamePropertys.gold:
  168. this.CallNextFrame(this.RefreshGlod.bind(this));
  169. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  170. break;
  171. case GamePropertys.diamond:
  172. this.CallNextFrame(this.RefreshDiamond.bind(this));
  173. break;
  174. case GamePropertys.currentWeaponId:
  175. this.CallNextFrame(this.RefreshGunModel.bind(this));
  176. break;
  177. case GamePropertys.currentLevel:
  178. this.CallNextFrame(this.RefreshLevel.bind(this));
  179. break;
  180. case GamePropertys.synthesisMaxWeaponId:
  181. this.CallNextFrame(this.RefreshQuickBuy.bind(this));
  182. break;
  183. }
  184. }
  185. private RefreshLevel():void{
  186. this.levelLabel.string="第"+GameModel.single.currentLevel.toString()+"关";
  187. }
  188. private RefreshGlod():void{
  189. if(this.glodLabel!=null){
  190. this.glodLabel.string=GameModel.single.gold.toString();
  191. }
  192. this.glodLabel1.string=GameModel.single.fullEarnings.toString()+"/秒";
  193. }
  194. private RefreshDiamond():void{
  195. if(this.diamondLabel!=null){
  196. this.diamondLabel.string=GameModel.single.diamond.toString();
  197. }
  198. }
  199. private weaponIcon:SpriteFrame;
  200. /**
  201. * 刷新快速购买按钮
  202. */
  203. private RefreshQuickBuy():void{
  204. this.quickBuyWeaponIcon.spriteFrame=this.weaponIcon;
  205. //价格
  206. let price:number=GameModel.single.GetQuickBuyPrice();
  207. // if(GameModel.single.gold<price){
  208. // this.quickBuyWeaponPriceLabel.color.set(Color.RED);
  209. // }else{
  210. // this.quickBuyWeaponPriceLabel.color.set(Color.WHITE);
  211. // }
  212. this.quickBuyWeaponPriceLabel.string=GameModel.single.GetQuickBuyPrice().toString();
  213. let weaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.CurrentQuickBuyWeaponId);
  214. loader.loadRes(weaponConfig.icon+"/spriteFrame",SpriteFrame,(err:Error,asset:SpriteFrame)=>{
  215. if(err!=null){
  216. console.error("加载武器ICON出错!");
  217. }
  218. this.quickBuyWeaponIcon.spriteFrame=this.weaponIcon=asset;
  219. })
  220. }
  221. /**
  222. * 购买武器
  223. */
  224. private QickBuyButtonClickHandler():void{
  225. if(GameModel.single.FindWeaponEmptyCell()==null){
  226. NoticeManager.ShowPrompt("没有武器槽位了!");
  227. return;
  228. }
  229. //价格
  230. let price:number=GameModel.single.GetQuickBuyPrice();
  231. //钱不够
  232. if(GameModel.single.gold<price){
  233. NoticeManager.ShowPrompt("金币不足");
  234. return;
  235. }
  236. GameModel.single.BuyWeapon(0,GameModel.single.CurrentQuickBuyWeaponId);
  237. }
  238. private ShopButtonClickHandler():void{
  239. GUIManager.single.Show(UIConst.SHOP_UI);
  240. this.HideSelf();
  241. }
  242. private startRotation:Quat=new Quat();
  243. private currentRotation:Quat=new Quat();
  244. private startPoint:Vec2=new Vec2();
  245. private currentPoint:Vec2=new Vec2();
  246. private TouchStartHandler(touch:Touch,touchEvent):void{
  247. let pos:Vec2=touch.getUILocation();
  248. console.log(pos);
  249. this.startPoint.set(pos.x,pos.y);
  250. this.startRotation.x=this.modelView.node.rotation.x;
  251. this.startRotation.set(this.modelView.node.rotation.x,this.modelView.node.rotation.y,this.modelView.node.rotation.z,this.modelView.node.rotation.w);
  252. }
  253. private TouchMoveHandler(touch:EventTouch,touchEvent):void{
  254. let pos:Vec2=touch.getUILocation();
  255. this.currentPoint.set(pos.x,pos.y);
  256. let dis:number=Vec2.distance(this.currentPoint,this.startPoint);
  257. let value:number=dis/view.getCanvasSize().width;
  258. if(this.currentPoint.x<this.startPoint.x){
  259. value=-value;
  260. }
  261. Quat.rotateY(this.currentRotation,this.startRotation,value);
  262. Quat.normalize(this.currentRotation,this.currentRotation);
  263. this.modelView.node.setRotation(this.currentRotation);
  264. }
  265. private TouchEndHandler(...arg):void{
  266. console.log("拖拽模型结束");
  267. }
  268. StartGame():void{
  269. this.HideSelf();
  270. SceneManager.single.Swicth("FightingScene");
  271. }
  272. }