49 lines
1.5 KiB
GDScript
49 lines
1.5 KiB
GDScript
class_name SimpleLinearBehavior
|
|
extends BulletBehavior
|
|
## Makes bullets move in [member Bullet.direction], potentially accelerating.
|
|
|
|
|
|
## Initial speed of the bullet when it is spawned.
|
|
@export_custom(0, "suffix:px/s") var initial_speed: float = 0.0
|
|
|
|
## 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_")
|
|
@export var action_speed_clamped: BulletAction
|
|
|
|
|
|
var _bullet_data: Dictionary[Bullet, Data] = {}
|
|
|
|
|
|
func _init_bullet(bullet: Bullet) -> void:
|
|
var data = Data.new()
|
|
data.speed = initial_speed
|
|
_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]
|
|
data.speed += acceleration * delta
|
|
if not data.already_clamped and (data.speed <= min_speed or data.speed >= max_speed):
|
|
data.already_clamped = true
|
|
if action_speed_clamped:
|
|
action_speed_clamped.perform(bullet)
|
|
data.speed = clampf(data.speed, min_speed, max_speed)
|
|
bullet.position += bullet.direction * data.speed * delta
|
|
|
|
|
|
class Data:
|
|
var speed: float
|
|
var already_clamped: bool = false
|