ArmBowDoublePlayer.cs 10.0 KB

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