Files
valheim/Assets/_Valheim/Scripts/Agent/Entity.cs
2025-07-04 14:16:14 +08:00

134 lines
2.5 KiB
C#

using System.Collections.Generic;
using DragonLi.Core;
using Mirror;
using UnityEngine;
/// <summary>
/// Base class of all Objects
/// </summary>
public abstract class Entity : NetworkBehaviour
{
/// <summary>
/// Some default value of rain entity
/// </summary>
public class Default
{
/// <summary>
/// Transform of this entity
/// </summary>
public const string kControllerKey = "controller";
}
/// <summary>
/// Data of this Rain entity
/// </summary>
[SerializeField]
[HideInInspector]
public DataContainer Data;
private Animator animatorComponent;
/// <summary>
/// Animator of this entity
/// </summary>
public Animator AnimatorComponent
{
get
{
if (animatorComponent == null)
{
animatorComponent = GetComponentInChildren<Animator>();
}
return animatorComponent;
}
}
private Collider colliderComponent;
/// <summary>
/// Collider of this component
/// </summary>
public Collider ColliderComponent
{
get
{
if (colliderComponent == null)
{
colliderComponent = GetComponentInChildren<Collider>();
}
return colliderComponent;
}
}
private Rigidbody rigidbodyComponent;
/// <summary>
/// Rigidbody of this entity
/// </summary>
public Rigidbody RigidbodyComponent
{
get
{
if (rigidbodyComponent == null)
{
rigidbodyComponent = GetComponentInChildren<Rigidbody>();
}
return rigidbodyComponent;
}
}
public virtual void Awake()
{
InitData();
OnAwake();
}
public virtual void Update()
{
OnUpdate();
}
public virtual void OnEnable()
{
OnEntityEnable();
}
/// <summary>
/// This will be called when Awake() was called.
/// </summary>
public virtual void OnAwake() { }
/// <summary>
/// This will be called when entity is Enable
/// </summary>
public virtual void OnEntityEnable() { }
/// <summary>
/// This will be called every frame
/// </summary>
public virtual void OnUpdate() { }
/// <summary>
/// This will be called when DeliverMessage has been called by others
/// </summary>
public virtual void OnReceiveMessage(string msg, object info) { }
public virtual void InitData() { }
/// <summary>
/// Convert this to target type
/// </summary>
/// <typeparam name="T">target type</typeparam>
/// <returns></returns>
public T As<T>() where T : Entity
{
return this as T;
}
/// <summary>
/// Deliver message into this entity.
/// </summary>
/// <param name="msg"></param>
public void DeliverMessage(string msg, object info)
{
OnReceiveMessage(msg, info);
}
}