Advertisement
B4ttleCat

Drizzle Dash

Jul 21st, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.Analytics;
  7. using UnityEngine.Experimental.PlayerLoop;
  8. using UnityEngine.UI;
  9.  
  10. public class GameController : MonoBehaviour
  11. {
  12.     #region Variables
  13.  
  14.     // inspector
  15.     [SerializeField] GameObject _cloudPrefab;
  16.     [SerializeField] private TextMeshProUGUI _playerOneText;
  17.     [SerializeField] private TextMeshProUGUI _playerTwoText;
  18.     [SerializeField] private Image _powerBar;
  19.     [SerializeField] float _cloudSpeed = 6f;
  20.     [SerializeField] AnimationCurve _travelCurve;
  21.  
  22.     //player turn
  23.     private int _playersTurn;
  24.     private bool _isPlayerOnesTurn;
  25.  
  26.     //cloud positioning
  27.     private Vector3 _currentCloudPos;
  28.     private Vector3 _convertedCurrentCloudPos;
  29.     private bool _cloudIsMoving;
  30.     private float _cloudTravelTime;
  31.     private bool _isCloudMoving;
  32.  
  33.     //power info
  34.     private float _timer;
  35.     private float _powerFillRate = 2f;
  36.     private float _power;
  37.  
  38.     #endregion
  39.  
  40.     void Start()
  41.     {
  42.         AdjustActivePlayerSettings();
  43.     }
  44.  
  45.     void Update()
  46.     {
  47.         if (Input.GetKeyDown(KeyCode.Space))
  48.         {
  49.             _powerBar.fillAmount = 0f;
  50.  
  51.             if (_isPlayerOnesTurn)
  52.             {
  53.                 AdjustPowerBarProperties(0, 1);
  54.             }
  55.             else
  56.             {
  57.                 AdjustPowerBarProperties(1, -1);
  58.             }
  59.         }
  60.  
  61.         if (Input.GetKey(KeyCode.Space) && _isCloudMoving == false)
  62.         {
  63.             _power = _powerBar.fillAmount += 1.0f / _powerFillRate * Time.deltaTime;
  64.         }
  65.  
  66.         if (Input.GetKeyUp(KeyCode.Space))
  67.         {
  68.             Vector3 cloudPos = _cloudPrefab.transform.position;
  69.             Vector3 nextCloudPos = CalculateNextPos(_power);
  70.  
  71.             StartCoroutine(Move(cloudPos, nextCloudPos, _travelCurve, TimeToTravel(cloudPos.x, nextCloudPos.x)));
  72.         }
  73.     }
  74.  
  75.  
  76.     // Animation of cloud
  77.     IEnumerator Move(Vector3 pos1, Vector3 pos2, AnimationCurve ac, float timeToMove)
  78.     {
  79. //      Debug.Log("Moving from " + pos1 + " to " + pos2);
  80.         _isCloudMoving = true;
  81.         float timer = 0.0f;
  82.  
  83.         while (timer <= timeToMove)
  84.         {
  85.             pos1.x += (pos2.x - pos1.x) * ac.Evaluate(timer / timeToMove);
  86.             _cloudPrefab.transform.position = pos1;
  87.             timer += Time.deltaTime;
  88.  
  89.             yield return null;
  90.         }
  91.         pos1.x += (pos2.x - pos1.x) * ac.Evaluate(1);
  92.         _cloudPrefab.transform.position = pos1;
  93.  
  94.         AdjustActivePlayerSettings();
  95.     }
  96.  
  97.     private float TimeToTravel(float currentPos, float nextPos)
  98.     {
  99.         float distance = nextPos - currentPos;
  100. //      Debug.Log(distance);
  101. //      Debug.Log(Mathf.Abs(distance / _cloudSpeed));
  102.         return Mathf.Abs(distance / _cloudSpeed);
  103.     }
  104.  
  105.     private void AdjustPowerBarProperties(int direction, int flipMultiplier)
  106.     {
  107.         Vector3 barScale = _powerBar.rectTransform.localScale;
  108.         barScale.x = flipMultiplier;
  109.         _powerBar.rectTransform.localScale = barScale;
  110.     }
  111.  
  112.     private Vector3 CalculateNextPos(float power)
  113.     {
  114.         _currentCloudPos = _cloudPrefab.transform.position;
  115.         _convertedCurrentCloudPos = Camera.main.WorldToViewportPoint(_currentCloudPos);
  116.  
  117.         float xPos = _isPlayerOnesTurn ? _convertedCurrentCloudPos.x + power : _convertedCurrentCloudPos.x - power;
  118.  
  119.         Vector3 convertedNextCloudPos = new Vector3(xPos, _convertedCurrentCloudPos.y, _convertedCurrentCloudPos.z);
  120.         Vector3 nextCloudPos = Camera.main.ViewportToWorldPoint(convertedNextCloudPos);
  121.  
  122.         return nextCloudPos;
  123.     }
  124.  
  125.     private void AdjustActivePlayerSettings()
  126.     {
  127.         Color nonActivePlayerText = new Color(255f, 255f, 255f, 0.2f);
  128.         Color activePlayerText = new Color(255f, 255f, 255f, 255f);
  129.  
  130.         _isPlayerOnesTurn = !_isPlayerOnesTurn;
  131. //      Debug.Log("Player One's turn: " + _isPlayerOnesTurn);
  132.  
  133.         if (_isPlayerOnesTurn)
  134.         {
  135.             _playerOneText.color = activePlayerText;
  136.             _playerTwoText.color = nonActivePlayerText;
  137.         }
  138.         else
  139.         {
  140.             _playerOneText.color = nonActivePlayerText;
  141.             _playerTwoText.color = activePlayerText;
  142.         }
  143.  
  144.         _isCloudMoving = false;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement