WeaponBase.ts 7.2 KB

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