using Godot; public partial class Player : Sprite2D { // 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) { // If we are currently animating a slide, ignore the keyboard if (_isMoving) return; 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) { SlideTile((int)direction.X, (int)direction.Y); } } private void SlideTile(int dirX, int dirY) { int testGridX = _gridX; int testGridY = _gridY; // 1. The "Slide on Ice" Loop while (true) { 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; } // 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; } }