123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using Adon.Game.BO;
- using AdonGameKit;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Adon.Game.Helper
- {
- /// <summary>
- /// 玩家帮助类
- /// </summary>
- public static class PlayerHelperAdon
- {
- /// <summary>
- /// 攻击范围内获取最近怪物的距离
- /// </summary>
- /// <param name="attacker">攻击者</param>
- /// <param name="searchRange">搜索范围</param>
- /// <param name="mob"></param>
- /// <returns></returns>
- public static float GetNearEnemyDistance(BaseHero attacker, float searchRange, out GameObject mob)
- {
- //Collider[] monsters = Physics.OverlapSphere(attacker.transform.position, searchRange, 1 << LayerMask.NameToLayer("Enemy"));
- List<Role> monsters = RoleManager.Instance.GetLiveMonsters();
- float num = -1f;
- mob = null;
- if (monsters.Count > 0)
- {
- for (int i = 0; i < monsters.Count; i++)
- {
- if (monsters[i] != null && monsters[i].isDie == false)
- {
- float distance = GetDistance(attacker.gameObject, monsters[i].gameObject);
- if (distance <= searchRange)
- {
- if (num < 0f)
- {
- num = distance;
- mob = monsters[i].gameObject;
- }
- else if (distance < num)
- {
- num = distance;
- mob = monsters[i].gameObject;
- }
- }
- }
- }
- }
- return num;
- }
- /// <summary>
- /// 攻击范围内怪物列表
- /// </summary>
- /// <param name="attacker">攻击者</param>
- /// <param name="searchRange">搜索范围</param>
- /// <param name="mob"></param>
- /// <returns></returns>
- public static List<GameObject> GetNearEnemysDistance(GameObject attacker, float searchRange)
- {
- List<Role> monsters = RoleManager.Instance.GetLiveMonsters();
- float num = -1f;
- List<GameObject> mobs = new List<GameObject>();
- if (monsters.Count > 0)
- {
- for (int i = 0; i < monsters.Count; i++)
- {
- if (monsters[i] != null && monsters[i].mData.HP_Final > 0)
- {
- float distance = GetDistance(attacker.gameObject, monsters[i].gameObject);
- if (distance <= searchRange)
- {
- if (num < 0f)
- {
- num = distance;
- mobs.Add(monsters[i].gameObject);
- }
- else if (distance < num)
- {
- num = distance;
- mobs.Add(monsters[i].gameObject);
- }
- }
- }
- }
- }
- return mobs;
- }
- public static List<GameObject> GetAllEnemysDistance(GameObject attacker, float searchRange)
- {
- List<Role> monsters = RoleManager.Instance.GetLiveMonsters();
- List<GameObject> mobs = new List<GameObject>();
- if (monsters.Count > 0)
- {
- for (int i = 0; i < monsters.Count; i++)
- {
- if (monsters[i] != null && monsters[i].mData.HP_Final > 0)
- {
- float distance = GetDistance(attacker.gameObject, monsters[i].gameObject);
- if (distance <= searchRange)
- {
- mobs.Add(monsters[i].gameObject);
- }
- }
- }
- }
- return mobs;
- }
- /// <summary>
- /// 获取范围内敌人
- /// </summary>
- /// <param name="searchRange">搜寻范围</param>
- /// <param name="detectionAngle">检测角度</param>
- /// <param name="hero">英雄对象</param>
- /// <returns></returns>
- public static List<GameObject> GetNearMonsters(float searchRange, float detectionAngle, GameObject hero)
- {
- List<GameObject> monsters = new List<GameObject>();
- if (RoleManager.Instance.GetLiveMonsters().Count > 0)//存在的怪
- {
- for (int i = 0; i < RoleManager.Instance.GetLiveMonsters().Count; i++)//遍历列表
- {
- GameObject monsterObj = RoleManager.Instance.GetLiveMonsters()[i].gameObject;
- float distance = GetDistance(hero, monsterObj);//怪物与英雄的距离
- if (distance <= searchRange)//距离小于等于搜寻范围
- {
- Vector3 eyePos = hero.transform.position;
- Vector3 toMonster = monsterObj.transform.position - eyePos;//向量
- if (Vector3.Dot(toMonster.normalized, hero.transform.forward) > Mathf.Cos(detectionAngle * 0.5f * Mathf.Deg2Rad))
- {
- Debug.DrawRay(eyePos, toMonster, Color.blue);//绘制视线
- monsters.Add(monsterObj);
- }
- }
- }
- }
- return monsters;
- }
- /// <summary>
- /// 获取怪物周围的英雄
- /// </summary>
- /// <param name="searchRange">搜寻范围</param>
- /// <param name="mob">怪物对象</param>
- /// <returns></returns>
- public static List<GameObject> GetNearEnemyHero(float searchRange, float detectionAngle, GameObject mob)
- {
- List<GameObject> heros = new List<GameObject>();
- if (RoleManager.Instance.GetPlayer() != null)
- {
- GameObject heroObj = RoleManager.Instance.GetPlayer().gameObject;
- float distance = GetDistance(mob, heroObj);//怪物与英雄的距离
- if (distance <= searchRange)//距离小于等于搜寻范围
- {
- Vector3 eyePos = mob.transform.position;
- Vector3 toPlayer = heroObj.transform.position - eyePos;//向量
- if (Vector3.Dot(toPlayer.normalized, mob.transform.forward) > Mathf.Cos(detectionAngle * 0.5f * Mathf.Deg2Rad))
- {
- Debug.DrawRay(eyePos, toPlayer, Color.blue);//绘制视线
- heros.Add(heroObj);
- }
- }
- }
- if (heros.Count > 0)
- {
- return heros;
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 获取敌人近身位置
- /// </summary>
- /// <param name="src">自身对象</param>
- /// <param name="enemy">敌人对象</param>
- /// <param name="range">范围</param>
- /// <param name="angle">角度</param>
- /// <returns></returns>
- public static Vector3 GetEnemyNearPosition(GameObject src, GameObject enemy, float range, float angle)
- {
- Vector3 vector = enemy.transform.position - src.transform.position;//目标向量
- vector.Normalize();
- vector = vector * range;
- return Quaternion.AngleAxis(angle, Vector3.up) * vector + enemy.transform.position;
- }
- /// <summary>
- /// 获取对象之间的距离
- /// </summary>
- /// <param name="src"></param>
- /// <param name="target"></param>
- /// <returns></returns>
- public static float GetDistance(GameObject src, GameObject target)
- {
- return Vector3.Distance(src.transform.position, target.transform.position);
- }
- //internal static float GetNearEnemyDistance(BaseCharacterAdon m_BaseCharacterAdon, float m_AutoAttackRange, out GameObject targetMonster)
- //{
- // throw new NotImplementedException();
- //}
- /// <summary>
- /// 计算夹角的角度 0~360
- /// </summary>
- /// <param name="from_"></param>
- /// <param name="to_"></param>
- /// <returns></returns>
- public static float Angle_360(Vector3 from_, Vector3 to_)
- {
- Vector3 v3 = Vector3.Cross(from_, to_);
- if (v3.y > 0)
- return Vector3.Angle(from_, to_);
- else
- return 360 - Vector3.Angle(from_, to_);
- }
- }
- }
|