WeaponBase.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { _decorator, Component, Node, CCInteger, AnimationComponent, find, instantiate, Vec3, director, Vec2, log, Prefab, loader, AnimationState } from 'cc';
  2. import { GameModel } from '../../../models/GameModel';
  3. import { EventDispatcher } from '../../../../engines/events/EventDispatcher';
  4. import GameConfigManager from '../../../models/GameConfigManager';
  5. import CCSAnimationUtils from '../../../../engines/utils/CCSAnimationUtils';
  6. import { FireEventComponent } from './components/FireEventComponent';
  7. import { SoundManager } from '../../../../engines/sounds/SoundManager';
  8. const { ccclass, property } = _decorator;
  9. export class WeaponBase extends EventDispatcher{
  10. /**
  11. * 武器配置
  12. */
  13. public weaponConfig:any;
  14. /**
  15. * 武器的预制体
  16. */
  17. protected weaponPrefab:Prefab;
  18. /**
  19. * 子弹数量更改
  20. */
  21. public static EVENT_BULLET_CHANGE:string="EVENT_BULLET_CHANGE";
  22. protected leftHand:AnimationComponent;
  23. protected rightHand:AnimationComponent;
  24. protected leftSocket:Node;
  25. protected rightSocket:Node;
  26. protected leftWeapon:Node;
  27. protected rightWeapon:Node;
  28. protected gunfirePrefab:Prefab;
  29. protected leftGunFire:AnimationComponent;
  30. protected rightGunFire:AnimationComponent;
  31. /**
  32. * 开火方向 1 左边 2中间 3左边
  33. */
  34. public fireKey:number=0;
  35. protected isLeftFire:boolean;
  36. /**
  37. * 开火中
  38. */
  39. protected isFireing:boolean=false;
  40. protected isFire:boolean=false;
  41. public isReload:boolean=false;
  42. constructor(leftHand:AnimationComponent,rightHand:AnimationComponent){
  43. super();
  44. this.leftHand=leftHand;
  45. // this.leftHand.addComponent(FireEventComponent);
  46. this.rightHand=rightHand;
  47. // this.rightHand.addComponent(FireEventComponent);
  48. //武器配置
  49. this.weaponConfig=GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
  50. this.weaponPrefab=loader.getRes(this.weaponConfig.prefab);
  51. this.InitHands();
  52. this.InitWeapons();
  53. this.Idle();
  54. }
  55. protected InitHands():void{
  56. this.leftSocket=find("RootNode/Arm/upper_arm_R/lower_arm_R/transform1/hand_R/transform2/Guns",this.leftHand.node);
  57. this.rightSocket=find("RootNode/Arm/upper_arm_R/lower_arm_R/transform1/hand_R/transform2/Guns",this.rightHand.node);
  58. // CCSAnimationUtils.SetAnimationEvent(this.leftHand,this.weaponConfig.handFireAnimation,this.weaponConfig.fireAnimationEvents);
  59. // CCSAnimationUtils.SetAnimationEvent(this.rightHand,this.weaponConfig.handFireAnimation,this.weaponConfig.fireAnimationEvents);
  60. //根据武器配置设置手臂动画速度
  61. CCSAnimationUtils.SetAnimationSpeed(this.leftHand,this.weaponConfig.handFireAnimation,this.weaponConfig.handFireAnimationSpeed);
  62. CCSAnimationUtils.SetAnimationSpeed(this.leftHand,this.weaponConfig.handReloadAnimation,this.weaponConfig.handReloadAnimationSpeed);
  63. CCSAnimationUtils.SetAnimationSpeed(this.rightHand,this.weaponConfig.handFireAnimation,this.weaponConfig.handFireAnimationSpeed);
  64. CCSAnimationUtils.SetAnimationSpeed(this.rightHand,this.weaponConfig.handReloadAnimation,this.weaponConfig.handReloadAnimationSpeed);
  65. }
  66. protected InitWeapons():void{
  67. this.leftWeapon=instantiate(this.weaponPrefab);
  68. this.leftSocket.addChild(this.leftWeapon);
  69. this.rightWeapon=instantiate(this.weaponPrefab);
  70. this.rightSocket.addChild(this.rightWeapon);
  71. this.gunfirePrefab=loader.getRes("d3d/gunFire/GunFire");
  72. this.leftGunFire=instantiate(this.gunfirePrefab).getComponent(AnimationComponent);
  73. this.rightGunFire=instantiate(this.gunfirePrefab).getComponent(AnimationComponent);
  74. let socket:Node=find(this.weaponConfig.fireSocketPath,this.leftWeapon);
  75. if(socket==null){
  76. console.error("配置挂点错误:"+this.weaponConfig);
  77. }else{
  78. socket.addChild(this.leftGunFire.node);
  79. }
  80. socket=find(this.weaponConfig.fireSocketPath,this.rightWeapon);
  81. if(socket==null){
  82. console.error("配置挂点错误:"+this.weaponConfig);
  83. }else{
  84. socket.addChild(this.rightGunFire.node);
  85. }
  86. }
  87. StartFire(fireKey:number):void{
  88. this.fireKey=fireKey;
  89. this.isFireing=true;
  90. this.MoveFirePosition();
  91. }
  92. StopFire():void{
  93. this.isFireing=false;
  94. if(this.isReload){
  95. return;
  96. }
  97. this.Idle();
  98. }
  99. Update(dt:number):void{
  100. if(this.isFireing==false||this.isReload){
  101. return;
  102. }
  103. if(this.isFire==false){
  104. //换弹
  105. if(this.bulletCount<=0){
  106. this.Reload();
  107. return;
  108. }
  109. this.Fire(true);
  110. }
  111. }
  112. /**
  113. * 换弹
  114. */
  115. Reload():void{
  116. this.isReload=true;
  117. this.leftHand.play(this.weaponConfig.handReloadAnimation);
  118. this.rightHand.play(this.weaponConfig.handReloadAnimation);
  119. //左右手同步
  120. this.leftHand.on(AnimationComponent.EventType.FINISHED,this.ReloadComplete,this,true);
  121. SoundManager.single.StopSound("Weapon");
  122. SoundManager.single.PlaySound("Weapon",this.weaponConfig.reloadSound);
  123. }
  124. /**
  125. * 换弹完成
  126. */
  127. ReloadComplete():void{
  128. this.bulletCount=this.maxBulletCount;
  129. this.isReload=false;
  130. if(this.isFireing==false){
  131. this.Idle();
  132. }
  133. }
  134. /**
  135. * 将手移动到指定位置
  136. */
  137. MoveFirePosition():void{
  138. if(this.fireKey==0){
  139. return;
  140. }
  141. let Pos:Vec3;
  142. if(this.fireKey==1){
  143. Pos=this.leftHand.node.position;
  144. Pos.x=-0.4;
  145. this.leftHand.node.setPosition(Pos);
  146. }else if(this.fireKey==3){
  147. Pos=this.rightHand.node.position;
  148. Pos.x=0.4;
  149. this.rightHand.node.setPosition(Pos);
  150. }else if(this.fireKey==2){
  151. Pos=this.leftHand.node.position;
  152. Pos.x=-0.1;
  153. this.leftHand.node.setPosition(Pos);
  154. Pos=this.rightHand.node.position;
  155. Pos.x=0.1;
  156. this.rightHand.node.setPosition(Pos);
  157. }
  158. }
  159. /**
  160. * 开火
  161. */
  162. Fire(bullet:boolean):void{
  163. this.isFire=true;
  164. }
  165. /**
  166. * 空闲
  167. */
  168. private Idle():void{
  169. if(this.isFire){
  170. if(this.isLeftFire){
  171. this.rightHand.crossFade("Idle",0.5);
  172. }else{
  173. this.leftHand.crossFade("Idle",0.5)
  174. }
  175. }else{
  176. this.leftHand.crossFade("Idle",0.5);
  177. this.rightHand.crossFade("Idle",0.5);
  178. }
  179. }
  180. /**
  181. * 剩余子弹数量
  182. */
  183. get bulletCount():number{
  184. return this._bulletCount;
  185. }
  186. private _bulletCount:number=0;
  187. set bulletCount(value:number){
  188. this._bulletCount=value;
  189. if(this._bulletCount<0){
  190. this._bulletCount=0;
  191. }
  192. this.DispatchEvent(WeaponBase.EVENT_BULLET_CHANGE);
  193. }
  194. /**
  195. * 弹夹数量
  196. */
  197. get maxBulletCount():number{
  198. return this.weaponConfig.clip;
  199. }
  200. /**
  201. * 换弹进度
  202. */
  203. get reloadProgress():number{
  204. if(this.isReload){
  205. let state:AnimationState=this.leftHand.getState(this.weaponConfig.handReloadAnimation);
  206. return state.time/state.length;
  207. }
  208. return -1;
  209. }
  210. }