WeaponCellListView.ts 9.1 KB

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