Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LevelCreator : MonoBehaviour
- {
- [SerializeField] private PathCreator _pathCreator;
- [SerializeField] private Tower _towerTemplate;
- [SerializeField] private int _humanTowerCount;
- [Space]
- [Header("Boosters Settings")]
- [SerializeField] private JumpBooster[] _jumpBoosterTemplates;
- [Range(0,1)]
- [SerializeField] private float _boostersFrequency;
- [Range(0, 10)]
- [SerializeField] private float _distanceBefore;
- [Space]
- [Header("Obstacles Settings")]
- [SerializeField] private Obstacle _obstacleTemplate;
- [Range(0, 1)]
- [SerializeField] private float _obstacleFrequency;
- private float roadStartMargin = 5f;
- private float roadEndMargin = 10f;
- private void Start()
- {
- GenerateLevel();
- }
- private void GenerateLevel()
- {
- float roadLength = _pathCreator.path.length - roadStartMargin - roadEndMargin;
- float distanceBetweenTower = roadLength / _humanTowerCount;
- float distanceTravelled = roadStartMargin;
- float delta = 0.1f;
- Vector3 spawnPoint, nextPoint, direction;
- for(int i = 0; i < _humanTowerCount; i++)
- {
- distanceTravelled += distanceBetweenTower;
- spawnPoint = _pathCreator.path.GetPointAtDistance(distanceTravelled, EndOfPathInstruction.Stop);
- nextPoint = _pathCreator.path.GetPointAtDistance(distanceTravelled + delta, EndOfPathInstruction.Stop);
- direction = nextPoint - spawnPoint;
- Tower tower = Instantiate(_towerTemplate, spawnPoint, Quaternion.identity);
- tower.transform.LookAt(nextPoint);
- TryToCreateBooster(tower, distanceTravelled);
- if(i < _humanTowerCount - 1)
- {
- TryToCreateObstacle(tower, distanceTravelled + distanceBetweenTower / 2f);
- }
- }
- }
- private void TryToCreateBooster(Tower tower, float towerPathPoint)
- {
- float dice = Random.value;
- if ((dice > _boostersFrequency) || (tower.Size == 1)) return;
- JumpBooster booster;
- if (tower.Size == 2)
- {
- booster = _jumpBoosterTemplates[0];
- }
- else
- {
- booster = _jumpBoosterTemplates[Random.Range(0, _jumpBoosterTemplates.Length)];
- }
- CreateBooster(booster, tower, towerPathPoint, tower.transform);
- }
- private void CreateBooster(JumpBooster booster, Tower tower, float pathPoint, Transform parent)
- {
- float distance = pathPoint - _distanceBefore;
- Vector3 spawnPoint = _pathCreator.path.GetPointAtDistance(distance, EndOfPathInstruction.Stop);
- Vector3 nextPoint = _pathCreator.path.GetPointAtDistance(distance + 0.1f, EndOfPathInstruction.Stop);
- Instantiate(booster, spawnPoint, Quaternion.LookRotation(nextPoint - spawnPoint), parent);
- }
- private void TryToCreateObstacle(Tower tower, float obstaclePoint)
- {
- float dice = Random.value;
- if (dice > _obstacleFrequency) return;
- int size = Random.Range(1, 3);
- Vector3 spawnPoint = _pathCreator.path.GetPointAtDistance(obstaclePoint, EndOfPathInstruction.Stop);
- Vector3 nextPoint = _pathCreator.path.GetPointAtDistance(obstaclePoint + 0.1f, EndOfPathInstruction.Stop);
- Obstacle obstacle = Instantiate(_obstacleTemplate, spawnPoint, Quaternion.LookRotation(nextPoint - spawnPoint));
- obstacle.Size = size;
- if (size == 2) CreateBooster(_jumpBoosterTemplates[0], tower, obstaclePoint, obstacle.transform);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class PlayerTower : MonoBehaviour
- {
- [SerializeField] private Human _startHuman;
- [SerializeField] private Transform _distanceChecker;
- [SerializeField] private float _fixationMaxDistance;
- [SerializeField] private BoxCollider _checkCollider;
- private List<Human> _humans;
- private Rigidbody _rigidbody;
- //private float _rigidbodyPositionY;
- private float displaceScale = 1.5f;
- private float _displaceValue = 0;
- public event UnityAction<int> HumanAdded;
- public event UnityAction<int> HumanRemoved;
- private void Start()
- {
- _humans = new List<Human>();
- Vector3 spawnpoint = transform.position;
- _humans.Add(Instantiate(_startHuman, spawnpoint, Quaternion.identity, transform));
- _displaceValue = (_humans[0].FixationPoint.position - spawnpoint).magnitude;
- _checkCollider.center = _distanceChecker.localPosition + new Vector3(0, _checkCollider.bounds.size.y / 2f, 0);
- _rigidbody = GetComponent<Rigidbody>();
- _rigidbody.position += Vector3.up * 1.5f;
- UpdateAnimations();
- HumanAdded?.Invoke(_humans.Count);
- Debug.Log("_displaceValue " + _displaceValue);
- }
- private void OnCollisionEnter(Collision collision)
- {
- if (collision.gameObject.TryGetComponent(out Human human))
- {
- //Debug.Log("Human Collision");
- Tower collisionTower = human.GetComponentInParent<Tower>();
- if(collisionTower != null)
- {
- List<Human> collectedHumans = collisionTower.CollectHuman(_distanceChecker, _fixationMaxDistance);
- if (collectedHumans != null)
- {
- _humans[0].StopRun();
- InsertHumansFromCollisionTower(collectedHumans);
- RebuildTower(_humans, collectedHumans.Count);
- DisplaceCheckers(_humans[0]);
- //GetComponent<Jumper>().SetToGround();
- UpdateAnimations();
- HumanAdded?.Invoke(_humans.Count);
- }
- collisionTower.Break();
- }
- }
- else if(collision.gameObject.TryGetComponent(out Obstacle obstacle))
- {
- Debug.Log("Collision with obstacle, size " + obstacle.Size);
- if (_distanceChecker.position.y >= (obstacle.transform.position.y + obstacle.transform.localScale.y)) return;
- int removingHumansCount = Mathf.Min(_humans.Count, obstacle.Size);
- List<Human> collisionHumans = _humans.GetRange(0, removingHumansCount);
- _humans.RemoveRange(0, removingHumansCount);
- for (int i = 0; i < collisionHumans.Count; i++)
- {
- collisionHumans[i].gameObject.layer = LayerMask.NameToLayer("Destroying");
- collisionHumans[i].Push(i, 10);
- Destroy(collisionHumans[i].gameObject, 5f);
- }
- if (_humans.Count == 0)
- {
- Destroy(gameObject);
- ResetGame();
- return;
- }
- NormalizeHumanPositions(_humans);
- DisplaceCheckers(_humans[0]);
- UpdateAnimations();
- HumanRemoved?.Invoke(_humans.Count);
- }
- }
- private void InsertHumansFromCollisionTower(List<Human> humansFromCollidionTower)
- {
- for (int i = humansFromCollidionTower.Count - 1; i >= 0; i--)
- {
- _humans.Insert(0, humansFromCollidionTower[i]);
- }
- }
- private void RebuildTower(List<Human> humans, int humansFromCollisionTowerCount)
- {
- Debug.Log("RebuildTower, adding " + humansFromCollisionTowerCount);
- for (int i = 0; i < humans.Count; i++)
- {
- Human insertHuman = humans[i];
- bool needCorrection = i >= humansFromCollisionTowerCount;
- SetHumanPosition(insertHuman);
- }
- NormalizeHumanPositions(humans);
- }
- private void NormalizeHumanPositions(List<Human> humans)
- {
- for (int i = 1; i < humans.Count; i++)
- {
- float offsetY = _displaceValue * i;
- _humans[i].transform.localPosition = _humans[0].transform.localPosition + new Vector3(0, offsetY, 0);
- }
- }
- private void SetHumanPosition(Human human)
- {
- human.transform.parent = transform;
- human.transform.localPosition = new Vector3(0, human.transform.localPosition.y, 0);
- human.transform.localRotation = Quaternion.identity;
- }
- private void DisplaceCheckers(Human human)
- {
- Vector3 distanceCheckerNewPosition = _distanceChecker.position;
- distanceCheckerNewPosition.y = human.transform.position.y;
- _distanceChecker.position = distanceCheckerNewPosition;
- //_checkCollider.center = _distanceChecker.localPosition;
- _checkCollider.center = _distanceChecker.localPosition + new Vector3(0, _checkCollider.bounds.size.y / 2f, 0);
- }
- private void UpdateAnimations()
- {
- _humans[0].StopTexting();
- _humans[0].Run();
- for(int i = 1; i < _humans.Count; i++)
- {
- float rnd = Random.value;
- if(rnd < 0.5f)
- {
- _humans[i].Texting();
- }
- else
- {
- _humans[i].StopTexting();
- }
- }
- }
- private void ResetGame()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- public class Tower : MonoBehaviour
- {
- [SerializeField] private Vector2Int _humanInTowerRange;
- [SerializeField] private Human[] _humansTemplates;
- [Space]
- [Range(10, 1000)]
- [SerializeField] private float _breakForceStrength;
- public int Size => _humanInTower.Count;
- private List<Human> _humanInTower;
- private void Awake()
- {
- _humanInTower = new List<Human>();
- int humanInTowerCount = Random.Range(_humanInTowerRange.x, _humanInTowerRange.y);
- SpawnHumans(humanInTowerCount);
- }
- private void SpawnHumans(int humanCount)
- {
- Vector3 spawnPoint = transform.position;
- for(int i = 0; i < humanCount; i++)
- {
- Human spawnedHuman = _humansTemplates[Random.Range(0, _humansTemplates.Length)];
- _humanInTower.Add(Instantiate(spawnedHuman, spawnPoint, Quaternion.identity, transform));
- _humanInTower[i].transform.localPosition = new Vector3(0, _humanInTower[i].transform.localPosition.y, 0);
- spawnPoint = _humanInTower[i].FixationPoint.position;
- }
- }
- public List<Human> CollectHuman(Transform distanceChecker, float fixationMaxDistance)
- {
- for(int i = 0; i < _humanInTower.Count; i++)
- {
- float distanceBetweenPoints = CheckDistanceY(distanceChecker, _humanInTower[i].FixationPoint.transform);
- Debug.Log("distanceBetweenPoints: " + distanceBetweenPoints + " fixationMaxDistance: " + fixationMaxDistance);
- if (distanceBetweenPoints < fixationMaxDistance)
- {
- List<Human> collectedHumans = _humanInTower.GetRange(0, i + 1);
- _humanInTower.RemoveRange(0, i + 1);
- return collectedHumans;
- }
- }
- return null;
- }
- private float CheckDistanceY(Transform distanceChecker, Transform humanFixationPoint)
- {
- Vector3 distanceCheckerY = new Vector3(0, distanceChecker.position.y, 0);
- Vector3 humanFixationPointY = new Vector3(0, humanFixationPoint.position.y, 0);
- return Vector3.Distance(distanceCheckerY, humanFixationPointY);
- }
- public void Break()
- {
- for(int i = 0; i < _humanInTower.Count; i++)
- {
- _humanInTower[i].gameObject.layer = LayerMask.NameToLayer("Destroying");
- _humanInTower[i].Push(i, 10);
- }
- Destroy(gameObject, 3f);
- //Destroy(gameObject);
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class Jumper : MonoBehaviour
- {
- [SerializeField] private float _jumpForce;
- [SerializeField] private Transform _ground;
- private Rigidbody _rigidbody;
- private bool _isGrounded;
- private float _boostMiltiplier;
- //private Vector3 _groundPosition;
- private void Awake()
- {
- _rigidbody = GetComponent<Rigidbody>();
- _boostMiltiplier = 1f;
- }
- private void Update()
- {
- if(Input.GetMouseButtonDown(0) && _isGrounded == true)
- {
- //_groundPosition = _rigidbody.position;
- _rigidbody.AddForce(Vector3.up * _jumpForce * _boostMiltiplier);
- _isGrounded = false;
- }
- }
- private void OnCollisionEnter(Collision collision)
- {
- if(collision.gameObject.TryGetComponent(out Road road))
- {
- _isGrounded = true;
- }
- }
- public void SetBoostMultiplier(float multiplier)
- {
- _boostMiltiplier = multiplier;
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class JumpBooster : MonoBehaviour
- {
- [SerializeField] private float _boostMultiplier;
- private Jumper _jumper;
- public float Value => _boostMultiplier;
- private void Awake()
- {
- _jumper = FindObjectOfType<Jumper>();
- }
- private void OnTriggerEnter(Collider other)
- {
- _jumper.SetBoostMultiplier(_boostMultiplier);
- }
- private void OnTriggerExit(Collider other)
- {
- _jumper.SetBoostMultiplier(1f);
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class PlayerTracker : MonoBehaviour
- {
- [SerializeField] private PlayerTower _playerTower;
- [SerializeField] private float _moveSpeed;
- [SerializeField] private Vector3 _offsetPosition;
- [SerializeField] private Vector3 _offsetRotation;
- private Vector3 _targetOffsetPosition;
- private void OnEnable()
- {
- _playerTower.HumanAdded += OnHumanAdded;
- _playerTower.HumanRemoved += OnHumanRemoved;
- }
- private void OnDisable()
- {
- _playerTower.HumanAdded -= OnHumanAdded;
- _playerTower.HumanRemoved -= OnHumanRemoved;
- }
- private void LateUpdate()
- {
- UpdatePosition();
- _offsetPosition = Vector3.MoveTowards(_offsetPosition, _targetOffsetPosition, _moveSpeed * Time.deltaTime);
- }
- private void UpdatePosition()
- {
- transform.position = _playerTower.transform.position;
- transform.localPosition += _offsetPosition;
- Vector3 lookAtPoint = _playerTower.transform.position + _offsetRotation;
- transform.localPosition = Vector3.MoveTowards(transform.localPosition, _targetOffsetPosition, _moveSpeed * Time.deltaTime);
- transform.LookAt(lookAtPoint);
- }
- private void OnHumanAdded(int count)
- {
- float factor = (count > 1) ? (float)count / (float)(count - 1) : 1f;
- factor = Mathf.Sqrt(factor);
- _targetOffsetPosition = _offsetPosition + (Vector3.up + Vector3.back) * factor;
- UpdatePosition();
- }
- private void OnHumanRemoved(int count)
- {
- float factor = (count > 1) ? (float)(count - 1) / (float)count : 1f;
- factor = Mathf.Sqrt(factor);
- _targetOffsetPosition = _offsetPosition - (Vector3.up + Vector3.back) * factor;
- UpdatePosition();
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class Obstacle : MonoBehaviour
- {
- private int _size = 1;
- public int Size {
- get { return _size; }
- set { _size = value; UpdateSize(_size); }
- }
- private float freeAreaHeight = 0.45f;
- private float oneBlockHeight = 1.5f;
- private void Start()
- {
- UpdateSize(_size);
- }
- private void UpdateSize(int size)
- {
- Vector3 localScale = transform.localScale;
- localScale.y = (float)_size * oneBlockHeight - freeAreaHeight;
- transform.localScale = localScale;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- [RequireComponent(typeof(Animator))]
- public class Human : MonoBehaviour
- {
- [SerializeField] private Transform _fixationPoint;
- private Animator _animator;
- public Transform FixationPoint => _fixationPoint;
- private void Awake()
- {
- _animator = GetComponent<Animator>();
- }
- public void Push(int index, float strength)
- {
- //strength = 500;
- Transform t = transform;
- int sign = (index % 2 == 0) ? -1 : 1;
- Vector3 forceDir = (t.right * Random.Range(5, 10) * sign + t.forward * (-1) + t.up * 0.1f).normalized;
- t.parent = null;
- StopRun();
- StopTexting();
- _animator.SetBool("isDead", true);
- Rigidbody rb = t.gameObject.AddComponent<Rigidbody>();
- rb.isKinematic = false;
- rb.drag = 2.5f;
- rb.AddForce(forceDir * strength, ForceMode.Impulse);
- }
- public void Run()
- {
- _animator.SetBool("isRunning", true);
- }
- public void StopRun()
- {
- _animator.SetBool("isRunning", false);
- }
- public void Texting()
- {
- _animator.SetBool("isTexting", true);
- }
- public void StopTexting()
- {
- _animator.SetBool("isTexting", false);
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- [RequireComponent(typeof(Rigidbody))]
- public class PathFollower : MonoBehaviour
- {
- [SerializeField] private float _speed = 1.9f;
- [SerializeField] private float _lookAtSpeed = .1f;
- [SerializeField] private PathCreator _pathCreator;
- private Rigidbody _rigidbody;
- private float _distanceTravelled;
- private void Awake()
- {
- _rigidbody = GetComponent<Rigidbody>();
- _rigidbody.MovePosition(_pathCreator.path.GetPointAtDistance(_distanceTravelled));
- }
- private void Update()
- {
- Vector3 currentDirection = transform.forward;
- Vector3 targetDirection;
- _distanceTravelled += Time.deltaTime * _speed;
- Vector3 nextPoint = _pathCreator.path.GetPointAtDistance(_distanceTravelled, EndOfPathInstruction.Loop);
- Vector3 currentPoint = transform.position;
- nextPoint.y = currentPoint.y;
- targetDirection = (nextPoint - currentPoint).normalized;
- transform.LookAt(Vector3.Slerp(currentPoint + currentDirection, currentPoint + targetDirection, _lookAtSpeed * Time.deltaTime)); ;
- //transform.LookAt(nextPoint);
- //_rigidbody.MovePosition(nextPoint);
- transform.position = nextPoint;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement