ArmBow.cs 11 KB

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