Slide the geezers boat race about

This commit is contained in:
2026-08-18 15:22:25 +01:00
parent de0e8d8d1f
commit 703e130a78

View File

@@ -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"))
{
velocity.X -= 1.0f;
}
if (Input.IsActionPressed("ui_down"))
{
velocity.Y += 1.0f;
}
if (Input.IsActionPressed("ui_up"))
{
velocity.Y -= 1.0f;
}
Position += velocity * _speed * (float)delta;
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;
}
}