|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +public class CameraManager : MonoBehaviour |
| 6 | +{ |
| 7 | + // Main camera in scene |
| 8 | + public Camera mainCamera; |
| 9 | + |
| 10 | + // Player Manager in scene |
| 11 | + public PlayerManager playerManager; |
| 12 | + |
| 13 | + public float transitionDuration = 1f; |
| 14 | + public float zoomDuration = 1f; |
| 15 | + |
| 16 | + private bool cameraMoving = false; |
| 17 | + private bool cameraZooming = false; |
| 18 | + private bool cameraZoomed = false; |
| 19 | + private GameObject cameraTarget; |
| 20 | + private List<string> players = new List<string>(); |
| 21 | + private int playersCurrentIndex = 0; |
| 22 | + private GameObject currentSelectionParticle; |
| 23 | + |
| 24 | + TallyInputSystem inputs; |
| 25 | + |
| 26 | + private void Awake() |
| 27 | + { |
| 28 | + inputs = new TallyInputSystem(); |
| 29 | + inputs.Player.SelectLeft.performed += ctx => CameraSelectLeft(); |
| 30 | + inputs.Player.SelectRight.performed += ctx => CameraSelectRight(); |
| 31 | + inputs.Player.ZoomIn.started += ctx => StartCoroutine(ZoomIn()); |
| 32 | + inputs.Player.ZoomOut.started += ctx => StartCoroutine(ZoomOut()); |
| 33 | + inputs.Player.Enable(); |
| 34 | + } |
| 35 | + |
| 36 | + private void Update() |
| 37 | + { |
| 38 | + if (!cameraMoving && !cameraZooming && cameraZoomed) |
| 39 | + { |
| 40 | + mainCamera.transform.position = new Vector3(cameraTarget.transform.position.x, cameraTarget.transform.position.y, -10); |
| 41 | + } |
| 42 | + //else |
| 43 | + //{ |
| 44 | + //targetPosition = new Vector3(cameraTarget.transform.position.x, cameraTarget.transform.position.y, -10); |
| 45 | + //StartCoroutine(Transition()); |
| 46 | + //mainCamera.orthographicSize = 10; |
| 47 | + //if (currentSelectionParticle != null) currentSelectionParticle.SetActive(false); |
| 48 | + //} |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + void CameraSelectLeft() |
| 53 | + { |
| 54 | + if (!cameraMoving) |
| 55 | + { |
| 56 | + if (playersCurrentIndex == 0) |
| 57 | + playersCurrentIndex = players.Count - 1; |
| 58 | + else |
| 59 | + playersCurrentIndex--; |
| 60 | + |
| 61 | + Destroy(currentSelectionParticle); |
| 62 | + playerManager.playerDict.TryGetValue(players[playersCurrentIndex], out GameObject tempPlayer); |
| 63 | + currentSelectionParticle = (GameObject)Instantiate(playerManager.selectionParticle, tempPlayer.GetComponent<Player>().effects.transform, false); |
| 64 | + cameraTarget = tempPlayer.GetComponent<Player>().playerCharacter; |
| 65 | + |
| 66 | + if (cameraZoomed) |
| 67 | + StartCoroutine(Transition()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + void CameraSelectRight() |
| 72 | + { |
| 73 | + if (!cameraMoving) |
| 74 | + { |
| 75 | + if (playersCurrentIndex == players.Count - 1) |
| 76 | + playersCurrentIndex = 0; |
| 77 | + else |
| 78 | + playersCurrentIndex++; |
| 79 | + |
| 80 | + Destroy(currentSelectionParticle); |
| 81 | + playerManager.playerDict.TryGetValue(players[playersCurrentIndex], out GameObject tempPlayer); |
| 82 | + currentSelectionParticle = (GameObject)Instantiate(playerManager.selectionParticle, tempPlayer.GetComponent<Player>().effects.transform, false); |
| 83 | + cameraTarget = tempPlayer.GetComponent<Player>().playerCharacter; |
| 84 | + |
| 85 | + if (cameraZoomed) |
| 86 | + StartCoroutine(Transition()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void AddPlayer(string username) |
| 91 | + { |
| 92 | + players.Add(username); |
| 93 | + playerManager.playerDict.TryGetValue(players[playersCurrentIndex], out GameObject tempPlayer); |
| 94 | + cameraTarget = tempPlayer.GetComponent<Player>().playerCharacter; |
| 95 | + if (currentSelectionParticle == null) currentSelectionParticle = (GameObject)Instantiate(playerManager.selectionParticle, tempPlayer.GetComponent<Player>().effects.transform, false); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + IEnumerator Transition() |
| 100 | + { |
| 101 | + float t = 0.0f; |
| 102 | + Vector3 startingPos = mainCamera.transform.position; |
| 103 | + |
| 104 | + if (!cameraMoving && !cameraZooming) |
| 105 | + { |
| 106 | + cameraMoving = true; |
| 107 | + while (t < 1.0f) |
| 108 | + { |
| 109 | + t += Time.deltaTime * (Time.timeScale / transitionDuration); |
| 110 | + |
| 111 | + mainCamera.transform.position = Vector3.Lerp(startingPos, new Vector3(cameraTarget.transform.position.x, cameraTarget.transform.position.y, -10), t); |
| 112 | + yield return 0; |
| 113 | + } |
| 114 | + cameraMoving = false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + IEnumerator Transition(Vector3 targetPosition) |
| 119 | + { |
| 120 | + float t = 0.0f; |
| 121 | + Vector3 startingPos = mainCamera.transform.position; |
| 122 | + |
| 123 | + if (!cameraMoving && !cameraZooming) |
| 124 | + { |
| 125 | + cameraMoving = true; |
| 126 | + while (t < 1.0f) |
| 127 | + { |
| 128 | + t += Time.deltaTime * (Time.timeScale / transitionDuration); |
| 129 | + |
| 130 | + |
| 131 | + mainCamera.transform.position = Vector3.Lerp(startingPos, targetPosition, t); |
| 132 | + yield return 0; |
| 133 | + } |
| 134 | + cameraMoving = false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + IEnumerator ZoomIn() |
| 140 | + { |
| 141 | + float t = 0.0f; |
| 142 | + float startingZoom = mainCamera.orthographicSize; |
| 143 | + if (!cameraMoving && !cameraZooming && !cameraZoomed) |
| 144 | + { |
| 145 | + StartCoroutine(Transition()); |
| 146 | + cameraZooming = true; |
| 147 | + while (t < 1.0f) |
| 148 | + { |
| 149 | + t += Time.deltaTime * (Time.timeScale / zoomDuration); |
| 150 | + |
| 151 | + mainCamera.orthographicSize = Mathf.Lerp(startingZoom, 10, t); |
| 152 | + yield return 0; |
| 153 | + } |
| 154 | + cameraZooming = false; |
| 155 | + cameraZoomed = true; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + IEnumerator ZoomOut() |
| 161 | + { |
| 162 | + float t = 0.0f; |
| 163 | + float startingZoom = mainCamera.orthographicSize; |
| 164 | + if (!cameraMoving && !cameraZooming && cameraZoomed) |
| 165 | + { |
| 166 | + StartCoroutine(Transition(new Vector3(0, 1, -10))); |
| 167 | + cameraZooming = true; |
| 168 | + cameraZoomed = false; |
| 169 | + while (t < 1.0f) |
| 170 | + { |
| 171 | + t += Time.deltaTime * (Time.timeScale / zoomDuration); |
| 172 | + |
| 173 | + mainCamera.orthographicSize = Mathf.Lerp(startingZoom, 25, t); |
| 174 | + yield return 0; |
| 175 | + } |
| 176 | + cameraZooming = false; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments