bullets bullets bullets bullets
This commit is contained in:
parent
846040c0e4
commit
55059788b2
25 changed files with 139 additions and 91 deletions
|
|
@ -1,32 +0,0 @@
|
|||
@abstract
|
||||
class_name BulletBehavior
|
||||
extends Resource
|
||||
|
||||
|
||||
## 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)
|
||||
|
||||
|
||||
## 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
|
||||
|
|
@ -97,8 +97,6 @@ func _enter_tree() -> void:
|
|||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if not Engine.is_editor_hint():
|
||||
time_elapsed += delta
|
||||
|
||||
if face_direction:
|
||||
rotation = direction.angle()
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ extends BulletAction
|
|||
|
||||
|
||||
## [BulletAction]s to perform when triggered.
|
||||
@export var actions: Array[BulletAction]
|
||||
@export var actions: Array[BulletAction] = []
|
||||
|
||||
|
||||
func _perform(bullet: Bullet) -> void:
|
||||
|
|
@ -21,6 +21,10 @@ func _perform(bullet: Bullet) -> void:
|
|||
bullet_set.preset = preset
|
||||
bullet_set.global_position = bullet.global_position
|
||||
if local_coords:
|
||||
if match_direction:
|
||||
bullet_set.rotation = bullet.rotation
|
||||
bullet.get_parent().add_child(bullet_set)
|
||||
else:
|
||||
if match_direction:
|
||||
bullet_set.global_rotation = bullet.global_rotation
|
||||
bullet.get_parent().get_parent().add_child(bullet_set)
|
||||
57
systems/bullets/bullet_behaviors/bullet_behavior.gd
Normal file
57
systems/bullets/bullet_behaviors/bullet_behavior.gd
Normal 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
|
||||
|
|
@ -15,6 +15,8 @@ extends BulletBehavior
|
|||
@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
|
||||
|
||||
|
||||
|
|
@ -1,16 +1,17 @@
|
|||
class_name BulletPreset
|
||||
extends Resource
|
||||
|
||||
|
||||
@export_group("Bullets")
|
||||
@export var behavior: BulletBehavior = null
|
||||
|
||||
@export var pattern: BulletSpawnPattern = null
|
||||
|
||||
@export var textures: Array[Texture2D] = []
|
||||
|
||||
@export var hitbox_size: Vector2i = Vector2i.ZERO
|
||||
|
||||
@export var face_direction: bool = false
|
||||
|
||||
@export_group("Spawning")
|
||||
@export var pattern: BulletSpawnPattern = null
|
||||
@export var rounds: int = 1
|
||||
|
||||
@export var round_delay: float = 0.0
|
||||
|
||||
@export_group("Bullet Set")
|
||||
@export var set_behavior: BulletSetBehavior = null
|
||||
|
|
|
|||
|
|
@ -5,6 +5,10 @@ extends Node2D
|
|||
@export var preset: BulletPreset
|
||||
|
||||
|
||||
var spawn_offset: Vector2 = Vector2.ZERO
|
||||
var spawn_rotation: float = 0.0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
for _n in preset.rounds:
|
||||
preset.pattern.spawn_bullets(self, preset)
|
||||
|
|
@ -14,6 +18,10 @@ func _ready() -> void:
|
|||
func _physics_process(delta: float) -> void:
|
||||
if get_child_count() == 0:
|
||||
queue_free()
|
||||
|
||||
if preset.set_behavior:
|
||||
preset.set_behavior.process_set(self, delta)
|
||||
|
||||
for bullet in get_children():
|
||||
if bullet is Bullet:
|
||||
preset.behavior.process_bullet(bullet, delta)
|
||||
|
|
|
|||
12
systems/bullets/set_behaviors/bullet_set_behavior.gd
Normal file
12
systems/bullets/set_behaviors/bullet_set_behavior.gd
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
@abstract
|
||||
class_name BulletSetBehavior
|
||||
extends Resource
|
||||
|
||||
|
||||
## Process one physics tick for a [BulletSet].
|
||||
func process_set(bullet_set: BulletSet, delta: float) -> void:
|
||||
_process_set(bullet_set, delta)
|
||||
|
||||
|
||||
## Called once ever physics tick for the [BulletSet].
|
||||
@abstract func _process_set(bullet_set: BulletSet, delta: float) -> void
|
||||
1
systems/bullets/set_behaviors/bullet_set_behavior.gd.uid
Normal file
1
systems/bullets/set_behaviors/bullet_set_behavior.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cu4iq25sb3y5f
|
||||
11
systems/bullets/set_behaviors/multi_set_behavior.gd
Normal file
11
systems/bullets/set_behaviors/multi_set_behavior.gd
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class_name MultiSetBehavior
|
||||
extends BulletSetBehavior
|
||||
## Processes multiple [BulletSetBehavior]s in order for the [BulletSet].
|
||||
|
||||
|
||||
@export var behaviors: Array[BulletSetBehavior] = []
|
||||
|
||||
|
||||
func _process_set(bullet_set: BulletSet, delta: float) -> void:
|
||||
for behavior in behaviors:
|
||||
behavior.process_set(bullet_set, delta)
|
||||
1
systems/bullets/set_behaviors/multi_set_behavior.gd.uid
Normal file
1
systems/bullets/set_behaviors/multi_set_behavior.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cijy4yedxklfd
|
||||
11
systems/bullets/set_behaviors/rotation_set_behavior.gd
Normal file
11
systems/bullets/set_behaviors/rotation_set_behavior.gd
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class_name RotationSetBehavior
|
||||
extends BulletSetBehavior
|
||||
|
||||
|
||||
@export_custom(0, "radians_as_degrees,suffix:°/s") var rotation_speed: float = 0.0
|
||||
@export_custom(0, "radians_as_degrees,suffix:°/s") var spawn_rotation_speed: float = 0.0
|
||||
|
||||
|
||||
func _process_set(bullet_set: BulletSet, delta: float) -> void:
|
||||
bullet_set.rotation += rotation_speed * delta
|
||||
bullet_set.spawn_rotation += spawn_rotation_speed * delta
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://5scjonwmg8tw
|
||||
|
|
@ -16,11 +16,11 @@ func spawn_bullets(bullet_set: BulletSet, preset: BulletPreset) -> void:
|
|||
preset.face_direction
|
||||
)
|
||||
|
||||
var line = line_normal.orthogonal()
|
||||
var line = line_normal.orthogonal().rotated(bullet_set.spawn_rotation)
|
||||
var weight = float(i) / float(bullet_count - 1)
|
||||
weight -= 0.5
|
||||
if bullet_count == 1:
|
||||
weight = 0.0
|
||||
bullet.position = line * weight * line_width
|
||||
bullet.position = bullet_set.spawn_offset + line * weight * line_width
|
||||
|
||||
bullet_set.add_bullet(bullet)
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ extends BulletSpawnPattern
|
|||
|
||||
func spawn_bullets(bullet_set: BulletSet, preset: BulletPreset) -> void:
|
||||
for i in bullet_count:
|
||||
var angle = (float(i) / float(bullet_count)) * TAU + angle_offset
|
||||
var angle = (float(i) / float(bullet_count)) * TAU + angle_offset + bullet_set.spawn_rotation
|
||||
var bullet = Bullet.create(
|
||||
preset.textures.pick_random(),
|
||||
preset.hitbox_size,
|
||||
Vector2.from_angle(angle + direction_rotation),
|
||||
preset.face_direction
|
||||
)
|
||||
bullet.position = Vector2.from_angle(angle) * distance_offset
|
||||
bullet.position = bullet_set.spawn_offset + Vector2.from_angle(angle) * distance_offset
|
||||
bullet_set.add_bullet(bullet)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue