using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ZeminScript : MonoBehaviour
{
    int sayi = 0;
    // Start is called before the first frame update
    void Start()
    {
        ObjeYarat(100);
    }

    // Update is called once per frame
    void Update()
    {   // Zıplatalım
        if (Input.GetKey(KeyCode.Return) || Input.touchCount == 1)
        {
            List<GameObject> rootObjects = new List<GameObject>();
            Scene scene = SceneManager.GetActiveScene();
            scene.GetRootGameObjects(rootObjects);

            // iterate root objects and do something
            for (int i = 0; i < rootObjects.Count; ++i)
            {
                GameObject gameObject = rootObjects[i];
                if (gameObject.name.StartsWith("Object"))
                {
                    Rigidbody rb = gameObject.GetComponent<Rigidbody>();
                    rb.AddForce(
                        Random.Range(-10.0f, 10.0f),
                        Random.Range(10.0f, 50.0f),
                        Random.Range(-10.0f, 10.0f)
                    );
                }
            }
        }

        // Yeni nasnaler
        if (Input.GetKey(KeyCode.Space) || Input.touchCount == 2)
        {
            ObjeYarat(200);
        }

        // Klavye etkileşimi
        float vrt = Input.GetAxis("Vertical");
        float hrz = Input.GetAxis("Horizontal");

        // Not: Time.deltaTime ile çarpmak lazım
        vrt *= -30*Time.deltaTime;
        hrz *= 30*Time.deltaTime;

        GameObject go = GameObject.Find("Zemin");
        go.transform.Rotate(vrt,0,hrz);
    }

    void ObjeYarat(int length)
    {
        for (int i = 0; i < length; i++)
        {
            GameObject go;
            int type = Random.Range(1, 4);
            if (type == 1)
                go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            else if (type == 2)
                go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            else
                go = GameObject.CreatePrimitive(PrimitiveType.Cube);

            go.transform.position = new Vector3(
                    Random.Range(-5f, 5f),
                    Random.Range( 2f, 50f),
                    Random.Range(-5f, 5f)
                );
            ++sayi;
            go.name = "Object-"+sayi;

            Rigidbody rb = go.AddComponent<Rigidbody>();
            rb.AddForce(
                    Random.Range(-50f, 50f),
                    Random.Range( 10f, 50f),
                    Random.Range(-50f, 50f)
                );

            // Renk ataması
            Color clr = new Color(
                    Random.Range(0f, 1f),
                    Random.Range(0f, 1f),
                    Random.Range(0f, 1f)
            );
            go.GetComponent<Renderer>().material.color = clr;
        }
    }
}
