WeaponBase.ts 9.7 KB

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