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

public class MoveClient : MonoBehaviour {

    // Use this for initialization
    public NetworkClientUI client;

    
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero
                                             // get the touch position from the screen touch to world point
            
            Vector2 touchedPos = Camera.main.ScreenToWorldPoint(new Vector2(touch.position.x, touch.position.y));
            Vector2 floats = new Vector2(touch.position.x/ Screen.width, touch.position.y / Screen.width);
            
            if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved )
            {
                // lerp and set the position of the current object to that of the touch, but smoothly over time.
                if (client.isConnect())
                {
                    NetworkClientUI.SendJoystickInfo(floats.x, floats.y);
                    transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime * 25f);
                }
                
            }
        }
    }
}
