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

public class Player1Controller : MonoBehaviour {

    // Use this for initialization
    Rigidbody rb;

    void Start () {
        rb = GetComponent<Rigidbody>();
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKey(KeyCode.RightArrow))
        {
            this.transform.Translate(Vector3.right * Time.deltaTime);
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            this.transform.Translate(Vector3.left * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            this.transform.Translate(Vector3.up * Time.deltaTime);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            this.transform.Translate(Vector3.down * Time.deltaTime);
        }
    }

    private void OnCollisionEnter(Collision other)
    {
        Debug.Log(other.gameObject.tag);
        if (other.gameObject.tag == "Neutral") {
          
            other.gameObject.tag = "Player1";
            other.gameObject.GetComponent<Rigidbody>().AddForce((other.transform.position - transform.position) * UnityEngine.Random.Range(2, 7)* 100);
            other.gameObject.GetComponent<NeutralRandomMove>().enabled = false;
            other.gameObject.GetComponent<Player1Controller>().enabled = true;
            other.gameObject.GetComponent<Player2Controller>().enabled = false;
            other.gameObject.GetComponent<SpriteRenderer>().color = GetComponent<SpriteRenderer>().color;
            StartCoroutine(StopForce(other));
        }

    }

    private IEnumerator StopForce(Collision other)
    {
        yield return new WaitForSeconds(0.15f);
        other.gameObject.GetComponent<Rigidbody>().drag = 100;
        other.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
        other.gameObject.GetComponent<Rigidbody>().isKinematic = false;
    }
}
