further elaboration on bullets
This commit is contained in:
parent
da9111bcf2
commit
393c3b670a
16 changed files with 218 additions and 29 deletions
|
|
@ -3,10 +3,30 @@ class_name BulletBehavior
|
|||
extends Resource
|
||||
|
||||
|
||||
## Called when a bullet is spawned with this behavior in order to set up
|
||||
## behavior-specific state.
|
||||
@warning_ignore("unused_parameter")
|
||||
func init_bullet(bullet: Bullet) -> void: pass
|
||||
## Performs behavior-specific initialization for a [Bullet].
|
||||
func init_bullet(bullet: Bullet) -> void:
|
||||
bullet.recycled.connect(deinit_bullet.bind(bullet), CONNECT_ONE_SHOT)
|
||||
_init_bullet(bullet)
|
||||
|
||||
## Called to process a tick of a bullet's movement.
|
||||
@abstract func process_bullet(bullet: Bullet, delta: float) -> void
|
||||
|
||||
## Cleans up behavior-specific state for a [Bullet].
|
||||
func deinit_bullet(bullet: Bullet) -> void:
|
||||
_deinit_bullet(bullet)
|
||||
|
||||
|
||||
## Processes one tick of a [Bullet]'s movement.
|
||||
func process_bullet(bullet: Bullet, delta: float) -> void:
|
||||
_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
|
||||
|
|
|
|||
|
|
@ -9,7 +9,41 @@ extends BulletBehavior
|
|||
## 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
|
||||
|
||||
func process_bullet(bullet: Bullet, delta: float) -> void:
|
||||
var speed = initial_speed + acceleration * bullet.time_elapsed
|
||||
bullet.position += bullet.direction * speed * delta
|
||||
@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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue