WeaponCellListView.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { _decorator, Component, Node, EventTouch, LayoutComponent, Vec2, Rect, Size, Vec3, loader, instantiate, assert } from 'cc';
  2. import { BaseView } from '../../../engines/gui/BaseView';
  3. import { GUIManager } from '../../../engines/gui/GUIManager';
  4. import { GUIMediator } from '../../../engines/gui/GUIMediator';
  5. import { DataModelEventType } from '../../../engines/models/DataModelEventType';
  6. import { NoticeManager } from '../../../engines/notices/NoticeManager';
  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 { PrepareMediator } from './PrepareMediator';
  13. import { WeaponCellScript } from './WeaponCellScript';
  14. const { ccclass, property } = _decorator;
  15. export class WeaponCellListView extends BaseView{
  16. private currentSelect:WeaponCell;
  17. constructor(mediator:GUIMediator){
  18. super(mediator);
  19. }
  20. OnShow():void{
  21. this.mediator.weaponDragIcon.node.active=false;
  22. this.RefreshWeaponList();
  23. this.AddEvent();
  24. }
  25. onHide():void{
  26. this.RemoveEvent();
  27. }
  28. private AddEvent():void{
  29. this.mediator.node.on(Node.EventType.TOUCH_START,this.WeaponListTouchStartHandler,this);
  30. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged,0);
  31. }
  32. private RemoveEvent():void{
  33. this.mediator.node.off(Node.EventType.TOUCH_START,this.WeaponListTouchStartHandler,this);
  34. this.mediator.node.off(Node.EventType.TOUCH_MOVE,this.WeaponListTouchMoveHandler,this);
  35. this.mediator.node.off(Node.EventType.TOUCH_END,this.WeaponListTouchEndHandler,this);
  36. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged);
  37. }
  38. private GameModelPropertyChanged(key:string):void{
  39. switch (key) {
  40. case GamePropertys.WeaponCell:
  41. this.RefreshWeaponList();
  42. break;
  43. }
  44. }
  45. /**
  46. * 刷新武器列表
  47. */
  48. private RefreshWeaponList():void{
  49. let weaponCell:WeaponCell;
  50. let weaponCellView:Node;
  51. let weaponCellScript:WeaponCellScript;
  52. if(this.weaponList.node.children.length!=12){
  53. for (let index = 0; index < 12; index++) {
  54. weaponCellView=instantiate(this.mediator.WeaponCellPrefab);
  55. this.weaponList.node.addChild(weaponCellView);
  56. }
  57. }
  58. let list:WeaponCell[]=GameModel.single.weaponCells;
  59. for (let index = 0; index < list.length; index++) {
  60. weaponCell = list[index];
  61. weaponCellView=this.weaponList.node.children[index];
  62. weaponCellScript=weaponCellView.getComponent(WeaponCellScript);
  63. weaponCellScript.UpdateWeaponCell(weaponCell);
  64. }
  65. }
  66. private CheckSelected(weaponCellData:WeaponCell):void{
  67. this.currentSelect=weaponCellData;
  68. let weaponCellScript:WeaponCellScript;
  69. let list:WeaponCell[]=GameModel.single.weaponCells;
  70. let weaponCell:WeaponCell;
  71. let weaponCellView:Node;
  72. for (let index = 0; index < list.length; index++) {
  73. weaponCell = list[index];
  74. weaponCellView=this.weaponList.node.children[index];
  75. weaponCellScript=weaponCellView.getComponent(WeaponCellScript);
  76. weaponCellScript.ChangeSelected(weaponCellData);
  77. }
  78. }
  79. private CheckMark():void{
  80. let weaponCellScript:WeaponCellScript;
  81. let list:WeaponCell[]=GameModel.single.weaponCells;
  82. let weaponCell:WeaponCell;
  83. let weaponCellView:Node;
  84. for (let index = 0; index < list.length; index++) {
  85. weaponCell = list[index];
  86. weaponCellView=this.weaponList.node.children[index];
  87. weaponCellScript=weaponCellView.getComponent(WeaponCellScript);
  88. if(this.currentSelect==null){
  89. weaponCellScript.Mark(false);
  90. }else{
  91. if(this.currentSelect==weaponCellScript.weaponCell){
  92. continue;
  93. }
  94. if(this.currentSelect.weaponId==weaponCellScript.weaponCell.weaponId){
  95. weaponCellScript.Mark(true);
  96. }else{
  97. weaponCellScript.Mark(false);
  98. }
  99. }
  100. }
  101. }
  102. private startNode:Node;
  103. private startPos:Vec2=new Vec2();
  104. private tempPos:Vec2=new Vec2();
  105. private isDrag:boolean;
  106. private WeaponListTouchStartHandler(touch:EventTouch):void{
  107. touch.getUILocation(this.tempPos);
  108. this.startPos.x=this.tempPos.x;
  109. this.startPos.y=this.tempPos.y;
  110. this.startNode=this.FindTouchNode(this.weaponList.node,this.tempPos);
  111. if(this.startNode!=null){
  112. let weaponCellScript:WeaponCellScript=this.startNode.getComponent(WeaponCellScript);
  113. if(weaponCellScript==null){
  114. console.error("武器槽未挂载WeaponCellScript脚本");
  115. return;
  116. }
  117. if(weaponCellScript.weaponCell.weaponId<0){
  118. console.log("没有武器的武器槽不让选中也不让拖拽");
  119. return;
  120. }
  121. this.isDrag=false;
  122. this.mediator.node.on(Node.EventType.TOUCH_MOVE,this.WeaponListTouchMoveHandler,this);
  123. this.mediator.node.on(Node.EventType.TOUCH_END,this.WeaponListTouchEndHandler,this);
  124. this.CheckSelected(weaponCellScript.weaponCell);
  125. }
  126. }
  127. private FindTouchNode(root:Node,touchPos:Vec2):Node{
  128. for (let index = 0; index < root.children.length; index++) {
  129. const element = root.children[index];
  130. if(this.HitNode(element,touchPos)){
  131. return element;
  132. }
  133. }
  134. return null;
  135. }
  136. /**
  137. * 判断是否击中该节点
  138. * @param node
  139. * @param touchPos
  140. */
  141. private HitNode(node:Node,touchPos:Vec2):boolean{
  142. let childSize:Size;
  143. let childPos:Vec2=new Vec2();
  144. let childRect:Rect=new Rect();
  145. this.getNodePos(node,childPos);
  146. childSize=node.getContentSize();
  147. childRect.x=childPos.x
  148. childRect.y=childPos.y;
  149. childRect.width=childSize.x;
  150. childRect.height=childSize.y;
  151. if(childRect.contains(touchPos)){
  152. return true;
  153. }
  154. return false;
  155. }
  156. private WeaponListTouchMoveHandler(touch:EventTouch):void{
  157. touch.getUILocation(this.tempPos);
  158. let dis:number=Vec2.distance(this.startPos,this.tempPos);
  159. if(dis>5){
  160. if(this.isDrag==false){
  161. this.isDrag=true;
  162. this.CheckMark();
  163. //拖拽图标
  164. this.mediator.weaponDragIcon.node.active=true;
  165. let weaponCellScript:WeaponCellScript=this.startNode.getComponent(WeaponCellScript);
  166. this.mediator.weaponDragIcon.spriteFrame=weaponCellScript.iconLoader.spriteFrame;
  167. }
  168. this.mediator.weaponDragIcon.node.setPosition(this.tempPos.x,this.tempPos.y,0);
  169. // console.log("拖拽移动到:"+target.name);
  170. }
  171. }
  172. private WeaponListTouchEndHandler(touch:EventTouch):void{
  173. touch.getUILocation(this.tempPos);
  174. this.mediator.node.off(Node.EventType.TOUCH_MOVE,this.WeaponListTouchMoveHandler,this);
  175. this.mediator.node.off(Node.EventType.TOUCH_END,this.WeaponListTouchEndHandler,this);
  176. if(this.isDrag){
  177. this.mediator.weaponDragIcon.node.active=false;
  178. //优先判断武器槽
  179. let target:Node=this.FindTouchNode(this.weaponList.node,this.tempPos);
  180. if(target==null){
  181. //删除
  182. if(this.HitNode(this.mediator.deleteWeaponNode,this.tempPos)){
  183. if(this.currentSelect==GameModel.single.currentWeaponCell){
  184. NoticeManager.ShowPrompt("不能销毁当前使用的武器!");
  185. }else{
  186. GameModel.single.RemoveWeapon(this.currentSelect.cellId);
  187. }
  188. }else if(this.HitNode(this.mediator.equipWeaponNode,this.tempPos))
  189. {//装配武器
  190. GameModel.single.EquipWeapon(this.currentSelect);
  191. }
  192. }else{
  193. if(this.startNode!=target){
  194. if(GameConfigManager.WeaponIsMaxLevel(this.currentSelect.weaponId)){
  195. console.log("武器已到达最高等级")
  196. return;
  197. }
  198. let weaponCellScript:WeaponCellScript=target.getComponent(WeaponCellScript);
  199. if(weaponCellScript!=null){
  200. let value:number=Math.random();
  201. if(value<0.5){
  202. GameModel.single.SynthesisWeapon(this.currentSelect.cellId,weaponCellScript.weaponCell.cellId);
  203. }else{
  204. if(this.currentSelect.weaponId!=weaponCellScript.weaponCell.weaponId){
  205. NoticeManager.ShowPrompt("不同的武器无法合成!");
  206. }else{
  207. GUIManager.single.Show(UIConst.FREE_UPGRADE_UI,{a:this.currentSelect,b:weaponCellScript.weaponCell});
  208. }
  209. }
  210. }
  211. }
  212. }
  213. this.currentSelect=null;
  214. this.CheckMark();
  215. }else{
  216. console.log("点击");
  217. }
  218. }
  219. OnDestory():void{
  220. }
  221. private getNodePos(node:Node,out:Vec2):void{
  222. let nodePos:Vec3=new Vec3();
  223. let parentPos:Vec3=new Vec3();
  224. let anchorPoint:Vec2=new Vec2();
  225. node.getAnchorPoint(anchorPoint);
  226. let nodeSize:Size=node.getContentSize();
  227. while(node){
  228. node.getPosition(parentPos);
  229. nodePos.x+=parentPos.x;
  230. nodePos.y+=parentPos.y;
  231. node=node.parent;
  232. }
  233. out.x=nodePos.x-anchorPoint.x*nodeSize.width;
  234. out.y=nodePos.y-anchorPoint.y*nodeSize.height;
  235. }
  236. private get weaponList():LayoutComponent{
  237. return this.mediator.weaponList;
  238. }
  239. private get mediator():PrepareMediator{
  240. return this.__mediator as PrepareMediator;
  241. }
  242. }