Skip to content

Commit f9e4aec

Browse files
committed
finished the first version of the camera
1 parent c91af9c commit f9e4aec

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

Runtime/Camera/CameraManager.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static CameraManager Instance{
1515
}
1616
#region code-functions
1717
[HideInInspector] public UnityEvent<Transform> OnTargetChange = new UnityEvent<Transform>();
18-
[HideInInspector] public UnityEvent<float> OnZoom = new UnityEvent<float>();
18+
[HideInInspector] public UnityEvent<float, float> OnCameraShake = new UnityEvent<float, float>();
1919
#endregion
2020

2121
#region camera-variables
@@ -39,11 +39,8 @@ public Transform Target{
3939
}
4040
}
4141
#endregion
42-
#region zoom-variables
43-
[Header("Zoom settings")]
44-
[SerializeField] protected float zoomSpeed = 4f;
45-
protected float originalCameraSize;
46-
protected float zoomFactor;
42+
#region shake-variables
43+
protected Coroutine shakeRoutine;
4744
#endregion
4845
/// <summary>
4946
/// This region will be focused on the basic camera functions as well as the workflow of it.
@@ -71,7 +68,6 @@ private void LateUpdate() {
7168
}
7269
protected virtual void Work(){
7370
FollowTarget();
74-
HandleZoom();
7571
}
7672
protected virtual void FollowTarget(){
7773
if(Target == null)return;
@@ -89,11 +85,20 @@ protected virtual void FollowTarget(){
8985
}
9086
this.transform.position = new Vector3(xNew, yNew, transform.position.z);
9187
}
92-
protected virtual void HandleZoom(){
93-
float targetSize = originalCameraSize - zoomFactor;
94-
if(targetSize != currentCamera.orthographicSize){
95-
currentCamera.orthographicSize = Mathf.Lerp(currentCamera.orthographicSize, targetSize, Time.deltaTime * zoomSpeed);
88+
protected virtual IEnumerator Shake(float duration, float magnitude) {
89+
Vector3 originalPos3 = currentCamera.transform.localPosition;
90+
Vector2 originalPos = currentCamera.transform.localPosition;
91+
float elapsedTime = 0f;
92+
while (elapsedTime < duration) {
93+
currentCamera.transform.localPosition = new Vector3(originalPos.x + Random.insideUnitCircle.x * magnitude, originalPos.y + Random.insideUnitCircle.y * magnitude, -10f);
94+
95+
elapsedTime += Time.deltaTime;
96+
yield return null;
9697
}
98+
99+
currentCamera.transform.localPosition = originalPos3;
100+
//DefaultPosition();
101+
shakeRoutine = null;
97102
}
98103
#region publicfunctions
99104
//Region for every function that will be called from other scripts.
@@ -104,12 +109,16 @@ public virtual void SetTarget(Transform target, bool xLocked = false, bool yLock
104109
hasYLocked = yLocked;
105110
OnTargetChange.Invoke(target);
106111
}
107-
public virtual void SetZoom(float zoomAmount, float zoomSpeed = 4f, string reason=""){
108-
if(debug)BDebug.Log($"Camera is now zooming: {zoomAmount}, {reason}", "Camera", Color.green);
109-
zoomFactor = zoomAmount;
110-
this.zoomSpeed = zoomSpeed;
111-
OnZoom.Invoke(zoomAmount);
112+
public virtual void SetShake(float duration, float magnitude, string reason = "") {
113+
if(debug)BDebug.Log($"Camera is now shaking for {duration}seconds at {magnitude}X, {reason}", "Camera", Color.green);
114+
if (shakeRoutine != null) {
115+
return;
116+
}
117+
shakeRoutine = StartCoroutine(Shake(duration, magnitude));
118+
OnCameraShake.Invoke(duration, magnitude);
112119
}
120+
121+
113122
#endregion
114123
}
115124
public enum Updates{

Tests/Runtime/EntityExample.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ private void Start() {
1515
CameraManager.Instance.OnTargetChange.AddListener((target)=>{
1616
Debug.Log(target);
1717
});
18+
CameraManager.Instance.OnCameraShake.AddListener((d,m)=>{
19+
Debug.Log(d+m);
20+
});
1821
}
1922
private void Update() {
2023
if(BGameInput.Instance.GetKeyPress("Interaction")){
@@ -29,7 +32,7 @@ private void Update() {
2932
if(BGameInput.Instance.GetKeyPress("Example")){
3033
var rnd = Random.Range(0, entitysOnExample.Count - 1);
3134

32-
CameraManager.Instance.SetZoom(2, 4, "I want");
35+
CameraManager.Instance.SetShake(.5f, 1.5f, "Testing the zoom!");
3336

3437
entitysOnExample[rnd].MoveTo(new Vector2(1,0));
3538
}

0 commit comments

Comments
 (0)