using UnityEngine;
using UnityEngine.Networking;

public class MoveTank : NetworkBehaviour
{

	public GameObject bulletPrefab;
	public Transform endOfCanon;

	void Update()
	{

		if (!isLocalPlayer)
		{
			return;
		}
		var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
		var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;

		transform.Rotate(0, x, 0);
		transform.Translate(0, 0, z);
		if (Input.GetKeyDown(KeyCode.Space))
		{

			CmdFire();
		}

	}
	public override void OnStartLocalPlayer()
	{
		GetComponent<MeshRenderer>().material.color = Color.green;
		GameObject.Find("Main Camera").transform.parent = gameObject.transform;
		GameObject.Find("Main Camera").transform.localPosition = new Vector3 (0.0f, transform.position.y+10, 0);

	}
	[Command]
	void CmdFire()
	{
		// Create the Bullet from the Bullet Prefab
		var bullet = (GameObject)Instantiate (
			bulletPrefab,
			endOfCanon.position,
			endOfCanon.rotation);

		// Add velocity to the bullet
		bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
		NetworkServer.Spawn(bullet);    
		// Destroy the bullet after 2 seconds
		Destroy(bullet, 2.0f);
	}

} 
