Entity

Abstract Node for common behavior between all entities.

Inherits : KinematicBody2D

Inherited by : Player, Ennemy

Constants

SELECT_CLOSEST = “closest”
One of existing targetting method.
This one will choose the closest ennemy to the player.

Signals

running
Signal emitted when the entity is moving.
This signal should be emitted from the _physics_process() function of the child class (not handled in this class).

Emitted by : Player._process() Received by : Shooter._on_running_timeout()

immobile
Signal emitted when the entity is not moving.
This signal should be emitted from the _physics_process() function of the child class (not handled in this class).

Emitted by : Player._process() Received by : Shooter._on_immobile_timeout()

Description

This is an Abstract Node, meaning it’s just a script (there is no scene associated to this script).

It is used to define common behavior between all entities to avoid code duplication.

Common behaviors handled by this Node are mainly :

  • Common properties and associated methods
  • Knockback
  • Fire projectile
  • Hit by projectile

Properties description

knockbacker_pos

Type Vector2
Default null
Position of the knockbacker.
When an entity is knockbacked, the knockbacker position is kept in memory and used later for knockback direction computations.

knockbacker_pow

Type int
Power of the knockbacker.
When an entity is knockbacked, the knockbacker power is kept in memory and used later for knockback direction computations.

knockback_frame

Type int
Default 0
Current frame of the knockback.
Used together with knockback_ratios in order to create a smooth knockback animation.

knockback_ratios

Type array of float
Default [9, 8, 6.5, 4.5, 2]
Speed ratios of the knockback.
Used to create a smooth knockback animation.

knockback_power

Type int
Default 100

Knockback power of this entity.

damage

Type int
Default 10

Knockback damages inflicted by this entity to other knockbacked entities.

destination

Type Vector2
Default null
Aimed position by the entity.
This is used to control to entity : it will go in the direction of destination.
For example, if the entity is located to (0, 0) and the destination is set to (1, 0), entity will move to the right.

target

Type WeakRef
Default null
Reference to the targetted entity.
Entity may be targetting nothing. The target is used when firing weapon : the bullet will be fired in the direction of the current target (and hopefully touch the target !).

speed

Type int
Default 100

Walking speed of the entity.

Methods description

set_destination(dest=null)

Arguments

dest
The new destination for this entity.
Default to null.
Method used to set a new destination (or update current destination) for this entity.
Destination is not updated if null is given.

reset_destination()

Reset the current destination to null.

get_base_velocity()

Return Vector2
Compute the base velocity of the entity, going in the direction of the destination using speed.
The computed velocity is returned.

knockback_from(collider)

Arguments

collider
The Entity colliding with the current entity.
Position and knockback_power of the collider is used to compute knockback.
Knockback the current entity. This method only save the value of knockbacker_pos and knockbacker_pow.
The knockback computations happen in apply_knockback().

apply_knockback(velocity)

Return Vector2

Arguments

velocity
Velocity to update with the knockback.

Compute the knockback velocity, based on :

is_targetting()

Return bool

In order to know if the entity is targetting something or not, this method should be called.

select_target(group_name, selection_method="closest")

Return float

Arguments

group_name
The name of the group from where to choose a new target.
selection_method
Selection method to use for choosing a new target.
Defaults to select_closest.
Select a new target among the given group of Entity.
For now only one selection method is implemented (select_closest), but later it will be possible to choose among other methods.
The value returned is the best value used to compare Entity of the group. For example for select_closest method, it is the negation of the distance (so the biggest, the closest).
If there is no Entity is the group, it return null.

fire(projectile, atk_speed)

Arguments

projectile
Instancied projectile to fire.
atk_speed
Attack speed to use to fire.

This method is used as a general method for firing a projectile. For specific behaviors, please refer to each Node.

This method compute the angle between the Entity and the target, then rotate the projectile to this direction and update the speed of the projectile to fire it.
It also rotate the Entity sprite to face the target when firing.

This method does nothing if no target is selected.

interrupt_shooting()

This method does nothing. It may be overwrited by child Nodes to define a behavior in case shooting should be interrupted. Here there is nothing to interrupt due to the current implementation of fire().
It is called by the shooter class.

hit_by(projectile)

Arguments

projectile
Projectile colliding with the Entity.

This method does 2 things :

  • Update the health bar of the entity based on the damage of the projectile.
  • Knockback the entity from the projectile.