bullet texture animations

This commit is contained in:
Haze Weathers 2026-01-01 22:29:40 -06:00
parent f44f38b58c
commit 3d2fde3381
11 changed files with 160 additions and 18 deletions

View file

@ -13,12 +13,19 @@ signal recycled()
## Texture to draw for the bullet.
@export var texture: Texture2D = null:
@export var base_graphic: AnimationStrip = null:
set(value):
texture = value
base_graphic = value
queue_redraw()
_update_visibility_notifier()
@export var color: Color
@export var overlay_graphic: AnimationStrip = null:
set(value):
overlay_graphic = value
queue_redraw()
## Size of the bullet's collision box.
@export var hitbox_size: Vector2i = Vector2i.ZERO:
set(value):
@ -60,10 +67,12 @@ static func create(
#bullet = Bullet.new()
var bullet := Bullet.new()
if not preset.textures.is_empty():
bullet.texture = preset.textures.pick_random()
if not preset.base_graphics.is_empty():
var index = randi() % preset.base_graphics.size()
bullet.base_graphic = preset.base_graphics[index]
bullet.overlay_graphic = preset.overlay_graphics[index]
if not preset.colors.is_empty():
bullet.modulate = preset.colors.pick_random()
bullet.color = preset.colors.pick_random()
bullet.hitbox_size = preset.hitbox_size
bullet.face_direction = preset.face_direction
@ -103,15 +112,18 @@ func _enter_tree() -> void:
_update_visibility_notifier()
func _physics_process(delta: float) -> void:
func _physics_process(_delta: float) -> void:
if not Engine.is_editor_hint():
queue_redraw()
if face_direction:
rotation = direction.angle()
func _draw() -> void:
if texture:
draw_texture(texture, -texture.get_size() * 0.5)
if base_graphic:
base_graphic.draw(self, time_elapsed, color)
if overlay_graphic:
overlay_graphic.draw(self, time_elapsed, Color.WHITE)
func _get_configuration_warnings() -> PackedStringArray:
@ -121,14 +133,14 @@ func _get_configuration_warnings() -> PackedStringArray:
# sets the canvas item up to notify when it leaves the screen
# this essentially mimics `VisibleOnScreenNotifier` without an additional node
func _update_visibility_notifier() -> void:
if not texture or not is_inside_tree() or Engine.is_editor_hint():
if not base_graphic or not base_graphic.texture or not is_inside_tree() or Engine.is_editor_hint():
return
# the visibility rect is set to twice the size of the texture to add a little margin
# (func(): pass) is the cleanest way i could think of to have a callback that does nothing
RenderingServer.canvas_item_set_visibility_notifier(
get_canvas_item(), true,
Rect2(-texture.get_size(), texture.get_size() * 2.0),
Rect2(-base_graphic.texture.get_size(), base_graphic.texture.get_size() * 2.0),
(func(): pass), _on_screen_exited
)