SHOW:
|
|
- or go back to the newest paste.
| 1 | using System.Collections; | |
| 2 | using System.Collections.Generic; | |
| 3 | using UnityEngine; | |
| 4 | using TMPro; | |
| 5 | using UnityEngine.SceneManagement; | |
| 6 | ||
| 7 | public class FleetScript : MonoBehaviour | |
| 8 | {
| |
| 9 | //how often does the fleet move | |
| 10 | public float moveTime = 2; | |
| 11 | //speed | |
| 12 | public float moveSpeed; | |
| 13 | //move counter | |
| 14 | int moveCounter = 0; | |
| 15 | //movement direction | |
| 16 | int moveDir; | |
| 17 | ||
| 18 | public bool gameRun; | |
| 19 | ||
| 20 | public TMP_Text endText; | |
| 21 | ||
| 22 | void Start() | |
| 23 | {
| |
| 24 | moveDir = 1; | |
| 25 | StartGame(); | |
| 26 | } | |
| 27 | ||
| 28 | void Update() | |
| 29 | {
| |
| 30 | if(gameRun && transform.childCount == 0) | |
| 31 | {
| |
| 32 | //If the game is still on and the fleet doesn't have aliens anymore we stop the game | |
| 33 | StopGame(true); | |
| 34 | } | |
| 35 | //resetting the game when the player presses R | |
| 36 | if (Input.GetKeyDown(KeyCode.R)) SceneManager.LoadScene(0); | |
| 37 | } | |
| 38 | ||
| 39 | public void StopGame(bool win) | |
| 40 | {
| |
| 41 | //Stop the game and movement | |
| 42 | CancelInvoke("Move");
| |
| 43 | gameRun = false; | |
| 44 | //Switch on the text and fill it appropriately | |
| 45 | endText.gameObject.SetActive(true); | |
| 46 | endText.text = win ? "You Win!" : "You Lose!"; | |
| 47 | } | |
| 48 | ||
| 49 | public void StartGame() | |
| 50 | {
| |
| 51 | //sterting calling a method with specified interval | |
| 52 | InvokeRepeating("Move", moveTime, moveTime);
| |
| 53 | //starting the game and disabling the text | |
| 54 | gameRun = true; | |
| 55 | endText.gameObject.SetActive(false); | |
| 56 | } | |
| 57 | ||
| 58 | void Move() | |
| 59 | {
| |
| 60 | //depending on the direction change the position | |
| 61 | transform.position += new Vector3(moveSpeed * moveDir, 0, 0); | |
| 62 | moveCounter += moveDir; | |
| 63 | ||
| 64 | //Move down if the counter goes above 4 or below -4 | |
| 65 | if (moveCounter <= -4 || moveCounter >= 4) | |
| 66 | {
| |
| 67 | moveDir *= -1; | |
| 68 | transform.position -= new Vector3(0, moveSpeed, 0); | |
| 69 | } | |
| 70 | } | |
| 71 | } |