ArmBow.cs 11 KB

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