using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Tank))]
public class PlayerTankController : MonoBehaviour
{

    private Tank _tank;

    void Start()
    {
        _tank = GetComponent<Tank>();
        _tank.fraction = 1;

        GameModel model = FindObjectOfType(typeof(GameModel)) as GameModel;
        model.RegisterTank(_tank);
        model.lives = _tank.lives;
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
            _tank.Shoot();
    }

    void FixedUpdate()
    {
        _tank.RotateTover(Camera.mainCamera.transform.eulerAngles.y - 90, Time.fixedDeltaTime);
        _tank.Move(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).normalized, Time.fixedDeltaTime);
    }

    void OnDestroy()
    {
        GameModel model = FindObjectOfType(typeof(GameModel)) as GameModel;
        if (model)
            model.UnRegister(_tank);
    }
}
