ArmBow.cs 11 KB

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