Entity - StudioAspen/Project-Dreamscape-Aspen-2024-2025 GitHub Wiki

Entity Class Documentation

The Entity class serves as a foundational base for any character or object in the game that requires common functionalities, such as health management, ground detection, movement, state-based behaviors, and interactions like taking damage. Both enemies and players should inherit from this class to leverage its default behaviors and customization options. This class also includes built-in support for pooling entities via Unity's ObjectPool.

Class Inheritance and Purpose

  • Inheritance: All enemies and players should inherit from the Entity class.
  • Purpose: By inheriting from this class, derived objects gain access to default behaviors for handling damage, detecting the ground, managing levels and health, and using a state machine for managing behavior states.
  • Pooling: Entities can be managed through an ObjectPool, which optimizes memory and performance by reusing objects rather than creating/destroying them repeatedly.

Class Fields

  1. References

    private Animator animator;                       // Controls animation transitions for the entity.
    private GlobalPhysicsSettings physicsSettings;   // References global physics settings.
  2. Health & Level

    public int CurrentHealth;             // Tracks current health value.
    public int MaxHealth;                 // Tracks maximum health value.
    public int Level;                     // Represents experience or strength level.
  3. Movement & Detection

    public bool IsGrounded;               // Indicates if the entity is on the ground.
    protected float SpeedModifier;        // Multiplier for the entity's speed.
    protected float baseSpeed;            // Base movement speed.
    protected Vector3 velocity;           // Current velocity vector.
    protected float rotationSpeed;        // Speed at which the entity rotates.
    private float targetDetectionRadius;  // Detection radius for targets.
  4. Team Identification

    public int Team;  // Integer identifier for entity's team.
  5. Events

    public UnityEvent<Vector3, GameObject> OnEntityTakeDamage;   // Triggered when the entity takes damage, telling where the entity was hit and what the source was.
    public UnityEvent<GameObject> OnEntityDeath;                 // Invoked when the entity dies, telling who the killer was.
    public UnityEvent<Entity> OnKillEntity;                      // Triggered when this entity kills another, telling who the victim was

State Machine

The Entity class includes a state machine that manages transitions between various states:

  1. State Fields
    public BaseState CurrentState { get; private set; }       // Tracks the current state.
    public BaseState DefaultState { get; private set; }       // The default or idle state.
    protected EntityEmptyState EntityEmptyState;              // Default idle state.
    protected EntityHitState EntityHitState;                  // Active when the entity takes damage.
    protected EntityDeathState EntityDeathState;              // Active upon the entity's death.

Key Methods

  1. Initialization & Lifecycle Management

    protected virtual void OnAwake();         // Initializes components.
    protected virtual void OnStart();         // Sets the default state.
    protected virtual void OnOnEnable();      // Resets health and start state when enabled.
    protected virtual void OnOnDisable();     // Cleanup or reset actions when disabled.
  2. State Management

    protected virtual void InitializeStates();               // Instantiates state objects.
    protected void SetStartState(BaseState state);           // Defines the initial state.
    protected void SetDefaultState(BaseState state);         // Sets the default state.
    public void ChangeState(BaseState state);                // Switches between states.
  3. Health Management

    public virtual void TakeDamage(int dmg, Vector3 hitPoint, GameObject source);         // Handles damage and death.
    public virtual void TakeDamageWithoutState(int dmg, Vector3 hitPoint, GameObject source); // Applies damage without state change.
    public void Heal(int health);                         // Increases health by a specified amount.
    public void Kill();                                   // Forcefully reduces health to zero.
  4. Movement & Rotation

    public virtual void LookAt(Vector3 target);           // Rotates to face a target.
    public void SetSpeedModifier(float speed);            // Adjusts speed multiplier.
  5. Target Detection

    public List<Entity> GetNearbyTargets();               // Returns a list of nearby entities.
    public List<Entity> GetNearbyEntities(float radius);  // Returns entities within radius.
    public List<T> GetNearbyEntitiesByType<T>(float radius) where T : Entity; // Returns entities of specific type.
    public bool IsBlockedFromEntity(Entity entity);       // Checks if there's an obstruction.
  6. Pooling Support

    public void SetObjectPool(ObjectPool<GameObject> objectPool);    // Assigns the entity to a pool.
    public virtual void Die();                                       // Releases to pool or destroys if unpooled.
⚠️ **GitHub.com Fallback** ⚠️