Projectile

Abstract Node for common behavior between all projectiles.

Inherits : KinematicBody2D

Inherited by : Arrow, Bullet

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 projectiles to avoid code duplication.

Common behaviors handled by this Node are mainly :

  • Collision with Entities
  • Bouncing (or not) on walls
  • Projectile being stabbed into walls

Properties description

speed

Type int
Default 1000

Speed of the projectile.

bounce_nb

Type int
Default 0

Number of walls-bounce allowed.

remain_time

Type float
Default 0

Number of seconds the projectile stay stabbed into a wall before being freed.

damage

Type int
Default 25

Amount of damage inflicted when entity collide with this projectile.

knockback_power

Type int
Default 10

Knockback power of this projectile.

velocity

Type Vector2
Default (0, 0)

Current velocity of the projectile.

Methods description

init(s=1000, d=25, bn=0, rt=0, kp=10)

Arguments

s
The new speed of the created Projectile.
Default to 1000.
d
The new damage of the created Projectile.
Default to 25.
bn
The new bounce_nb of the created Projectile.
Default to 0.
rt
The new remain_time of the created Projectile.
Default to 0.
kp
The new knockback_power of the created Projectile.
Default to 10.

This method is used to initialize as we want a new projectile, instead of setting each property by hand.

physics_process()

Arguments

delta
Delta (see Godot documentation for more details).

Process the main physic of the projectile :

  • Move according to current velocity
  • If a collision with an entity happen, free this projectile and hit the entity.
  • If a collision with a wall happen, bounce, and later stay stabbed in the wall.

bounce(collision)

Return bool

Arguments

collider
Colliding object.
If the projectile can still bounce (bounce_nb is not exhausted yet), update the current velocity to account the bounce.
It returns true if the projectile is bounced, false if it cannot bounce anymore.

impact()

Stab the projectile into the wall (effectively immobolizing the projectile).
It starts a timer for remain_time seconds, which call _on_timer_timeout() when done.

_on_timer_timeout()

Method called when the projectile have been stabbed into the wall for remain_time seconds.
Simply free the projectile.

Receives signals

timeout
This signal is emitted when the timer (that waited for remain_time seconds) ends.