WeaponBase.ts 8.9 KB

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