//This code create by CodeEngine

using Games.LogicObj;
using Games.Scene;
using UnityEngine;

namespace SPacket.SocketInstance
{
    public class GC_TELEMOVEHandler : Ipacket
    {
        public uint Execute(PacketDistributed ipacket)
        {
            var packet = (GC_TELEMOVE) ipacket;
            if (null == packet)
                return (uint) PACKET_EXE.PACKET_EXE_ERROR;
            //enter your logic
            var character = Singleton<ObjManager>.GetInstance().FindObjCharacterInScene(packet.ObjId);
            var targetPos = new Vector2(packet.TargetPosX, packet.TargetPosZ).ToClientPos();
            if (character == null)
            {
                //ObjManager.Instance.MoveHidePlayer(packet.ObjId, targetPos.x, targetPos.y);
                return (uint) PACKET_EXE.PACKET_EXE_CONTINUE;
            }
            else if (character.IsDie())
                return (uint) PACKET_EXE.PACKET_EXE_CONTINUE;
            //如果_objChar为轻功状态,则打断轻功
            //if (character.MovementState == MoveState.JumpDelay)
            //    character.EndLightSkillMove();
            //var autoMove = character.GetComponent<AutoMove>();
            //if (autoMove != null)
            //    autoMove.ResetAutoMove();
            //向目标点移动
            float speed = packet.Speed;
            if (speed <= 0)
                speed = character.BaseAttr.MoveSpeed;
            Obj_Character.TeleMoveExtraAction teleMoveExtraAction = null;
            if (packet.HasExtparamtype)
            {
                switch (packet.Extparamtype)
                {
                    case 0:
                        teleMoveExtraAction = new Obj_Character.TeleMoveInvisible(character, packet.Extparam1.ToClientTime(), packet.Extparam2.ToClientTime());
                        break;
                    case 1:
                        teleMoveExtraAction = new Obj_Character.TeleMoveAnim(character, packet.Extparam1, packet.Extparam2);
                        break;
                }
            }
            Quaternion? faceDir;
            if (packet.HasFinishfacedir)
                faceDir = CommonUtility.ServerRadianToQuaternion(packet.Finishfacedir);
            else if (packet.HasNeedChangeFaceto && packet.NeedChangeFaceto > 0)
                faceDir = Quaternion.LookRotation((targetPos - character.Position.RemoveY()).InsertY());
            else
                faceDir = null;
            character.SyncMove(character.Position.RemoveY(), targetPos, MoveState.TeleMove, speed, faceDir, teleMoveExtraAction);

            //是否需要播放附加动作
            if (packet.HasAnimaId && packet.AnimaId > -1 && character.AnimLogic != null)
            {
                character.AnimLogic.Play(packet.AnimaId);
                //现在这里播放击退效果 todo临时的
                //_objChar.PlayEffect(121);
            }
            return (uint) PACKET_EXE.PACKET_EXE_CONTINUE;
        }
    }
}