WeaponBase.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { _decorator, Component, Node, CCInteger, AnimationComponent, find, instantiate, Vec3, director } from 'cc';
  2. import { GameModel } from '../../../models/GameModel';
  3. import { EventDispatcher } from '../../../../engines/events/EventDispatcher';
  4. const { ccclass, property } = _decorator;
  5. export class WeaponBase extends EventDispatcher{
  6. /**
  7. * 子弹数量更改
  8. */
  9. public static EVENT_BULLET_CHANGE:string="EVENT_BULLET_CHANGE";
  10. protected leftHand:AnimationComponent;
  11. protected rightHand:AnimationComponent;
  12. protected leftSocket:Node;
  13. protected rightSocket:Node;
  14. protected leftWeapon:AnimationComponent;
  15. protected rightWeapon:AnimationComponent;
  16. protected fireKey:number=0;
  17. protected lastFireTime:number=0;
  18. protected isLeftFire:boolean;
  19. /**
  20. * 开火中
  21. */
  22. protected isFire:boolean=false;
  23. protected isReload:boolean=false;
  24. constructor(leftHand:AnimationComponent,rightHand:AnimationComponent){
  25. super();
  26. this.leftHand=leftHand;
  27. this.rightHand=rightHand;
  28. this.leftSocket=find("RootNode/Arm/upper_arm_R/lower_arm_R/transform1/hand_R/transform2/Guns",this.leftHand.node);
  29. this.rightSocket=find("RootNode/Arm/upper_arm_R/lower_arm_R/transform1/hand_R/transform2/Guns",this.rightHand.node);
  30. this.leftWeapon=instantiate(GameModel.single.currentWeaponPrefab).getComponent(AnimationComponent);
  31. this.leftSocket.addChild(this.leftWeapon.node);
  32. this.rightWeapon=instantiate(GameModel.single.currentWeaponPrefab).getComponent(AnimationComponent);
  33. this.rightSocket.addChild(this.rightWeapon.node);
  34. this.Idle(true);
  35. }
  36. StartFire(fireKey:number):void{
  37. this.fireKey=fireKey;
  38. this.isFire=true;
  39. this.MoveFirePosition();
  40. }
  41. StopFire():void{
  42. this.isFire=false;
  43. this.Idle();
  44. }
  45. Update(dt:number):void{
  46. if(this.isFire==false||this.isReload){
  47. return;
  48. }
  49. let currentTime=director.getCurrentTime();
  50. if(currentTime-this.lastFireTime>GameModel.single.currentWeaponConfig.firingRate*1000){
  51. this.Fire();
  52. this.bulletCount--;
  53. this.lastFireTime=currentTime;
  54. }
  55. //换弹
  56. if(this.bulletCount<=0){
  57. this.Reload();
  58. }
  59. }
  60. /**
  61. * 换弹
  62. */
  63. Reload():void{
  64. this.isReload=true;
  65. this.leftHand.crossFade("Reload");
  66. this.rightHand.crossFade("Reload");
  67. this.leftHand.on(AnimationComponent.EventType.FINISHED,this.ReloadComplete,this,true);
  68. }
  69. /**
  70. * 换弹完成
  71. */
  72. ReloadComplete():void{
  73. this.bulletCount=this.maxBulletCount;
  74. this.isReload=false;
  75. }
  76. /**
  77. * 将手移动到指定位置
  78. */
  79. MoveFirePosition():void{
  80. if(this.fireKey==0){
  81. return;
  82. }
  83. let Pos:Vec3;
  84. if(this.fireKey==1){
  85. Pos=this.leftHand.node.position;
  86. Pos.x=-0.3;
  87. this.leftHand.node.setPosition(Pos);
  88. }else if(this.fireKey==3){
  89. Pos=this.rightHand.node.position;
  90. Pos.x=0.3;
  91. this.rightHand.node.setPosition(Pos);
  92. }else if(this.fireKey==2){
  93. Pos=this.leftHand.node.position;
  94. Pos.x=-0.06;
  95. this.leftHand.node.setPosition(Pos);
  96. Pos=this.rightHand.node.position;
  97. Pos.x=0.06;
  98. this.rightHand.node.setPosition(Pos);
  99. }
  100. }
  101. /**
  102. * 开火
  103. */
  104. private Fire():void{
  105. if(this.fireKey==1){
  106. this.leftHand.crossFade("Fire");
  107. this.isLeftFire=true;
  108. }else if(this.fireKey==3){
  109. this.rightHand.crossFade("Fire");
  110. this.isLeftFire=false;
  111. }else{
  112. this.isLeftFire=!this.isLeftFire;
  113. if(this.isLeftFire){
  114. this.leftHand.crossFade("Fire");
  115. }else{
  116. this.rightHand.crossFade("Fire");
  117. }
  118. }
  119. }
  120. /**
  121. * 空闲
  122. */
  123. private Idle(isAll?:boolean):void{
  124. if(isAll){
  125. this.leftHand.crossFade("Idle",1.5);
  126. this.rightHand.crossFade("Idle",1.5);
  127. }else{
  128. if(this.isLeftFire){
  129. this.leftHand.crossFade("Idle",1.5);
  130. }else{
  131. this.rightHand.crossFade("Idle",1.5);
  132. }
  133. }
  134. }
  135. /**
  136. * 剩余子弹数量
  137. */
  138. get bulletCount():number{
  139. return this._bulletCount;
  140. }
  141. private _bulletCount:number=0;
  142. set bulletCount(value:number){
  143. this._bulletCount=value;
  144. this.DispatchEvent(WeaponBase.EVENT_BULLET_CHANGE);
  145. }
  146. /**
  147. * 弹夹数量
  148. */
  149. get maxBulletCount():number{
  150. return GameModel.single.currentWeaponConfig.clip;
  151. }
  152. }