WeaponCellListView.ts 9.7 KB

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