PrepareMediator.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { _decorator, Component, Node, ModelComponent, find, Prefab, loader, instantiate, Mesh, systemEvent, SystemEventType, Quat, Vec2, director, game, view, Touch, LabelComponent } from 'cc';
  2. import { GUIMediator } from '../../../engines/gui/GUIMediator';
  3. import { DataModelEventType } from '../../../engines/models/DataModelEventType';
  4. import { SceneManager } from '../../../engines/scenes/SceneManager';
  5. import GameConfigManager from '../../models/GameConfigManager';
  6. import { GameModel } from '../../models/GameModel';
  7. import { GamePropertys } from '../../models/GamePropertys';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('PrepareMediator')
  10. export class PrepareMediator extends GUIMediator {
  11. @property({
  12. type:LabelComponent
  13. })
  14. glodLabel:LabelComponent=null;
  15. @property({
  16. type:LabelComponent
  17. })
  18. glodLabel1:LabelComponent=null;
  19. @property({
  20. type:LabelComponent
  21. })
  22. diamondLabel:LabelComponent=null;
  23. @property({
  24. type:LabelComponent
  25. })
  26. gunNameLabel:LabelComponent=null;
  27. private modelView:ModelComponent;
  28. private prefabInstance:Node;
  29. private prefabModelComponent:ModelComponent;
  30. OnShow(data?:any):void{
  31. super.OnShow(data);
  32. this.RefreshGunModel();
  33. this.RefreshGlod();
  34. this.RefreshDiamond();
  35. this.AddEvents();
  36. }
  37. /**
  38. * 初始化枪的模型
  39. */
  40. private RefreshGunModel():void{
  41. if(this.modelView==null){
  42. let modelNode:Node=find("Canvas/ModelView");
  43. this.modelView=modelNode.getComponent(ModelComponent);
  44. }
  45. this.modelView.node.active=true;
  46. let weaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
  47. let prefab:Prefab=loader.getRes(weaponConfig.prefab);
  48. this.prefabInstance=instantiate(prefab);
  49. this.prefabModelComponent=this.prefabInstance.getComponentInChildren(ModelComponent);
  50. this.modelView.mesh=this.prefabModelComponent.mesh;
  51. this.modelView.setMaterial(this.prefabModelComponent.getMaterial(0),0);
  52. if(this.gunNameLabel!=null){
  53. this.gunNameLabel.string=weaponConfig.name;
  54. }
  55. }
  56. OnHide():void{
  57. this.RemoveEvents();
  58. this.modelView.mesh=null;
  59. this.modelView.setMaterial(null,0);
  60. this.prefabInstance.destroy();
  61. this.modelView.node.active=false;
  62. }
  63. private AddEvents():void{
  64. systemEvent.on(cc.SystemEventType.TOUCH_START,this.TouchStartHandler,this);
  65. systemEvent.on(cc.SystemEventType.TOUCH_MOVE,this.TouchMoveHandler,this);
  66. systemEvent.on(cc.SystemEventType.TOUCH_MOVE,this.TouchEndHandler,this);
  67. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged,0);
  68. }
  69. private RemoveEvents():void{
  70. systemEvent.off(SystemEventType.TOUCH_START,this.TouchStartHandler,this);
  71. systemEvent.off(SystemEventType.TOUCH_MOVE,this.TouchMoveHandler,this);
  72. systemEvent.off(SystemEventType.TOUCH_MOVE,this.TouchEndHandler,this);
  73. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged);
  74. }
  75. private GameModelPropertyChanged(key:string):void{
  76. switch (key) {
  77. case GamePropertys.gold:
  78. this.CallNextFrame(this.RefreshGlod.bind(this));
  79. break;
  80. case GamePropertys.diamond:
  81. this.CallNextFrame(this.RefreshDiamond.bind(this));
  82. break;
  83. case GamePropertys.currentWeaponId:
  84. this.CallNextFrame(this.RefreshGunModel.bind(this));
  85. break;
  86. }
  87. }
  88. private RefreshGlod():void{
  89. if(this.glodLabel!=null){
  90. this.glodLabel.string=GameModel.single.gold.toString();
  91. }
  92. }
  93. private RefreshDiamond():void{
  94. if(this.diamondLabel!=null){
  95. this.diamondLabel.string=GameModel.single.diamond.toString();
  96. }
  97. }
  98. private startRotation:Quat=new Quat();
  99. private currentRotation:Quat=new Quat();
  100. private startPoint:Vec2=new Vec2();
  101. private currentPoint:Vec2=new Vec2();
  102. private TouchStartHandler(touch:Touch,touchEvent):void{
  103. let pos:Vec2=touch.getLocation();
  104. this.startPoint.set(pos.x,pos.y);
  105. this.startRotation.x=this.modelView.node.rotation.x;
  106. this.startRotation.set(this.modelView.node.rotation.x,this.modelView.node.rotation.y,this.modelView.node.rotation.z,this.modelView.node.rotation.w);
  107. }
  108. private TouchMoveHandler(touch,touchEvent):void{
  109. let pos:Vec2=touch.getLocation();
  110. this.currentPoint.set(pos.x,pos.y);
  111. let dis:number=Vec2.distance(this.currentPoint,this.startPoint);
  112. let value:number=dis/view.getCanvasSize().width;
  113. Quat.rotateY(this.currentRotation,this.startRotation,value);
  114. Quat.normalize(this.currentRotation,this.currentRotation);
  115. this.modelView.node.setRotation(this.currentRotation);
  116. }
  117. private TouchEndHandler(...arg):void{
  118. console.log("拖拽模型结束");
  119. }
  120. StartGame():void{
  121. this.HideSelf();
  122. SceneManager.single.Swicth("FightingScene");
  123. }
  124. }