ArmBow.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. [SerializeField] GameObject bullet;
  12. [SerializeField] GameObject armObj;
  13. [SerializeField] GameObject bowObj;
  14. BowCamera _bowCamera;
  15. BowCamera bowCamera {
  16. get {
  17. if (!_bowCamera) _bowCamera = GetComponentInParent<BowCamera>();
  18. return _bowCamera;
  19. }
  20. }
  21. //有效射击目标集合,可以理解为射中集合中的目标才能得分
  22. public HashSet<TargetBody> validTargets = new HashSet<TargetBody>();
  23. //本地坐标z
  24. public const float localPosZ = -0.1f;
  25. //射箭姿态记录索引倒退数,根据射击难度制造误差
  26. [NonSerialized] public int shootBackTime = 0;
  27. [NonSerialized] public float shootOffsetAngleScale = 0;
  28. //禁止准备的外部接口
  29. [NonSerialized] public bool banReady = false;
  30. //禁止射击的外部接口
  31. [NonSerialized] public bool banShoot = false;
  32. //禁止逻辑,只用于同步状态和渲染,目前用于联机
  33. [NonSerialized] public bool banLogic = false;
  34. //过去几帧的镜头值记录
  35. Quaternion[] cameraRotations = new Quaternion[6];
  36. int cameraRotationHasRecordCount = 0;
  37. #region logic state
  38. [NonSerialized] public int phase = -1; //当前阶段
  39. void UpdatePhase() {
  40. if (phase == -1) return;
  41. if (phase == 0) { //拉弓前的准备
  42. if (arm_ani_index_cur != 0) {
  43. AP_arm.play(arm_ani_index_cur = 0, WrapMode.Once);
  44. AP_arm.completeCallback = onComplete;
  45. }
  46. if (bow_ani_index_cur != 0) {
  47. AP_bow.play(bow_ani_index_cur = 0, WrapMode.Once);
  48. }
  49. } else if (phase == 1) { //拉弓过程
  50. if (arm_ani_index_cur != 2) {
  51. AP_arm.play(arm_ani_index_cur = 2, WrapMode.Once);
  52. AP_arm.completeCallback = onComplete;
  53. }
  54. if (bow_ani_index_cur != 2) {
  55. AP_bow.play(bow_ani_index_cur = 2, WrapMode.Once);
  56. }
  57. this.bowCamera.updateFollowPullBow();
  58. } else if (phase == 2) { //拉完完成,等待发射
  59. if (arm_ani_index_cur != 2) {
  60. AP_arm.play(arm_ani_index_cur = 2, WrapMode.Once);
  61. AP_arm.completeCallback = onComplete;
  62. }
  63. if (bow_ani_index_cur != 2) {
  64. AP_bow.play(bow_ani_index_cur = 2, WrapMode.Once);
  65. }
  66. } else if (phase == 3) { //射出后
  67. if (arm_ani_index_cur != 3) {
  68. AP_arm.play(arm_ani_index_cur = 3, WrapMode.Once);
  69. AP_arm.completeCallback = onComplete;
  70. }
  71. if (bow_ani_index_cur != 3) {
  72. AP_bow.play(bow_ani_index_cur = 3, WrapMode.Once);
  73. }
  74. }
  75. if (!IsCanShoot()) {
  76. this.bowCamera.updateGiveUpPullBow();
  77. }
  78. }
  79. void onComplete(AnimationPlayerCompleteResult res) {
  80. if (res.index == 0 && !banLogic) {
  81. this.phase = 1;
  82. }
  83. else if (res.index == 2 && !banLogic) {
  84. this.phase = 2;
  85. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  86. }
  87. }
  88. public bool IsCanShoot() {
  89. return phase == 2;
  90. }
  91. #endregion
  92. #region display state
  93. int arm_ani_index_cur = -1;
  94. int bow_ani_index_cur = -1;
  95. #endregion
  96. private static ArmBow _ins;
  97. public static ArmBow ins {
  98. get {
  99. if (!_ins) {
  100. _ins = GameObject.FindObjectOfType<ArmBow>();
  101. }
  102. return _ins;
  103. }
  104. }
  105. void Awake()
  106. {
  107. _ins = this;
  108. this.transform.localPosition = new Vector3(0.0865f, -1.692f, -0.1f);
  109. //initdata
  110. Vector3 localPos = transform.localPosition; localPos.z = localPosZ; transform.localPosition = localPos;
  111. int currentShootLevel = UserSettings.ins.shootLevel;
  112. shootBackTime = new int[]{0, 2, 5}[currentShootLevel];
  113. shootOffsetAngleScale = new float[]{0, 10f, 10f}[currentShootLevel];
  114. //不同模式下设置速度,用于调整射击间隔
  115. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  116. {
  117. AP_arm.speed = 20;
  118. AP_bow.speed = 20;
  119. }
  120. //根据 hideInfraredBowAndArrow 判断是否显示隐藏
  121. if (PlayerPrefs.HasKey("hideInfraredBowAndArrow") && PlayerPrefs.GetInt("hideInfraredBowAndArrow") != 0) {
  122. armObj.SetActive(false);
  123. bowObj.SetActive(false);
  124. //解除默认 hide 状态
  125. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 2);
  126. }
  127. else {
  128. //枪模式下隐藏手臂等
  129. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  130. {
  131. armObj.SetActive(false);
  132. bowObj.SetActive(false);
  133. AP_arm.speed = 10;
  134. }
  135. else {
  136. armObj.SetActive(UserSettings.ins.openBowAndArrow);
  137. bowObj.SetActive(UserSettings.ins.openBowAndArrow);
  138. }
  139. }
  140. }
  141. void Start()
  142. {
  143. this.readyShoot();
  144. }
  145. void OnDestroy()
  146. {
  147. if (_ins == this) _ins = null;
  148. }
  149. void OnEnable()
  150. {
  151. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  152. }
  153. void Update()
  154. {
  155. UpdatePhase();
  156. #if UNITY_EDITOR
  157. if (Input.GetKeyDown(KeyCode.Q)) this.ADS_fire(true);
  158. #endif
  159. }
  160. void FixedUpdate()
  161. {
  162. // 记录一些旋转角---start
  163. if (this.bowCamera) {
  164. for (int i = cameraRotations.Length - 1; i > 0 ; i--) {
  165. cameraRotations[i] = cameraRotations[i - 1];
  166. }
  167. cameraRotations[0] = this.bowCamera.transform.rotation;
  168. cameraRotationHasRecordCount++;
  169. }
  170. // 记录一些旋转角---end
  171. }
  172. public void readyShoot() {
  173. if (banLogic) return;
  174. this.bowCamera.SetCameraFieldOfViewRecord(this.bowCamera.defaultCameraFieldOfView);
  175. if (banReady) return;
  176. GameMgr.ins.gameMode.PauseTimeCounting(this);
  177. GameMgr.ins.gameMode.onBowReady();
  178. this.phase = 0;
  179. }
  180. public void ADS_fire(bool bAddCount = false) {
  181. if (!IsCanShoot() || banShoot || GameMgr.ins.gamePause || banLogic || GameMgr.ins.gameOver) return;
  182. //枪模式连接的情况下需要判断子弹
  183. if (Billboard.ins && Billboard.ins.isBulletStatus)
  184. {
  185. if (Billboard.ins.bulletManager.bulletZero()) return;
  186. //发射消耗子弹
  187. Billboard.ins.bulletManager.FireBullet();
  188. }
  189. GameMgr.ins.gameMode.onBowShoot();
  190. //记录射箭
  191. if(bAddCount)GameMgr.ins.userGameAnalyse.onShootEvent();
  192. this.phase = 3;
  193. shoot();
  194. }
  195. void shoot() {
  196. #region
  197. Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  198. Quaternion final_rotation = this.bowCamera.transform.rotation;
  199. if (cameraRotationHasRecordCount >= cameraRotations.Length) {
  200. absolute_rotation = cameraRotations[5];
  201. final_rotation = cameraRotations[5 - this.shootBackTime];
  202. }
  203. #endregion
  204. Quaternion oldCameraRotation = BowCamera.ins.transform.rotation;
  205. BowCamera.ins.transform.rotation = absolute_rotation;
  206. RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
  207. BowCamera.ins.transform.rotation = oldCameraRotation;
  208. Vector3 shootOutPosition = this.bowCamera.transform.position;
  209. Vector3 arrowEuler = absolute_rotation.eulerAngles;
  210. arrowEuler.z = 0; //绝对角可能是从原始九轴记录数组里取出来的,它的z可能不是0
  211. GameObject arrowCopy = Instantiate(GlobalData.MyDeviceMode == DeviceMode.Archery? arrow : bullet, shootOutPosition, Quaternion.Euler(arrowEuler));
  212. arrowCopy.SetActive(true);
  213. Vector3 s1 = arrowCopy.transform.localScale;
  214. Vector3 s2 = bowCamera.transform.localScale;
  215. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  216. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  217. arrowComp.armBow = this;
  218. arrowComp.shootOutPosition = shootOutPosition;
  219. arrowComp.absoluteRay = absoluteRay;
  220. arrowComp.offsetAngle = GameDebug.ins ?
  221. GameDebug.ins.GetOffsetAngle() :
  222. Quaternion.Angle(absolute_rotation, final_rotation);
  223. arrowComp.finalAngleAfterOffset = final_rotation.eulerAngles;
  224. if (GlobalData.MyDeviceMode == DeviceMode.Gun) {
  225. //枪的速度,设置800
  226. Arrow.speed = 400;
  227. AudioMgr.ins.PlayGunShoot(AudioMgr.GetAudioSource(arrowCopy));
  228. } else {
  229. if (ShootCheck.ins && !GameMgr.debugInEditor && !BowCamera.isTouchMode)
  230. {
  231. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  232. }
  233. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  234. }
  235. GameEventCenter.ins.onBowArrowShootOut?.Invoke(this, arrowComp);
  236. if (AimHandler.ins) AimHandler.ins.Ban9AxisCalculate(true);
  237. }
  238. public void Hide()
  239. {
  240. this.transform.localScale = Vector3.zero;
  241. }
  242. public void Show()
  243. {
  244. this.transform.localScale = new Vector3(1, 1, 1);
  245. }
  246. }