I_Jemin

Refact

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ContentModelStore">
<e p="$PROJECT_DIR$" t="IncludeRecursive">
<e p="Assembly-CSharp-Editor-firstpass.csproj" t="IncludeRecursive" />
<e p="Assembly-CSharp.csproj" t="IncludeRecursive" />
<e p="Assets" t="Include">
<e p="Plugins" t="Include">
<e p="Editor" t="Include">
<e p="JetBrains" t="Include">
<e p="Unity3DRider.cs" t="Include" />
</e>
</e>
</e>
<e p="Scripts" t="Include">
<e p="Bird.cs" t="Include" />
<e p="Column.cs" t="Include" />
<e p="ColumnPool.cs" t="Include" />
<e p="GameControl.cs" t="Include" />
<e p="IUnityInput.cs" t="Include" />
<e p="RepeatingBackground.cs" t="Include" />
<e p="ScrollingObject.cs" t="Include" />
</e>
<e p="Test Scripts" t="Include">
<e p="BirdTest.cs" t="Include" />
</e>
</e>
<e p="Bird-TDD.sln" t="IncludeFlat" />
<e p="Library" t="ExcludeRecursive" />
<e p="obj" t="ExcludeRecursive" />
<e p="packages" t="ExcludeRecursive" />
<e p="Temp" t="ExcludeRecursive">
<e p="bin" t="ExcludeRecursive" />
</e>
</e>
<e p="$USER_HOME$/Library/Caches/Rider2017.3/resharper-host/local/Transient/ReSharperHost/v11/SolutionCaches/_Bird-TDD.-1979534893.00" t="ExcludeRecursive" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ContentModelUserStore">
<explicitIncludes />
<explicitExcludes />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/.idea.Bird-TDD/riderModule.iml" filepath="$PROJECT_DIR$/.idea/.idea.Bird-TDD/riderModule.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed. Click to expand it.
<?xml version="1.0" encoding="UTF-8"?>
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$/../.." />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
......@@ -56,19 +56,19 @@ public class Bird : MonoBehaviour
anim.SetTrigger("Die");
//...and tell the game control about it.
if (GameControl.instance != null)
{
GameControl.instance.BirdDied();
}
GameControl.Instance.BirdDied();
}
void OnTriggerEnter2D(Collider2D other)
{
//If the bird hits the trigger collider in between the columns then
//tell the game control that the bird scored.
GameControl.instance.BirdScored();
if (other.CompareTag("Column"))
{
//If the bird hits the trigger collider in between the columns then
//tell the game control that the bird scored.
GameControl.Instance.BirdScored();
}
}
public void Jump()
......
......@@ -38,7 +38,7 @@ public class ColumnPool : MonoBehaviour
{
timeSinceLastSpawned += Time.deltaTime;
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
if (GameControl.Instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0f;
......
......@@ -4,33 +4,43 @@ using UnityEngine.AI;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameControl : MonoBehaviour
public class GameControl : MonoBehaviour
{
public static GameControl instance; //A reference to our game control script so we can access it statically.
public Text scoreText; //A reference to the UI text component that displays the player's score.
public GameObject gameOvertext; //A reference to the object that displays the text which appears when the player dies.
private static GameControl instance; //A reference to our game control script so we can access it statically.
public Text scoreText; //A reference to the UI text component that displays the player's score.
public GameObject gameOvertext; //A reference to the object that displays the text which appears when the player dies.
public int score { get; private set; } //The player's score.
public bool gameOver = false; //Is the game over?
public int score { get; private set; } //The player's score.
public bool gameOver = false; //Is the game over?
public float scrollSpeed = -1.5f;
void Awake()
public static GameControl Instance
{
//If we don't currently have a game control...
if (instance == null)
//...set this one to be it...
instance = this;
//...otherwise...
else if(instance != this)
//...destroy this one because it is a duplicate.
Destroy (gameObject);
get
{
if (instance != null)
{
return instance;
}
else
{
instance = FindObjectOfType<GameControl>();
if (instance == null)
{
instance = new GameObject().AddComponent<GameControl>();
}
}
return instance;
}
}
void Update()
{
//If the game is over and the player has pressed some input...
if (gameOver && Input.GetMouseButtonDown(0))
if (gameOver && Input.GetMouseButtonDown(0))
{
//...reload the current scene.
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
......@@ -54,9 +64,20 @@ public class GameControl : MonoBehaviour
public void BirdDied()
{
//Activate the game over text.
gameOvertext.SetActive (true);
if (gameOvertext != null)
{
//Activate the game over text.
gameOvertext.SetActive(true);
}
//Set the game to be over.
gameOver = true;
}
public void Reset()
{
gameOver = false;
score = 0;
}
}
\ No newline at end of file
......
......@@ -17,7 +17,7 @@ public class ScrollingObject : MonoBehaviour
rb2d.bodyType = RigidbodyType2D.Kinematic;
//Start the object moving.
rb2d.velocity = new Vector2 (GameControl.instance.scrollSpeed, 0);
rb2d.velocity = new Vector2 (GameControl.Instance.scrollSpeed, 0);
}
......@@ -25,13 +25,13 @@ public class ScrollingObject : MonoBehaviour
void Update()
{
// If the game is over, stop scrolling.
if(GameControl.instance.gameOver == true)
if(GameControl.Instance.gameOver == true)
{
rb2d.velocity = Vector2.zero;
}
else
{
rb2d.velocity = new Vector2 (GameControl.instance.scrollSpeed, 0);
rb2d.velocity = new Vector2 (GameControl.Instance.scrollSpeed, 0);
}
}
}
\ No newline at end of file
......
......@@ -29,6 +29,8 @@ namespace FlappyBird.PlayModeTest
bird = instance.AddComponent<Bird>();
bird.upForce = 500f;
GameControl.Instance.Reset();
}
[UnityTest]
......@@ -57,6 +59,7 @@ namespace FlappyBird.PlayModeTest
Assert.AreEqual(true, bird.IsDead);
}
[UnityTest]
public IEnumerator _Down_Fire1_Button_to_Bird_Jump_Up()
......@@ -77,12 +80,9 @@ namespace FlappyBird.PlayModeTest
[UnityTest]
public IEnumerator _Down_Fire1_Button_to_Bird_Speed_Not_Negative()
public IEnumerator _Bird_Jump_to_Bird_Speed_Not_Negative()
{
var unityInput = Substitute.For<IUnityInputService>();
unityInput.GetButtonDown("Fire1").Returns(true);
bird.UnityInput = unityInput;
bird.Jump();
yield return null;
......@@ -93,14 +93,13 @@ namespace FlappyBird.PlayModeTest
[UnityTest]
public IEnumerator _Down_Fire1_After_2sec_to_Bird_Y_Speed_Not_Negative()
public IEnumerator _Jump_After_2sec_to_Bird_Y_Speed_Not_Negative()
{
yield return new WaitForSeconds(2.0f);
var unityInput = Substitute.For<IUnityInputService>();
unityInput.GetButtonDown("Fire1").Returns(true);
bird.UnityInput = unityInput;
bird.Jump();
yield return null;
......@@ -113,11 +112,8 @@ namespace FlappyBird.PlayModeTest
[UnityTest]
public IEnumerator _Bird_Trigger_Collide_With_Column_Tag_to_Add_Score()
{
var gameContorl = new GameObject().AddComponent<GameControl>();
yield return null;
var score = gameContorl.score;
var score = GameControl.Instance.score;
var collider = new GameObject().AddComponent<BoxCollider2D>();
collider.tag = "Column";
......@@ -126,52 +122,47 @@ namespace FlappyBird.PlayModeTest
yield return null;
Assert.Greater(gameContorl.score,score);
Assert.AreEqual(score + 1,GameControl.Instance.score);
}
[UnityTest]
public IEnumerator _Bird_Trigger_Collide_With_Not_Column_Tag_to_Not_Change_Score()
{
var gameContorl = new GameObject().AddComponent<GameControl>();
var score = GameControl.Instance.score;
var collider = new GameObject().AddComponent<BoxCollider2D>();
yield return null;
var score = gameContorl.score;
bird.SendMessage("OnTriggerEnter2D",new Collider2D());
bird.SendMessage("OnTriggerEnter2D",collider);
yield return null;
Assert.AreEqual(gameContorl.score,score);
Assert.AreEqual(GameControl.Instance.score,score);
}
}
public class GameControlTest
{
private GameControl gameControl;
[SetUp]
public void BeforeEveryTest()
{
if (GameControl.instance != null)
{
GameObject.Destroy(GameControl.instance.gameObject);
}
gameControl = new GameObject("GameControl").AddComponent<GameControl>();
gameControl.scoreText = new GameObject().AddComponent<Text>();
gameControl.gameOvertext = new GameObject();
GameControl.Instance.scoreText = new GameObject("Score Text").AddComponent<Text>();
GameControl.Instance.gameOvertext = new GameObject("Game Over Text");
GameControl.Instance.Reset();
}
[Test]
public void _Bird_Scored_then_Displayd_Text_Contain_Updated_Score()
{
gameControl.BirdScored();
GameControl.Instance.BirdScored();
Assert.IsTrue(gameControl.scoreText.text.Contains(gameControl.score.ToString()));
Assert.IsTrue(GameControl.Instance.scoreText.text.Contains(GameControl.Instance.score.ToString()));
}
[UnityTest]
......@@ -179,7 +170,7 @@ namespace FlappyBird.PlayModeTest
{
var scrollingObject = new GameObject().AddComponent<ScrollingObject>();
gameControl.scrollSpeed = 0f;
GameControl.Instance.scrollSpeed = 0f;
yield return null;
yield return new WaitForFixedUpdate();
......@@ -192,15 +183,13 @@ namespace FlappyBird.PlayModeTest
{
var scrollingObject = new GameObject().AddComponent<ScrollingObject>();
gameControl.gameOver = true;
GameControl.Instance.gameOver = true;
yield return null;
yield return new WaitForFixedUpdate();
Assert.AreEqual(Vector2.zero, scrollingObject.GetComponent<Rigidbody2D>().velocity);
}
}
......