WeaponBase.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.PlaySound("Weapon",this.weaponConfig.reloadSound);
  122. }
  123. /**
  124. * 换弹完成
  125. */
  126. ReloadComplete():void{
  127. this.bulletCount=this.maxBulletCount;
  128. this.isReload=false;
  129. if(this.isFireing==false){
  130. this.Idle();
  131. }
  132. }
  133. /**
  134. * 将手移动到指定位置
  135. */
  136. MoveFirePosition():void{
  137. if(this.fireKey==0){
  138. return;
  139. }
  140. let Pos:Vec3;
  141. if(this.fireKey==1){
  142. Pos=this.leftHand.node.position;
  143. Pos.x=-0.4;
  144. this.leftHand.node.setPosition(Pos);
  145. }else if(this.fireKey==3){
  146. Pos=this.rightHand.node.position;
  147. Pos.x=0.4;
  148. this.rightHand.node.setPosition(Pos);
  149. }else if(this.fireKey==2){
  150. Pos=this.leftHand.node.position;
  151. Pos.x=-0.1;
  152. this.leftHand.node.setPosition(Pos);
  153. Pos=this.rightHand.node.position;
  154. Pos.x=0.1;
  155. this.rightHand.node.setPosition(Pos);
  156. }
  157. }
  158. /**
  159. * 开火
  160. */
  161. Fire(bullet:boolean):void{
  162. this.isFire=true;
  163. }
  164. /**
  165. * 空闲
  166. */
  167. private Idle():void{
  168. if(this.isFire){
  169. if(this.isLeftFire){
  170. this.rightHand.crossFade("Idle",0.5);
  171. }else{
  172. this.leftHand.crossFade("Idle",0.5)
  173. }
  174. }else{
  175. this.leftHand.crossFade("Idle",0.5);
  176. this.rightHand.crossFade("Idle",0.5);
  177. }
  178. }
  179. /**
  180. * 剩余子弹数量
  181. */
  182. get bulletCount():number{
  183. return this._bulletCount;
  184. }
  185. private _bulletCount:number=0;
  186. set bulletCount(value:number){
  187. this._bulletCount=value;
  188. if(this._bulletCount<0){
  189. this._bulletCount=0;
  190. }
  191. this.DispatchEvent(WeaponBase.EVENT_BULLET_CHANGE);
  192. }
  193. /**
  194. * 弹夹数量
  195. */
  196. get maxBulletCount():number{
  197. return this.weaponConfig.clip;
  198. }
  199. /**
  200. * 换弹进度
  201. */
  202. get reloadProgress():number{
  203. if(this.isReload){
  204. let state:AnimationState=this.leftHand.getState(this.weaponConfig.handReloadAnimation);
  205. return state.time/state.length;
  206. }
  207. return -1;
  208. }
  209. }