WeaponBase.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /**
  56. * 销毁
  57. */
  58. Destroy():void{
  59. this.weaponPrefab=null;
  60. //枪口火焰
  61. this.leftSocket.removeChild(this.leftWeapon);
  62. this.leftWeapon.destroy();
  63. this.leftWeapon=null;
  64. this.rightSocket.removeChild(this.rightWeapon);
  65. this.rightWeapon.destroy();
  66. this.rightWeapon=null;
  67. this.gunfirePrefab=null;
  68. this.leftGunFire.node.destroy();
  69. this.leftGunFire=null;
  70. this.rightGunFire.node.destroy();
  71. this.rightGunFire=null;
  72. }
  73. protected InitHands():void{
  74. this.leftSocket=find("RootNode/Arm/upper_arm_R/lower_arm_R/transform1/hand_R/transform2/Guns",this.leftHand.node);
  75. this.rightSocket=find("RootNode/Arm/upper_arm_R/lower_arm_R/transform1/hand_R/transform2/Guns",this.rightHand.node);
  76. // CCSAnimationUtils.SetAnimationEvent(this.leftHand,this.weaponConfig.handFireAnimation,this.weaponConfig.fireAnimationEvents);
  77. // CCSAnimationUtils.SetAnimationEvent(this.rightHand,this.weaponConfig.handFireAnimation,this.weaponConfig.fireAnimationEvents);
  78. //根据武器配置设置手臂动画速度
  79. CCSAnimationUtils.SetAnimationSpeed(this.leftHand,this.weaponConfig.handFireAnimation,this.weaponConfig.handFireAnimationSpeed);
  80. CCSAnimationUtils.SetAnimationSpeed(this.leftHand,this.weaponConfig.handReloadAnimation,this.weaponConfig.handReloadAnimationSpeed);
  81. CCSAnimationUtils.SetAnimationSpeed(this.rightHand,this.weaponConfig.handFireAnimation,this.weaponConfig.handFireAnimationSpeed);
  82. CCSAnimationUtils.SetAnimationSpeed(this.rightHand,this.weaponConfig.handReloadAnimation,this.weaponConfig.handReloadAnimationSpeed);
  83. }
  84. protected InitWeapons():void{
  85. this.leftWeapon=instantiate(this.weaponPrefab);
  86. this.leftSocket.addChild(this.leftWeapon);
  87. this.rightWeapon=instantiate(this.weaponPrefab);
  88. this.rightSocket.addChild(this.rightWeapon);
  89. this.gunfirePrefab=loader.getRes("d3d/gunFire/GunFire");
  90. this.leftGunFire=instantiate(this.gunfirePrefab).getComponent(AnimationComponent);
  91. this.rightGunFire=instantiate(this.gunfirePrefab).getComponent(AnimationComponent);
  92. let socket:Node=find(this.weaponConfig.fireSocketPath,this.leftWeapon);
  93. if(socket==null){
  94. console.error("配置挂点错误:"+this.weaponConfig);
  95. }else{
  96. socket.addChild(this.leftGunFire.node);
  97. }
  98. socket=find(this.weaponConfig.fireSocketPath,this.rightWeapon);
  99. if(socket==null){
  100. console.error("配置挂点错误:"+this.weaponConfig);
  101. }else{
  102. socket.addChild(this.rightGunFire.node);
  103. }
  104. }
  105. StartFire(fireKey:number):void{
  106. this.fireKey=fireKey;
  107. this.isFireing=true;
  108. this.MoveFirePosition();
  109. }
  110. StopFire():void{
  111. this.isFireing=false;
  112. if(this.isReload){
  113. return;
  114. }
  115. this.Idle();
  116. }
  117. Update(dt:number):void{
  118. if(this.isFireing==false||this.isReload){
  119. return;
  120. }
  121. if(this.isFire==false){
  122. //换弹
  123. if(this.bulletCount<=0){
  124. this.Reload();
  125. return;
  126. }
  127. this.Fire(true);
  128. }
  129. }
  130. /**
  131. * 换弹
  132. */
  133. Reload():void{
  134. this.isReload=true;
  135. this.leftHand.play(this.weaponConfig.handReloadAnimation);
  136. this.rightHand.play(this.weaponConfig.handReloadAnimation);
  137. //左右手同步
  138. this.leftHand.on(AnimationComponent.EventType.FINISHED,this.ReloadComplete,this,true);
  139. SoundManager.single.StopSound("Weapon");
  140. SoundManager.single.PlaySound("Weapon",this.weaponConfig.reloadSound);
  141. }
  142. /**
  143. * 换弹完成
  144. */
  145. ReloadComplete():void{
  146. this.bulletCount=this.maxBulletCount;
  147. this.isReload=false;
  148. if(this.isFireing==false){
  149. this.Idle();
  150. }
  151. }
  152. /**
  153. * 将手移动到指定位置
  154. */
  155. MoveFirePosition():void{
  156. if(this.fireKey==0){
  157. return;
  158. }
  159. let Pos:Vec3;
  160. if(this.fireKey==1){
  161. Pos=this.leftHand.node.position;
  162. Pos.x=-0.4;
  163. this.leftHand.node.setPosition(Pos);
  164. }else if(this.fireKey==3){
  165. Pos=this.rightHand.node.position;
  166. Pos.x=0.4;
  167. this.rightHand.node.setPosition(Pos);
  168. }else if(this.fireKey==2){
  169. Pos=this.leftHand.node.position;
  170. Pos.x=-0.1;
  171. this.leftHand.node.setPosition(Pos);
  172. Pos=this.rightHand.node.position;
  173. Pos.x=0.1;
  174. this.rightHand.node.setPosition(Pos);
  175. }
  176. }
  177. /**
  178. * 开火
  179. */
  180. Fire(bullet:boolean):void{
  181. this.isFire=true;
  182. }
  183. /**
  184. * 空闲
  185. */
  186. private Idle():void{
  187. if(this.isFire){
  188. if(this.isLeftFire){
  189. this.rightHand.crossFade("Idle",0.5);
  190. }else{
  191. this.leftHand.crossFade("Idle",0.5)
  192. }
  193. }else{
  194. this.leftHand.crossFade("Idle",0.5);
  195. this.rightHand.crossFade("Idle",0.5);
  196. }
  197. }
  198. /**
  199. * 剩余子弹数量
  200. */
  201. get bulletCount():number{
  202. return this._bulletCount;
  203. }
  204. private _bulletCount:number=0;
  205. set bulletCount(value:number){
  206. this._bulletCount=value;
  207. if(this._bulletCount<0){
  208. this._bulletCount=0;
  209. }
  210. this.DispatchEvent(WeaponBase.EVENT_BULLET_CHANGE);
  211. }
  212. /**
  213. * 弹夹数量
  214. */
  215. get maxBulletCount():number{
  216. return this.weaponConfig.clip;
  217. }
  218. /**
  219. * 换弹进度
  220. */
  221. get reloadProgress():number{
  222. if(this.isReload){
  223. let state:AnimationState=this.leftHand.getState(this.weaponConfig.handReloadAnimation);
  224. return state.time/state.length;
  225. }
  226. return -1;
  227. }
  228. }