✨ Player zoom
This commit is contained in:
@@ -11,6 +11,68 @@ public partial class Player : CharacterBody2D
|
||||
[Export] public float SprintAcceleration = 1800.0f; // Slightly faster acceleration when sprinting
|
||||
[Export] public float Deceleration = 1200.0f;
|
||||
[Export] public float RotationSpeed = 3.0f;
|
||||
[Export] public float MinZoom = 0.5f;
|
||||
[Export] public float MaxZoom = 2.0f;
|
||||
[Export] public float BaseZoomSpeed = 0.1f;
|
||||
[Export] public float MaxZoomSpeed = 0.5f;
|
||||
[Export] public float ZoomAcceleration = 0.05f;
|
||||
[Export] public float ZoomDecay = 0.9f;
|
||||
[Export] public float ZoomSmoothing = 10.0f;
|
||||
|
||||
private Camera2D _camera;
|
||||
private Vector2 _cameraTargetZoom = Vector2.One;
|
||||
private float _currentZoomSpeed;
|
||||
private int _lastZoomDirection;
|
||||
private double _lastZoomTime;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_camera = GetNode<Camera2D>("Camera2D");
|
||||
_cameraTargetZoom = _camera.Zoom;
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
// Handle mouse wheel zoom
|
||||
if (@event is InputEventMouseButton mouseEvent)
|
||||
{
|
||||
switch (mouseEvent.ButtonIndex)
|
||||
{
|
||||
case MouseButton.WheelDown when mouseEvent.Pressed:
|
||||
HandleZoomInput(1);
|
||||
break;
|
||||
case MouseButton.WheelUp when mouseEvent.Pressed:
|
||||
HandleZoomInput(-1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleZoomInput(int direction)
|
||||
{
|
||||
var currentTime = Time.GetTicksMsec();
|
||||
|
||||
// If same direction as last time, accelerate
|
||||
if (direction == _lastZoomDirection && (currentTime - _lastZoomTime) < 300)
|
||||
{
|
||||
_currentZoomSpeed = Mathf.Min(_currentZoomSpeed + ZoomAcceleration, MaxZoomSpeed);
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentZoomSpeed = BaseZoomSpeed;
|
||||
}
|
||||
|
||||
_lastZoomDirection = direction;
|
||||
_lastZoomTime = currentTime;
|
||||
|
||||
// Apply zoom with current speed
|
||||
var zoomFactor = 1.0f + (_currentZoomSpeed * -direction);
|
||||
_cameraTargetZoom = _camera.Zoom * zoomFactor;
|
||||
|
||||
// Clamp target zoom
|
||||
_cameraTargetZoom.X = Mathf.Clamp(_cameraTargetZoom.X, MinZoom, MaxZoom);
|
||||
_cameraTargetZoom.Y = Mathf.Clamp(_cameraTargetZoom.Y, MinZoom, MaxZoom);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
@@ -18,6 +80,16 @@ public partial class Player : CharacterBody2D
|
||||
var mousePos = GetGlobalMousePosition();
|
||||
var direction = GlobalPosition.DirectionTo(mousePos);
|
||||
Rotation = direction.Angle();
|
||||
|
||||
// Smoothly interpolate to target zoom
|
||||
var deltaF = (float)delta;
|
||||
_camera.Zoom = _camera.Zoom.Lerp(_cameraTargetZoom, ZoomSmoothing * deltaF);
|
||||
|
||||
// Decay zoom speed when not zooming
|
||||
if ((Time.GetTicksMsec() - _lastZoomTime) > 300)
|
||||
{
|
||||
_currentZoomSpeed = Mathf.Max(_currentZoomSpeed * ZoomDecay, BaseZoomSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
|
Reference in New Issue
Block a user