Eye of the Nile Docs
Everything you need to know to get started!
Loading...
Searching...
No Matches
BaseEntityController Class Referenceabstract

Detailed Description

Basic functionality for any entities that are hostile towards other entities. This script is abstract so it must be inherited by another script to be used. Every script inheriting from this must override ActivateAttack().

  • Patrol State (default): The entity will patrol an area (PatrolZone.cs) until an enemy (something on the enemyLayers) is within range.
  • Chase State: The entity will chase the enemy it detected until it's close enough to attack.
  • CloseAttack State: The entity will attack the enemy until it's no longer in range (either because it left or because it died)
  • Dead State: This script won't do anything if the entity dies.

Documentation updated 11/9/2024

Author
Roy Rapscual, Stephen Nuttal

Public Member Functions

void ChangeSpeed (float speedChange)
 Speeds up or slows down the entity's movement speed. A negative value for speedChange will slow down the entity.
 
void ChangeSpeed (float speedChange, float duration)
 Temporarily speeds up or slows down the entity's movement speed. A negative value for speedChange will slow down the entity.
 
void DestroyOnDeath ()
 Can be optionaly run by ObjectHealth.onDeath.
 

Protected Member Functions

void ActivateAttack ()
 Triggered by an event in the attack animation (or you can override TriggerAttack() if there is not attack animation). This function muse be overridden by any inheriting class.
 
virtual void TriggerAttack ()
 Triggers the entity's attack. This can be done through triggering an animation, spawning a projectile, or another method. By default, an "IsAttacking" animation is triggered.
 
virtual void PatrolState ()
 When the entity hasn't detected any enemies...
 
virtual void ChaseState ()
 If the entity has detected an enemy...
 
virtual void CloseAttackState ()
 If the entity is close enough to an enemy to attack...
 

Protected Attributes

ObjectHealth objectHealth
 Reference to the object responsible for managing the player's health.
 
Transform attackPoint
 Reference to the entity's attack point. It's a point in space that's a child of the entity, existing some distance in front of it. Projectiles spawn from the attack point, and melee attacks scan for enemies to damage from a certain radius around it.
 
int attackDamage = 30
 Amount of damage the entity's attack will deal.
 
float attackCooldown = 0.8f
 The amount of time between attacks.
 
float cooldownEndTime = 0f
 Amount of time until cooldown is over. Set to the current time + attackCooldown when the attack is triggered.
 
LayerMask enemyLayers
 Objects on these layers will be considered an enemy of this entity, and if detected, this entity will seek to attack. An object can be assigned to a layer in the Unity Editor from a drop down menu in the top right.
 
PatrolZone patrolZone
 A patrol zone is an object that has two points the entity will walk between if it does not detect an enemy.
 
float horizontalDirection = 0f
 Horizontal direction the entity is traveling in.
 
float detectionRange = 6f
 How far away the entity will detect an enemy from and start chasing.
 
float activateAttackRange = 3f
 How close the entity must be to an enemy to attack it.
 
float entityCenter = 1.4f
 
bool hostileDetected = false
 True if the entity has detected an enemy.
 
bool hostileInCloseRange = false
 True if the entity is close enough to an enemy to attack it.
 
Rigidbody2D rb
 Reference to the entity's rigidbody.
 
float moveVelocity = 6.0f
 How fast the entity should move.
 
float speedModifier = 0f
 The precent of moveVelocity the entity should move at. Used for slowing the entity down.
 
float linearDrag = 1.0f
 Amount of drag to apply to the rigidbody.
 
float groundedRaycastLength = 1.8f
 
GroundDetector groundDetector
 Reference to the entity's GroundDetector.
 
bool groundNeeded = true
 Can be set to false to let the entity walk even when it is not on the ground.
 
bool canWalk = true
 Used for disabling entity movement.
 
Animator animator
 Reference to the entity's animator.
 

Properties

EntityState EState = EntityState.Patrol [get, protected set]
 Current state the entity is in.
 

Private Member Functions

void Start ()
 Apply linear drag to the rigidbody.
 
void Update ()
 Activate the logic for the current state for this frame, based on EState.
 
void OnDrawGizmosSelected ()
 Displays radiuses of the detection and attack circles in the Unity Editor, specifically the scene view. This allows developers to see how far away an entity can see an enemy and how far way they will attack the enemy.
 
void Awake ()
 Set references to rigidbody, animator, object health, and GroundDetector.
 
IEnumerator ChangeSpeedClock (float speedChange, float duration)
 Clock for ChangeSpeed(). See ChangeSpeed() for details.
 

Member Function Documentation

◆ ActivateAttack()

void BaseEntityController.ActivateAttack ( )
abstractprotected

Triggered by an event in the attack animation (or you can override TriggerAttack() if there is not attack animation). This function muse be overridden by any inheriting class.

◆ Awake()

void BaseEntityController.Awake ( )
private

Set references to rigidbody, animator, object health, and GroundDetector.

◆ ChangeSpeed() [1/2]

void BaseEntityController.ChangeSpeed ( float speedChange)

Speeds up or slows down the entity's movement speed. A negative value for speedChange will slow down the entity.

Note
If speedChange is less than negative moveVelocity (implying the entity should move backwards), it will be set to -moveVelocity.
Parameters
speedChangeThe amount that will be added to speedModifier, which will be added to moveVelocity.

◆ ChangeSpeed() [2/2]

void BaseEntityController.ChangeSpeed ( float speedChange,
float duration )

Temporarily speeds up or slows down the entity's movement speed. A negative value for speedChange will slow down the entity.

Note
If speedChange is less than negative moveVelocity (implying the entity should move backwards), it will be set to -moveVelocity.
Parameters
speedChangeThe amount that will be added to speedModifier, which will be added to moveVelocity.
durationThe amount of time, in seconds, the speed change will last for.

◆ ChangeSpeedClock()

IEnumerator BaseEntityController.ChangeSpeedClock ( float speedChange,
float duration )
private

Clock for ChangeSpeed(). See ChangeSpeed() for details.

◆ ChaseState()

virtual void BaseEntityController.ChaseState ( )
protectedvirtual

If the entity has detected an enemy...

  1. Scan for enemies within the detection range.
  2. Scan for enemies close enough to attack.
  3. Face the entity towards the enemy and flip the sprite to face the new direction.
  4. Run movement animation and set velocity to proper direction and speed.
  5. Evaluate state
    • If the entity has died, set state to Dead.
    • If an enemy is close to attack, set state to CloseAttack
    • If there's no longer an enemy within the detection range, set state to Patrol
    • Otherwise, don't change state. Allow Chase state to continue.
Precondition
The entity has detected an enemy in its detection range.

Reimplemented in RockGolemController.

◆ CloseAttackState()

virtual void BaseEntityController.CloseAttackState ( )
protectedvirtual

If the entity is close enough to an enemy to attack...

  1. Scan for enemies close enough to attack.
  2. If an enemy is close enough, face the entity towards it and trigger attack.
  3. Stop movement animation if it's still happening.
  4. Evaluate state
    • If the entity has died, set state to Dead.
    • If no enemies are close enough to attack, set state to Chase.
    • Otherwise, don't change state. Allow CloseAttack state to continue.
Precondition
The entity is close enough to an enemy to attack it.

Reimplemented in RangedEntityController.

◆ DestroyOnDeath()

void BaseEntityController.DestroyOnDeath ( )

Can be optionaly run by ObjectHealth.onDeath.

◆ OnDrawGizmosSelected()

void BaseEntityController.OnDrawGizmosSelected ( )
private

Displays radiuses of the detection and attack circles in the Unity Editor, specifically the scene view. This allows developers to see how far away an entity can see an enemy and how far way they will attack the enemy.

Important
Must be commented out or removed to export the game. Otherwise, Unity will throw compiler errors.

◆ PatrolState()

virtual void BaseEntityController.PatrolState ( )
protectedvirtual

When the entity hasn't detected any enemies...

  1. Move towards one of the patrol zone's points. If the entity reaches one, go to the other one.
  2. Flip the sprite to face the direction the entity is facing.
  3. Run movement animation and set velocity to proper direction and speed.
  4. Scan for enemies within the detection range.
  5. Evaluate state
    • If the entity has died, set state to Dead.
    • If an enemy was detected, set state to Chase.
    • Otherwise, don't change state. Allow Patrol state to continue.
Precondition
Active when there are no objects on enemyLayers within detectionRange.

◆ Start()

void BaseEntityController.Start ( )
private

Apply linear drag to the rigidbody.

◆ TriggerAttack()

virtual void BaseEntityController.TriggerAttack ( )
protectedvirtual

Triggers the entity's attack. This can be done through triggering an animation, spawning a projectile, or another method. By default, an "IsAttacking" animation is triggered.

Reimplemented in MeleeEntityController, and RangedEntityController.

◆ Update()

void BaseEntityController.Update ( )
private

Activate the logic for the current state for this frame, based on EState.

Member Data Documentation

◆ activateAttackRange

float BaseEntityController.activateAttackRange = 3f
protected

How close the entity must be to an enemy to attack it.

◆ animator

Animator BaseEntityController.animator
protected

Reference to the entity's animator.

◆ attackCooldown

float BaseEntityController.attackCooldown = 0.8f
protected

The amount of time between attacks.

◆ attackDamage

int BaseEntityController.attackDamage = 30
protected

Amount of damage the entity's attack will deal.

◆ attackPoint

Transform BaseEntityController.attackPoint
protected

Reference to the entity's attack point. It's a point in space that's a child of the entity, existing some distance in front of it. Projectiles spawn from the attack point, and melee attacks scan for enemies to damage from a certain radius around it.

◆ canWalk

bool BaseEntityController.canWalk = true
protected

Used for disabling entity movement.

◆ cooldownEndTime

float BaseEntityController.cooldownEndTime = 0f
protected

Amount of time until cooldown is over. Set to the current time + attackCooldown when the attack is triggered.

◆ detectionRange

float BaseEntityController.detectionRange = 6f
protected

How far away the entity will detect an enemy from and start chasing.

◆ enemyLayers

LayerMask BaseEntityController.enemyLayers
protected

Objects on these layers will be considered an enemy of this entity, and if detected, this entity will seek to attack. An object can be assigned to a layer in the Unity Editor from a drop down menu in the top right.

◆ entityCenter

float BaseEntityController.entityCenter = 1.4f
protected

◆ groundDetector

GroundDetector BaseEntityController.groundDetector
protected

Reference to the entity's GroundDetector.

◆ groundedRaycastLength

float BaseEntityController.groundedRaycastLength = 1.8f
protected
Deprecated
Unused variable that was going to allow entities to jump, based on how player jumping used to work. This system has been replaced with a new system in PlayerMovement though, so that system should be implemented instead.
Todo
Implement jumping for entities.

◆ groundNeeded

bool BaseEntityController.groundNeeded = true
protected

Can be set to false to let the entity walk even when it is not on the ground.

◆ horizontalDirection

float BaseEntityController.horizontalDirection = 0f
protected

Horizontal direction the entity is traveling in.

◆ hostileDetected

bool BaseEntityController.hostileDetected = false
protected

True if the entity has detected an enemy.

◆ hostileInCloseRange

bool BaseEntityController.hostileInCloseRange = false
protected

True if the entity is close enough to an enemy to attack it.

◆ linearDrag

float BaseEntityController.linearDrag = 1.0f
protected

Amount of drag to apply to the rigidbody.

◆ moveVelocity

float BaseEntityController.moveVelocity = 6.0f
protected

How fast the entity should move.

◆ objectHealth

ObjectHealth BaseEntityController.objectHealth
protected

Reference to the object responsible for managing the player's health.

◆ patrolZone

PatrolZone BaseEntityController.patrolZone
protected

A patrol zone is an object that has two points the entity will walk between if it does not detect an enemy.

◆ rb

Rigidbody2D BaseEntityController.rb
protected

Reference to the entity's rigidbody.

◆ speedModifier

float BaseEntityController.speedModifier = 0f
protected

The precent of moveVelocity the entity should move at. Used for slowing the entity down.

Property Documentation

◆ EState

EntityState BaseEntityController.EState = EntityState.Patrol
getprotected set

Current state the entity is in.


The documentation for this class was generated from the following file: