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
)

View file

@ -33,10 +33,10 @@ func deinit_bullet(bullet: Bullet) -> void:
## 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)
#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:

View file

@ -1,10 +1,22 @@
@tool
class_name BulletPreset
extends Resource
@export_group("Bullets")
@export var behavior: BulletBehavior = null
@export var textures: Array[Texture2D] = []
@export var base_graphics: Array[AnimationStrip] = []:
set(value):
base_graphics = value
if base_graphics.size() != overlay_graphics.size():
overlay_graphics.resize(base_graphics.size())
notify_property_list_changed()
@export var overlay_graphics: Array[AnimationStrip] = []:
set(value):
overlay_graphics = value
if base_graphics.size() != overlay_graphics.size():
overlay_graphics.resize(base_graphics.size())
notify_property_list_changed()
@export var colors: Array[Color] = [Color.WHITE]
@export var hitbox_size: Vector2i = Vector2i.ZERO
@export var face_direction: bool = false

View file

@ -22,6 +22,7 @@ func _physics_process(delta: float) -> void:
for bullet in get_children():
if bullet is Bullet:
bullet.time_elapsed += delta
preset.behavior.process_bullet(bullet, delta)

View file

@ -0,0 +1,19 @@
class_name AnimationStrip
extends Resource
@export var texture: Texture
@export var frames: int = 1
@export var fps: float = 1.0
func draw(canvas_item: CanvasItem, time: float, modulate: Color = Color.WHITE) -> void:
var frame_size = Vector2((texture.get_width() / frames), float(texture.get_height()))
var current_frame = fmod(floorf(time * fps), frames)
print(time * fps)
canvas_item.draw_texture_rect_region(
texture,
Rect2(-frame_size * 0.5, frame_size),
Rect2(Vector2(current_frame * frame_size.x, 0.0), frame_size),
modulate
)

View file

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