using UnityEngine;
using TMPro;
public class CubeController : MonoBehaviour
{
[SerializeField] float jumpForce = 8f;
[SerializeField] float rotationSpeed = 100f;
[SerializeField] float raycastDistance = 0.8f;
[SerializeField] LayerMask groundLayer;
[SerializeField] int step = 5;
[SerializeField] int maxCubes = 300;
Rigidbody2D rb;
SpriteRenderer sr;
TextMeshPro txt;
int number;
int birthFrame;
static int cubeCount;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetCount() { cubeCount = 0; }
void Awake()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponentInChildren<SpriteRenderer>();
txt = GetComponentInChildren<TextMeshPro>();
if (rb == null || sr == null || txt == null)
{
Debug.LogError("На корневом Cube нужен Rigidbody2D, а внутри Visual (SpriteRenderer) и текст", this);
enabled = false;
return;
}
birthFrame = Time.frameCount;
cubeCount++;
RandomizeColors();
UpdateText();
}
void OnDestroy() { cubeCount--; }
void Update()
{
// вращается только картинка, физика не меняется
sr.transform.Rotate(0, 0, rotationSpeed * Time.deltaTime);
if (Time.frameCount == birthFrame) return;
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
Jump();
}
bool IsGrounded()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, raycastDistance, groundLayer);
return hit.collider != null;
}
void Jump()
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, 0f);
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
number += step;
RandomizeColors();
UpdateText();
SpawnClone();
}
void SpawnClone()
{
if (cubeCount >= maxCubes) return;
Camera cam = Camera.main;
if (cam == null) return;
Vector3 pos = cam.ViewportToWorldPoint(
new Vector3(Random.Range(0.1f, 0.9f), Random.Range(0.4f, 0.9f), 10f));
pos.z = 0f;
GameObject clone = Instantiate(gameObject, pos, Quaternion.identity);
clone.GetComponent<CubeController>().Init(number);
}
public void Init(int startNumber)
{
number = startNumber;
RandomizeColors();
UpdateText();
}
void RandomizeColors()
{
sr.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.8f, 1f);
txt.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.8f, 1f);
}
void UpdateText()
{
txt.text = number.ToString();
}
}