using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System;

public class PlayerMove : MonoBehaviour
{
    public float speed = 3f;
    Animator anim;
    [HideInInspector]
    public Vector3 move;
    MyPlayer myPlayer;

    /// <summary>
    /// 跟随模式
    /// </summary>
    public bool FollowMode;
    public bool Freeze;
    void Start()
    {
        anim = GetComponent<Animator>();
       // myPlayer = GameManage.Instance.me.GetComponent<MyPlayer>();
        if (!GameManage.Instance.is单机模式)
        {
            stdong.area = LoadManage.Instance.currentRoomArea;
            stBudong.area = LoadManage.Instance.currentRoomArea;
            byte[] idbyte = Encoding.UTF8.GetBytes(LoadManage.Instance.MyId);
            //动
            byte[] data1 = new byte[4 + idbyte.Length + 1];
            Array.Copy(BitConverter.GetBytes(idbyte.Length), 0, data1, 0, 4);
            Array.Copy(idbyte, 0, data1, 4, idbyte.Length);
            data1[4 + idbyte.Length] = 1;
            stdong.m_sOperaData = data1;
            //停
            byte[] data2 = new byte[4 + idbyte.Length + 1];
            Array.Copy(BitConverter.GetBytes(idbyte.Length), 0, data2, 0, 4);
            Array.Copy(idbyte, 0, data2, 4, idbyte.Length);
            data2[4 + idbyte.Length] = 0;
            stBudong.m_sOperaData = data2;
        }
    }


    st_Motions stdong = new st_Motions { m_iOperaType = 8};
    st_Motions stBudong = new st_Motions { m_iOperaType = 8 };
    bool issend = false;
    bool issendStop = false;
    void Update()
    {
        if (!GameManage.Instance.is单机模式)
        {
            //if (Input.GetKeyDown(KeyCode.Y))
            //{
            //    ChatPanel.instance.gameObject.SetActive(!ChatPanel.instance.gameObject.activeInHierarchy);
            //}

            //if (ChatPanel.instance.gameObject.activeInHierarchy)
            //{
            //    return;
            //}
        }

        if (Freeze) return;
        if (FollowMode) return;

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        move = new Vector3(x, 0, z);


        move = new Vector3(x, 0,z);

        if (move != Vector3.zero)
        {
            issendStop = false;
            //播放动画
            if (!issend)
            {
                if (LoadManage.Instance != null && LoadManage.Instance.RSclient != null)
                    LoadManage.Instance.RSclient.Send(stdong);
                issend = true;
            }
            // 转向
           
            move = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * move;
            transform.position += move * speed* Time.deltaTime;
            RotatePlayer();
        }
        else
        {
            issend = false;
            //停止动画
            if (!issendStop)
            {
                if (LoadManage.Instance!=null && LoadManage.Instance.RSclient != null)
                    LoadManage.Instance.RSclient.Send(stBudong);
                issendStop = true;
            }
        }
        UpdateAnim();
    }
    void UpdateAnim()
    {
        anim.SetFloat("InputMagnitude", move.magnitude);
    }
    private void RotatePlayer()
    {
        //向量v围绕y轴旋转cameraAngle.y度
        Vector3 vec = Quaternion.Euler(0, 0, 0) * move;
        Quaternion qua = Quaternion.LookRotation(vec);
        transform.rotation = Quaternion.Lerp(transform.rotation, qua, Time.deltaTime * 100);
    }
}