BowCamera.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. /* 弓的相机 */
  5. public class BowCamera : MonoBehaviour
  6. {
  7. //相机组件
  8. Camera _cameraComp;
  9. Camera cameraComp {
  10. get {
  11. if (!_cameraComp) _cameraComp = GetComponent<Camera>();
  12. return _cameraComp;
  13. }
  14. }
  15. //控制的手臂弓
  16. ArmBow _armBow;
  17. ArmBow armBow {
  18. get {
  19. if (!_armBow) _armBow = GetComponentInChildren<ArmBow>();
  20. return _armBow;
  21. }
  22. }
  23. //本地欧拉角记录值
  24. Vector3 localEulerAngles;
  25. //触摸检测器
  26. JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
  27. //触摸模式开关
  28. private static bool _isTouchMode = true;
  29. public static bool isTouchMode {
  30. get {
  31. if (CommonConfig.isReleaseVersion) return false;
  32. return _isTouchMode;
  33. }
  34. set {
  35. _isTouchMode = value;
  36. }
  37. }
  38. //左右转动范围限制
  39. float[] limitRangeRotateY = {-80, 80};
  40. //上下转动范围限制
  41. float[] limitRangeRotateX = {-80, 80};
  42. //禁止逻辑,只用于同步状态和渲染,目前用于联机
  43. [NonSerialized] public bool banLogic = false;
  44. private static BowCamera _ins;
  45. public static BowCamera ins {
  46. get {
  47. if (!_ins) {
  48. _ins = GameObject.FindObjectOfType<BowCamera>();
  49. }
  50. return _ins;
  51. }
  52. }
  53. void Awake() {
  54. _ins = this;
  55. localEulerAngles = transform.localEulerAngles;
  56. RecordDefaultCameraFieldOfView();
  57. if (UserSettings.ins.bowCameraFixed && !CommonConfig.isReleaseVersion) {
  58. bowCameraFixed = new BowCameraFixed(this);
  59. }
  60. }
  61. void Start() {
  62. touchChecker.onMoved += delegate(Touch t, bool isOnUI) {
  63. if (banLogic) return;
  64. if (isOnUI) return;
  65. //触摸控制镜头和拉弓射箭
  66. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, limitRangeRotateX[0], limitRangeRotateX[1]);
  67. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, limitRangeRotateY[0], limitRangeRotateY[1]);
  68. this.transform.localEulerAngles = this.localEulerAngles;
  69. };
  70. touchChecker.onEnded += delegate(Touch t, bool isOnUI) {
  71. if (banLogic) return;
  72. if (!isOnUI) armBow.ADS_fire();
  73. };
  74. }
  75. void Update()
  76. {
  77. //更新相机视野
  78. if (cameraComp && !banCameraFieldOfView) {
  79. cameraComp.fieldOfView = cameraFieldOfView;
  80. }
  81. if (banLogic) return;
  82. //满足以下条件则阻止控制输入
  83. if (GameMgr.ins.gameOver) {
  84. return;
  85. }
  86. if (GameMgr.debugInEditor) {
  87. //鼠标控制镜头和拉弓射箭
  88. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), limitRangeRotateX[0], limitRangeRotateX[1]);
  89. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), limitRangeRotateY[0], limitRangeRotateY[1]);
  90. this.transform.localEulerAngles = this.localEulerAngles;
  91. if (EventSystem.current.IsPointerOverGameObject()) return;
  92. }
  93. else if (isTouchMode) {
  94. touchChecker.Update();
  95. } else {
  96. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  97. //需要-镜头看向九轴姿态虚拟节点
  98. needLookAtPoint = true;
  99. }
  100. }
  101. //需要-镜头看向九轴姿态虚拟节点?
  102. bool needLookAtPoint = false;
  103. //镜头旋转转换比值
  104. float[] _bowRotateConvertRate = null;
  105. float bowRotateConvertRate {
  106. get {
  107. if (_bowRotateConvertRate == null) {
  108. _bowRotateConvertRate = new float[]{ UserSettings.ins.bowRotateConvert.GetRate() };
  109. }
  110. return _bowRotateConvertRate[0];
  111. }
  112. }
  113. void LateUpdate() {
  114. if (needLookAtPoint) {
  115. needLookAtPoint = false;
  116. //镜头看向九轴姿态虚拟节点
  117. this.transform.LookAt(CameraToLook.ins.point);
  118. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowRealBowQuat(this.transform.localEulerAngles);
  119. if (!CommonConfig.isReleaseVersion) {
  120. //镜头旋转比值转换
  121. Vector3 localAngles = this.transform.localEulerAngles;
  122. localAngles.x = Mathf.Clamp((localAngles.x > 180 ? localAngles.x - 360 : localAngles.x) * bowRotateConvertRate,
  123. limitRangeRotateX[0], limitRangeRotateX[1]);
  124. localAngles.y = Mathf.Clamp((localAngles.y > 180 ? localAngles.y - 360 : localAngles.y) * bowRotateConvertRate,
  125. limitRangeRotateY[0], limitRangeRotateY[1]);
  126. if (bowCameraFixed != null) {
  127. localAngles = bowCameraFixed.LimitBowAngle(localAngles);
  128. }
  129. this.transform.localEulerAngles = localAngles;
  130. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowGameBowQuat(this.transform.localEulerAngles);
  131. }
  132. }
  133. onAfterLateUpdate?.Invoke();
  134. }
  135. Action onAfterLateUpdate;
  136. //---------------相机视野的相关操作---------------------
  137. //视野值记录
  138. float cameraFieldOfView = 60;
  139. [NonSerialized] public float defaultCameraFieldOfView = 60;
  140. private void RecordDefaultCameraFieldOfView() {
  141. defaultCameraFieldOfView = cameraComp.fieldOfView;
  142. cameraFieldOfView = defaultCameraFieldOfView;
  143. }
  144. //禁止相机视野改变
  145. [NonSerialized] public bool banCameraFieldOfView = false;
  146. public void SetCameraFieldOfView(float value) {
  147. cameraComp.fieldOfView = value;
  148. }
  149. public void SetCameraFieldOfViewRecord(float value) {
  150. cameraFieldOfView = value;
  151. }
  152. //拉弓时的相机视野值变化
  153. public void updateFollowPullBow() {
  154. // if (cameraFieldOfView > 40) {
  155. // cameraFieldOfView -= 20 * Time.deltaTime;
  156. // } else {
  157. // cameraFieldOfView = 40;
  158. // }
  159. }
  160. //松开拉弓时的相机视野值变化
  161. public void updateGiveUpPullBow() {
  162. // if (cameraFieldOfView < 60) {
  163. // cameraFieldOfView += 20 * Time.deltaTime;
  164. // } else {
  165. // cameraFieldOfView = 60;
  166. // }
  167. }
  168. // 2022-04-28
  169. // ------ 添加固定镜头选项后,新增的API ------
  170. bool isArrowFollowing = false;
  171. public void SetArrowFollowing(bool value) {
  172. isArrowFollowing = value;
  173. cameraComp.enabled = !isArrowFollowing;
  174. AutoSwitchCamera();
  175. }
  176. bool isScaleAimDisplaying = false;
  177. public void SetScaleAimDisplaying(bool value) {
  178. isScaleAimDisplaying = value;
  179. AutoSwitchCamera();
  180. }
  181. void AutoSwitchCamera() {
  182. if (bowCameraFixed == null) {
  183. cameraComp.enabled = !isArrowFollowing;
  184. } else {
  185. bowCameraFixed.gameObject.SetActive(!isScaleAimDisplaying && !isArrowFollowing);
  186. cameraComp.enabled = isScaleAimDisplaying && !isArrowFollowing;
  187. }
  188. }
  189. public Camera GetRenderCamera() {
  190. if (bowCameraFixed == null) {
  191. return cameraComp;
  192. } else {
  193. if (bowCameraFixed.gameObject.activeSelf) {
  194. return bowCameraFixed.camera;
  195. } else {
  196. return cameraComp;
  197. }
  198. }
  199. }
  200. public BowCameraFixed bowCameraFixed = null;
  201. public class BowCameraFixed {
  202. public GameObject gameObject;
  203. public Transform transform;
  204. public Camera camera;
  205. private UnityStandardAssets.ImageEffects.Blur blur;
  206. //bowCameraComponent
  207. BowCamera bowCamera;
  208. private Camera targetCamera;
  209. private UnityStandardAssets.ImageEffects.Blur targetBlur;
  210. public BowCameraFixed(BowCamera bowCamera) {
  211. this.bowCamera = bowCamera;
  212. gameObject = new GameObject("BowCameraFixed");
  213. transform = gameObject.transform;
  214. //复制Camera组件
  215. targetCamera = bowCamera.cameraComp;
  216. camera = gameObject.AddComponent<Camera>();
  217. camera.tag = "MainCamera";
  218. camera.clearFlags = targetCamera.clearFlags;
  219. camera.backgroundColor = targetCamera.backgroundColor;
  220. camera.cullingMask = targetCamera.cullingMask;
  221. camera.fieldOfView = targetCamera.fieldOfView;
  222. camera.nearClipPlane = targetCamera.nearClipPlane;
  223. camera.depth = targetCamera.depth + 0.1f;
  224. camera.renderingPath = targetCamera.renderingPath;
  225. //复制Blur组件
  226. targetBlur = bowCamera.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
  227. if (targetBlur) {
  228. blur = gameObject.AddComponent<UnityStandardAssets.ImageEffects.Blur>();
  229. blur.enabled = targetBlur.enabled;
  230. blur.blurShader = targetBlur.blurShader;
  231. blur.blurSpread = targetBlur.blurSpread;
  232. blur.iterations = targetBlur.iterations;
  233. }
  234. //设置Transform属性
  235. transform.parent = bowCamera.transform.parent;
  236. transform.localPosition = bowCamera.transform.localPosition;
  237. transform.localScale = bowCamera.transform.localScale;
  238. transform.localRotation = Quaternion.identity;
  239. //监听和驱动LateUpdate
  240. bowCamera.onAfterLateUpdate += LateUpdate;
  241. //切换镜头
  242. bowCamera.AutoSwitchCamera();
  243. InitForLimitBound();
  244. }
  245. void LateUpdate() {
  246. if (gameObject.activeSelf) {
  247. CrossHair.ins.UpdatePostionWhenFixedCamera();
  248. }
  249. if (blur) {
  250. blur.enabled = targetBlur.enabled;
  251. if (blur.enabled) {
  252. blur.blurShader = targetBlur.blurShader;
  253. blur.blurSpread = targetBlur.blurSpread;
  254. blur.iterations = targetBlur.iterations;
  255. }
  256. }
  257. }
  258. //边界限制
  259. float[] rangeRotateY = {-80, 80};
  260. float rangeRotateX = 25;
  261. Vector3 vecF;
  262. Vector3 vecU;
  263. public int outBoundIndex = -1; //-1为未出界
  264. void InitForLimitBound() {
  265. for (int i = (int)rangeRotateY[0]; i < 0; i++) {
  266. Vector3 pos = transform.position + Quaternion.AngleAxis(i, Vector3.up) * transform.forward;
  267. pos = camera.WorldToViewportPoint(pos);
  268. if (pos.x >= 0) {
  269. rangeRotateY[0] = i;
  270. rangeRotateY[1] = -i - 4;
  271. break;
  272. }
  273. }
  274. rangeRotateX = camera.fieldOfView / 2;
  275. vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  276. vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  277. }
  278. public Vector3 LimitBowAngle(Vector3 outAngle) {
  279. float angleY = outAngle.y;
  280. float angleX = outAngle.x;
  281. outAngle.y = Mathf.Clamp(angleY, rangeRotateY[0], rangeRotateY[1]);
  282. Vector3 vec = Quaternion.AngleAxis(outAngle.y, vecU) * vecF;
  283. float rx = (float)(Math.Asin(vec.y) / Math.PI * 180) * (angleX < 0 ? 1 : -1);
  284. if (angleY < rangeRotateY[0]) outBoundIndex = 0;
  285. else if (angleY > rangeRotateY[1]) outBoundIndex = 1;
  286. else if (angleX < -rangeRotateX) outBoundIndex = 2;
  287. else if (angleX > rangeRotateX) outBoundIndex = 3;
  288. else outBoundIndex = -1;
  289. if (Mathf.Abs(angleX) > Mathf.Abs(rx)) {
  290. outAngle.x = rx;
  291. }
  292. //因为Vector3是struct,传递给函数是值传递而非引用传递
  293. return outAngle;
  294. }
  295. }
  296. }