Files
XMen/Assets/Scripts/Entity.cs
2025-07-10 14:49:53 +08:00

164 lines
2.8 KiB
C#

using DragonLi.Core;
using Mirror;
using System.Collections;
using System.Collections.Generic;
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>
/// Should all entity update?
/// </summary>
public static bool ShouldUpdate = true;
/// <summary>
/// Data of this Rain entity
/// </summary>
[SerializeField]
[HideInInspector]
public DataContainer Data;
/// <summary>
/// Will this entity update self?
/// </summary>
[SerializeField]
[HideInInspector]
protected bool updateSelf = true;
private Animator animator;
private Collider colliderComponent;
private Rigidbody rigidbodyComponent;
/// <summary>
/// Animator of this entity
/// </summary>
public Animator AnimatorComponent
{
get
{
if (animator == null)
{
animator = GetComponentInChildren<Animator>();
}
return animator;
}
}
/// <summary>
/// Collider of this component
/// </summary>
public Collider ColliderComponent
{
get
{
if (colliderComponent == null)
{
colliderComponent = GetComponentInChildren<Collider>();
}
return colliderComponent;
}
}
/// <summary>
/// Rigidbody of this entity
/// </summary>
public Rigidbody RigidbodyComponent
{
get
{
if (rigidbodyComponent == null)
{
rigidbodyComponent = GetComponentInChildren<Rigidbody>();
}
return rigidbodyComponent;
}
}
public virtual void Awake()
{
InitData();
OnAwake();
}
internal virtual void Update()
{
if (ShouldUpdate && updateSelf)
{
OnUpdate();
}
}
public virtual void OnEnable()
{
OnEntityEnable();
}
/// <summary>
/// This will be called when Awake() was called.
/// </summary>
protected virtual void OnAwake()
{
}
/// <summary>
/// This will be called when entity is Enable
/// </summary>
protected virtual void OnEntityEnable()
{
}
/// <summary>
/// This will be called every frame
/// </summary>
protected virtual void OnUpdate()
{
}
/// <summary>
/// This will be called when DeliverMessage has been called by others
/// </summary>
protected 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);
}
}