ArmBowDoublePlayer.cs 8.5 KB

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