paratate/systems/bullets/bullet_behaviors/linear_motion_behavior.gd

46 lines
1.4 KiB
GDScript

class_name LinearMotionBehavior
extends BulletBehavior
## Makes bullets move in [member Bullet.direction], potentially accelerating.
## Rate at which the bullet will speed up.
@export_custom(0, "suffix:px/s²") var acceleration: float = 0.0
## Speed will be clamped between [member min_speed] and [member max_speed].
@export_custom(0, "suffix:px/s") var min_speed: float = -INF
## Speed will be clamped between [member min_speed] and [member max_speed].
@export_custom(0, "suffix:px/s") var max_speed: float = INF
@export_group("Actions", "action_")
## Performed when the bullet reaches either [member min_speed] or
## [member max_speed] for the first time.
@export var action_speed_clamped: BulletAction
var _bullet_data: Dictionary[Bullet, Data] = {}
func _init_bullet(bullet: Bullet) -> void:
var data = Data.new()
_bullet_data[bullet] = data
func _deinit_bullet(bullet: Bullet) -> void:
_bullet_data.erase(bullet)
func _process_bullet(bullet: Bullet, delta: float) -> void:
if not _bullet_data.has(bullet):
return
var data = _bullet_data[bullet]
bullet.speed += acceleration * delta
if not data.already_clamped and (bullet.speed <= min_speed or bullet.speed >= max_speed):
data.already_clamped = true
if action_speed_clamped:
action_speed_clamped.perform(bullet)
bullet.speed = clampf(bullet.speed, min_speed, max_speed)
bullet.position += bullet.direction * bullet.speed * delta
class Data:
var already_clamped: bool = false