Skip to content

Commit

Permalink
Multiple Transition Points (#21)
Browse files Browse the repository at this point in the history
* Camera Tests are now on separate scene.

* ZS-40, 41 complete

* Points can now be removed

* Added tests for adding and removing points

* Added unity editor check blocks so build can go through

* Camera moves to specific Transition Point on Spacebar press

* ZS-42 Player can touch zone to trigger transition to specific point

* Added test to make sure camera moves to correct listed point

* Transition Zones can swap between two Transition Points

* Transition Zones now toggle between two points and move camera to them

* ZS-43 Complete
  • Loading branch information
Joshua-Dunne authored Dec 4, 2021
1 parent 297698b commit 8c01f27
Show file tree
Hide file tree
Showing 10 changed files with 766 additions and 29 deletions.
582 changes: 582 additions & 0 deletions RWM-P2-TEAM-C/Assets/Scenes/CameraTestScene.unity

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions RWM-P2-TEAM-C/Assets/Scenes/CameraTestScene.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 28 additions & 20 deletions RWM-P2-TEAM-C/Assets/Scripts/CameraMover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@
public class CameraMover : MonoBehaviour
{
public Camera mainCam;
public Vector2 transitionPoint;
public List<Vector2> transitionPoints;
public float speed = 0.1f;
public bool m_moving;

private Vector2 chosenPoint;
private List<Behaviour> heldComponents;

void Start()
{
mainCam = Camera.main;
}

public void StartMovement()
public void AddPoint(Vector2 t_point)
{

transitionPoint = GetComponent<ScreenTransition>().transitionPoint;
transitionPoints.Add(t_point);
}

public void RemoveLastPoint()
{
if(transitionPoints.Count > 0) // make sure points exist first
transitionPoints.RemoveAt(transitionPoints.Count - 1); // removes the last item in the list
}

public void StartMovement(int point, ScreenTransition.transitionTypes t_type)
{
chosenPoint = transitionPoints[point];
foreach (Behaviour behaviour in GetComponents<Behaviour>())
{
if(behaviour.enabled)
Expand All @@ -41,54 +51,51 @@ public void StartMovement()

IEnumerator movement()
{
bool wentThroughMove = false;
if(!m_moving)
{ // only do this enumerator if it isn't already happening, to stop overlapping
m_moving = true;
wentThroughMove = true;
if (GetComponent<ScreenTransition>().type == ScreenTransition.transitionTypes.HORIZONTAL)
{
if (mainCam.transform.position.x < transitionPoint.x)
if (mainCam.transform.position.x < chosenPoint.x)
{
while (mainCam.transform.position.x <= transitionPoint.x)
while (mainCam.transform.position.x <= chosenPoint.x)
{
mainCam.transform.position = new Vector3(mainCam.transform.position.x + speed, mainCam.transform.position.y, mainCam.transform.position.z);

yield return new WaitForSeconds(0.1f);
yield return new WaitForSeconds(0.025f);
}
}
else if (mainCam.transform.position.x > transitionPoint.x)
else if (mainCam.transform.position.x > chosenPoint.x)
{
while (mainCam.transform.position.x >= transitionPoint.x)
while (mainCam.transform.position.x >= chosenPoint.x)
{
mainCam.transform.position = new Vector3(mainCam.transform.position.x - speed, mainCam.transform.position.y, mainCam.transform.position.z);

yield return new WaitForSeconds(0.1f);
yield return new WaitForSeconds(0.025f);
}
}
}
if (GetComponent<ScreenTransition>().type == ScreenTransition.transitionTypes.HORIZONTAL)
if (GetComponent<ScreenTransition>().type == ScreenTransition.transitionTypes.VERTICAL)
{
if (mainCam.transform.position.y < transitionPoint.y)
if (mainCam.transform.position.y < chosenPoint.y)
{
while (mainCam.transform.position.y <= transitionPoint.y)
while (mainCam.transform.position.y <= chosenPoint.y)
{
mainCam.transform.position = new Vector3(mainCam.transform.position.x, mainCam.transform.position.y + speed, mainCam.transform.position.z);

yield return new WaitForSeconds(0.1f);
yield return new WaitForSeconds(0.025f);
}
}
else if (mainCam.transform.position.y > transitionPoint.y)
else if (mainCam.transform.position.y > chosenPoint.y)
{
while (mainCam.transform.position.y >= transitionPoint.y)
while (mainCam.transform.position.y >= chosenPoint.y)
{
mainCam.transform.position = new Vector3(mainCam.transform.position.x, mainCam.transform.position.y - speed, mainCam.transform.position.z);

yield return new WaitForSeconds(0.1f);
yield return new WaitForSeconds(0.025f);
}
}
}
Debug.Log("camera reached end");
}

// now that the mover is done, re-enable any disabled components
Expand All @@ -104,6 +111,7 @@ IEnumerator movement()
}
}

m_moving = false;
yield break;
}
}
4 changes: 3 additions & 1 deletion RWM-P2-TEAM-C/Assets/Scripts/TempInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public class TempInput : MonoBehaviour
{
public int transitionPoint;
public ScreenTransition.transitionTypes type;
// Start is called before the first frame update
void Start()
{
Expand All @@ -15,7 +17,7 @@ void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Player")
{
Camera.main.GetComponent<CameraMover>().StartMovement();
Camera.main.GetComponent<CameraMover>().StartMovement(transitionPoint, type);
}
}
}
38 changes: 38 additions & 0 deletions RWM-P2-TEAM-C/Assets/Scripts/TransitionHolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

[CustomEditor(typeof(CameraMover))]
public class TransitionEditor : Editor
{
private CameraMover cM;
private ScreenTransition sT;
private Camera mainCam;

private void OnEnable()
{
// Method 1
cM = (CameraMover) target;
mainCam = Camera.main;
sT = mainCam.GetComponent<ScreenTransition>();
}

public override void OnInspectorGUI()
{
if (GUILayout.Button("Add Point"))
{
cM.AddPoint(sT.transitionPoint);
}

if (GUILayout.Button("Remove Last Point"))
{
cM.RemoveLastPoint();
}

// Draw default inspector after button...
base.OnInspectorGUI();
}
}
#endif
11 changes: 11 additions & 0 deletions RWM-P2-TEAM-C/Assets/Scripts/TransitionHolder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions RWM-P2-TEAM-C/Assets/Scripts/TransitionZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransitionZone : MonoBehaviour
{
public int pointOne;
public int pointTwo;
public ScreenTransition.transitionTypes type;
bool swap = false;

// begin transition on collision with player only
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Player")
{
if(!Camera.main.GetComponent<CameraMover>().m_moving)
{
if (!swap)
{
Camera.main.GetComponent<CameraMover>().StartMovement(pointOne, type);
}
else
{
Camera.main.GetComponent<CameraMover>().StartMovement(pointTwo, type);
}

swap = !swap;
}
}
}
}
11 changes: 11 additions & 0 deletions RWM-P2-TEAM-C/Assets/Scripts/TransitionZone.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 51 additions & 8 deletions RWM-P2-TEAM-C/Assets/Tests/ScreenTransitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ public class ScreenTransitionTests
[SetUp]
public void Setup()
{
SceneManager.LoadScene("Game", LoadSceneMode.Single);
SceneManager.LoadScene("CameraTestScene", LoadSceneMode.Single);
}

[TearDown]
public void Teardown()
{
SceneManager.UnloadSceneAsync("Game");
SceneManager.UnloadSceneAsync("CameraTestScene");
}

[UnityTest]
public IEnumerator TransitionStarts()
{
setupCamera();
Vector3 pos = mainCam.transform.position;
mainCam.GetComponent<CameraMover>().StartMovement();
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(100.0f, 0.0f));
mainCam.GetComponent<CameraMover>().StartMovement(0, ScreenTransition.transitionTypes.HORIZONTAL);

yield return new WaitForSeconds(0.1f);

Expand All @@ -40,21 +41,63 @@ public IEnumerator TransitionStarts()
public IEnumerator TransitionEnds()
{
setupCamera();
Vector3 pos = mainCam.transform.position;
mainCam.GetComponent<ScreenTransition>().transitionPoint = new Vector2(1.0f, 0.0f);
mainCam.GetComponent<CameraMover>().StartMovement();
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(1.0f, 0.0f));
mainCam.GetComponent<CameraMover>().StartMovement(0, ScreenTransition.transitionTypes.HORIZONTAL);

yield return new WaitForSeconds(1.5f);
yield return new WaitForSeconds(0.5f);

Assert.AreEqual(true, mainCam.GetComponent<CameraMover>().m_moving);
Assert.AreEqual(false, mainCam.GetComponent<CameraMover>().m_moving);

}

[UnityTest]
public IEnumerator AddingPoints()
{
setupCamera();
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(1.0f, 0.0f));
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(2.0f, 0.0f));
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(3.0f, 0.0f));
yield return new WaitForSeconds(0.1f);

Assert.AreEqual(3, mainCam.GetComponent<CameraMover>().transitionPoints.Count);

}

[UnityTest]
public IEnumerator RemovingLastAddedPoint()
{
setupCamera();
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(1.0f, 0.0f));
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(2.0f, 0.0f));
mainCam.GetComponent<CameraMover>().RemoveLastPoint();
yield return new WaitForSeconds(0.1f);

Assert.AreEqual(1, mainCam.GetComponent<CameraMover>().transitionPoints.Count);

}

[UnityTest]
public IEnumerator MovesToCorrectPoint()
{
setupCamera();
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(1.0f, 0.0f));
mainCam.GetComponent<CameraMover>().AddPoint(new Vector2(1000.0f, 0.0f));
mainCam.GetComponent<CameraMover>().StartMovement(0, ScreenTransition.transitionTypes.HORIZONTAL);

yield return new WaitForSeconds(0.5f);

Assert.Less(mainCam.transform.position.x, mainCam.GetComponent<CameraMover>().transitionPoints[1].x);
Assert.Less(mainCam.transform.position.x, mainCam.GetComponent<CameraMover>().transitionPoints[1].x + 0.1f);
Assert.GreaterOrEqual(mainCam.transform.position.x, mainCam.GetComponent<CameraMover>().transitionPoints[0].x);

}

private void setupCamera()
{
mainCam = Camera.main;

// remove any other points that may exist on the camera for our tests
mainCam.GetComponent<CameraMover>().transitionPoints.Clear();
}
}
}
3 changes: 3 additions & 0 deletions RWM-P2-TEAM-C/ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ EditorBuildSettings:
- enabled: 1
path: Assets/Scenes/PlayerTestScene.unity
guid: 7ca3673c25a388e40a7b4e63568b9f53
- enabled: 1
path: Assets/Scenes/CameraTestScene.unity
guid: 14411ea5c258afa488afef7b2e76bf5d
- enabled: 1
path: Assets/Scenes/AITestScene.unity
guid: 5fcde85d828059048b34e4dfb942e688
Expand Down

0 comments on commit 8c01f27

Please sign in to comment.