paratate/systems/visuals/animation_strip.gd

25 lines
914 B
GDScript

class_name AnimationStrip
extends Resource
## A texture sliced into a number of horizontal frames and played at a
## certain fps.
## Texture containing all frames in one row.
@export var texture: Texture
## Number of frames to slice the texture into.
@export var frames: int = 1
## How many frames should be displayed per second.
@export var fps: float = 1.0
## Draws the frame that should be visible at a given point in time. This must be
## called during the [method CanvasItem._draw] of [param canvas_item].
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)
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
)