Files
DefendNJ/Assets/_DefendNJ/Scripts/Agent.cs

200 lines
4.6 KiB
C#

using System.Reflection;
using System;
using System.Collections;
using System.Collections.Generic;
using DragonLi.Frame;
using Mirror;
using Pathfinding;
using Pathfinding.RVO;
using UnityEngine;
public class Agent : NetworkBehaviour, IDamagable
{
[Header("当前血量")]
#if UNITY_EDITOR
[DisplayOnly]
#endif
[SyncVar]
public float health = 0f;
[Header("原始血量")]
#if UNITY_EDITOR
[DisplayOnly]
#endif
[SyncVar]
public float originHealth = 0f;
/// <summary>
/// 血量
/// </summary>
public float Health
{
get
{
return health;
}
set
{
health = value;
health = Mathf.Clamp(health, 0f, originHealth);
OnHeathChanged(health);
}
}
/// <summary>
/// 是否存活
/// </summary>
public bool IsAlive => Health > 0f;
private Animator animatorComponent;
/// <summary>
/// 动画组件
/// </summary>
public Animator AnimatorComponent
{
get
{
if (animatorComponent == null)
{
animatorComponent = GetComponentInChildren<Animator>();
}
return animatorComponent;
}
}
private Collider colliderComponent;
/// <summary>
/// 碰撞组件
/// </summary>
public Collider ColliderComponent
{
get
{
if (colliderComponent == null)
{
colliderComponent = GetComponentInChildren<Collider>();
}
return colliderComponent;
}
}
private Rigidbody rigidbodyComponent;
/// <summary>
/// 刚体组件
/// </summary>
public Rigidbody RigidbodyComponent
{
get
{
if (rigidbodyComponent == null)
{
rigidbodyComponent = GetComponentInChildren<Rigidbody>();
}
return rigidbodyComponent;
}
}
// A* / RVO
private IAstarAI _ai;
public IAstarAI ai
{
get
{
if (_ai == null) _ai = GetComponent<IAstarAI>();
return _ai;
}
}
private AIPath _aiPath;
public AIPath aiPath
{
get
{
if (_aiPath == null) _aiPath = GetComponent<AIPath>();
return _aiPath;
}
}
private RVOController _rvoController;
public RVOController rvoController
{
get
{
if (_rvoController == null) _rvoController = GetComponent<RVOController>();
return _rvoController;
}
}
/// <summary>
/// This will be called every frame
/// </summary>
public virtual void OnUpdate() { }
/// <summary>
/// On entity enable, this will be called when entity was being spawned.
/// </summary>
[Server]
public virtual void OnSpawn()
{
OnBorn();
}
/// <summary>
/// When this entity has been born
/// </summary>
public virtual void OnBorn() { }
/// <summary>
/// When agent receives damage this function will be called
/// If you want to re calculate damage you can do it here.
/// </summary>
/// <param name="value">How much</param>
/// <param name="info">Some information about this damage you want to tell damage receiver</param>
/// <param name="_sender">Who take this damage</param>
/// <returns></returns>
public virtual float OnReceiveDamage(float value, object info, Transform _sender)
{
return value;
}
/// <summary>
/// When health changed this function will be called
/// </summary>
/// <param name="currentHealth">current health</param>
public virtual void OnHeathChanged(float currentHealth) { }
/// <summary>
/// Add damage
/// </summary>
/// <param name="value">How much</param>
/// <param name="info">Some information about this damage you want to tell damage receiver</param>
/// <param name="_sender">Who take this damage</param>
[Server]
public virtual void ApplyDamage(float value, object info, Transform _sender)
{
// Debug.Log("造成伤害" + value);
if (IsAlive)
{
Die(info, _sender);
Health -= OnReceiveDamage(value, info, _sender);
if (!IsAlive)
{
}
}
}
/// <summary>
/// Let this entity die, and tell task system.
/// </summary>
public virtual void Die(object info, Transform _sender)
{
OnDeath(info, _sender);
}
/// <summary>
/// On entity die, this will be called, you can write despawn functions here.
/// </summary>
public virtual void OnDeath(object info, Transform _sender) { }
}