ArmBow.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.1f;
  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. this.transform.localPosition = new Vector3(0.0865f, -1.692f, -0.1f);
  106. //initdata
  107. Vector3 localPos = transform.localPosition; localPos.z = localPosZ; transform.localPosition = localPos;
  108. int currentShootLevel = UserSettings.ins.shootLevel;
  109. shootBackTime = new int[]{0, 2, 5}[currentShootLevel];
  110. shootOffsetAngleScale = new float[]{0, 10f, 10f}[currentShootLevel];
  111. }
  112. void Start()
  113. {
  114. this.readyShoot();
  115. }
  116. void OnDestroy()
  117. {
  118. if (_ins == this) _ins = null;
  119. }
  120. void OnEnable()
  121. {
  122. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  123. }
  124. void Update()
  125. {
  126. UpdatePhase();
  127. #if UNITY_EDITOR
  128. if (Input.GetKeyDown(KeyCode.Q)) this.ADS_fire();
  129. #endif
  130. }
  131. void FixedUpdate()
  132. {
  133. // 记录一些旋转角---start
  134. if (this.bowCamera) {
  135. for (int i = cameraRotations.Length - 1; i > 0 ; i--) {
  136. cameraRotations[i] = cameraRotations[i - 1];
  137. }
  138. cameraRotations[0] = this.bowCamera.transform.rotation;
  139. cameraRotationHasRecordCount++;
  140. }
  141. // 记录一些旋转角---end
  142. }
  143. public void readyShoot() {
  144. if (banLogic) return;
  145. this.bowCamera.SetCameraFieldOfViewRecord(this.bowCamera.defaultCameraFieldOfView);
  146. if (banReady) return;
  147. GameMgr.ins.gameMode.PauseTimeCounting(this);
  148. GameMgr.ins.gameMode.onBowReady();
  149. this.phase = 0;
  150. }
  151. public void ADS_fire() {
  152. if (!IsCanShoot() || banShoot || GameMgr.ins.gamePause || banLogic || GameMgr.ins.gameOver) return;
  153. GameMgr.ins.gameMode.onBowShoot();
  154. this.phase = 3;
  155. shoot();
  156. }
  157. void shoot() {
  158. #region
  159. Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  160. Quaternion final_rotation = this.bowCamera.transform.rotation;
  161. if (cameraRotationHasRecordCount >= cameraRotations.Length) {
  162. absolute_rotation = cameraRotations[5];
  163. final_rotation = cameraRotations[5 - this.shootBackTime];
  164. }
  165. #endregion
  166. Quaternion oldCameraRotation = BowCamera.ins.transform.rotation;
  167. BowCamera.ins.transform.rotation = absolute_rotation;
  168. RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
  169. BowCamera.ins.transform.rotation = oldCameraRotation;
  170. Vector3 shootOutPosition = this.bowCamera.transform.position;
  171. Vector3 arrowEuler = absolute_rotation.eulerAngles;
  172. arrowEuler.z = 0; //绝对角可能是从原始九轴记录数组里取出来的,它的z可能不是0
  173. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, Quaternion.Euler(arrowEuler));
  174. Vector3 s1 = arrowCopy.transform.localScale;
  175. Vector3 s2 = bowCamera.transform.localScale;
  176. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  177. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  178. arrowComp.armBow = this;
  179. arrowComp.shootOutPosition = shootOutPosition;
  180. arrowComp.absoluteRay = absoluteRay;
  181. arrowComp.offsetAngle = GameDebug.ins ?
  182. GameDebug.ins.GetOffsetAngle() :
  183. Quaternion.Angle(absolute_rotation, final_rotation);
  184. arrowComp.finalAngleAfterOffset = final_rotation.eulerAngles;
  185. if (ShootCheck.ins && !GameMgr.debugInEditor && !BowCamera.isTouchMode)
  186. {
  187. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  188. }
  189. GameEventCenter.ins.onBowArrowShootOut?.Invoke(this, arrowComp);
  190. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  191. if (AimHandler.ins) AimHandler.ins.Ban9AxisCalculate(true);
  192. }
  193. public void Hide()
  194. {
  195. this.transform.localScale = Vector3.zero;
  196. }
  197. public void Show()
  198. {
  199. this.transform.localScale = new Vector3(1, 1, 1);
  200. }
  201. }