WeaponCellListView.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { _decorator, Component, Node, EventTouch, LayoutComponent, Vec2, Rect, Size, Vec3, loader, instantiate } 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 weaponCell:WeaponCell;
  47. let weaponCellView:Node;
  48. let weaponCellScript:WeaponCellScript;
  49. if(this.weaponList.node.children.length!=12){
  50. for (let index = 0; index < 12; index++) {
  51. weaponCellView=instantiate(this.mediator.WeaponCellPrefab);
  52. this.weaponList.node.addChild(weaponCellView);
  53. }
  54. }
  55. let list:WeaponCell[]=GameModel.single.weaponCells;
  56. for (let index = 0; index < list.length; index++) {
  57. weaponCell = list[index];
  58. weaponCellView=this.weaponList.node.children[index];
  59. weaponCellScript=weaponCellView.getComponent(WeaponCellScript);
  60. weaponCellScript.UpdateWeaponCell(weaponCell);
  61. }
  62. }
  63. private CheckSelected(weaponCellData:WeaponCell):void{
  64. this.currentSelect=weaponCellData;
  65. let weaponCellScript:WeaponCellScript;
  66. let list:WeaponCell[]=GameModel.single.weaponCells;
  67. let weaponCell:WeaponCell;
  68. let weaponCellView:Node;
  69. for (let index = 0; index < list.length; index++) {
  70. weaponCell = list[index];
  71. weaponCellView=this.weaponList.node.children[index];
  72. weaponCellScript=weaponCellView.getComponent(WeaponCellScript);
  73. weaponCellScript.ChangeSelected(weaponCellData);
  74. }
  75. }
  76. private CheckMark():void{
  77. let weaponCellScript:WeaponCellScript;
  78. let list:WeaponCell[]=GameModel.single.weaponCells;
  79. let weaponCell:WeaponCell;
  80. let weaponCellView:Node;
  81. for (let index = 0; index < list.length; index++) {
  82. weaponCell = list[index];
  83. weaponCellView=this.weaponList.node.children[index];
  84. weaponCellScript=weaponCellView.getComponent(WeaponCellScript);
  85. if(this.currentSelect==null){
  86. weaponCellScript.Mark(false);
  87. }else{
  88. if(this.currentSelect==weaponCellScript.weaponCell){
  89. continue;
  90. }
  91. if(this.currentSelect.weaponId==weaponCellScript.weaponCell.weaponId){
  92. weaponCellScript.Mark(true);
  93. }else{
  94. weaponCellScript.Mark(false);
  95. }
  96. }
  97. }
  98. }
  99. private startNode:Node;
  100. private startPos:Vec2=new Vec2();
  101. private tempPos:Vec2=new Vec2();
  102. private isDrag:boolean;
  103. private WeaponListTouchStartHandler(touch:EventTouch):void{
  104. touch.getUILocation(this.tempPos);
  105. this.startPos.x=this.tempPos.x;
  106. this.startPos.y=this.tempPos.y;
  107. this.startNode=this.FindTouchNode(this.weaponList.node,this.tempPos);
  108. if(this.startNode!=null){
  109. let weaponCellScript:WeaponCellScript=this.startNode.getComponent(WeaponCellScript);
  110. if(weaponCellScript==null){
  111. console.error("武器槽未挂载WeaponCellScript脚本");
  112. return;
  113. }
  114. if(weaponCellScript.weaponCell.weaponId<0){
  115. console.log("没有武器的武器槽不让选中也不让拖拽");
  116. return;
  117. }
  118. this.isDrag=false;
  119. this.mediator.node.on(Node.EventType.TOUCH_MOVE,this.WeaponListTouchMoveHandler,this);
  120. this.mediator.node.on(Node.EventType.TOUCH_END,this.WeaponListTouchEndHandler,this);
  121. this.CheckSelected(weaponCellScript.weaponCell);
  122. }
  123. }
  124. private FindTouchNode(root:Node,touchPos:Vec2):Node{
  125. for (let index = 0; index < root.children.length; index++) {
  126. const element = root.children[index];
  127. if(this.HitNode(element,touchPos)){
  128. return element;
  129. }
  130. }
  131. return null;
  132. }
  133. /**
  134. * 判断是否击中该节点
  135. * @param node
  136. * @param touchPos
  137. */
  138. private HitNode(node:Node,touchPos:Vec2):boolean{
  139. let childSize:Size;
  140. let childPos:Vec2=new Vec2();
  141. let childRect:Rect=new Rect();
  142. this.getNodePos(node,childPos);
  143. childSize=node.getContentSize();
  144. childRect.x=childPos.x
  145. childRect.y=childPos.y;
  146. childRect.width=childSize.x;
  147. childRect.height=childSize.y;
  148. if(childRect.contains(touchPos)){
  149. return true;
  150. }
  151. return false;
  152. }
  153. private WeaponListTouchMoveHandler(touch:EventTouch):void{
  154. touch.getUILocation(this.tempPos);
  155. let dis:number=Vec2.distance(this.startPos,this.tempPos);
  156. if(dis>5){
  157. if(this.isDrag==false){
  158. this.isDrag=true;
  159. this.CheckMark();
  160. //拖拽图标
  161. this.mediator.weaponDragIcon.node.active=true;
  162. let weaponCellScript:WeaponCellScript=this.startNode.getComponent(WeaponCellScript);
  163. this.mediator.weaponDragIcon.spriteFrame=weaponCellScript.iconLoader.spriteFrame;
  164. }
  165. this.mediator.weaponDragIcon.node.setPosition(this.tempPos.x,this.tempPos.y,0);
  166. // console.log("拖拽移动到:"+target.name);
  167. }
  168. }
  169. private WeaponListTouchEndHandler(touch:EventTouch):void{
  170. touch.getUILocation(this.tempPos);
  171. this.mediator.node.off(Node.EventType.TOUCH_MOVE,this.WeaponListTouchMoveHandler,this);
  172. this.mediator.node.off(Node.EventType.TOUCH_END,this.WeaponListTouchEndHandler,this);
  173. if(this.isDrag){
  174. this.mediator.weaponDragIcon.node.active=false;
  175. //优先判断武器槽
  176. let target:Node=this.FindTouchNode(this.weaponList.node,this.tempPos);
  177. if(target==null){
  178. //删除
  179. if(this.HitNode(this.mediator.deleteWeaponNode,this.tempPos)){
  180. GameModel.single.RemoveWeapon(this.currentSelect.cellId);
  181. }else if(this.HitNode(this.mediator.equipWeaponNode,this.tempPos))
  182. {//装配武器
  183. GameModel.single.EquipWeapon(this.currentSelect);
  184. }
  185. }else{
  186. if(this.startNode!=target){
  187. if(GameConfigManager.WeaponIsMaxLevel(this.currentSelect.weaponId)){
  188. console.log("武器已到达最高等级")
  189. return;
  190. }
  191. let weaponCellScript:WeaponCellScript=target.getComponent(WeaponCellScript);
  192. if(weaponCellScript!=null){
  193. console.log("合成");
  194. GameModel.single.SynthesisWeapon(this.currentSelect.cellId,weaponCellScript.weaponCell.cellId);
  195. }
  196. }
  197. }
  198. this.currentSelect=null;
  199. this.CheckMark();
  200. }else{
  201. console.log("点击");
  202. }
  203. }
  204. OnDestory():void{
  205. }
  206. private getNodePos(node:Node,out:Vec2):void{
  207. let nodePos:Vec3=new Vec3();
  208. let parentPos:Vec3=new Vec3();
  209. let anchorPoint:Vec2=new Vec2();
  210. node.getAnchorPoint(anchorPoint);
  211. let nodeSize:Size=node.getContentSize();
  212. while(node){
  213. node.getPosition(parentPos);
  214. nodePos.x+=parentPos.x;
  215. nodePos.y+=parentPos.y;
  216. node=node.parent;
  217. }
  218. out.x=nodePos.x-anchorPoint.x*nodeSize.width;
  219. out.y=nodePos.y-anchorPoint.y*nodeSize.height;
  220. }
  221. private get weaponList():LayoutComponent{
  222. return this.mediator.weaponList;
  223. }
  224. private get mediator():PrepareMediator{
  225. return this.__mediator as PrepareMediator;
  226. }
  227. }