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

public class PlayerMovement : MonoBehaviour {

	private Rigidbody playerRigidbody;
	public float moveSpeed = 10f;

	private Transform mainCam;
	private Vector3 mainCamForward;
	private Vector3 mainCamMove;

	public bool playerIsDead = false;

	//public GameObject attractedTo;
	//public float strenghOfAttraction = 5.0f;

	public Material matOnDead;

	// Use this for initialization
	void Start () {
		
		playerRigidbody = GetComponent<Rigidbody> ();
		mainCam = Camera.main.transform;

	}

	void Update () {
		
		if (Input.GetKey (KeyCode.Space)) {
			//Scene currentScene = SceneManager.GetActiveScene();
			SceneManager.LoadScene ("scene_main");
		}

		if (Input.GetKey (KeyCode.Escape)) {
			Application.Quit ();
		
		}


	}
	
	// Update is called once per frame
	void FixedUpdate () {
		// Vector3 playerInputs = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
		// playerRigidbody.MovePosition (transform.position + (playerInputs * moveSpeed * Time.deltaTime));
		if (playerIsDead) {
			GetComponent<Renderer> ().sharedMaterial = matOnDead;
			return;
		}

		mainCamForward = Vector3.Scale (mainCam.forward, new Vector3 (1, 0, 1)).normalized;
		mainCamMove = Input.GetAxis ("Vertical") * mainCamForward + Input.GetAxis ("Horizontal") * mainCam.right;

		playerRigidbody.MovePosition (transform.position + (mainCamMove * moveSpeed * Time.deltaTime));

		//if (attractedTo.tag == "garbage_attract") {
		//	Vector3 direction = attractedTo.transform.position - transform.position;
		//	attractedTo.GetComponent<Rigidbody> ().AddForce (strenghOfAttraction * direction);
		//}

	}
		
}