ArmBow.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /* 弓对象 */
  6. public class ArmBow : MonoBehaviour
  7. {
  8. [SerializeField] AnimationPlayer AP_arm;
  9. [SerializeField] AnimationPlayer AP_bow;
  10. [SerializeField] public GameObject arrow;
  11. BowCamera _bowCamera;
  12. BowCamera bowCamera {
  13. get {
  14. if (!_bowCamera) _bowCamera = GetComponentInParent<BowCamera>();
  15. return _bowCamera;
  16. }
  17. }
  18. //有效射击目标集合,可以理解为射中集合中的目标才能得分
  19. public HashSet<TargetBody> validTargets = new HashSet<TargetBody>();
  20. //本地坐标z
  21. public const float localPosZ = -0.15f;
  22. //射箭姿态记录索引倒退数,根据射击难度制造误差
  23. [NonSerialized] public int shootBackTime = 0;
  24. [NonSerialized] public float shootOffsetAngleScale = 0;
  25. //禁止准备的外部接口
  26. [NonSerialized] public bool banReady = false;
  27. //禁止射击的外部接口
  28. [NonSerialized] public bool banShoot = false;
  29. //禁止逻辑,只用于同步状态和渲染,目前用于联机
  30. [NonSerialized] public bool banLogic = false;
  31. //过去几帧的镜头值记录
  32. Quaternion[] cameraRotations = new Quaternion[6];
  33. int cameraRotationHasRecordCount = 0;
  34. #region logic state
  35. [NonSerialized] public int phase = -1; //当前阶段
  36. void UpdatePhase() {
  37. if (phase == -1) return;
  38. if (phase == 0) { //拉弓前的准备
  39. if (arm_ani_index_cur != 0) {
  40. AP_arm.play(arm_ani_index_cur = 0, WrapMode.Once);
  41. AP_arm.completeCallback = onComplete;
  42. }
  43. if (bow_ani_index_cur != 0) {
  44. AP_bow.play(bow_ani_index_cur = 0, WrapMode.Once);
  45. }
  46. } else if (phase == 1) { //拉弓过程
  47. if (arm_ani_index_cur != 2) {
  48. AP_arm.play(arm_ani_index_cur = 2, WrapMode.Once);
  49. AP_arm.completeCallback = onComplete;
  50. }
  51. if (bow_ani_index_cur != 2) {
  52. AP_bow.play(bow_ani_index_cur = 2, WrapMode.Once);
  53. }
  54. this.bowCamera.updateFollowPullBow();
  55. } else if (phase == 2) { //拉完完成,等待发射
  56. if (arm_ani_index_cur != 2) {
  57. AP_arm.play(arm_ani_index_cur = 2, WrapMode.Once);
  58. AP_arm.completeCallback = onComplete;
  59. }
  60. if (bow_ani_index_cur != 2) {
  61. AP_bow.play(bow_ani_index_cur = 2, WrapMode.Once);
  62. }
  63. } else if (phase == 3) { //射出后
  64. if (arm_ani_index_cur != 3) {
  65. AP_arm.play(arm_ani_index_cur = 3, WrapMode.Once);
  66. AP_arm.completeCallback = onComplete;
  67. }
  68. if (bow_ani_index_cur != 3) {
  69. AP_bow.play(bow_ani_index_cur = 3, WrapMode.Once);
  70. }
  71. }
  72. if (!IsCanShoot()) {
  73. this.bowCamera.updateGiveUpPullBow();
  74. }
  75. }
  76. void onComplete(AnimationPlayerCompleteResult res) {
  77. if (res.index == 0 && !banLogic) {
  78. this.phase = 1;
  79. }
  80. else if (res.index == 2 && !banLogic) {
  81. this.phase = 2;
  82. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  83. }
  84. }
  85. public bool IsCanShoot() {
  86. return phase == 2;
  87. }
  88. #endregion
  89. #region display state
  90. int arm_ani_index_cur = -1;
  91. int bow_ani_index_cur = -1;
  92. #endregion
  93. private static ArmBow _ins;
  94. public static ArmBow ins {
  95. get {
  96. if (!_ins) {
  97. _ins = GameObject.FindObjectOfType<ArmBow>();
  98. }
  99. return _ins;
  100. }
  101. }
  102. void Awake()
  103. {
  104. _ins = this;
  105. //initdata
  106. Vector3 localPos = transform.localPosition; localPos.z = localPosZ; transform.localPosition = localPos;
  107. int currentShootLevel = UserSettings.ins.shootLevel;
  108. shootBackTime = new int[]{0, 2, 5}[currentShootLevel];
  109. shootOffsetAngleScale = new float[]{0, 10f, 10f}[currentShootLevel];
  110. }
  111. void Start()
  112. {
  113. this.readyShoot();
  114. }
  115. void OnEnable()
  116. {
  117. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  118. }
  119. void Update()
  120. {
  121. UpdatePhase();
  122. if (Input.GetKeyDown(KeyCode.Q)) this.ADS_fire();
  123. }
  124. void FixedUpdate()
  125. {
  126. // 记录一些旋转角---start
  127. if (this.bowCamera) {
  128. for (int i = cameraRotations.Length - 1; i > 0 ; i--) {
  129. cameraRotations[i] = cameraRotations[i - 1];
  130. }
  131. cameraRotations[0] = this.bowCamera.transform.rotation;
  132. cameraRotationHasRecordCount++;
  133. }
  134. // 记录一些旋转角---end
  135. }
  136. public void readyShoot() {
  137. if (banLogic) return;
  138. this.bowCamera.SetCameraFieldOfViewRecord(this.bowCamera.defaultCameraFieldOfView);
  139. if (banReady) return;
  140. GameMgr.ins.gameMode.PauseTimeCounting(this);
  141. GameMgr.ins.gameMode.onBowReady();
  142. this.phase = 0;
  143. }
  144. public void ADS_fire() {
  145. if (!IsCanShoot() || banShoot || GameMgr.ins.gamePause || banLogic || GameMgr.ins.gameOver) return;
  146. GameMgr.ins.gameMode.onBowShoot();
  147. this.phase = 3;
  148. shoot();
  149. }
  150. void shoot() {
  151. #region
  152. Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  153. Quaternion final_rotation = this.bowCamera.transform.rotation;
  154. if (cameraRotationHasRecordCount >= cameraRotations.Length) {
  155. absolute_rotation = cameraRotations[5];
  156. final_rotation = cameraRotations[5 - this.shootBackTime];
  157. }
  158. #endregion
  159. Quaternion oldCameraRotation = BowCamera.ins.transform.rotation;
  160. BowCamera.ins.transform.rotation = absolute_rotation;
  161. RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
  162. BowCamera.ins.transform.rotation = oldCameraRotation;
  163. Vector3 shootOutPosition = this.bowCamera.transform.position;
  164. Vector3 arrowEuler = absolute_rotation.eulerAngles;
  165. arrowEuler.z = 0; //绝对角可能是从原始九轴记录数组里取出来的,它的z可能不是0
  166. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, Quaternion.Euler(arrowEuler));
  167. Vector3 s1 = arrowCopy.transform.localScale;
  168. Vector3 s2 = bowCamera.transform.localScale;
  169. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  170. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  171. arrowComp.armBow = this;
  172. arrowComp.shootOutPosition = shootOutPosition;
  173. arrowComp.absoluteRay = absoluteRay;
  174. arrowComp.offsetAngle = GameDebug.ins ?
  175. GameDebug.ins.GetOffsetAngle() :
  176. Quaternion.Angle(absolute_rotation, final_rotation);
  177. arrowComp.finalAngleAfterOffset = final_rotation.eulerAngles;
  178. if (ShootCheck.ins && !GameMgr.debugInEditor && !BowCamera.isTouchMode)
  179. {
  180. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  181. }
  182. GameEventCenter.ins.onBowArrowShootOut?.Invoke(this, arrowComp);
  183. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  184. if (AimHandler.ins) AimHandler.ins.BanControlObjRotate(true);
  185. }
  186. public void Hide()
  187. {
  188. this.transform.localScale = Vector3.zero;
  189. }
  190. public void Show()
  191. {
  192. this.transform.localScale = new Vector3(1, 1, 1);
  193. }
  194. }