Game.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. namespace Shapes2D {
  2. using UnityEngine;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. public class Game : MonoBehaviour {
  6. public Bird bird;
  7. public GameObject pipeSetPrefab;
  8. public float pipeDistance = 8; // distance between pipes in world units
  9. public float scrollSpeed = 2; // world units per second
  10. public Transform pipeSpawnLocation, pipeKillLocation;
  11. public float pipeVariation = 5; // world units up a pipe can go from its starting position
  12. float pipeTimer;
  13. public Button startButton, resetButton;
  14. public Text score;
  15. Transform pipes;
  16. public Shapes2D.Shape ground;
  17. // Use this for initialization
  18. void Start () {
  19. AdjustWidths();
  20. pipes = new GameObject().transform;
  21. pipes.name = "Pipes";
  22. pipes.SetParent(transform);
  23. pipeTimer = pipeDistance / scrollSpeed;
  24. }
  25. void AdjustWidths() {
  26. // adjust the width of the sky & ground to the camera size
  27. Vector3 scale = GameObject.Find("Sky").transform.localScale;
  28. scale.x = (Camera.main.orthographicSize * 2) / ((float) Screen.height / Screen.width) + 1;
  29. GameObject.Find("Sky").transform.localScale = scale;
  30. scale = GameObject.Find("Grass").transform.localScale;
  31. scale.x = (Camera.main.orthographicSize * 2) / ((float) Screen.height / Screen.width) + 1;
  32. GameObject.Find("Grass").transform.localScale = scale;
  33. scale = GameObject.Find("Dirt").transform.localScale;
  34. scale.x = (Camera.main.orthographicSize * 2) / ((float) Screen.height / Screen.width) + 1;
  35. GameObject.Find("Dirt").transform.localScale = scale;
  36. }
  37. public void OnStartButton() {
  38. startButton.gameObject.SetActive(false);
  39. resetButton.gameObject.SetActive(false);
  40. if (bird.IsDead()) {
  41. bird.Reset();
  42. for (int i = 0; i < pipes.childCount; i++) {
  43. Transform p = pipes.GetChild(i);
  44. p.gameObject.SetActive(false);
  45. }
  46. } else if (!bird.IsPlaying()) {
  47. bird.Play();
  48. }
  49. }
  50. // Update is called once per frame
  51. void Update () {
  52. score.text = bird.GetScore().ToString();
  53. if (bird.IsDead()) {
  54. startButton.gameObject.SetActive(false);
  55. resetButton.gameObject.SetActive(true);
  56. return;
  57. }
  58. if (!bird.IsPlaying()) {
  59. startButton.gameObject.SetActive(true);
  60. resetButton.gameObject.SetActive(false);
  61. return;
  62. }
  63. // scroll all the pipes
  64. for (int i = 0; i < pipes.childCount; i++) {
  65. Transform p = pipes.GetChild(i);
  66. if (!p.gameObject.activeSelf)
  67. continue;
  68. p.position -= new Vector3(scrollSpeed * Time.deltaTime, 0, 0);
  69. if (p.position.x < pipeKillLocation.position.x)
  70. p.gameObject.SetActive(false);
  71. }
  72. // spawn/reset a pipe if it's time
  73. pipeTimer += Time.deltaTime;
  74. if (pipeTimer >= pipeDistance / scrollSpeed) {
  75. pipeTimer -= pipeDistance / scrollSpeed;
  76. Transform newPipe = null;
  77. for (int i = 0; i < pipes.childCount; i++) {
  78. Transform p = pipes.GetChild(i);
  79. if (!p.gameObject.activeSelf) {
  80. newPipe = p;
  81. newPipe.gameObject.SetActive(true);
  82. break;
  83. }
  84. }
  85. if (newPipe == null) {
  86. newPipe = Instantiate<GameObject>(pipeSetPrefab).transform;
  87. newPipe.SetParent(pipes);
  88. }
  89. newPipe.transform.position = pipeSpawnLocation.position
  90. + new Vector3(0, Random.Range(0, pipeVariation), 0);
  91. }
  92. // scroll the ground pattern so it looks like it's moving
  93. // not currently sure why this needs to have a scale factor
  94. ground.settings.fillOffset += new Vector2(scrollSpeed * Time.deltaTime / 1.27f, 0);
  95. }
  96. }
  97. }