From 7dfd0c45e9745f068f09cda203b24f8cf14894ad Mon Sep 17 00:00:00 2001 From: kleonc <9283098+kleonc@users.noreply.github.com> Date: Fri, 15 Dec 2023 19:20:03 +0100 Subject: [PATCH] Fix animated tile random start time not scaled to animation duration --- scene/2d/tile_map.cpp | 5 +++-- scene/2d/tile_map.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index f40ee1f506b..c6034e9feb5 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -288,7 +288,7 @@ int TileMap::get_rendering_quadrant_size() const { return rendering_quadrant_size; } -void TileMap::draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame, Color p_modulation, const TileData *p_tile_data_override, real_t p_animation_offset) { +void TileMap::draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame, Color p_modulation, const TileData *p_tile_data_override, real_t p_normalized_animation_offset) { ERR_FAIL_COND(!p_tile_set.is_valid()); ERR_FAIL_COND(!p_tile_set->has_source(p_atlas_source_id)); ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_tile(p_atlas_coords)); @@ -353,6 +353,7 @@ void TileMap::draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref< } else { real_t speed = atlas_source->get_tile_animation_speed(p_atlas_coords); real_t animation_duration = atlas_source->get_tile_animation_total_duration(p_atlas_coords) / speed; + real_t animation_offset = p_normalized_animation_offset * animation_duration; // Accumulate durations unaffected by the speed to avoid accumulating floating point division errors. // Aka do `sum(duration[i]) / speed` instead of `sum(duration[i] / speed)`. real_t time_unscaled = 0.0; @@ -360,7 +361,7 @@ void TileMap::draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref< real_t frame_duration_unscaled = atlas_source->get_tile_animation_frame_duration(p_atlas_coords, frame); real_t slice_start = time_unscaled / speed; real_t slice_end = (time_unscaled + frame_duration_unscaled) / speed; - RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, animation_duration, slice_start, slice_end, p_animation_offset); + RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, animation_duration, slice_start, slice_end, animation_offset); Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, frame); tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping()); diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index 22efbf018f5..cfc4f95c893 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -123,7 +123,7 @@ public: void set_rendering_quadrant_size(int p_size); int get_rendering_quadrant_size() const; - static void draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame = -1, Color p_modulation = Color(1.0, 1.0, 1.0, 1.0), const TileData *p_tile_data_override = nullptr, real_t p_animation_offset = 0.0); + static void draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame = -1, Color p_modulation = Color(1.0, 1.0, 1.0, 1.0), const TileData *p_tile_data_override = nullptr, real_t p_normalized_animation_offset = 0.0); // Layers management. int get_layers_count() const;