bullets bullets bullets bullets

This commit is contained in:
Haze Weathers 2025-12-31 14:09:06 -06:00
parent 846040c0e4
commit 55059788b2
25 changed files with 139 additions and 91 deletions

View file

@ -0,0 +1,57 @@
@abstract
class_name BulletBehavior
extends Resource
@export_group("Actions", "action_")
## Delay in seconds before performing [member action_on_delay].
@export_range(0.0, 1.0, 0.01, "or_greater", "suffix:s") var action_delay: float = 0.0
## Performed once the bullet has existed for [member action_delay] seconds.
@export var action_on_delay: BulletAction
## Interval in seconds at which [member action_on_interval] is performed.
@export_range(0.0, 1.0, 0.01, "or_greater", "suffix:s") var action_interval: float = 0.0
## Performed every [member action_interval] seconds.
@export var action_on_interval: BulletAction
var _bullet_interval_cooldowns: Dictionary[Bullet, float] = {}
## Performs behavior-specific initialization for a [Bullet].
func init_bullet(bullet: Bullet) -> void:
if action_on_interval:
_bullet_interval_cooldowns[bullet] = action_interval
bullet.recycled.connect(deinit_bullet.bind(bullet), CONNECT_ONE_SHOT)
_init_bullet(bullet)
## Cleans up behavior-specific state for a [Bullet].
func deinit_bullet(bullet: Bullet) -> void:
_bullet_interval_cooldowns.erase(bullet)
_deinit_bullet(bullet)
## Processes one tick of a [Bullet]'s movement.
func process_bullet(bullet: Bullet, delta: float) -> void:
var last_time = bullet.time_elapsed
bullet.time_elapsed += delta
if last_time < action_delay and bullet.time_elapsed >= action_delay and action_on_delay:
action_on_delay.perform(bullet)
if action_on_interval:
_bullet_interval_cooldowns[bullet] -= delta
if _bullet_interval_cooldowns[bullet] <= 0.0:
_bullet_interval_cooldowns[bullet] += action_interval
_process_bullet(bullet, delta)
## Called when a [Bullet] is spawned with this behavior in order to set up
## behavior-specific state.
@abstract func _init_bullet(bullet: Bullet) -> void
## Called when a [Bullet] is recycled in order to clean up behavior-specific state.
@abstract func _deinit_bullet(bullet: Bullet) -> void
## Called to process a tick of a [Bullet]'s movement.
@abstract func _process_bullet(bullet: Bullet, delta: float) -> void

View file

@ -0,0 +1 @@
uid://djcajenyac4sd

View file

@ -0,0 +1,51 @@
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_")
## 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()
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

View file

@ -0,0 +1 @@
uid://dntp60my5f65m