ArmBowDoublePlayer.cs 8.8 KB

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