coding_giants

l16 - FleetScript

Mar 26th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     public AlienLeaderScript leader;
  23.  
  24.     void Start()
  25.     {
  26.        
  27.         StartGame();
  28.     }
  29.  
  30.     void Update()
  31.     {
  32.         if(gameRun && transform.childCount == 0)
  33.         {
  34.             //If the game is still on and the fleet doesn't have aliens anymore we stop the game
  35.             StopGame(true);
  36.         }
  37.         //resetting the game when the player presses R
  38.         if (Input.GetKeyDown(KeyCode.R)) SceneManager.LoadScene(0);
  39.     }
  40.  
  41.     public void StopGame(bool win)
  42.     {
  43.         //Stop the game and movement
  44.         CancelInvoke("Move");
  45.         leader.StopLeader();
  46.         gameRun = false;
  47.         //Switch on the text and fill it appropriatel
  48.         endText.gameObject.SetActive(true);
  49.         endText.text = win ? "You Win!" : "You Lose!";
  50.     }
  51.  
  52.     public void StartGame()
  53.     {
  54.         moveDir = 1;
  55.         //sterting calling a method with specified interval
  56.         InvokeRepeating("Move", moveTime, moveTime);
  57.         leader.StartLeader();
  58.         //starting the game and disabling the text
  59.         gameRun = true;
  60.         endText.gameObject.SetActive(false);
  61.     }
  62.  
  63.     void Move()
  64.     {
  65.         //depending on the direction change the position
  66.         transform.position += new Vector3(moveSpeed * moveDir, 0, 0);
  67.         moveCounter += moveDir;
  68.  
  69.         //Move down if the counter goes above 4 or below -4
  70.         if (moveCounter <= -4 || moveCounter >= 4)
  71.         {
  72.             moveDir *= -1;
  73.             transform.position -= new Vector3(0, moveSpeed, 0);
  74.         }
  75.     }
  76. }
Add Comment
Please, Sign In to add comment