BowCamera.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. public static bool isTouchMode = true;
  29. public static BowCamera ins;
  30. void Awake() {
  31. ins = this;
  32. localEulerAngles = transform.localEulerAngles;
  33. }
  34. void Start() {
  35. touchChecker.onMoved += delegate(Touch t, bool isOnUI) {
  36. if (isOnUI) return;
  37. //触摸控制镜头和拉弓射箭
  38. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, -36, 36);
  39. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, -180, 180);
  40. this.transform.localEulerAngles = this.localEulerAngles;
  41. };
  42. touchChecker.onEnded += delegate(Touch t, bool isOnUI) {
  43. if (!isOnUI) armBow.ADS_fire();
  44. };
  45. }
  46. void Update()
  47. {
  48. //更新相机视野
  49. if (cameraComp && !banCameraFieldOfView) {
  50. cameraComp.fieldOfView = cameraFieldOfView;
  51. }
  52. //满足以下条件则阻止控制输入
  53. if (GameMgr.ins.gameOver) {
  54. return;
  55. }
  56. if (GameMgr.debugInEditor) {
  57. //鼠标控制镜头和拉弓射箭
  58. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), -36, 36);
  59. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), -180, 180);
  60. this.transform.localEulerAngles = this.localEulerAngles;
  61. if (EventSystem.current.IsPointerOverGameObject()) return;
  62. if (Input.GetMouseButtonDown(0)) {
  63. armBow.ADS_fire();
  64. }
  65. }
  66. else if (isTouchMode) {
  67. touchChecker.Update();
  68. } else {
  69. //镜头看向九轴姿态虚拟节点
  70. this.transform.LookAt(CameraToLook.ins.point);
  71. }
  72. }
  73. //---------------相机视野的相关操作---------------------
  74. //视野值记录
  75. float cameraFieldOfView = 60;
  76. //禁止相机视野改变
  77. [NonSerialized] public bool banCameraFieldOfView = false;
  78. public void SetCameraFieldOfView(float value) {
  79. cameraComp.fieldOfView = value;
  80. }
  81. public void SetCameraFieldOfViewRecord(float value) {
  82. cameraFieldOfView = value;
  83. }
  84. //拉弓时的相机视野值变化
  85. public void updateFollowPullBow() {
  86. if (cameraFieldOfView > 40) {
  87. cameraFieldOfView -= 20 * Time.deltaTime;
  88. } else {
  89. cameraFieldOfView = 40;
  90. }
  91. }
  92. //松开拉弓时的相机视野值变化
  93. public void updateGiveUpPullBow() {
  94. if (cameraFieldOfView < 60) {
  95. cameraFieldOfView += 20 * Time.deltaTime;
  96. } else {
  97. cameraFieldOfView = 60;
  98. }
  99. }
  100. }