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

public class Ball : MonoBehaviour {
    public float intensity;
    private Rigidbody2D rb2d;
    public float MaxVelocity;
    public float MaxDownward;

	// Use this for initialization
	void Start () {
        rb2d = GetComponent<Rigidbody2D>();
        InitializeColor();
	}
	
	// Update is called once per frame
	void FixedUpdate () {
        if (Input.GetMouseButtonDown(0))
            rb2d.AddForce(Vector2.up * intensity);

        if (rb2d.velocity.y > MaxVelocity)
            rb2d.AddForce(Vector2.down * 30);

        if (rb2d.velocity.y < MaxDownward)
            rb2d.AddForce(Vector2.up * intensity);
	}

    void InitializeColor()
    {
        ManagerReferences refs = GameObject.Find("Menager").GetComponent<ManagerReferences>();

        if (refs != null)
        {
            int colorCount = refs.Colors.Length;

            int randomIndex = Random.Range(0, colorCount - 1);

            Color newColor = refs.Colors[randomIndex];

            SpriteRenderer render = GetComponent<SpriteRenderer>();
            render.color = newColor;
        }
    }
}
