From fafc415a92a7261573984cced0e0e6ea4f283aa8 Mon Sep 17 00:00:00 2001 From: TechieDamien Date: Sat, 2 Aug 2025 10:56:52 +0100 Subject: [PATCH] Adds input buffering to allow you to queue a turn after a turn --- Scripts/snake_part.gd | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Scripts/snake_part.gd b/Scripts/snake_part.gd index 1eb3f10..4db2a39 100644 --- a/Scripts/snake_part.gd +++ b/Scripts/snake_part.gd @@ -11,6 +11,8 @@ var state : States = States.ALIVE var colour_index : int = 0 var current_direction : Vector2 = Vector2.RIGHT var old_direction : Vector2 = Vector2.RIGHT +var buffered_direction : Vector2 = Vector2.ZERO +var should_buffer_direction : bool = false var inputs : Dictionary[String, Vector2] = {"right": Vector2.RIGHT, "left": Vector2.LEFT, @@ -199,6 +201,12 @@ func process_movement(new_direction : Vector2, prevent_movment : bool = false) - # Update the sprite update_sprite() + + if buffered_direction != Vector2.ZERO: + current_direction = buffered_direction + buffered_direction = Vector2.ZERO + else: + should_buffer_direction = false func update_sprite() -> void: @@ -399,7 +407,11 @@ func _unhandled_input(event: InputEvent) -> void: if event is InputEventKey and part_type == PartTypes.HEAD: for dir in inputs.keys(): if event.is_action_pressed(dir) and current_direction != -inputs[dir]: - current_direction = inputs[dir] + if should_buffer_direction: + buffered_direction = inputs[dir] + else: + current_direction = inputs[dir] + should_buffer_direction = true # If we leave the arena