ArmBow.cs 11 KB

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