Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Analytics;
- using UnityEngine.Experimental.PlayerLoop;
- using UnityEngine.UI;
- public class GameController : MonoBehaviour
- {
- #region Variables
- // inspector
- [SerializeField] GameObject _cloudPrefab;
- [SerializeField] private TextMeshProUGUI _playerOneText;
- [SerializeField] private TextMeshProUGUI _playerTwoText;
- [SerializeField] private Image _powerBar;
- [SerializeField] float _cloudSpeed = 6f;
- [SerializeField] AnimationCurve _travelCurve;
- //player turn
- private int _playersTurn;
- private bool _isPlayerOnesTurn;
- //cloud positioning
- private Vector3 _currentCloudPos;
- private Vector3 _convertedCurrentCloudPos;
- private bool _cloudIsMoving;
- private float _cloudTravelTime;
- private bool _isCloudMoving;
- //power info
- private float _timer;
- private float _powerFillRate = 2f;
- private float _power;
- #endregion
- void Start()
- {
- AdjustActivePlayerSettings();
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- _powerBar.fillAmount = 0f;
- if (_isPlayerOnesTurn)
- {
- AdjustPowerBarProperties(0, 1);
- }
- else
- {
- AdjustPowerBarProperties(1, -1);
- }
- }
- if (Input.GetKey(KeyCode.Space) && _isCloudMoving == false)
- {
- _power = _powerBar.fillAmount += 1.0f / _powerFillRate * Time.deltaTime;
- }
- if (Input.GetKeyUp(KeyCode.Space))
- {
- Vector3 cloudPos = _cloudPrefab.transform.position;
- Vector3 nextCloudPos = CalculateNextPos(_power);
- StartCoroutine(Move(cloudPos, nextCloudPos, _travelCurve, TimeToTravel(cloudPos.x, nextCloudPos.x)));
- }
- }
- // Animation of cloud
- IEnumerator Move(Vector3 pos1, Vector3 pos2, AnimationCurve ac, float timeToMove)
- {
- // Debug.Log("Moving from " + pos1 + " to " + pos2);
- _isCloudMoving = true;
- float timer = 0.0f;
- while (timer <= timeToMove)
- {
- pos1.x += (pos2.x - pos1.x) * ac.Evaluate(timer / timeToMove);
- _cloudPrefab.transform.position = pos1;
- timer += Time.deltaTime;
- yield return null;
- }
- pos1.x += (pos2.x - pos1.x) * ac.Evaluate(1);
- _cloudPrefab.transform.position = pos1;
- AdjustActivePlayerSettings();
- }
- private float TimeToTravel(float currentPos, float nextPos)
- {
- float distance = nextPos - currentPos;
- // Debug.Log(distance);
- // Debug.Log(Mathf.Abs(distance / _cloudSpeed));
- return Mathf.Abs(distance / _cloudSpeed);
- }
- private void AdjustPowerBarProperties(int direction, int flipMultiplier)
- {
- Vector3 barScale = _powerBar.rectTransform.localScale;
- barScale.x = flipMultiplier;
- _powerBar.rectTransform.localScale = barScale;
- }
- private Vector3 CalculateNextPos(float power)
- {
- _currentCloudPos = _cloudPrefab.transform.position;
- _convertedCurrentCloudPos = Camera.main.WorldToViewportPoint(_currentCloudPos);
- float xPos = _isPlayerOnesTurn ? _convertedCurrentCloudPos.x + power : _convertedCurrentCloudPos.x - power;
- Vector3 convertedNextCloudPos = new Vector3(xPos, _convertedCurrentCloudPos.y, _convertedCurrentCloudPos.z);
- Vector3 nextCloudPos = Camera.main.ViewportToWorldPoint(convertedNextCloudPos);
- return nextCloudPos;
- }
- private void AdjustActivePlayerSettings()
- {
- Color nonActivePlayerText = new Color(255f, 255f, 255f, 0.2f);
- Color activePlayerText = new Color(255f, 255f, 255f, 255f);
- _isPlayerOnesTurn = !_isPlayerOnesTurn;
- // Debug.Log("Player One's turn: " + _isPlayerOnesTurn);
- if (_isPlayerOnesTurn)
- {
- _playerOneText.color = activePlayerText;
- _playerTwoText.color = nonActivePlayerText;
- }
- else
- {
- _playerOneText.color = nonActivePlayerText;
- _playerTwoText.color = activePlayerText;
- }
- _isCloudMoving = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement