ArmBow.cs 7.4 KB

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