Skip to content

Commit c629461

Browse files
committed
RandomFrameSpriteBehavior: Randomize frame progress too
Previously this behaviour would randomize the starting frame of the parent AnimatedSprite2D. But this can still lead to unwanted synchronicity between instances of the sprite. For example, in Fray's End we have a big field of barley. The barley animation has 2 frames (actually it has 4 but it's 2 unique frames each duplicated once), and so there are only 4 possible starting positions, leading the field to have a quite visible lock-step sway. As well as setting the `frame` property, also set `frame_progress`, which is the position within the chosen frame (between 0.0 for the start and 1.0 for the end). In addition, reset both `frame` and `frame_progress` when saving the containing scene. (Setting `AnimatedSprite2D.frame` implicitly sets `AnimatedSprite2D.frame_progress` to `0.0`.) Previously we only cleared `frame_progress` which means that, for instance, `cat.tscn` has a particular `frame` saved.
1 parent 67fe109 commit c629461

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

scenes/game_logic/sprite_behaviors/random_frame_sprite_behavior.gd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ extends BaseSpriteBehavior
1010

1111
func _ready() -> void:
1212
var frames_length: int = sprite.sprite_frames.get_frame_count(sprite.animation)
13-
sprite.frame = randi_range(0, frames_length)
13+
# TODO: weight the choice of frame by the relative lengths of frames.
14+
# i.e. if an animation has one frame lasting 800ms, and four frames lasting 100ms each,
15+
# we should be 8× more likely to pick the 800ms frame than each of the other 4.
16+
sprite.set_frame_and_progress(randi_range(0, frames_length), randf())
1417

1518

1619
func _notification(what: int) -> void:
1720
match what:
1821
NOTIFICATION_EDITOR_PRE_SAVE:
19-
sprite.frame_progress = 0
22+
sprite.frame = 0

0 commit comments

Comments
 (0)