hero-mark-2/scripts/sprite_ext.gd

43 lines
979 B
GDScript

extends Sprite
## emitted when the stop frame is reached
signal stopped()
## speed at which to animate
export var fps: float = 0.0
## frame to stop on when reached
export var stop_at_frame: int = -1
## whether to animate back and forth instead of loop
export var ping_pong: bool = false
## whether to maintain fixed global rotation
export var rotation_lock: float = NAN
## whether the animation is paused
var stopped: bool = false
var _time: float = 0.0
func _process(delta):
# rotation lock
if not is_nan(rotation_lock):
set_deferred("global_rotation_degrees", rotation_lock)
# global_rotation_degrees = rotation_lock
# do not animate if paused
if stopped:
return
# accumulate time and animate
_time += abs(fps) * delta
if _time >= 1.0:
_time -= 1.0
# posmod to loop around
frame = posmod(frame + int(sign(fps)), hframes * vframes)
if frame == stop_at_frame:
stopped = true
emit_signal("stopped")
if ping_pong and frame == 0:
fps = -fps