public void ReplaceHero(RoleCtrl source, HeroPlatform target)
        {
            if (ForwardHeroList.Contains(source))
            {
                if (ForwardHeroPlatformList.Contains(target))
                {
                    return;
                }
                ForwardHeroList.Remove(source);
                BackwardHeroList.Add(source);
            }
            else if (BackwardHeroList.Contains(source))

            {
                if (BackwardHeroPlatformList.Contains(target))
                {
                    return;
                }
                BackwardHeroList.Remove(source);
                ForwardHeroList.Add(source);
            }
            else
            {
                Debug.Log("错误:还没有加入");
            }
        }
 private void OnRoleDieCallBack(RoleCtrl ctrlTemp)
 {
     if (ctrlTemp.CurRoleType == RoleType.Hero)
     {
         HeroPlatform platform = GetHeroPlatform(ctrlTemp.CurRoleInfo.RoleId);
         platform.RefreshRoleId(0);
         RemoveHero(ctrlTemp);
         if (HeroList.Count <= 0)
         {
             m_LevelProcedure.ChangeState(LevelProcedure.Fail);
         }
     }
     else
     {
         if (MonsterList.Contains(ctrlTemp))
         {
             MonsterList.Remove(ctrlTemp);
             if (MonsterList.Count <= 0)
             {
                 m_LevelProcedure.ChangeState(LevelProcedure.Win);
             }
         }
         else
         {
             Debug.LogError("错误;角色被已经被移除了");
         }
     }
     ctrlTemp.OnRoleDie -= OnRoleDieCallBack;
 }
        //在平台上创建英雄
        public void CreateHero(long roleId, Vector2 screenPos)
        {
            HeroInfo info = Global.HeroInfoList.Find(x => x.RoleId == roleId);

            if (info != null)
            {
                HeroEntity heroEntity = HeroDBModel.Instance.Get(info.HeroID);
                GameObject go         = RoleMgr.Instance.LoadRole(RoleType.Hero, heroEntity.PrefabName);
                RoleCtrl   ctrl       = go.GetComponent <RoleCtrl>();
                ctrl.Init(RoleType.Hero, info, null);
                HeroPlatform platform = GetHeroPlatformByScreenPos(screenPos);
                platform.RefreshRoleId(ctrl.CurRoleInfo.RoleId);
                ctrl.RefreshPlatfrom(platform);
                ctrl.StandOnPlatfrom();
                go.transform.position = platform.transform.TransformPoint(platform.StandPos);
                bool inFoward = ForwardHeroPlatformList.Contains(platform);
                if (inFoward)
                {
                    AddHero(ctrl, true);
                }
                else
                {
                    AddHero(ctrl, false);
                }
                ctrl.OnRoleDestory += OnRoleDestoryCallBack;
                ctrl.OnRoleDie     += OnRoleDieCallBack;
                return;
            }
            Debug.Log("错误:在模拟服务器上找不到持有的英雄信息");
        }
        //交换平台上的英雄
        public void SwapHeroOnPlatfrom(long sourceRoleId, long targetRoleId)
        {
            RoleCtrl sourcrRole = GetHero(sourceRoleId);
            RoleCtrl targetRole = GetHero(targetRoleId);

            HeroPlatform sourcrePlatform = GetHeroPlatform(sourceRoleId);
            HeroPlatform targetPlatform  = GetHeroPlatform(targetRoleId);

            if (sourcrRole == null || targetRole == null || sourcrePlatform == null || targetPlatform == null)
            {
                Debug.LogError("错误");
                return;
            }


            ReplaceHero(sourcrRole, targetPlatform);
            ReplaceHero(targetRole, sourcrePlatform);

            sourcrePlatform.RefreshRoleId(targetRole.CurRoleInfo.RoleId);
            targetPlatform.RefreshRoleId(sourcrRole.CurRoleInfo.RoleId);

            sourcrRole.RefreshPlatfrom(targetPlatform);
            sourcrRole.StandOnPlatfrom();
            targetRole.RefreshPlatfrom(sourcrePlatform);
            targetRole.StandOnPlatfrom();
        }
        private void OnUIGoFightCallBak(object[] p)
        {
            long    roleId    = (long)p[0];
            Vector2 screenPos = (Vector2)p[1];

            HideDragItem();
            //是否有平台
            HeroPlatform platform = GameSceneCtrl.Instance.GetHeroPlatformByScreenPos(screenPos);

            if (platform != null)
            {
                if (platform.RoleId != 0)
                {
                    //替换
                    SimulatedDatabase.Instance.ReplaceFightHero(platform.RoleId, roleId);
                    GameSceneCtrl.Instance.ReplaceHeroOnPlatform(platform.RoleId, roleId, screenPos);
                    UpdateAllHeroInfo();
                }
                else
                {
                    //创建
                    if (GameSceneCtrl.Instance.HasPlatform(screenPos))
                    {
                        GameSceneCtrl.Instance.CreateHero(roleId, screenPos);
                        SimulatedDatabase.Instance.AddFightHero(roleId);
                        UpdateAllHeroInfo();
                    }
                }
            }
        }
        //刷新英雄平台
        public void RefreshHeroPlatform(long roleId, HeroPlatform platfrom)
        {
            RoleCtrl     role           = GetHero(roleId);
            HeroPlatform sourcePlatform = GetHeroPlatform(roleId);

            ReplaceHero(role, platfrom);

            sourcePlatform.RefreshRoleId(0);
            platfrom.RefreshRoleId(role.CurRoleInfo.RoleId);

            role.RefreshPlatfrom(platfrom);
            role.StandOnPlatfrom();
        }
        //平台上是否有英雄
        public bool HasPlatform(Vector2 screenPos)
        {
            HeroPlatform platform = GetHeroPlatformByScreenPos(screenPos);

            if (platform != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
 private void OnRoleDestoryCallBack(Transform obj)
 {
     if (m_LevelProcedure.CurState == LevelProcedure.HeroEditor)
     {
         RoleCtrl roleCtrl = obj.GetComponent <RoleCtrl>();
         if (roleCtrl.CurRoleType == RoleType.Hero)
         {
             HeroPlatform platform = GetHeroPlatform(roleCtrl.CurRoleInfo.RoleId);
             platform.RefreshRoleId(0);
             RemoveHero(roleCtrl);
         }
     }
 }
        public HeroPlatform GetHeroPlatformByScreenPos(Vector2 screenPos)
        {
            Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);

            RaycastHit2D[] hits = Physics2D.RaycastAll(worldPos, Vector2.up, 0.01f);
            for (int i = 0; i < hits.Length; i++)
            {
                if (hits[i].collider != null)
                {
                    HeroPlatform h = hits[i].collider.GetComponent <HeroPlatform>();
                    if (h != null)
                    {
                        return(h);
                    }
                }
            }
            return(null);
        }
        //英雄脱离战场
        private void OnSceneLevelFightCallBack(object[] p)
        {
            RoleCtrl ctrl     = (RoleCtrl)p[0];
            Vector3  scenePos = (Vector3)p[1];

            HeroPlatform platform = GameSceneCtrl.Instance.GetHeroPlatformByScreenPos(scenePos);

            //依然在平台内
            if (platform != null)
            {
                SetGameObjectLayer(ctrl.gameObject, "Role", 0);
                ctrl.transform.SetParent(null);
                if (platform.RoleId != 0)
                {
                    //替换
                    GameSceneCtrl.Instance.SwapHeroOnPlatfrom(ctrl.CurRoleInfo.RoleId, platform.RoleId);
                }
                else
                {
                    //刷新
                    GameSceneCtrl.Instance.RefreshHeroPlatform(ctrl.CurRoleInfo.RoleId, platform);
                }
            }
            else
            {
                //在UI内
                RectTransform rect       = ((UIGameSceneView)UISceneCtrl.Instance.CurrentUIScene).HeroGridRect;
                bool          inHeroGrid = RectTransformUtility.RectangleContainsScreenPoint(rect, scenePos, UISceneCtrl.Instance.CurrentUIScene.UICamera);
                if (inHeroGrid)
                {
                    //移除这个战斗英雄
                    SimulatedDatabase.Instance.RemoveFightHero(ctrl.CurRoleInfo.RoleId);
                    GameObject.DestroyImmediate(ctrl.gameObject);
                    UpdateAllHeroInfo();
                }
                else
                {
                    //复原
                    SetGameObjectLayer(ctrl.gameObject, "Role", 0);
                    ctrl.transform.SetParent(null);
                    ctrl.StandOnPlatfrom();
                }
            }
        }
        private bool CheckHeroAndPlatformNoDifference(long roleId, bool forward)
        {
            RoleCtrl heroTemp = HeroList.Find(x => x.CurRoleInfo.RoleId == roleId);

            if (heroTemp == null)
            {
                Debug.Log("错误:没有这个英雄");
                return(false);
            }
            if (forward)
            {
                HeroPlatform forwardPlatform = ForwardHeroPlatformList.Find(x => x.RoleId == roleId);
                RoleCtrl     hero            = ForwardHeroList.Find(x => x.CurRoleInfo.RoleId == roleId);
                if (hero == null || forwardPlatform == null)
                {
                    Debug.Log("错误:平台和英雄 不一致");
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                HeroPlatform forwardPlatform = BackwardHeroPlatformList.Find(x => x.RoleId == roleId);
                RoleCtrl     hero            = BackwardHeroList.Find(x => x.CurRoleInfo.RoleId == roleId);
                if (hero == null || forwardPlatform == null)
                {
                    Debug.Log("错误:平台和英雄 不一致");
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
        public HeroPlatform GetHeroPlatform(long roleId)
        {
            HeroPlatform platform = ForwardHeroPlatformList.Find(x => x.RoleId == roleId);

            if (platform == null)
            {
                platform = BackwardHeroPlatformList.Find(x => x.RoleId == roleId);
                if (platform == null)
                {
                    Debug.Log("错误:平台不存在");
                    return(null);
                }
                else
                {
                    CheckHeroAndPlatformNoDifference(roleId, false);
                }
            }
            else
            {
                CheckHeroAndPlatformNoDifference(roleId, true);
            }
            return(platform);
        }
 public void RefreshPlatfrom(HeroPlatform platform)
 {
     CurHeroPlatform = platform;
 }