Advertisement
thejaymer1998

GamePlayerControllerScript

Jun 18th, 2024 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.89 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.SocialPlatforms.Impl;
  6. using UnityEngine.UI;
  7.  
  8. public class Level01PlayerControllerTest : MonoBehaviour
  9. {
  10.     // Role: ???
  11.  
  12.     private bool moveRight = false;
  13.     private bool moveLeft = false;
  14.     private bool moveUp = true;
  15.     private bool moveDown = false;
  16.     public float driveSpeed;
  17.     public float switchSpeed;
  18.     private float acc = 0.005f;
  19.     private Rigidbody2D rb2d;
  20.     public GameObject level01Player;
  21.     //private Animator animator;
  22.  
  23.     private bool switchLeft = false;
  24.     private bool switchRight = true;
  25.  
  26.     // Timer
  27.     private float timer = 0.0f;
  28.     private int seconds;
  29.  
  30.     private Level01DestroyByContact destroyByContact;
  31.  
  32.     public TMPro.TMP_Text level01ScoreText;
  33.     public TMPro.TMP_Text level01GameTimeText;
  34.     public TMPro.TMP_Text level01GameTimeScoreText;
  35.     public TMPro.TMP_Text level01LivesScoreText;
  36.     public TMPro.TMP_Text level01FinalScoreText;
  37.     public GameObject newLevel01GameTimeText;
  38.     public GameObject newLevel01HighScoreText;
  39.     public int level01Score;
  40.     public int level01GameTimeScore;
  41.     public int level01LivesScore;
  42.     public int level01FinalScore;
  43.     public int level01HighScore;
  44.     public int level01GameTime;
  45.     private bool newLevel01LongestGameTime;
  46.     private bool newLevel01HighScore;
  47.  
  48.     public int level01NewLongestGameTimeBonus;
  49.     public TMPro.TMP_Text level01NewLongestGameTimeBonusText;
  50.     public int level01RemainingLivesBonus;
  51.     public TMPro.TMP_Text level01RemainingLivesBonusText;
  52.  
  53.  
  54.     public TMPro.TMP_Text testBonusText;
  55.  
  56.     //Test Variables 02
  57.     public Transform laneAPoint;
  58.     public Transform laneBPoint;
  59.     public Transform laneCPoint;
  60.  
  61.     [Tooltip("How long the movement will take in seconds.")]
  62.     public float moveDuration = 2;
  63.  
  64.     public bool inLaneA = false;
  65.     public bool inLaneB = false;
  66.     public bool inLaneC = false;
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     private void Start()
  73.     {
  74.         GameObject destroyByContactObject = GameObject.FindWithTag("Player");
  75.         if (destroyByContactObject != null)
  76.         {
  77.             destroyByContact = destroyByContactObject.GetComponent<Level01DestroyByContact>();
  78.         }
  79.         if (destroyByContact == null)
  80.         {
  81.             Debug.Log("Cannot find 'DestroyByContact' script");
  82.         }
  83.        
  84.         rb2d = transform.GetComponent<Rigidbody2D>();
  85.         //animator = GetComponent<Animator>();
  86.  
  87.         switchLeft = false;
  88.         switchRight = false;
  89.  
  90.         inLaneA = false;
  91.         inLaneB = true;
  92.         inLaneC = false;
  93.  
  94.         level01HighScore = PlayerPrefs.GetInt("Level01HighScore", 0);
  95.         level01GameTime = PlayerPrefs.GetInt("Level01LongestGameTime", 0);
  96.        
  97.         newLevel01LongestGameTime = false;
  98.         newLevel01GameTimeText.SetActive(false);
  99.  
  100.         //animator.SetBool("SwitchLeft", false);
  101.         //animator.SetBool("SwitchRight", false);
  102.     }
  103.  
  104.     void Update()
  105.     {
  106.         // Timer
  107.         // Seconds in Float
  108.         timer += Time.deltaTime;
  109.         // Convert Time Float to Int
  110.         seconds = (int)(timer);
  111.         // Print(seconds)
  112.         level01Score = seconds;
  113.         level01ScoreText.text = "Time: " + level01Score;
  114.         level01GameTime = level01Score;
  115.  
  116.         // Establishing Final Score
  117.         // Establishing Final Score - Game Time
  118.         level01GameTimeText.text = "Game Time : " + level01Score;
  119.         // Establishing Final Score - Game Time Score
  120.         level01GameTimeScore = level01GameTime * 5;
  121.         level01GameTimeScoreText.text = "Time Score: " + level01GameTimeScore;
  122.         // Establishing Final Score - Game Time Bonus Score
  123.         level01NewLongestGameTimeBonusText.text = "Time Bonus: " + level01NewLongestGameTimeBonus;
  124.         // Establishing Final Score - Lives Bonus
  125.         level01RemainingLivesBonus = destroyByContact.level01LivesBonus;
  126.         level01RemainingLivesBonusText.text = "Lives Bonus: " + level01RemainingLivesBonus;
  127.  
  128.         testBonusText.text = "From DBC " + level01RemainingLivesBonus;
  129.         // Establishing Final Score - Final Score
  130.         level01FinalScore = level01GameTimeScore + level01NewLongestGameTimeBonus + level01RemainingLivesBonus;
  131.         level01FinalScoreText.text = "Final Score: " + level01FinalScore;
  132.  
  133.         // Establishing High Score
  134.         level01HighScore = level01FinalScore;
  135.  
  136.         SetLongestGameTime();
  137.     }
  138.  
  139.     private void FixedUpdate()
  140.     {
  141.         // Activates Move Up Animation & Movement
  142.         if (moveUp == true)
  143.         {
  144.             driveSpeed += acc * Time.deltaTime;
  145.             transform.Translate(driveSpeed * Time.deltaTime * Vector3.up);
  146.         }
  147.  
  148.  
  149.         // Activates Movement Left & Right Movement
  150.         if (switchLeft == true && switchRight == false)
  151.         {
  152.             if (inLaneA == false && inLaneB == true && inLaneC == false)
  153.             {
  154.                 // remember, 10 - 5 is 5, so target - position is always your direction.
  155.                 Vector3 dir = laneAPoint.position - transform.position;
  156.            
  157.                 // magnitude is the total length of a vector.
  158.                 // getting the magnitude of the direction gives us the amount left to move
  159.                 float dist = dir.magnitude;
  160.            
  161.                 // this makes the length of dir 1 so that you can multiply by it.
  162.                 dir = dir.normalized;
  163.            
  164.                 // the amount we can move this frame
  165.                 float move = switchSpeed * Time.deltaTime;
  166.            
  167.                 // limit our move to what we can travel.
  168.                 if(move > dist) move = dist;
  169.            
  170.                 // apply the movement to the object.
  171.                 transform.Translate( dir * move);
  172.             }
  173.             else if (inLaneA == false && inLaneB == false && inLaneC == true)
  174.             {
  175.                 // remember, 10 - 5 is 5, so target - position is always your direction.
  176.                 Vector3 dir = laneBPoint.position - transform.position;
  177.            
  178.                 // magnitude is the total length of a vector.
  179.                 // getting the magnitude of the direction gives us the amount left to move
  180.                 float dist = dir.magnitude;
  181.            
  182.                 // this makes the length of dir 1 so that you can multiply by it.
  183.                 dir = dir.normalized;
  184.            
  185.                 // the amount we can move this frame
  186.                 float move = switchSpeed * Time.deltaTime;
  187.            
  188.                 // limit our move to what we can travel.
  189.                 if(move > dist) move = dist;
  190.            
  191.                 // apply the movement to the object.
  192.                 transform.Translate( dir * move);
  193.             }
  194.         }
  195.         else if (switchRight == true && switchLeft == false)
  196.         {
  197.             if (inLaneA == true && inLaneB == false && inLaneC == false)
  198.             {
  199.                 // remember, 10 - 5 is 5, so target - position is always your direction.
  200.                 Vector3 dir = laneBPoint.position - transform.position;
  201.            
  202.                 // magnitude is the total length of a vector.
  203.                 // getting the magnitude of the direction gives us the amount left to move
  204.                 float dist = dir.magnitude;
  205.            
  206.                 // this makes the length of dir 1 so that you can multiply by it.
  207.                 dir = dir.normalized;
  208.            
  209.                 // the amount we can move this frame
  210.                 float move = switchSpeed * Time.deltaTime;
  211.            
  212.                 // limit our move to what we can travel.
  213.                 if(move > dist) move = dist;
  214.            
  215.                 // apply the movement to the object.
  216.                 transform.Translate( dir * move);
  217.             }
  218.             else if (inLaneA == false && inLaneB == true && inLaneC == false)
  219.             {
  220.                 // remember, 10 - 5 is 5, so target - position is always your direction.
  221.                 Vector3 dir = laneCPoint.position - transform.position;
  222.            
  223.                 // magnitude is the total length of a vector.
  224.                 // getting the magnitude of the direction gives us the amount left to move
  225.                 float dist = dir.magnitude;
  226.            
  227.                 // this makes the length of dir 1 so that you can multiply by it.
  228.                 dir = dir.normalized;
  229.            
  230.                 // the amount we can move this frame
  231.                 float move = switchSpeed * Time.deltaTime;
  232.            
  233.                 // limit our move to what we can travel.
  234.                 if(move > dist) move = dist;
  235.            
  236.                 // apply the movement to the object.
  237.                 transform.Translate( dir * move);
  238.             }
  239.         }
  240.     }
  241.  
  242.    
  243.     private void OnTriggerEnter2D(Collider2D other)
  244.     {
  245.         if (other.CompareTag("LaneA"))
  246.         {
  247.             inLaneA = true;
  248.             inLaneB = false;
  249.             inLaneC = false;
  250.         }
  251.         else if (other.CompareTag("LaneB"))
  252.         {
  253.             inLaneA = false;
  254.             inLaneB = true;
  255.             inLaneC = false;
  256.         }
  257.         else if (other.CompareTag("LaneC"))
  258.         {
  259.             inLaneA = false;
  260.             inLaneB = false;
  261.             inLaneC = true;
  262.         }
  263.     }
  264.     public void SwitchLeft()
  265.     {
  266.         switchLeft = true;
  267.         switchRight = false;
  268.     }
  269.  
  270.     public void SwitchRight()
  271.     {
  272.         switchRight = true;
  273.         switchLeft = false;
  274.     }
  275.  
  276.     public void SetLongestGameTime()
  277.     {
  278.         //Updates Longest Game Time when Player achieves a New Longest Game Time
  279.         if (level01GameTime > PlayerPrefs.GetInt("Level01LongestGameTime", 0))
  280.         {
  281.             PlayerPrefs.SetInt("Level01LongestGameTime", level01GameTime);
  282.  
  283.             newLevel01LongestGameTime = true;
  284.             WillEnableGameTime();
  285.         }
  286.     }
  287.  
  288.     public void SetHighScore()
  289.     {
  290.         // Updates High Score for Level 01
  291.         if (level01HighScore > PlayerPrefs.GetInt("Level01HighScore"))
  292.         {
  293.             PlayerPrefs.SetInt("Level01HighScore", level01HighScore);
  294.  
  295.             newLevel01HighScore = true;
  296.             WillEnableHighScore();
  297.         }
  298.  
  299.         //SetLongestGameTime();
  300.     }
  301.  
  302.     IEnumerator HideShowNewGameTime()
  303.     {
  304.         // Displays New Longest Game Time for 3 Seconds
  305.         if (newLevel01LongestGameTime == true)
  306.         {
  307.             yield return new WaitForSeconds(3f);
  308.                 newLevel01LongestGameTime = false;
  309.             WillEnableGameTime();
  310.         }
  311.     }
  312.  
  313.     IEnumerator HideShowNewHighScore()
  314.     {
  315.         // Displays New High Score for 3 Seconds
  316.         if (newLevel01LongestGameTime == true)
  317.         {
  318.             yield return new WaitForSeconds(3f);
  319.                 newLevel01HighScore = false;
  320.             WillEnableHighScore();
  321.         }
  322.     }
  323.  
  324.     public void WillEnableGameTime()
  325.     {
  326.         // Displays New Longest Game Time Text
  327.         if (newLevel01LongestGameTime == true)
  328.         {
  329.             newLevel01GameTimeText.SetActive(true);
  330.             StartCoroutine(HideShowNewGameTime());
  331.  
  332.             level01NewLongestGameTimeBonus = 100;
  333.         }
  334.         // Destroys New Longest Game Time Text
  335.         else if (newLevel01LongestGameTime == false)
  336.         {
  337.             newLevel01GameTimeText.SetActive(false);
  338.             Destroy(newLevel01GameTimeText);
  339.         }
  340.     }
  341.  
  342.     public void WillEnableHighScore()
  343.     {
  344.         // Displays New High Score Text
  345.         if (newLevel01HighScore == true)
  346.         {
  347.             newLevel01HighScoreText.SetActive(true);
  348.             StartCoroutine(HideShowNewHighScore());
  349.         }
  350.         // Destroys New Longest Game Time Text
  351.         else if (newLevel01HighScore == false)
  352.         {
  353.             newLevel01HighScoreText.SetActive(false);
  354.             Destroy(newLevel01HighScoreText);
  355.         }
  356.     }
  357. }
  358.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement