PrepareMediator.ts 10 KB

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