﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using com.ootii.Input;

public class SnakeControls : MonoBehaviour
{
    public GameState gameState;

    public Rigidbody snakeHead;
    public Snake snake;
    public float moveSpeed = 3f;
    public float rotationSpeed = 3f;

    // Use this for initialization
    void Start () {
		snakeHead = GetComponent<Rigidbody>();
	}
	
	// Update is called once per frame
	void Update () {

    }

    int rotationSwitch = 1;

    void FixedUpdate()
    {
        if (gameState.state != GameState.GameStates.Play)
        {
            return;
        }
        Movement();

        if (InputManager.IsPressed("Escape"))
        {
            gameState.GameOver();
        }
    }

    void Movement()
    {

        snakeHead.MovePosition(transform.position + transform.forward * Time.deltaTime * moveSpeed);

        if (snake.switchedControls)
        {
            rotationSwitch = -1;
        }
        else
        {
            rotationSwitch = 1;
        }

        if (InputManager.IsPressed("Right"))
        {
            snakeHead.transform.Rotate(transform.up * rotationSwitch, Time.deltaTime * rotationSpeed);
        }
        if (InputManager.IsPressed("Left"))
        {
            snakeHead.transform.Rotate(-transform.up * rotationSwitch, Time.deltaTime * rotationSpeed);
        }
    }
}
