You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.1 KiB

2 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RunningTrack : MonoBehaviour {
public static RunningTrack Instance;
public float spawnY, spawnZ;
public Transform wallContainer;
public Wall prefab;
[Header("At start")]
public float wallSpeed;
public float wallSpawnDistance;
public float maxWallWidth;
[Header("With time")]
public float maxWallSpeed;
public float minWallSpawnDistance;
public float maxMaxWallWidth;
public bool wallSpeedGrowing, wallSpawnDistanceShrinking, maxWallWidthGrowing;
public float wallSpeedGrowingSpeed, wallSpawnDistanceShrinkingSpeed, maxWallWidthGrowingSpeed;
[SerializeField]
private float currentWallSpeed, currentWallSpawnDistance, currentMaxWallWidth;
private float spawnTime;
public float WallSpeed {
get => currentWallSpeed;
}
public float WallSpawnDistance {
get => currentWallSpawnDistance;
}
public float MaxWallWidth {
get => currentMaxWallWidth;
}
private void Awake() {
Instance = this;
}
void Start() {
ResetTrack();
}
void Update() {
spawnTime += Time.deltaTime;
if (spawnTime > currentWallSpawnDistance / currentWallSpeed) {
spawnTime = 0;
Vector3 scale = new Vector3(Random.Range(1f, currentMaxWallWidth), 1, 1);
float trackW = transform.localScale.x;
float spawnX = Random.Range((-trackW + scale.x) / 2, (trackW - scale.x) / 2);
Wall newWall = Instantiate(prefab, new Vector3(spawnX, spawnY, spawnZ), Quaternion.identity, wallContainer);
newWall.transform.localScale = scale;
newWall.moveSpeed = currentWallSpeed;
}
UpdateStats();
}
private void UpdateStats() {
if (wallSpeedGrowing) {
currentWallSpeed += wallSpeedGrowingSpeed * Time.deltaTime;
if (currentWallSpeed > maxWallSpeed) {
currentWallSpeed = maxWallSpeed;
wallSpeedGrowing = false;
wallSpawnDistanceShrinking = true;
UIControl.Instance.NewMessage("Wall gap size is shrinking...");
}
}
if (wallSpawnDistanceShrinking) {
currentWallSpawnDistance -= wallSpawnDistanceShrinkingSpeed * Time.deltaTime;
if (currentWallSpawnDistance < minWallSpawnDistance) {
currentWallSpawnDistance = minWallSpawnDistance;
wallSpawnDistanceShrinking = false;
maxWallWidthGrowing = true;
UIControl.Instance.NewMessage("Walls are getting wider...");
}
}
if (maxWallWidthGrowing) {
currentMaxWallWidth += maxWallWidthGrowingSpeed * Time.deltaTime;
if (currentMaxWallWidth > maxMaxWallWidth) {
currentMaxWallWidth = maxMaxWallWidth;
maxWallWidthGrowing = false;
UIControl.Instance.NewMessage("Max difficulty reached!");
}
}
}
public void ResetTrack() {
currentWallSpeed = wallSpeed;
currentWallSpawnDistance = wallSpawnDistance;
currentMaxWallWidth = maxWallWidth;
wallSpeedGrowing = true;
wallSpawnDistanceShrinking = false;
maxWallWidthGrowing = false;
foreach (Wall wall in new ArrayList(wallContainer.GetComponentsInChildren<Wall>())) {
Destroy(wall.gameObject);
}
UIControl.Instance.NewMessage("You are gaining speed...");
}
}