From 703e130a7840a90ad0a3aeb9f6cff6c25b42e8f3 Mon Sep 17 00:00:00 2001 From: parsons Date: Tue, 18 Aug 2026 15:22:25 +0100 Subject: [PATCH] Slide the geezers boat race about --- Player.cs | 85 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/Player.cs b/Player.cs index 25f559d..63cb867 100644 --- a/Player.cs +++ b/Player.cs @@ -2,30 +2,83 @@ using Godot; public partial class Player : Sprite2D { - // Speed in pixels per second - private float _speed = 400.0f; + // 1. Logical Grid Coordinates (0,0 is top left) + private int _gridX = 0; + private int _gridY = 0; + + // The default Godot icon is 128x128 pixels + private int _tileSize = 128; + + // State machine flag to prevent input while sliding + private bool _isMoving = false; public override void _Process(double delta) { - Vector2 velocity = Vector2.Zero; + // If we are currently animating a slide, ignore the keyboard + if (_isMoving) return; - if (Input.IsActionPressed("ui_right")) + Vector2 direction = Vector2.Zero; + + // Trigger only once per key press + if (Input.IsActionJustPressed("ui_right")) direction.X = 1; + else if (Input.IsActionJustPressed("ui_left")) direction.X = -1; + else if (Input.IsActionJustPressed("ui_down")) direction.Y = 1; + else if (Input.IsActionJustPressed("ui_up")) direction.Y = -1; + + if (direction != Vector2.Zero) { - velocity.X += 1.0f; + SlideTile((int)direction.X, (int)direction.Y); } - if (Input.IsActionPressed("ui_left")) + } + + private void SlideTile(int dirX, int dirY) + { + int testGridX = _gridX; + int testGridY = _gridY; + + // 1. The "Slide on Ice" Loop + while (true) { - velocity.X -= 1.0f; - } - if (Input.IsActionPressed("ui_down")) - { - velocity.Y += 1.0f; - } - if (Input.IsActionPressed("ui_up")) - { - velocity.Y -= 1.0f; + int nextGridX = testGridX + dirX; + int nextGridY = testGridY + dirY; + + // Check if the next step is outside the 5x4 boundary + if (nextGridX < 0 || nextGridX >= 5 || nextGridY < 0 || nextGridY >= 4) + { + break; // Stop looking, we hit a wall! + } + + // TODO: In the future, we will also check: + // if (_gridArray[nextGridX, nextGridY] != null) { break; } + + // If the space is valid, update our test coordinates and loop again + testGridX = nextGridX; + testGridY = nextGridY; } - Position += velocity * _speed * (float)delta; + // 2. Did we actually move? + if (testGridX == _gridX && testGridY == _gridY) + { + return; // We were already against a wall, do nothing + } + + // 3. Update our logical position to the final destination + _gridX = testGridX; + _gridY = testGridY; + + Vector2 targetPixelPos = new Vector2(_gridX * _tileSize, _gridY * _tileSize); + + // 4. Calculate animation time based on distance traveled + // So moving 4 tiles doesn't look 4x faster than moving 1 tile + float distance = Mathf.Abs(dirX != 0 ? _gridX - testGridX : _gridY - testGridY) * _tileSize; + // Use a base speed (e.g., 600 pixels per second) + float tweenDuration = distance / 600.0f; + + // 5. Animate the slide + _isMoving = true; + Tween tween = CreateTween(); + + tween.TweenProperty(this, "position", targetPixelPos, tweenDuration); + tween.Finished += () => _isMoving = false; } } \ No newline at end of file