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

public class MyPlayer : FunctionSync_PositionRoate
{
    public string id = "";
    public string name = "";
    public TextMesh text;
    public GameObject m_carryingIcon;
    public MeshRenderer m_carryingIconMeshRenderer;
    private float value;
    public Animator anim;
    public bool isTeacher;
    /// <summary>
    /// 预制体类型
    /// </summary>
    public byte PrefbType;
    public void Init(string id,string name,byte isTeather,byte PrefbType)
    {
        this.id = id;
        this.name = name;
        this.PrefbType = PrefbType;
        isPositionSync = true;
        isRoateSync = true;
        //初始化同步
        if (LoadManage.Instance != null)
        {
            InitDynamic(id, id == LoadManage.Instance.MyId);
        }
        else
        {
            InitDynamic(id, true);
        }
 
        //名字
        text =GetComponentInChildren<TextMesh>();
        text.text = name;
        m_carryingIcon = text.transform.Find("CarryingIcon").gameObject;
        m_carryingIconMeshRenderer = m_carryingIcon.GetComponent<MeshRenderer>();


        //自己或学生隐藏textmesh
        if (LoadManage.Instance == null)
        {
            text.gameObject.SetActive(false);
        }
        else
        {
            if (id == LoadManage.Instance.MyId)
            {
                text.gameObject.SetActive(false);
            }
            else
            {
                text.gameObject.SetActive(true);
            }
        }
        m_carryingIcon.SetActive(isTeacher);

        anim = GetComponent<Animator>();
    }

    Vector3 lastpos=new Vector3();
    public float OnceTime = 0.06f;
    public override void LateUpdate()
    {
        if (!GameManage.Instance.is单机模式)
        {
            //发送同步
            if (id == LoadManage.Instance.MyId)
            {
                if (Vector3.Distance(lastpos, transform.position) >= OnceTime)
                {
                    lastpos = transform.position;
                    SendSync();
                }
            }
            else
            {
                base.LateUpdate();
            }
        }
        

        if (text != null)
        {
            text.transform.LookAt(Camera.main.transform);
            text.transform.Rotate(Vector3.up, 180, Space.Self);
        }
    }
    /// <summary>
    /// 立即同步一次
    /// </summary>
    public void SyncAtOnce()
    {
        if (id == LoadManage.Instance.MyId)
        {
            lastpos = transform.position;
            SendSync();
        }
    }
}