279 lines
5.0 KiB
C#
279 lines
5.0 KiB
C#
// ***********************************************************************
|
|
// Assembly : Game
|
|
// Author : Kimch
|
|
// Created : 2020-09-02
|
|
// Description :
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "Cha_Auto" company="Kimch"></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
namespace G
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class Cha_Auto : MonoBehaviour
|
|
{
|
|
[Flags]
|
|
public enum AutoType
|
|
{
|
|
None,
|
|
SelectTarget = 0x1,
|
|
AttackTarget = 0x2,
|
|
CastSkill = 0x4,
|
|
All = -1,
|
|
}
|
|
|
|
#region Field
|
|
|
|
/// <summary>
|
|
/// 自己选择目标
|
|
/// </summary>
|
|
public float selectTargetRange = 0.5f;
|
|
|
|
|
|
public Transform selectCenter;
|
|
public Transform selectCircle;
|
|
|
|
/// <summary>
|
|
/// 最近目标
|
|
/// </summary>
|
|
private Transform _nearestTarget;
|
|
/// <summary>
|
|
/// 性能考虑
|
|
/// </summary>
|
|
private float _lastUpdateTime;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private EntityMainPlayer _scriptCha;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private AutoType _autoType;
|
|
|
|
#endregion
|
|
|
|
#region Property
|
|
|
|
public Transform nearestTarget
|
|
{
|
|
get => _nearestTarget;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 自动攻击目标
|
|
/// </summary>
|
|
public bool autoAttackTarget
|
|
{
|
|
get => _scriptCha.autoAttack;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 自动施放技能
|
|
/// </summary>
|
|
public bool autoCastSkill => _scriptCha.autoCastSkill;
|
|
|
|
#endregion
|
|
|
|
#region Method
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="autoType"></param>
|
|
public void StartAuto(AutoType autoType = AutoType.All)
|
|
{
|
|
_autoType = autoType;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public void StopAuto()
|
|
{
|
|
_autoType = AutoType.None;
|
|
}
|
|
|
|
private void AutoSelectTarget()
|
|
{
|
|
if (selectTargetRange < float.Epsilon)
|
|
{
|
|
_nearestTarget = null;
|
|
return;
|
|
}
|
|
|
|
var minSM = selectTargetRange * selectTargetRange;
|
|
var chaP = (selectCenter ?? this.transform).position;
|
|
|
|
//优先选择boss
|
|
var bosses = EntityManager.Instance.FindAllBossInScene();
|
|
if (bosses != null && bosses.Count > 0)
|
|
{
|
|
foreach (var boss in bosses)
|
|
{
|
|
var tarT = boss.transform;
|
|
var curSM = Vector3.SqrMagnitude(chaP - tarT.position);
|
|
if (curSM < minSM)
|
|
{
|
|
minSM = curSM;
|
|
_nearestTarget = tarT;
|
|
}
|
|
}
|
|
}
|
|
|
|
//小怪
|
|
var minions = EntityManager.Instance.FindAllMinionInScene();
|
|
if (minions != null && minions.Count > 0)
|
|
{
|
|
foreach (var minion in minions)
|
|
{
|
|
if (minion.gameObject.layer == GameLayer.UnitLayer)
|
|
{
|
|
var tarT = minion.transform;
|
|
var curSM = Vector3.SqrMagnitude(chaP - tarT.position);
|
|
if (curSM < minSM)
|
|
{
|
|
minSM = curSM;
|
|
_nearestTarget = tarT;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//选择攻击目标
|
|
if (_nearestTarget)
|
|
{
|
|
_scriptCha.AutoSelectTarget(_nearestTarget);
|
|
return;
|
|
}
|
|
//道具
|
|
var props = EntityManager.Instance.FindAllPropInScene();
|
|
if (props != null && props.Count > 0)
|
|
{
|
|
foreach (var prop in props)
|
|
{
|
|
var tarT = prop.transform;
|
|
var curSM = Vector3.SqrMagnitude(chaP - tarT.position);
|
|
if (curSM < minSM)
|
|
{
|
|
minSM = curSM;
|
|
_nearestTarget = tarT;
|
|
}
|
|
}
|
|
}
|
|
//选择道具
|
|
if (_nearestTarget)
|
|
{
|
|
_scriptCha.AutoSelectProp(_nearestTarget);
|
|
_nearestTarget = null;
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private void AutoAttackTarget()
|
|
{
|
|
if (autoAttackTarget)
|
|
{
|
|
if (_nearestTarget)
|
|
_scriptCha.AutoAttackTarget(_nearestTarget);
|
|
else
|
|
_scriptCha.AutoFindPortal();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private void AutoCastSkill()
|
|
{
|
|
if (autoCastSkill && _nearestTarget)
|
|
{
|
|
_scriptCha.AutoCastSkill();
|
|
}
|
|
}
|
|
|
|
private void AutoStudySkill()
|
|
{
|
|
|
|
}
|
|
|
|
private void AutoOpenBox()
|
|
{
|
|
|
|
}
|
|
|
|
private void AutoFindPortal()
|
|
{
|
|
|
|
}
|
|
|
|
private void UpdateAuto()
|
|
{
|
|
if (GameLevel.Instance.levelFinish)
|
|
return;
|
|
|
|
if (_autoType.HasFlag(AutoType.SelectTarget))
|
|
AutoSelectTarget();
|
|
if (_autoType.HasFlag(AutoType.AttackTarget))
|
|
{
|
|
AutoAttackTarget();
|
|
AutoFindPortal();
|
|
}
|
|
if (_autoType.HasFlag(AutoType.CastSkill))
|
|
{
|
|
AutoCastSkill();
|
|
|
|
AutoStudySkill();
|
|
AutoOpenBox();
|
|
AutoFindPortal();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void Awake()
|
|
{
|
|
_scriptCha = GetComponent<EntityMainPlayer>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_autoType == AutoType.None)
|
|
return;
|
|
|
|
if (Time.time - _lastUpdateTime > 0.05f)
|
|
{
|
|
_lastUpdateTime = Time.time;
|
|
UpdateAuto();
|
|
}
|
|
|
|
if (_nearestTarget)
|
|
{
|
|
var tarP = _nearestTarget.position;
|
|
tarP.y = 0.01f;
|
|
selectCircle.transform.position = tarP;
|
|
|
|
var tarR = _nearestTarget.rotation.eulerAngles;
|
|
tarR.x = 90f;
|
|
tarR.z = 0f;
|
|
selectCircle.transform.eulerAngles = tarR;
|
|
}
|
|
else
|
|
{
|
|
selectCircle.transform.position = new Vector3(0, -100, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|