您的位置:首页 > 其它

U3D 战斗场景中怪物位置的箭头指示

2014-09-11 11:10 337 查看
在游戏中的战斗场景中 每个怪物都有一个行为控制器(一个控制器就是一个gameobject)
MLActionController 继承自MonoBehaviour 当MonoBehaviour启用时,其Update在每一帧被调用。在此函数中可以判断怪物是否在玩家视野范围内以屏幕的一半为基准

 void ScreenVisibleUpdate()

        {

            if (!IsPlayerSelf

                && !IsBreakItem

                && IsActive)

            {

                Vector3 monsterPosition = Util.WorldToScreenPoint(ThisTransform.position);

                if (MLLevelManager.Singleton.Hero)

                {

                    Vector3 temp = MLLevelManager.Singleton.Hero.ThisTransform.position;

                    Vector3 HeroPosition = Util.WorldToScreenPoint(temp);

                    if ((Math.Abs(HeroPosition.x - monsterPosition.x) > (Screen.width / 2 + Screen.width * 10 / 200))

                        || (Math.Abs(monsterPosition.y - HeroPosition.y) > (Screen.height / 2 + Screen.height * 10 / 200)))

                    {

                        if (!IsPlayerPartner)

                        {

                            OnACBecameInvisible(monsterPosition, true);

                        }

                        else

                        {

                            OnACBecameInvisible(monsterPosition, false);

                        }

                    }

                    else

                    {

                        OnACBecameVisible(monsterPosition);

                    }

                }

            }

        }

////////////////怪物进视野或者出视野的判定

      public void OnACBecameVisible(Vector3 Monsterposition)

        {

            if (!IsPlayerSelf

                && IsActive)

            {

                MLSceneArrow win = MLUIManager.Singleton.FindUIController<MLSceneArrow>();

                win.DeleteMonster(ThisObject);

            }

        }

        //出视野

        public void OnACBecameInvisible(Vector3 Monsterposition, bool IfMonster)

        {

            if (!IsPlayerSelf

                && IsActive

                && IsAlived)

            {

                MLSceneArrow win = MLUIManager.Singleton.FindUIController<MLSceneArrow>();

                win.InitDlg();

                win.AddMonster(ThisObject, Monsterposition, IfMonster);

            }

        }

//////////////////在scenearrow的UI中对于箭头的显示 因为重复性 所以选用了prefabs

   GameObject ArrowObject = MLResourceLoader.Instance.LoadAsset("UIPrefab/Logics/monster_arrow", typeof(GameObject)) as GameObject;; //箭头

///根据怪物位置的箭头转向处理函数

 public void AddMonster(GameObject Monster, Vector3 MonsterPosition,bool IfMonster)

        {

            Vector3 HeroPosition = Camera.main.WorldToScreenPoint(MLLevelManager.Singleton.Hero.ThisTransform.position);

            double x = MonsterPosition.x - HeroPosition.x;

            double y = MonsterPosition.y - HeroPosition.y;

            //MLResourceLoader.Instance.LoadAsset("UIPrefab/Logics/monster_arrow", typeof(MLImageComponent));

            if (!_monsterArrow.ContainsKey(Monster))

            {

                GameObject obj;

                if (IfMonster)

                {

                    obj = MLPrefabInstancePool<MLPrefabInstanceQueuePersistant>.Singleton.Instanciate(ArrowObject);

                }

                else

                {

                    obj = MLPrefabInstancePool<MLPrefabInstanceQueuePersistant>.Singleton.Instanciate(ParnterObject);

                }

                if(obj != null)

                {

                    MLImageComponent imageObj;

                    imageObj = obj.GetComponent<MLImageComponent>();

                    _monsterArrow.Add(Monster, imageObj);

                }

            }

            int HalfWidth = _mwindth / 2;

            int HalfHight = _mHight / 2;

            float MinusX = Math.Abs(MonsterPosition.x - HeroPosition.x);

            float MinusY = Math.Abs(MonsterPosition.y - HeroPosition.y);

        

            if(_monsterArrow[Monster] != null)

            {

                double a1 = Math.Atan(Math.Abs(y / x));

                Vector3 postion = new Vector3();

                Vector3 rot = Vector3.zero;

                //8象限计算

                if ((MonsterPosition.x >= HeroPosition.x) && MinusY < HalfHight)

                {

                    postion.x = _mwindth;

                    postion.y = MonsterPosition.y - HeroPosition.y;

                    rot.z = 0;

                }

                if((MonsterPosition.y > HeroPosition.y) &&(MinusX < HalfWidth))

                {

                    postion.x = MonsterPosition.x - HeroPosition.x;

                    postion.y = _mHight;

                    rot.z = 90;

                }

                if((MonsterPosition.x < HeroPosition.x) &&(MinusY < HalfHight))

                {

                    postion.x = 0 - _mwindth;

                    postion.y = MonsterPosition.y - HeroPosition.y;

                    rot.z = 180;

                }

                if((MonsterPosition.y < HeroPosition.y) && (MinusX < HalfWidth))

                {

                    postion.x = MonsterPosition.x - HeroPosition.x;

                    postion.y = 0 - _mHight;

                    rot.z = 270;

                }

                if(MonsterPosition.x > HeroPosition.x && MonsterPosition.y > HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)

                {

                    postion.x = _mwindth;

                    postion.y = _mHight;

                    rot.z = (float)((a1 * 180) / Math.PI);      //旋转角度

                }

                if(MonsterPosition.x < HeroPosition.x && MonsterPosition.y > HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)

                {

                    postion.x = 0 - _mwindth;

                    postion.y = _mHight;

                    rot.z = (float)((Math.PI - a1) * 180 / Math.PI);

                }

               

                if(MonsterPosition.x < HeroPosition.x && MonsterPosition.y < HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)

                {

                    postion.x = 0 - _mwindth;

                    postion.y = 0 - _mHight;

                    rot.z = (float)((Math.PI + a1) * 180 / Math.PI);

                }

                if(MonsterPosition.x > HeroPosition.x && MonsterPosition.y < HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)

                {

                    postion.x = _mwindth;

                    postion.y = 0 - _mHight;

                    rot.z = (float)((2 * Math.PI - a1) * 180 / Math.PI);

                }

                _monsterArrow[Monster].transform.parent = mEmptyPanel.Root.transform;

                _monsterArrow[Monster].transform.localPosition = postion;

                Vector3 Scale= new Vector3(0.75f,0.75f,0.75f);

                _monsterArrow[Monster].transform.localScale = Scale;

                _monsterArrow[Monster].SetLocalRotation(rot);

            }

       

        }


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  U3D prefabs